#include "msp430x14x.h" //xt1 32768KHZ xt2 8MHZ ,红外接收头接于P1.0
#include<12864-9.h>
//**********************************************************************
#define HONGWAI_1 0x01//红外接收头接于P1.0
#define HONGWAI_0 0xfe
#define HONGWAI_IN (P1IN&HONGWAI_1) //P1.0输入
//**********************************************************************
char IRcode[4]={0,0,0,0}; //定义一个4字节的数组用来存储代码
void int_timer(void)
{
TACTL|=TASSEL1+TACLR;//选择SMCLK作为定时器的时钟
TACTL|=MC0;//增加模式,并且使能中断
TACCR0=9999;//中断周期
}
void int_ex(void) //初始化P1.0口外部中断
{
P1DIR&=HONGWAI_0;//输入口
P1SEL&=HONGWAI_0; //使用普通IO
P1IE|=HONGWAI_1;//外部中断允许
P1IES|=HONGWAI_1;//下降沿触发中断
P1IFG= 0x00; // 清除P1口中断标志
}
void sent_byte2(char dat)
{
while((IFG2&UTXIFG1)==0);//判断发送缓冲区是否结束
U1TXBUF=dat;
}
#pragma vector=UART1RX_VECTOR
__interrupt void UART1_RX_ISR(void)
{
char data=0;
data=U1RXBUF; //读取接受到的数据并且发送到PC机
P1OUT=data;
sent_byte2(data+1);
}
void int_UART2(void)
{
P3SEL |= 0xc0; // P3.6,7 = USART0 TXD/RXD
UCTL0 |= SWRST; //复位串口
ME2 |= UTXE1 + URXE1; // 允许接收、发送
UCTL1 |= CHAR; // 8位数据
UTCTL1 |= SSEL0; // UCLK = ACLK
UBR01 = 0x03; // 波特率9600
UBR11 = 0x00; //
UMCTL1 = 0x4A; // 波特率小数部分
UCTL1 &= ~SWRST; // 串口停止复位,正常工作
IE2 |= URXIE1; // 使能接收中断
_EINT(); //系统中断允许
}
void int_clk()
{
char i;
BCSCTL1&=~XT2OFF; //打开XT振荡器
BCSCTL2|=SELM1+SELS+DIVS_2+DIVS_1;//MCLK 8M and SMCLK 1M 8分频
do
{
IFG1 &= ~OFIFG; //清除振荡错误标志
for(i = 0; i < 100; i++)
_NOP(); //延时等待
}
while ((IFG1 & OFIFG) != 0); //如果标志为1继续循环等待
IFG1&=~OFIFG;
}
void delay_07ms(char h)//0.7ms
{
char p;
for(p=h;p>0;p--)
{
int_timer();
while(TAR<=700);
TACTL=0; //停止计数器
TAR=0;
}
}
void delay_1ms(void)//1ms
{
int_timer();
while(TAR<=1000);
TACTL=0; //停止计数器
TAR=0;
}
#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
char m,n,k,CodeTemp;
int j;
P1IE&=HONGWAI_0;//禁止P1.0中断
if(P1IFG&HONGWAI_1==HONGWAI_1)
{
P1IFG=0;//清中断标志
for(k=0;k<10;k++)
{
delay_07ms(1);
if (HONGWAI_IN) //如果0.7ms后HONGWAI_IN=1,说明不是引导码
{
k=10;break;
}
else
if(k==9) //如果持续了10×0.7ms=7ms的低电平,说明是引导码
{
while(HONGWAI_IN==0);
delay_07ms(5); //跳过持续4.5ms的高电平
for(m=0;m<4;m++) //分别读取4个字节
{
for(n=1;n<=8;n++) //每个字节8个bit的判断
{
while(HONGWAI_IN==0); //等待上升沿
delay_07ms(1); //从上升沿那一时刻开始延时0.9ms,再判断IRsignal
if(HONGWAI_IN) //如果IRsignal是"1",则向右移入一位"1"
{
delay_1ms(); //延时1ms
CodeTemp=CodeTemp|0x80;
if(n<8) CodeTemp=CodeTemp>>1;
}
else
if(n<8)CodeTemp=CodeTemp>>1;//如果IRsignal是"0",则向右移一位,自动补"0"
}
IRcode[m]=CodeTemp;
CodeTemp=0;
}
// sent_byte2(IRcode[3]);
for(j=50;j>0;j--)
delay_1ms(); //延时1ms
}
}
}
P1IE|=HONGWAI_1;//外部中断允许
}
int main( void )
{
WDTCTL = WDTPW + WDTHOLD;
int_clk(); //初始化时钟
lcd_init();
lcd_full_disp(0);//清屏
int_ex(); //初始化P1.0口外部中断
_BIS_SR(GIE);
//int_UART2();
while(1)
{ d_hz(1,0,0,0);
d_hz(3,0,1,0);
d_hz(5,0,2,0);
dchar_16(7,0,':',0);
dchar_16(8,0,IRcode[0]/100+48,0);
dchar_16(9,0,IRcode[0]%100/10+48,0);
dchar_16(10,0,IRcode[0]%10+48,0);
d_hz(1,1,0,0);
d_hz(3,1,1,0);
d_hz(5,1,2,0);
d_hz(7,1,3,0);
d_hz(9,1,2,0);
dchar_16(11,1,':',0);
dchar_16(12,1,IRcode[1]/100+48,0);
dchar_16(13,1,IRcode[1]%100/10+48,0);
dchar_16(14,1,IRcode[1]%10+48,0);
d_hz(1,2,4,0);
d_hz(3,2,5,0);
d_hz(5,2,2,0);
dchar_16(7,2,':',0);
dchar_16(8,2,IRcode[2]/100+48,0);
dchar_16(9,2,IRcode[2]%100/10+48,0);
dchar_16(10,2,IRcode[2]%10+48,0);
d_hz(1,3,4,0);
d_hz(3,3,5,0);
d_hz(5,3,2,0);
d_hz(7,3,3,0);
d_hz(9,3,2,0);
dchar_16(11,3,':',0);
dchar_16(12,3,IRcode[3]/100+48,0);
dchar_16(13,3,IRcode[3]%100/10+48,0);
dchar_16(14,3,IRcode[3]%10+48,0);
// _BIS_SR(LPM1_bits+ GIE); //打开低功耗模式1、打开系统中断允许
}
}
const uchar HZ[16*96]={
//用(0) 户(1) 码(2) 反(3) 操(4) 作(5)
0x00,0x00,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE,0x22,0x22,0x22,0x22,0xFE,0x00,0x00,
0x80,0x40,0x30,0x0F,0x02,0x02,0x02,0x02,0xFF,0x02,0x02,0x42,0x82,0x7F,0x00,0x00,/*"用",0*/
0x00,0x00,0x00,0xF8,0x88,0x88,0x88,0x89,0x8A,0x8E,0x88,0x88,0x88,0xF8,0x00,0x00,
0x80,0x40,0x30,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"户",1*/
0x02,0x82,0xE2,0x5E,0x42,0xC2,0x00,0x02,0xFA,0x82,0x82,0x82,0xFE,0x80,0x00,0x00,
0x01,0x00,0x7F,0x10,0x10,0x3F,0x00,0x04,0x04,0x04,0x44,0x84,0x40,0x3F,0x00,0x00,/*"码",2*/
0x00,0x00,0xFE,0x12,0x72,0x92,0x12,0x12,0x12,0x11,0x91,0x71,0x01,0x00,0x00,0x00,
0x40,0x30,0x4F,0x40,0x20,0x21,0x12,0x0C,0x0C,0x12,0x11,0x20,0x60,0x20,0x00,0x00,/*"反",3*/
0x08,0x08,0xFF,0x88,0x48,0x00,0xF0,0x97,0xF5,0x05,0xF5,0x95,0x97,0xF0,0x00,0x00,
0x42,0x81,0x7F,0x00,0x40,0x42,0x22,0x12,0x0A,0xFF,0x06,0x0A,0x32,0x62,0x22,0x00,/*"操",4*/
0x80,0x40,0x20,0xF8,0x07,0x22,0x18,0x0C,0xFB,0x48,0x48,0x48,0x68,0x48,0x08,0x00,
0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0xFF,0x04,0x04,0x04,0x04,0x06,0x04,0x00,/*"作",5*/
};