|
各位老師,這里的誤法錯誤需要怎么更改。
1.
#include <STC8G.H>
#include "IR.h"
//根據(jù)協(xié)議發(fā)送數(shù)據(jù),IO口模擬時序?qū)崿F(xiàn)
//延時8.77us,是一個周期的1/3 的時間 一個周期26.3us,38KHZ
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit s4=P3^3;
void main()
{
unsigned char anjian;
P1M1 = 0x00; //默認00與STC89系列IO通用,標準雙向IO模式
P1M0 = 0x00; //如果是使用STC89系列單片機,本段程序可以刪除
P3M1 = 0x00;
P3M0 = 0x00;
P5M1 = 0x00;
P5M0 = 0x00;
while(1) //無條件永遠執(zhí)行大循環(huán)
{
if(!s1) //這些按鍵按下,就會使anjian變量賦以相應的值。
anjian=1;
if(!s2)
anjian=2;
if(!s3)
anjian=3;
if(!s4)
anjian=4;
switch(anjian)
{
case 1:Send_NEC_Message(User_Code,Key1_Code);break;
case 2:Send_NEC_Message(User_Code,Key2_Code);break;
case 3:Send_NEC_Message(User_Code,Key3_Code);break;
case 4:Send_NEC_Message(User_Code,Key4_Code);break;
default:break;
}
}
}
2.
#ifndef __IR_H__
#define __IR_H__
#include<STC8G.H>
#define User_Code 0xff00
#define Key1_Code 0x41 //命令碼 按鍵1
#define Key2_Code 0x42 //命令碼 按鍵2
#define Key3_Code 0x43 //命令碼 按鍵3
#define Key4_Code 0x44 //命令碼 按鍵4
sbit IR_EN=P1^7;
void Send_NEC_Message(unsigned int User_Code,unsigned char data_Code);
#endif
3.
#include "IR.h"
#include "intrins.h"
void Delay8_77us(void) //@11.0592MHz
{
unsigned char data i;
_nop_();
i = 30;
while (--i);
}
//載波發(fā)射
void Send_IR(unsigned int i)
{
while (--i);
{
IR_EN=1;
Delay8_77us();
IR_EN=0;
Delay8_77us();
Delay8_77us();
}
}
//載波不發(fā)射
void No_Send_IR(unsigned int i)
{
while (--i);
{
IR_EN=0;
Delay8_77us();
Delay8_77us();
Delay8_77us();
}
}
//發(fā)射數(shù)據(jù) 0 ,符合NEC協(xié)議
void Send_NEC_0()
{
Send_IR(21);
No_Send_IR(21);
}
//發(fā)射數(shù)據(jù) 1 ,符合NEC協(xié)議
void Send_NEC_1()
{
Send_IR(21);
No_Send_IR(64);
}
//發(fā)射一幀完整的NEC信息 引導碼+用戶碼低八位+用戶碼高八位+8位數(shù)據(jù)碼+8位數(shù)據(jù)碼的反碼+結(jié)束碼“0”
void Send_NEC_Message(unsigned int User_Code,unsigned char data_Code)
{
unsigned int temp;
unsigned char i;
Send_IR(342);
No_Send_IR(171);
temp=User_Code;
for(i=0;i<16;i++)
{
if(temp&0x0001)
Send_NEC_1();
else
Send_NEC_0();
temp>>=1;
}
temp=data_Code;
for(i=0;i<8;i++)
{
if(temp&0x01)
Send_NEC_1();
else
Send_NEC_0();
temp>>=1;
}
temp=~data_Code;
for(i=0;i<8;i++)
{
if(temp&0x01)
Send_NEC_1();
else
Send_NEC_0();
temp>>=1;
}
Send_NEC_0();
}
|
-
1.png
(347.24 KB, 下載次數(shù): 24)
下載附件
2024-7-25 16:56 上傳
-
2.png
(223.45 KB, 下載次數(shù): 19)
下載附件
2024-7-25 16:56 上傳
-
3.png
(377.33 KB, 下載次數(shù): 18)
下載附件
2024-7-25 16:56 上傳
|