久久ER99热精品一区二区-久久精品99国产精品日本-久久精品免费一区二区三区-久久综合九色综合欧美狠狠

新聞中心

EEPW首頁 > 嵌入式系統 > 設計應用 > 基于單片機的紅外發射C語言程序

基于單片機的紅外發射C語言程序

作者: 時間:2016-11-11 來源:網絡 收藏
#include

sbit P3_4 = P2^1;

本文引用地址:http://cqxgywz.com/article/201611/316576.htm

static bit OP; //紅外發射管的亮滅
static unsigned int count; //延時計數器
static unsigned int endcount; //終止延時計數
static unsigned char flag; //紅外發送標志
char iraddr1; //十六位地址的第一個字節
char iraddr2; //十六位地址的第二個字節

void SendIRdata(char p_irdata);
void delay();

void main(void)
{
count = 0;
flag = 0;
OP = 0;
P3_4 = 0;

EA = 1; //允許CPU中斷
TMOD = 0x11; //設定時器0和1為16位模式1
ET0 = 1; //定時器0中斷允許


TH0 = 0xFF;
TL0 = 0xE8; //設定時值0為38K 也就是每隔26us中斷一次
TR0 = 1;//開始計數

iraddr1=3;
iraddr2=252;


do{
unsigned char i;
delay();
SendIRdata(12);
}while(1);
}

//定時器0中斷處理
void timeint(void) interrupt 1
{
TH0=0xFF;
TL0=0xE8; //設定時值為38K 也就是每隔26us中斷一次
count++;

if (flag==1)
{
OP=~OP;
}
else
{
OP = 0;
}
P3_4 = OP;
}


void SendIRdata(char p_irdata)
{
int i;
char irdata=p_irdata;

//發送9ms的起始碼
endcount=223;
flag=1;
count=0;
do{}while(count

//發送4.5ms的結果碼
endcount=117;
flag=0;
count=0;
do{}while(count

//發送十六位地址的前八位
irdata=iraddr1;
for(i=0;i<8;i++)
{

//先發送0.56ms的38KHZ紅外波(即編碼中0.56ms的低電平)
endcount=10;
flag=1;
count=0;
do{}while(count

//停止發送紅外信號(即編碼中的高電平)
if(irdata-(irdata/2)*2) //判斷二進制數個位為1還是0
{
endcount=41; //1為寬的高電平
}
else
{
endcount=15; //0為窄的高電平
}
flag=0;
count=0;
do{}while(count

irdata=irdata>>1;
}

//發送十六位地址的后八位
irdata=iraddr2;
for(i=0;i<8;i++)
{
endcount=10;
flag=1;
count=0;
do{}while(count

if(irdata-(irdata/2)*2)
{
endcount=41;
}
else
{
endcount=15;
}
flag=0;
count=0;
do{}while(count

irdata=irdata>>1;
}

//發送八位數據
irdata=p_irdata;
for(i=0;i<8;i++)
{
endcount=10;
flag=1;
count=0;
do{}while(count

if(irdata-(irdata/2)*2)
{
endcount=41;
}
else
{
endcount=15;
}
flag=0;
count=0;
do{}while(count

irdata=irdata>>1;
}

//發送八位數據的反碼
irdata=~p_irdata;
for(i=0;i<8;i++)
{
endcount=10;
flag=1;
count=0;
do{}while(count

if(irdata-(irdata/2)*2)
{
endcount=41;
}
else
{
endcount=15;
}
flag=0;
count=0;
do{}while(count

irdata=irdata>>1;
}

endcount=10;
flag=1;
count=0;
do{}while(count flag=0;
}

void delay()
{
int i,j;
for(i = 0; i < 400; i++){
for(j = 0; j < 200; j++){
}
}
}



評論


技術專區

關閉