51单片机红外遥控发射NEC协议程序
时间: 2023-08-11 15:07:45 浏览: 146
NEC协议的红外遥控器驱动程序.pdf
以下是51单片机红外遥控发射NEC协议的程序代码:
```
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[33] = {0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff};
uchar count, i, j;
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void sendone(void)
{
P1 = 0;
delay(1);
P1 = 1;
delay(2);
P1 = 0;
delay(1);
}
void sendzero(void)
{
P1 = 0;
delay(1);
P1 = 1;
delay(1);
P1 = 0;
delay(2);
}
void send(uchar data)
{
uchar i;
for(i=0;i<8;i++)
{
if(data&0x01)
sendone();
else
sendzero();
data >>= 1;
}
}
void main()
{
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
TR0 = 1;
ET0 = 1;
EA = 1;
while(1)
{
send(0x00);
send(0x00);
send(0x00);
send(0x00);
send(0x00);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
send(0xff);
send(0x00);
delay(100);
}
}
void timer0() interrupt 1
{
TH0 = 0xFC;
TL0 = 0x67;
count++;
if(count == 15)
{
P1 = 1;
delay(1);
P1 = 0;
count = 0;
}
}
```
该程序使用了51单片机的定时器和外部中断,通过红外发射管发送NEC协议格式的红外码。在main函数中循环发送了一组红外码,通过定时器中断控制红外码的发送频率。其中sendone和sendzero函数分别表示发送1和0的红外码,send函数用于发送8位数据。注意在使用该程序时需要根据具体硬件电路设计进行相应的修改。
阅读全文