用atmega16单片机 vs1838b红外接收总是不对 使用的是GCC环境 采用ICP捕获功能 劳烦
/*------------------------------------红外解码程序-------------------------------MCU: ATmega16
Crystal: 4.0000Mhz
---------------------------------------------------------------------------------*/
#include "avr/io.h"
#include "avr/interrupt.h"
#include "util/delay.h"
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
/*------------------------------显示-----------------------------------*/
//#define LED_DATA PORTB
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)
{ //四位共阴数码管,十六进制显示
PORTB=tab;
}
void port_init(void)
{
DDRA|= 1<<PA2;
PORTA&=~(1<<PA2);
DDRB= 0xFF;
PORTB&=0x00;
}
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 |= ((1<<CS10)|(1<<ICES1)); //start Timer,输入捕捉上升沿使能,1分频
// TCCR1B &=~(1<<ICES1);
}
uint arr={0x00}; //保存捕捉值
uchar times=0; //记录捕捉次数
uchar index_1=0; //数组索引
uchar ir_code={0x00}; //红外遥控键码
uchar OK_flag=0; //解码完毕标志
/*------------------------------------------------------------------------------
模块名称:PD6(ICP1)引脚上升沿捕捉中断程序
影响: 将捕捉值保存在arr中
___ ___
| | | |
___| |_________| |__________
|<-----t----->|
DEC HEX
t=9ms 计数值=54000 0xD2F0
t=4.5ms 计数值=27000 0x6978
t=2.25ms 计数值=13500 0x34BC
t=1.125ms 计数值=6750 0x1A5E
------------------------------------------------------------------------------*/
/*------------------------------解码程序----------------------------------------
结果:得到uchar型的键码ir_code,如果解码出错,则ir_code=0xff
-----------------------------------------------------------------------------*/
void get_code(void)
{
uchar i,dat=0,_dat=0;/*键码*//*键码反码*/
uint temp=0x0000;
for(i=0;i<16;i++)
{
if(arr<4480)
{
//“0”
temp>>=1;
}
if(arr>5600)
{
//“1”
temp>>=1;
temp|=0x80;
}
}
dat=(uchar)(temp&0x00ff);/*键码*/
_dat=(uchar)((temp>>8)&0x00ff);/*键码反码*/
/*“键码”与“键码反码相与,如果为0则解码正确*/
if((dat&_dat)==0)
ir_code=dat;
else
{
TCNT1H = 0x00;
TCNT1L = 0x00;
ir_code=0xff;//解码出错
PORTD|=1<<PD3; //灯亮 数据总是错 显示的要么是’0‘,要么是‘F’
}
times=0;
index_1=0;
TCNT1H = 0x00;
TCNT1L = 0x00;
}
void init_devices(void)
{
asm("cli"); //开启总中断
port_init();
timer1_init();
MCUCR=0x00;
GICR= 0x00;
TIMSK|=1<<TICIE1;
asm("sei"); //开启总中断
}
void main(void)
{
uchar dis_flag=0;
DDRD|=1<<PD3;
PORTD&=~(1<<PD3);
init_devices();
while(1)
{
if (OK_flag)
{
get_code();//解码
OK_flag=0;
dis_flag=1;
}
if(dis_flag==1)
{
dis_flag=0;
dis_int(ir_code);
}
// for(i=0;i<200;i++)
}
}
ISR(TIMER1_CAPT_vect)
{
uint value;
value=ICR1L;
value|=(uint)(ICR1H << 8);
TCNT1H = 0x00;
TCNT1L = 0x00;
times++;
if(times>18&×<35)
{
//从第19个上升沿开始保存,直到第34个,短按时共有36个上升沿
arr=value;
index_1++;
// PORTD|=1<<PD3;
}
else if(times>=35)
{
//第35个上升沿到来时,解码完毕
times=0;
index_1=0;
OK_flag=1;
}
TIFR=(1<<ICF1);//写“1”清零
TCNT1=0;
} 程序里的笑脸亮了 呵呵,笑脸好玩! 这个问题楼主解决了没有??
页:
[1]