📄 a.c
字号:
/*****************************************************
Project :
Version :
Date : 2007-5-31
Author : Cheung
Company : hhuc
Comments:
Chip type : ATmega16L
Program type : Application
Clock frequency : 1.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include<mega16.h>
#include<stdlib.h> //标准库和内存分配函数,数字转换字符
#include<delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include<lcd.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uint arr[16]; //捕捉中断之间的时间值;
uchar times; //捕捉中断次数;
uchar index; //数组索引;
uchar ir_code; //接受红外编码;
uchar ok_flag; //解码完毕标示;
uchar head; //引导码标志;
uchar hex[3]; //十六进制解码
void time1_initial() //定时器1初始化
{
TCCR1B=0x81; //输入噪声消除,下降沿捕捉,系统时钟1M;
TCNT1H=0x00;
TCNT1L=0x00;
TIMSK=0x20; //输入捕捉中断使能;
}
void port_init()
{
DDRD=0x00;
PORTD=0xff;
}
void dec_to_hex(uchar dec) //十进制转换十六进制显示
{
uchar temp[2],i;
temp[0]=dec/16;
temp[1]=dec%16;
for(i=0;i<2;i++){
switch(temp[i]){
case 0:temp[i]='0';break;
case 1:temp[i]='1';break;
case 2:temp[i]='2';break;
case 3:temp[i]='3';break;
case 4:temp[i]='4';break;
case 5:temp[i]='5';break;
case 6:temp[i]='6';break;
case 7:temp[i]='7';break;
case 8:temp[i]='8';break;
case 9:temp[i]='9';break;
case 10:temp[i]='A';break;
case 11:temp[i]='B';break;
case 12:temp[i]='C';break;
case 13:temp[i]='D';break;
case 14:temp[i]='E';break;
case 15:temp[i]='F';break;
}
hex[i]=temp[i];
}
}
// Timer 1 input capture interrupt service routine
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{
uint value,temp;
value=ICR1L;
temp=ICR1H;
TCNT1H=0x00;
TCNT1L=0x00; //设置value值即为TCNT(new)-TCNT(old)
times++; //纪录一个中断
value|=temp<<8;
if(value>12500&&value<14500)
{
head=1; //识别引导码
times=2;
}
if(head&&(times>28)&&(times<45)) //从第29个下降沿开始保存
{
arr[index]=value;
index++;
}
else if(head&×>=45) //当下降沿大于46时,解码完毕
{
times=1;
index=0;
head=0;
ok_flag=1; //解码完毕标示
}
}
/*------------------------------解码程序----------------------------------------
结果:得到uchar型的键码ir_code,如果解码出错,则ir_code=0xff
-----------------------------------------------------------------------------*/
void get_code()
{
uchar i,data,_data; //data为键码,_data为键码反码
uint temp=0; //键码(16位)
for(i=0;i<16;i++)
{
if(arr[i]<1225) //"0码"
temp<<=1;
if(arr[i]>2150) //"1码"
{
temp<<=1;
temp|=1;
}
}
_data=(uchar)(temp&0x00ff); //取temp低八位
data=(uchar)((temp>>8)&0x00ff); //取temp高八位
if((data&_data)==0x00) //校验接受键码
ir_code=data;
else{
TCNT1H=0x00;
TCNT1L=0x00;
//ir_code=0xff; //解码出错
return;
}
}
void main()
{
uchar code[5]; //用于液晶显示所用的存储数据数组应设置长点!
uchar showhex[]="The Code Is:0x";
uchar showdec[]="Dec Code:";
//WDTCR=0xF;
time1_initial();
port_init();
delay_ms(200);
lcd_init(16);
#asm("sei") // Global enable interrupts
while(1)
{
//#asm("wdr")
if(ok_flag)
{
get_code();
dec_to_hex(ir_code);
ok_flag=0;
lcd_clear();
itoa(ir_code,code);
}
lcd_gotoxy(0,0);
lcd_puts(showhex);
lcd_gotoxy(14,0);
lcd_puts(hex);
lcd_gotoxy(3,1);
lcd_puts(showdec);
lcd_gotoxy(13,1);
lcd_puts(code);
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -