AVR单片机uPD6121/LC7461M/HT6222/AS6122等红外编码芯片的解码程序MCU: ATmega16 Crystal: 6.0000MHz
#include <iom16v.h>
#include <macros.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
/*------------------------------显示-----------------------------------*/
#define LED_DATA PORTB
const uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
/*
0 1 2 3 4 5 6 7 8 9 A B C D E F
*/
void delay(void){
uint i;
for(i=0;i<2000;i++);
}
void dis_int(uint dis_data)
{ //四位共阴数码管,十六进制显示
LED_DATA=tab[(uchar)((dis_data&0xf000)>>12)]; //显示千位
PORTD=0xf7;
delay(); //延时
LED_DATA=tab[(uchar)((dis_data&0x0f00)>>8)]; //显示百位
PORTD=0xfb;
delay(); //延时
LED_DATA=tab[(uchar)(dis_data&0x00f0)>>4]; //显示十位
PORTD=0xfd;
delay(); //延时
LED_DATA=tab[(uchar)dis_data&0x000f]; //显示个位
PORTD=0xfe;
delay(); //延时
}
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0xFF;
PORTC = 0xFF;
DDRC = 0xFF;
PORTD = 0xFF;
DDRD = 0x0F;
}
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00; //setup
TCNT1L = 0x00;
ICR1H = 0x00;
ICR1L = 0x08;
TCCR1A = 0x00;
/*一体化接收头的DATA引脚接到ATmega16的PD6(ICP1)引脚,采用上升沿捕捉的方法解码*/
/*晶振为6M,无分频,用8M太大(分频则太小)*/
TCCR1B = 0x41; //start Timer
}
uint arr[16];//保存捕捉值
uchar times;//记录捕捉次数
uchar index;//数组索引
uchar ir_code;//红外遥控键码
uchar OK_flag;//解码完毕标志
/*------------------------------------------------------------------------------
模块名称:PD6(ICP1)引脚上升沿捕捉中断程序
影响: 将捕捉值保存在arr[16]中
___ ___
| | | |
___| |_________| |__________
|<-----t----->|
DEC HEX
t=9ms 计数值=54000 0xD2F0
t=4.5ms 计数值=27000 0x6978
t=2.25ms 计数值=13500 0x34BC
t=1.125ms 计数值=6750 0x1A5E
------------------------------------------------------------------------------*/
#pragma interrupt_handler timer1_capt_isr:6
void timer1_capt_isr(void)
{
uint value;
value=ICR1L;
//CLI();
value|=(uint)(ICR1H << 8);
TCNT1H = 0x00;
TCNT1L = 0x00;
times++;
if(times>18&×<35){
//从第19个上升沿开始保存,直到第34个,短按时共有36个上升沿
arr[index]=value;
index++;
}
else if(times>=35){
//第35个上升沿到来时,解码完毕
times=0;
index=0;
OK_flag=1;
}
//SEI();
}
/*------------------------------解码程序----------------------------------------