标题:
分享一个STC单片机的NEC遥控解码程序与线路
[打印本页]
作者:
taojc
时间:
2020-3-14 18:39
标题:
分享一个STC单片机的NEC遥控解码程序与线路
解码NEC遥控,由串口输出,采用STC超小型单片机
分享了线路图,原代码,HEX码
51hei.png
(85.24 KB, 下载次数: 37)
下载附件
2020-3-14 18:46 上传
单片机源程序如下:
#include <main.h>
//#include <reg52.h>
#include <stdio.h>
void Timer2Init(void) //30??@24.000MHz
{
AUXR &= 0xFB; //?????12T??
T2L = 0xA0; //??????
T2H = 0x15; //??????
AUXR |= 0x10; //???2????
EA=1;
IE2=0x04|IE2;
TR2=1;
}
/*----------------------------
UART 中断服务程序
-----------------------------*/
void Uart() interrupt 4 using 1
{
if (RI)
{
RI = 0; //清除RI位
P0 = SBUF; //P0显示串口数据
P22 = RB8; //P2.2显示校验位
}
if (TI)
{
TI = 0; //清除TI位
busy = 0; //清忙标志
}
}
/*----------------------------
发送串口数据
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //等待前面的数据发送完成
ACC = dat; //获取校验位P (PSW.0)
if (P) //根据P来设置校验位
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //设置校验位为0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //设置校验位为1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //设置校验位为1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //设置校验位为0
#endif
}
busy = 1;
SBUF = ACC; //写数据到UART数据寄存器
}
/*----------------------------
发送字符串
----------------------------*/
void SendString(char *s)
{
while (*s) //检测字符串结束标志
{
SendData(*s++); //发送当前字符
}
}
void Sendint(unsigned int DATA)
{unsigned char UPDATA,LODATA;
LODATA=DATA&0xff;
UPDATA=(DATA&0xff00)>>8;
SendData(UPDATA);
SendData(LODATA);
SendData(0x0D);
SendData(0x0A);
}
/*****************************************************************************************************
Function name: Delay
Input: t
output: None
By: Tao JC
Date: 06-Apr2005
*******************************************************************************************************/
void delay(unsigned long t)
{
t*=10;
while (t--) ;
}
/**********************************************************************************************************
*******************************************************************************************************/
void init_counter(void)
{
// TCON=0x55;
// IE=0x87;
IP=0x01;
// TH0=0x00;
// TL0=0x00;
TH0=RELOAD_HI; //reload timer reset timer
TL0=RELOAD_LO; //reload timer reset timer
TR0=1; //start timer
// T2CON=0x00;
// ET2=1;
// RCAP2H=0x00;
// RCAP2L=0x00;
// TR2=1;
}
/*****************************************************************************************************
Function name: Time_out
Input: RELOAD_HI,RELOAD_LO
output: Counter value
By: Tao JC
Date: 22-Jan-2006
Function: count the pluse width of the IR input
*******************************************************************************************************/
void Time_out() interrupt 1{
TR0=0;
// RED_LED=0;
// GRN_LED=0;
//if timer out, stop timer
/*
if(PowerKeyPress)
counter++;
if (counter >=60)
{PWR_BTN=0;
GRN_LED=0;
RED_LED=0;
counter=0;
PowerKeyPress=0;
}
counter++;
if(counter<=300)
{RED_LED=0;
GRN_LED=0;
}
if(counter>=600)
{RED_LED=1;
GRN_LED=1;
counter=0;
}
*/
TH0=RELOAD_HI; //reset timer
TL0=RELOAD_LO; //reset timer
// TH0=0xff; //reset timer
// TL0=0xff; //reset timer
Pulse_Qty = Pulse_index;
if (Pulse_Qty ==4)
{
EX0=0;
Cont_Key_Pressed = 1;
}
Pulse_index=0; //clear data
// Output_Switch=1; //output data is allowed
TR0=1;
Remote_DataType = 0; // 1: Key Pressed, 2: Repeat Key Pressed
Remote_Data = 0x00ff;
}
void ExtInt0Init(void) //30??@24.000MHz
{
IT0 = 0; //??INT0????? (1:???? 0:???????)
EX0 = 1; //??INT0??
EA = 1;
}
/*****************************************************************************************************
Function name: IR interrupt routine
Input: IR_IN
output: DATA_OUT
By: Tao JC
Date: 15-May-2011 modify to fit the NEC code
Function: if any clock on the P3.3, trig the DATA-OUT
*******************************************************************************************************/
void IR_int() interrupt 0
{
TR0=0; //stop timer
EX0=0;
RED_LED=0;
// Lead code = 9ms + 4.5ms, 1 cycle >= 0.5us
T_Pulse[Pulse_index]=(TH0-RELOAD_HI)*256 +(TL0-RELOAD_LO); // get the counter value
// if(T_Pulse[Pulse_index]>(128*NECT)) // NECT = 250,
if(T_Pulse[Pulse_index]>(80*NECT)) // NECT = 250,
{ // 80*250 = 20000, 20000*0.5 = 10ms
// 128 *250 = 32000,
Pulse_index=0;
Remote_DataType = 0; // 1: Key Pressed, 2: Repeat Key Pressed
Remote_Data = 0x00ff;
/* if (First_Int == 1)
{
First_Int = 0;
Pulse_index=1;
T_Pulse[Pulse_index] = 0x4666;
}
*/
}
else
{
if(T_Pulse[Pulse_index]>(64*NECT)) // 64 *250 = 16000,16000*0.5 = 8ms
{ // 1 cycle = 0.5625
Pulse_index=1;
//T_Pulse[Pulse_index]=(TH0-RELOAD_HI)*256 +(TL0-RELOAD_LO);
}
}
if(Pulse_index<=68)
{
Pulse_index++;
//printf("pulse index= %bu \n", Pulse_index );
}
if(Pulse_index>=68)
{
Output_Switch=1;
RED_LED=1;
//printf("output switch=1\n");
Pulse_index=0;
}
TH0=RELOAD_HI; //reload timer reset timer
TL0=RELOAD_LO; //reload timer reset timer
XOR=!XOR; //wait for next pulse
//GRN_LED=XOR;
IP=0x01; //set interrupt priority =1
//TR0=1; //start timer
if (Output_Switch == 1)
{
EX0=0;
TR0=0; //stop timer
}
else
{
EX0=1;
TR0=1;
}
}
void Timer2_init_baud()
{
ACC = P_SW1;
ACC &= ~(S1_S0 | S1_S1); //S1_S0=0 S1_S1=0
P_SW1 = ACC; //(P3.0/RxD, P3.1/TxD)
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8位可变波特率
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9位可变波特率,校验位初始为1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9位可变波特率,校验位初始为0
#endif
T2L = (65536 - (FOSC/4/BAUD)); //设置波特率重装值
T2H = (65536 - (FOSC/4/BAUD))>>8;
AUXR = 0x14; //T2为1T模式, 并启动定时器2
AUXR |= 0x01; //选择定时器2为串口1的波特率发生器
ES = 1; //使能串口1中断
EA = 1;
}
/**********************************************************************
Function name: timer 2 interrupt
Input: internal generate 31.25ms timer signal
Output: toggle BLINK
By: Tao JC
Date: 31-May-2005
****************************************************************************/
void system_tick(void) interrupt 12{
TF2=0;
TR2=0; //stop timer
RCAP2H=RELOAD_HI_T2; //reload timer high
RCAP2L=RELOAD_LO_T2; //Reload timer low
TR2=1; //start timer 0
if(Second_Count)
{
Second_Count--;
if(Second_Count==0)
{
Second_Count=ONOFFDURATION;
GRN_LED=1;
RED_LED=1;
PWR_BTN=0;
} // 1.25 s, change step once
}
}
/******************************************************************************************************/
/*****************************************************************************************************/
void Timer0Init(void) //30??@24.000MHz
{
AUXR &= 0x7F; //?????12T??
TMOD &= 0xF0; //???????
TL0 = 0xA0; //??????
TH0 = 0x15; //??????
TF0 = 0; //??TF0??
TR0 = 1; //???0????
ET0=1;
}
void System_Init()
{
//init_uart();
Timer0Init();
Timer2Init();
ExtInt0Init();
KeyInput=0xffff;
P0=0xFF;
UART_INT=0;
PWR_BTN=0;
TR2=1;
// init_counter();
Timer2_init_baud();
SendString("****Baud 115200 **********\r\n");
SendString("**IR SENSOR HW Version 1.0***\r\n");
SendString("**IR SENSOR FW Version 1.4***\r\n");
SendString("****************************\r\n");
// GRN_LED=0;
// RED_LED=0;
First_Int = 1;
Cont_Key_Pressed = 0;
Pulse_index=0;
XOR=1; //reset the 74LS86
Output_Switch=0;
Error_Flag=0;
Remote_DataType = 0; // 1: Key Pressed, 2: Repeat Key Pressed
Remote_Data = 0x00ff;
Repeat_Key_Counter = 0;
}
char xlat2char(int x)
{
char res = 0xff;
if (x>=0 && x<=9){
res = x+'0';
} else if(x >= 0xA && x <= 0xF) {
res = 'a' + (x-0xA);
}
return res;
}
void main(void) //主程序
{
unsigned char i=0;
unsigned int IR_Data;
unsigned char Temp_Data;
//unsigned char Error_Pattern;
System_Init();
EA=1;
KeyInput_valid_flag = 0;
while(1)
{
if (Output_Switch==1)
{
//RED_LED=0;
//Lead code check
// Lead code = 9ms + 4.5ms
/*
printf("location 1,Pulse width= %x \n",T_Pulse[1]); // 0x4646
printf("location 2,Pulse width= %x \n",T_Pulse[2]); // 0x108a
printf("location 3,Pulse width= %x \n",T_Pulse[3]); // 0x4646
printf("location 4,Pulse width= %x \n",T_Pulse[4]); // 0x108a
printf("location 5,Pulse width= %x \n",T_Pulse[5]); // 0x4646
printf("location 6,Pulse width= %x \n",T_Pulse[6]); // 0x108a
printf("location 7,Pulse width= %x \n",T_Pulse[7]); // 0x4646
printf("location 8,Pulse width= %x \n",T_Pulse[8]); // 0x108a
printf("location 9,Pulse width= %x \n",T_Pulse[9]); // 0x4646
printf("location 10,Pulse width= %x \n",T_Pulse[10]); // 0x108a
printf("location 11,Pulse width= %x \n",T_Pulse[11]); // 0x4646
printf("location 12,Pulse width= %x \n",T_Pulse[12]); // 0x108a
printf("location 13,Pulse width= %x \n",T_Pulse[13]); // 0x4646
printf("location 14,Pulse width= %x \n",T_Pulse[14]); // 0x108a
printf("location 15,Pulse width= %x \n",T_Pulse[15]); // 0x4646
printf("location 16,Pulse width= %x \n",T_Pulse[16]); // 0x108a
printf("location 17,Pulse width= %x \n",T_Pulse[17]); // 0x4646
printf("location 18,Pulse width= %x \n",T_Pulse[18]); // 0x108a
printf("location 19,Pulse width= %x \n",T_Pulse[19]); // 0x4646
printf("location 20,Pulse width= %x \n",T_Pulse[20]); // 0x108a
printf("location 21,Pulse width= %x \n",T_Pulse[21]); // 0x4646
printf("location 22,Pulse width= %x \n",T_Pulse[22]); // 0x108a
printf("location 23,Pulse width= %x \n",T_Pulse[23]); // 0x4646
printf("location 24,Pulse width= %x \n",T_Pulse[24]); // 0x108a
printf("location 25,Pulse width= %x \n",T_Pulse[25]); // 0x4646
printf("location 25,Pulse width= %x \n",T_Pulse[26]); // 0x108a
printf("location 27,Pulse width= %x \n",T_Pulse[27]); // 0x4646
printf("location 28,Pulse width= %x \n",T_Pulse[28]); // 0x108a
printf("location 29,Pulse width= %x \n",T_Pulse[29]); // 0x4646
printf("location 30,Pulse width= %x \n",T_Pulse[30]); // 0x108a
printf("location 31,Pulse width= %x \n",T_Pulse[31]); // 0x4646
printf("location 32,Pulse width= %x \n",T_Pulse[32]); // 0x108a
printf("location 33,Pulse width= %x \n",T_Pulse[33]); // 0x4646
printf("location 34,Pulse width= %x \n",T_Pulse[34]); // 0x108a
*/
if(T_Pulse[1]>(64*NECT))
{
if(T_Pulse[2]>(48*NECT)||T_Pulse[2]<(14*NECT))
{
Error_Flag=1;
//printf("error location 2,Pulse width= %x \n",T_Pulse[2]);
// IR_Data=0xff;
Output_Switch=0;
KeyInput_valid_flag = 0;
}
}
else
{
Error_Flag=1;
//printf("error location 1,pulse width= %x \n",T_Pulse[1] );
// IR_Data=0xff;
Output_Switch=0;
KeyInput_valid_flag = 0;
}
// NEC decoder
i=3;
T_Pulse[1]=0;
T_Pulse[2]=0;
while(i<=65&&Output_Switch)
{
// #define SHORTT 1350 = 0.56ms,
// #define LONGT 3000 = 1.69ms
// Data 0 = 0.56ms + 0.565ms = 1.125ms
// Data 1 = 0.56ms + 1.69ms = 2.25ms
if(T_Pulse[i]<SHORTT)
{
T_Pulse[i]=0;
if(T_Pulse[i+1]<SHORTT)
Temp_Data=0;
else
if(T_Pulse[i+1]>LONGT)
Temp_Data=1;
IR_Data=IR_Data<<1;
IR_Data=Temp_Data+IR_Data;
T_Pulse[i+1]=0;
i=i+2;
KeyInput_valid_flag = 1;
}
else //error handle
{
IR_Data=0xff;
Pulse_index=0;
Remote_DataType = 0; // 1: Key Pressed, 2: Repeat Key Pressed
Remote_Data = 0x00ff;
Output_Switch=0;
//printf("error location 3, Pulse width = %bu,%x \n", i,T_Pulse[i]);
KeyInput_valid_flag = 0;
}
}
Pulse_index=0; //clear the data buffer
Remote_DataType = 0; // 1: Key Pressed, 2: Repeat Key Pressed
Remote_Data = 0x00ff;
KeyInput=IR_Data;
//printf("KeyInput = %x \n", KeyInput);
if (KeyInput_valid_flag == 1)
{
// printf("%04x \n", KeyInput);
Repeat_Key_Counter = 0;
Cont_Key_Pressed = 0;
}
UART_INT=1;
// GRN_LED=0;
if(KeyInput!=0xff)
Output_Switch=0;
TH0=RELOAD_HI; //reload timer reset timer
TL0=RELOAD_LO; //reload timer reset timer
IP=0x01; //set interrupt priority =1
TR0=1;
EX0=1;
}
else
{
/*
printf("Output_Switch = 0, Pulse_Qty= %x \n",Pulse_Qty); //
// printf("Output_Switch = 0, Remote_test,Pulse width= %x,First_Int = %x \n",Remote_test,First_Int); //
printf("Output_Switch = 0, location 1,Pulse width= %x \n",T_Pulse[1]); // 0x4646
printf("Output_Switch = 0, location 2,Pulse width= %x \n",T_Pulse[2]); // 0x108a
printf("Output_Switch = 0, location 3,Pulse width= %x \n",T_Pulse[3]); // 0x4646
printf("Output_Switch = 0, location 4,Pulse width= %x \n",T_Pulse[4]); // 0x108a
*/
if (Cont_Key_Pressed == 1)
{
if (Repeat_Key_Counter <= 0)
{
Repeat_Key_Counter++;
Cont_Key_Pressed = 0;
T_Pulse[1] =0;
T_Pulse[2] =0;
T_Pulse[3] =0;
T_Pulse[4] =0;
Pulse_index=0; //clear the data buffer
UART_INT=1;
TH0=RELOAD_HI; //reload timer reset timer
TL0=RELOAD_LO; //reload timer reset timer
IP=0x01; //set interrupt priority =1
TR0=1;
EX0=1;
}
else
{
Cont_Key_Pressed = 0;
T_Pulse[1] =0;
T_Pulse[2] =0;
T_Pulse[3] =0;
T_Pulse[4] =0;
Pulse_index=0; //clear the data buffer
// KeyInput=IR_Data;
KeyInput=0x55aa;
// printf("KeyInput = %x \n", KeyInput);
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
code.zip
(74.19 KB, 下载次数: 22)
2020-3-14 18:39 上传
点击文件名下载附件
下载积分: 黑币 -5
IR sensor.pdf
(31.24 KB, 下载次数: 14)
2020-3-14 18:39 上传
点击文件名下载附件
下载积分: 黑币 -5
tec_datasheet_gp1ud26xk27_28_yk_series.pdf
(105.61 KB, 下载次数: 15)
2020-3-14 18:39 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
wsh791
时间:
2020-3-15 00:33
学习一下
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1