這是一個紅外發碼解碼的原理,利用一個普通的紅外發射管,利用一個IO口,發射38KHz的載波與按鍵。這個原理圖中沒有按鍵,所以程序中就發了固定的紅外碼值,如果需要加入按鍵的話直接加入按鍵掃描的程序即可。本程序是的開發語言是mini C(為了適用特別的編譯器),基本上是和C語言一樣的,只是個別關鍵字不一樣而已。
電路原理圖如下:
原理圖.png (46.77 KB, 下載次數: 29)
下載附件
2019-10-15 09:40 上傳
單片機源程序如下:
//發射源碼
#include "extern.h"
.RAMADR 0
WORD zero;
WORD count;
WORD set_count;
WORD cnt0;
BYTE FLAG_1;
//WORD cnt1;
//BYTE cnt2;
//BYTE cnt3;
//BYTE irdata;
BYTE ircode;
//.RAMADR SYSTEM
BIT ir : PA.3;
BIT ir_flag : FLAG_1.0;
void INIT(void)
{
PA=0XFF; //IO
PAC=0B_1111_1111; //輸入設置 1為輸出 PA4輸入
PAPH=0X00; //無上拉
$ PADIER 0B_0000_0000; //使能數字輸入及喚醒功能,0數字,1模擬
// disgint ;
inten.2=1; //tm16溢出中斷
intrq=0;
t16m=0B_001_00_000; //SYSCLK_4m,1分頻,8位中斷,1/(4M/1) = 0.25us
integs=0B_0000_0000;
WORD t_count=215;
stt16 t_count; //tm16置初值,定時0.25*(256-204)=13us
engint; //使能中斷
}
void delay_ms(void)//24ms
{
while(1)
{
cnt0++;
if(cnt0>=8000)
{
cnt0=0;
break;
}
}
}
void delay_s(void)
{
BYTE i = 2;//2
do
{
delay_ms();
}while(i--);
}
void ir_sendbyte(void)
{
BYTE i = 8;
do
{
i--;
set_count = 43;//發送560us高電平
ir_flag = 1;//啟動紅外發射
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;//關閉定時器
if(ircode.0) set_count = 130;//判斷紅外編碼最低位,若為1則為1.69ms低電平
else set_count = 43;//否則為560us低電平
ir_flag = 0;//低電平不發射
count = 0;
set1 t16m.5;
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;
ircode = ircode>>1;
}while(i);
}
void ir_send1(void)
{
set_count = 782;//692發送編碼中的引導碼(9ms+4.5ms)
ir_flag = 1;//啟動紅外
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;//關閉定時器
set_count = 346;//發送編碼中的4.5ms低電平
ir_flag = 0;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;//關閉定時器
ircode = 0x00;//用戶碼前8位
ir_sendbyte();
ircode = 0xff;//用戶碼后8位
ir_sendbyte();
ircode = 0x56;//8位數據碼0x56 測試0x45 反0xba
ir_sendbyte();
ircode = 0xa9;//數據碼反碼0xa9
ir_sendbyte();
set_count = 43;//發送編碼中的560us高電平
ir_flag = 1;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;//關閉定時器
ir = 1;
delay_ms();//23ms
set_count = 782;//發送引導碼
ir_flag = 1;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;
set_count = 346;//發送編碼中的4.5ms低電平
ir_flag = 0;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;
set_count = 43;
ir_flag = 1;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;
ir = 1;
}
void ir_send2(void)
{
set_count = 782;//692發送編碼中的引導碼(9ms+4.5ms)
ir_flag = 1;//啟動紅外
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;//關閉定時器
set_count = 346;//發送編碼中的4.5ms低電平
ir_flag = 0;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;//關閉定時器
ircode = 0x00;//用戶碼前8位
ir_sendbyte();
ircode = 0xff;//用戶碼后8位
ir_sendbyte();
ircode = 0x34;//8位數據碼0x34 測試0x47 反0xb8
ir_sendbyte();
ircode = 0xcb;//數據碼反碼0xcb
ir_sendbyte();
set_count = 43;//發送編碼中的560us高電平
ir_flag = 1;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;//關閉定時器
ir = 1;
delay_ms();//23ms
set_count = 782;//發送引導碼
ir_flag = 1;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;
set_count = 346;//發送編碼中的4.5ms低電平
ir_flag = 0;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;
set_count = 43;
ir_flag = 1;
count = 0;
set1 t16m.5;//開啟定時器
while(1)
{
if(count > set_count) break;
}
set0 t16m.5;
ir = 1;
}
void FPPA0 (void)
{
.ADJUST_IC SYSCLK=IHRC/4 // SYSCLK=IHRC/2
// Insert Initial Code
//清除RAM
Zero = _SYS(RAM_SIZE)-1;
a = 0;
do
{
*Zero = a;
}while(--Zero$0);
count = 0;
ir_flag = 0;
INIT();
while (1)
{
ir_send1();
delay_s();
ir_send2();
delay_s();
// wdreset;
}
}
|