紅外解碼數(shù)碼管顯示+串口接收
#include
//***********************************************************
// TC9012 紅外遙控接收數(shù)碼管顯示+串口接收-----成功! *
//***********************************************************
#define uchar unsigned char //定義一下方便使用
#define uint unsigned int
//unsigned char code dgw[] = {0xfe,0xfd,0xfb,0xf7};//數(shù)碼管控制
unsigned char scan[4]={0xf7,0xfb,0xfd,0xfe};//p1位選擇
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char code led_tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char disbuf[4];//數(shù)碼管顯示緩沖
uchar irdate[33];
char DAT[4]={0};
uchar temp,keynum,ledstop=8,irtime;
uchar startfleg,bitnum,irallok;
uchar irok;
uchar ircode[4];
uchar jia,flog;
void delay(unsigned char x) ;//x*0.14MS
//sbit sm = P1^0;
sbit BEEP = P3^7 ; //聲音輸出接口
/*
紅外接收,并識(shí)別遙控上的數(shù)字 P3.7聲音輸出接口
P0數(shù)碼管段選 P1數(shù)碼管位選 P3.2紅外輸入端
*/
void delay1ms(int n) //誤差 0us
{
unsigned char a,b;
int m;
for(m=0;m
for(b=9;b>0;b--)
for(a=1;a>0;a--);
}
}
void DELAY_MS (unsigned int a)//延時(shí)程序
{
unsigned int i;
while( --a != 0)
{
for(i = 0; i < 125; i++);
}
}
////////////////////////
//簡(jiǎn)化顯示函數(shù)
void disp(int x)
{
int i,temp = 0x01;
DAT[0]=x/1000;
DAT[1]=x/100%10;
DAT[2]=x/10%10;
DAT[3]=x%10;
for(i=0;i<4;i++)
{
P1 = ~temp;
P0 = tab[DAT[i]];
DELAY_MS(4);
temp = temp<<1; //左移位
}
}
void irwork() // disbuf[0]=(((KeyValue&0xf0)>>4)&0x0f);//機(jī)器碼,取高4位
{
// 16進(jìn)制顯示(鍵值)
disbuf[0] = (((ircode[1]&0xf0)>>4)&0x0f);
disbuf[1] = ircode[1]&0x0f;
disbuf[2] = (((ircode[2]&0xf0)>>4)&0x0f);
disbuf[3] = ircode[2]&0x0f;
//以下是十進(jìn)制顯示(鍵碼)
//disbuf[0] = ircode[2]; //通過緩沖得到數(shù)值
//disbuf[1] = ircode[3];
}
void LedDisp()//鍵值顯示
{
unsigned char i;
for(i=0;i<4;i++)
{
P0=tab[disbuf[i]];
P1 = scan[i];
DELAY_MS(5);
P1=0xff;
}
}
void irtimeA() //中斷初始化
{
TMOD = 0X22;//設(shè)定時(shí)器0并用工作方式2
TH0 = 0;//高位賦初值
TL0 = 0;//低位賦初值 //使定時(shí)器256us中斷一次
EA = 1;//開啟總中斷開關(guān)
ET0 = 1;//啟動(dòng)定時(shí)中斷
TR0 = 1;//開啟定時(shí)器
EX0 = 1;//打開外部中斷
IT0 = 1;//選擇外部中斷方式1
TH1 = 0XF3;//定時(shí)器初值高8位設(shè)置
TL1 = 0XF3;//定時(shí)器初值低8位設(shè)置
TR1 = 1;//啟動(dòng)定時(shí)器
SM0 = 0;
SM1 = 1;//設(shè)置串口工作方式
REN = 1;//使串口接收使能
//EA = 1;//打開總中斷開關(guān)
ES = 1;//打開串口中斷
PCON = 0x80;//波特率倍頻(屏蔽本句波特率為2400)
//IP = 0x10;
}
void beep()
{
unsigned char i ;
for (i=0 ;i<100 ;i++)
{
delay1ms(10);
BEEP=!BEEP;
}
BEEP=1 ;
}
void irpros(void) //對(duì)接收到的數(shù)據(jù)處理
{
uchar k,value, i,j;
k=1;
for(j=0;j<4;j++)
{
for(i=0;i<8;i++) //循環(huán)8次得到一段8位的碼
{
value = value>>1;
if(irdate[k]>6) //判斷有幾個(gè)irtime=255us 大于6個(gè)是1
{
value = value | 0x80;
}
k++;
}
ircode[j] = value; //將一段8位的碼存入數(shù)組中
}
irallok = 1;
}
main()
{
//uchar wx=0x01,js;
irtimeA();
while(1)
{
if(irok==1)
{
irpros();
irok = 0;
}
if(irallok==1)
{
irwork();
//ircal();
irallok = 0;
beep();
ES = 0;//關(guān)閉串口中斷
///////////////////解碼完畢串口接收顯示///
flog = 0;
SBUF = ircode[0]; //單片機(jī)向pc發(fā)送數(shù)據(jù)////
while(!TI);//等待數(shù)據(jù)發(fā)送完畢 ////
TI = 0; //發(fā)送標(biāo)志位置0 ////
ES = 1;//打開串口中斷 ////
//DELAY_MS(5);
//////////////////////////////////////////
flog = 0;
SBUF = ircode[1]; //單片機(jī)向pc發(fā)送數(shù)據(jù)////
while(!TI);//等待數(shù)據(jù)發(fā)送完畢 ////
TI = 0; //發(fā)送標(biāo)志位置0 ////
ES = 1;//打開串口中斷 ////
//DELAY_MS(5);
//////////////////////////////////////////
flog = 0;
SBUF = ircode[2]; //單片機(jī)向pc發(fā)送數(shù)據(jù)////
while(!TI);//等待數(shù)據(jù)發(fā)送完畢 ////
TI = 0; //發(fā)送標(biāo)志位置0 ////
ES = 1;//打開串口中斷 ////
//DELAY_MS(5);
//////////////////////////////////////////
flog = 0;
SBUF = ircode[3]; //單片機(jī)向pc發(fā)送數(shù)據(jù)////
while(!TI);//等待數(shù)據(jù)發(fā)送完畢 ////
TI = 0; //發(fā)送標(biāo)志位置0 ////
ES = 1;//打開串口中斷 ////
//////////////////////////////////////////發(fā)送完畢標(biāo)志位
flog = 0;
SBUF =0x64; //單片機(jī)向pc發(fā)送數(shù)據(jù)////
while(!TI);//等待數(shù)據(jù)發(fā)送完畢 ////
TI = 0; //發(fā)送標(biāo)志位置0 ////
ES = 1;//打開串口中斷 ////
//////////////////////////////////////////
}
LedDisp();
//disp(shiyan);
//disp(disbuf[1]);
//ledshine();
//ledshines();
}
}
void timer0() interrupt 1 //定時(shí)器0中斷
{
irtime++;
}
void inter0() interrupt 0 //外部中斷
{
if(startfleg)
{
if(irtime>32) //判斷引導(dǎo)碼是否發(fā)送完畢
{
bitnum = 0;
}
irdate[bitnum] = irtime; //接收數(shù)據(jù)并把數(shù)據(jù)存入數(shù)組
irtime = 0;
bitnum++;
if(bitnum==33)
{
bitnum = 0;
irok = 1;
}
}
else
{
startfleg = 1; //是否接收到數(shù)據(jù)標(biāo)志位
irtime = 0;
}
//serial() interrupt 4;
}
void serial() interrupt 4 //串口中斷函數(shù)
{
//a = SBUF;
RI = 0; //中斷接收標(biāo)志位置0
flog = 1;
}
評(píng)論