📄 1.c
字号:
/* *****************************************************************/
/* 电子技术实验室论坛单片机开发板演示程序 */
/* 红外测试 */
/* 1602显示 */
/* 晶体:12M */
/* MCU:STC89S51,52orAT89系列 */
/* 版本: V1.0 (2008/03/20) */
/* 技术交流论坛:http://mcudz.5d6d.com */
/*【声明】此程序仅用于学习参考,为学习板赠送资料,不得用做商业用途 */
/* */
/*******************************************************************/
#include<reg52.h>
//#include<stdio.h>
//#include<intrins.h>
////////////////////////////////////////////////
#define TURE 1
#define FALSE 0
extern int printf (const char *, ...);
////////////////////////////////////////////////
code unsigned char seg7code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff}; //共阳数码管段码
code unsigned char number[]={128,129,130,131};
code unsigned char list[][8]={"第一个键", "第二个键","第三个键", "第四个键"};
sbit IR=P3^2;//红外接口标志
unsigned int hour,minute,second,count;
char data TimeNum[]=" ";
//unsigned char date;
//unsigned char k;
////////////////////////////////////////////
unsigned char irtime;//红外用全局变量
unsigned char i;
bit flag;
bit irpro_ok,irok;
unsigned char IRcord[4];
unsigned char irdata[34];
void LED_display (void);
void displayall(void);
//unsigned char date;
//////////////////////////////////////////////
void Delay(unsigned char mS);
void Ir_work(void);
void Ircordpro(void);
////////////////////////////////////////////////////////////////
void Delay(unsigned char mS)//delay mS
{
unsigned char us,usn;
while(mS!=0)
{
usn = 4;
while(usn!=0)
{
us=0xf0;
while (us!=0) {us--;}
usn--;
}
mS--;
}
}
//////////////////////////////////////////////////////////////////
//************************************************************* //
//////////////////////////////////////////////////////////////////
void tim0_isr (void) interrupt 1 using 1//定时器0中断服务函数
{
irtime++;
}
///////////////////////////////////////////////////////////////////////
void ex0_isr (void) interrupt 0 using 0//外部中断0服务函数
{
if(!flag)
{
if(irtime<0x3c&&irtime>=0x35)
{
i=0;
flag=1;
}
} //用于红外码值接收
irdata[i]=irtime;//读取时长
irtime=0;
i++;
if(i==33)
{
irok=1;
i=0;
flag=0;
}
}
////////////////////////////////////////////////////////////////////
//******************串口初始化函数****************************//
//************************************************************//
void TIM0init(void)//定时器0初始化
{
EA = 1;
IT0 = 1; // Configure interrupt 0 for falling edge on /INT0 (P3.2)
EX0 = 1; // Enable EX0 Interrupt断
TMOD=0x22; //定时器T1,在方式2中断产生波特率
PCON &= 0x00; // 波特率不倍增 PCON=0x00; //SMOD=0
SCON=0x40;
TH1=0xfd; //波特率设置为9600
TL1=0xfd;
ET0=1;
TH0=0x14;
TL0=0x14;
TR0=1; //开定时器T1运行控制位
TR1=1;
}
/******************************************************************/
void Ir_work(void)//红外键值散转程序
{
if (IRcord[2]=~IRcord[3])
{
TimeNum[1] = seg7code[IRcord[0]/16];
TimeNum[2] = seg7code[IRcord[0]%16];
TimeNum[3] = seg7code[IRcord[1]/16];
TimeNum[4] = seg7code[IRcord[1]%16];
TimeNum[5] = seg7code[IRcord[2]/16];
TimeNum[6] = seg7code[IRcord[2]%16];
TimeNum[7] = seg7code[IRcord[3]/16];
TimeNum[8] = seg7code[IRcord[3]%16];
LED_display();
}
}
void LED_display(void) //显示子程序
{
P0=0xfe; //P0.0=0,选通第一位
P2=TimeNum[1]; //段显
Delay(1);
P2=0xff;
P0=0xfd; //P0.1=0,选通第二位
P2=TimeNum[2]; //段显
Delay(1);
P2=0xff;
P0=0xfb;
P2=TimeNum[3];
Delay(1);
P2=0xff;
P0=0xf7;
P2=TimeNum[4];
Delay(1);
P2=0xff;
P0=0xef;
P2=TimeNum[5];
Delay(1);
P2=0xff;
P0=0xdf;
P2=TimeNum[6];
Delay(1);
P2=0xff;
P0=0xbf;
P2=TimeNum[7];
Delay(1);
P2=0xff;
P0=0x7f;
P2=TimeNum[8];
Delay(1);
P2=0xff;
}
/*****************************************************************/
void Ircordpro(void)//红外码值处理函数
{
static unsigned char x, j, y;
static unsigned char cord,value;
y=1;
for(x=0;x<4;x++)//处理4个字节
{
for(j=1;j<=8;j++) //处理1个字节8位
{
cord=irdata[y];
if(cord>0x07)//大于某值为1
{
value=value|0x80;
}
else
{
value=value;
}
if(j<8)
{
value=value>>1;
}
++y;
}
IRcord[x]=value;
value=0;
} irpro_ok=1;//处理完毕标志位置1
}
/******************************************************************/
/*void displayall(void)
{ unsigned char h;
printf("%x",(int)IRcord[2]);
printf("\n");
for(h=0;h<5;h++)
{
if(number[i]==IRcord[2])
{
printf("%s",list[i]);
printf("\n");
break;
}
}
}
/*********************************************************************/
/////////////////////////////////////////////////////////////////
void main(void)
{
TIM0init();
while(1)//主循环
{Ir_work();
if(irok)
{
Ircordpro();
irok=0;
}
if(irpro_ok)//step press key
{
irpro_ok=0;
//displayall();
SBUF=IRcord[2];
while(!TI);
TI=0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -