這個程序實現K1D和中國電信機頂盒遙控器解碼。
剛剛從51單片機轉到AVR ,也剛剛學習用C編程,一些語句格式糾錯搞了一天,終于完成,效果很好。共享給大家。
我這里只貼上解碼部分的文件,顯示部分文件就算了。大家知道的。
第一次發帖,請大家鼓勵一下!
//ICC-AVR application builder : 2020/3/25
// Target : 8515 TH186_K1D遙控器解碼
// Crystal: 8.0000Mhz
#include <io8515v.h>
#include <macros.h>
#include "lcd1602.h" //兩個C文件都要這
//////////////////////////////////
void Sound_bj(char t) //報警聲
{
char r;
for(r=0;r<t;r++)
{ KL_OFF;
delay_ms(3);
KL_ON;
delay_ms(3);
}
}
/////////////////////////////////
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00;
DDRC = 0xFF;
//PORTD = 0x00;
DDRD = 0xF8;
}
//TIMER0 initialize - prescale:256
// desired value: 10KHz
// actual value: 10.417KHz (4.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop timer
TCNT0 = 0xfc; //=fd set count value 10kHz
TCCR0 = 0x04; //start timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x02; //int0 02=下降沿中斷,03=上升沿中斷
GIMSK = 0x40;
TIMSK = 0x02;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//////////////////////定時器0中斷////////////////////////////////////////
#pragma interrupt_handler timer0_ovf_isr:8
char time_js0; //定時中斷計數器
void timer0_ovf_isr(void)//reload counter value
{
TCNT0 = 0xfc;//FD;
time_js0++;
}
///////////////////////外部中斷PD2接遙控接收///////////////////
#pragma interrupt_handler int0_isr:2
char js1=0,js5=0;
char jsq8=0,jsq9=0;
char wlcd[9]={""};
void int0_isr(void) //external interupt on INT0
{
time_js0 &=0xf0;
if(time_js0==0x00) //=0
{
js1 >>= 1;
js1 &= 0x7f;
jsq8++;
if (jsq8==8) {jsq8=0;
wlcd[jsq9]= js1; //實際"1"=0x00,所以+1變為0x01
jsq9++;
}
}
else if(time_js0==0x10) //=1
{
js1 >>= 1;
js1 |= 0x80;
jsq8++;
if (jsq8==8) {jsq8=0;
wlcd[jsq9]= js1;//實際"1"=0x00,所以+1變為0x01
jsq9++;
}
}
else if(time_js0>0x20) //前導碼等
{
jsq8=0; jsq9=0;js1=0;
}
time_js0=0;
}
//////////////////////////////////////////////////////////////////////////////
void main(void)
{
init_devices(); //初始化系統
LCD_init(); //LCD初始化
while(1)
{
if (jsq9==4)
{
jsq9=0;
LCD_SET_XY(0,0);
js5=(wlcd[2] >>4)+0x30;
if(js5>0x39){js5=js5+0x07;}
Write_Data(js5);//寫入當前H字符并顯示
js5=(wlcd[2] &0x0f)+0x30;
if(js5>0x39){js5=js5+0x07;}
Write_Data(js5);//寫入當前L字符并顯示
Sound_bj(8); //報警聲
}
//
//wlcd[8]=0x08;
//wlcd[9]=0x01;
//LCD_write_str(0, 0,wlcd );
}
}
|