#include <msp430.h>
#define CPU_F ((double)16000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
int cnt,key=0,TEMP=0,times=0;
volatile int iflag=0;
void key_init(){
P1DIR |= BIT7;
P1REN |= BIT7;
P1IFG &= ~ BIT7;
P1IES |= BIT7;
P1IE |= BIT7;
P2DIR &= ~ BIT2 ;
P2REN |= BIT2;//Connects to the resistance
P2OUT |= BIT2;//Pull-Up Resistance
P2IES |= BIT2;
P2IFG &= ~ BIT2;
P2IE |= BIT2;
}
void ir_open(){
iflag=0;
P2DIR |= BIT3;//P2.2, P2.3Êä³ö
P2SEL &= ~BIT3; //P2.2:IO P2.3:TA0
P2OUT &= ~BIT3;
//38K->P2.3
//TBCCR0=15;
TBCCR0=210;
TBCTL = TASSEL_2+MC_1+TACLR;
TBCCTL0=CCIE;
}
void ir_start(){
P2OUT |= BIT3;
iflag=1;
delay_us(9000);
//TBCTL =MC_1;
P2OUT &= ~BIT3;
iflag=0;
delay_us(4500);
//TBCTL =MC_0;
}
void ir_next(){
P2OUT |= BIT3;
iflag=1;
delay_us(9000);
P2OUT &= ~BIT3;
iflag=0;
delay_us(2250);
}
void ir_send_byte(unsigned char c){
unsigned char i;
for(i = 0; i != 8; ++i){
P2OUT |= BIT3;
iflag=1;
delay_us(560);
P2OUT &= ~BIT3;
iflag=0;
if(c&0x01){
delay_us(1685);
}
else{
delay_us(565);
}
c >>= 1;
}
}
void ir_send_3byte(unsigned char c){
unsigned char i;
for(i = 0; i !=3; ++i){
P2OUT |= BIT3;
iflag=1;
delay_us(560);
P2OUT &= ~BIT3;
iflag=0;
if(c&0x01){
delay_us(1685);
}
else{
delay_us(565);
}
c >>= 1;
}
}
void ir_end(){
P2OUT &= ~BIT3;
iflag=0;
delay_us(300);
P2OUT |= BIT3;
iflag=1;
}
void ir_put_temp(unsigned char c){
ir_start();
ir_send_byte(0x79);
ir_send_byte(0x0e);
ir_send_byte(0x30);
ir_send_byte(0x50);
ir_send_3byte(0x02);
P2OUT |= BIT3;
iflag=1;
delay_us(600);
P2OUT &= ~BIT3;
iflag=0;
delay_us(19700);
ir_send_byte(0x10);
ir_send_byte(0x21);
ir_send_byte(0x00);
ir_send_byte(0x10);
//P2OUT |= BIT3;
//iflag=1;
//delay_us(600);
P2OUT &= ~BIT3;
iflag=0;
delay_ms(2000);
//ir_end();
}
void ir_put(unsigned char c){
ir_start();
ir_send_byte(0x00);
ir_send_byte(0x09);
ir_send_byte(0x20);
ir_send_byte(0x50);
ir_send_3byte(0x02);
P2OUT |= BIT3;
iflag=1;
delay_us(600);
P2OUT &= ~BIT3;
iflag=0;
delay_us(19700);
ir_send_byte(0x00);
ir_send_byte(0x20);
ir_send_byte(0x00);
ir_send_byte(c);
P2OUT |= BIT3;
iflag=1;
delay_us(600);
P2OUT &= ~BIT3;
iflag=0;
delay_ms(2000);
//ir_end();
}
void ir_put_on(unsigned char c){
ir_start();
ir_send_byte(0x79);
ir_send_byte(0x00);
ir_send_byte(0x30);
ir_send_byte(0x50);
ir_send_3byte(0x02);
P2OUT |= BIT3;
iflag=1;
delay_us(600);
P2OUT &= ~BIT3;
iflag=0;
delay_us(19700);//19700
ir_send_byte(0x10);
ir_send_byte(0x21);
ir_send_byte(0x00);
ir_send_byte(0x30);
P2OUT &= ~BIT3;
iflag=0;
delay_ms(2000);
//ir_end();
}
int main(void)
{
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL1 = DCORSEL_5; // Select DCO range 16MHz operation
UCSCTL2 |= 499; // Set DCO Multiplier for 16MHz
// (N + 1) * FLLRef = Fdco
// (249 + 1) * 32768 = 8MHz
__bic_SR_register(SCG0);
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |=BIT0 ; // P1.0 output
key_init();
ir_open();
__bis_SR_register(GIE);
while(1)
{
P1OUT ^= BIT0;
ir_put_on(16);//work!
times=1;
}
}
//#pragma vector = TIMERB_A0_VECTOR
#pragma vector=TIMERB0_VECTOR
__interrupt void TIMERB0_ISR()
{
if(iflag==1)
{
P2OUT ^= BIT3;
//P1OUT ^= BIT0;
}
}
#pragma vector = PORT1_VECTOR
__interrupt void Port1_IRS()
{
int state;
state=P1IN&BIT7;
delay_ms(500);
if(state==(P1IN&BIT7))
//irda_receive();
{
times=0;
P1OUT ^=BIT0;
TEMP=20;
}
P1IFG &= ~ BIT7 ;
}
#pragma vector = PORT2_VECTOR
__interrupt void Port2_IRS()
{
int state;
state=P2IN&BIT2;
delay_ms(500);
if(state==(P2IN&BIT2))
//irda_receive();
{
times=0;
P1OUT ^=BIT0;
key++;
if(key==3)
key=1;
}
P2IFG &= ~ BIT2 ;
}
//格力YBOF2,实现16度开空调。
|