我知道這樣做不是很好,但我應經調了一天了,才迫不得已上來問。根據一線工人的紅外接收程序移植到mega16,調了一天都沒調通,具體表現為一直顯示0,但是用示波器能看到接收的波形。論壇的高手們,請幫我分析一下,程序有點亂,將就著看吧。萬分感謝!!
發射碼的格式
源程序
//ICC-AVR application builder : 2010-1-15 1:20:47
// Target : M16
// Crystal: 8.0000Mhz
#include "iom16v.h"
#include "macros.h"
#include "declare.h"
#include "define.h"
extern uchar key_code;//遙控鍵值
void main(void)
{
CLI();
init_devices();//MCU初始化
LCD1602_init();//液晶初始化
SEI();
LCD1602_set_xy( 0,1 );
LCD1602_write_data(1+0x30);
while(1)
{
LCD1602_set_xy( 0, 0);
LCD1602_write_data(key_code/100+0x30);
LCD1602_write_data(key_code%100/10+0x30);
LCD1602_write_data(key_code%100%10+0x30);
//delay_nms(250);
}
}
#include "iom16v.h"
#include "macros.h"
#include "declare.h"
#include "define.h"
uchar key_code=0;//遙控鍵值
uchar buf_key_code=0;//鍵值暫存
uint key_bit_count=0;//鍵編碼脈沖計數
uint count=0;//定時中斷次數計數
uint buf_count=0;//定時中斷計數暫存
uchar ir_status=0;//脈沖接收器所處的狀態,0:無信號,1:數據編碼接收區
uchar buf_INT0_PIN;//INT0的管腳狀態
void init_devices(void)
{
//INT0_port |= INT0_EN; //上拉
INT0_DDR &= ~INT0_EN; //輸入
//INT0_DDR |= INT0_EN;
TCCR1B = 0x00; //停止
TCNT1H = 0xFF; //10uSec,
TCNT1L = 0xF6;
//TCCR1B = 0x02; //8分頻
MCUCR = 0x02;//下降沿觸發
GICR = 0x40;//INT0使能
TIMSK = 0x80; //T1中斷使能
}
#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
void timer1_ovf_isr(void)//2
{
TCNT1H = 0xFF;
TCNT1L = 0xF6;
count++;//定時器中斷次數累加
}
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)//6
{
TCCR1B = 0x02;//開定時器中斷
if(count>60 && count<450)//如果信號合法
{
buf_count = count;//則放入buf_count,count清0,對下一個脈沖信號計時
count = 0;
}
delay_nus(108); //延時100us以消除下降沿跳變抖動
buf_INT0_PIN = INT0_PIN & INT0_EN;
if(buf_INT0_PIN == 0)//INT0引腳穩定為低電平,則表法確實是信號
{
count=10; //count重新計時,因上面延時了100us,故要補償10次TO中斷
}
if(buf_count>60 && buf_count<450)//若收到的信號合法,則再進行信號分析
{
if(ir_status == 0)//如果之前未收到引導碼
{
if(buf_count>350 && buf_count<450)//判斷是否引導碼4ms
{
ir_status=1;//系統標記
buf_count=0;
}
}
else if(ir_status == 1)//進入數據編碼接收
{
if(key_bit_count < 8)//收到數據少于8位,則將收到的數據寫入buf_key_code
{
if(buf_count>150 && buf_count<250)
{
buf_count = 0;
buf_key_code |= 0x01;//收到1
buf_key_code <<= 1;
key_bit_count++;//數據脈沖累加
}
else if(buf_count>60 && buf_count<150)//收到0
{
buf_count=0;
buf_key_code <<= 1;//收到0
key_bit_count++;
}
}
else //若收完8位數據則做以下處理
{
ir_status=0;//接收狀態返回到空閑
key_code=buf_key_code;
key_bit_count=0;
buf_key_code=0;
buf_count=0;
TCCR1B = 0x00;
}
}
}
}
[ 此貼被hfd999在2010-01-20 15:02重新編輯 ]