标题:
STC15F单片机红外接收(遥控风扇)源码
[打印本页]
作者:
YYHA
时间:
2018-11-4 11:19
标题:
STC15F单片机红外接收(遥控风扇)源码
//////STC15F104////////////
#include <STC15F2K60S2.H>
#include "74HC595.H" //包含74HC595头文件
#define uchar unsigned char
#define uint unsigned int
#define T50MS (65536-50000) //12T模式
/************* 用户系统配置 **************/
#define MAIN_Fosc 12000000L //定义主时钟, 模拟串口和红外接收会自动适应。5~36MHZ
#define D_TIMER0 125 //选择定时器时间, us, 红外接收要求在60us~250us之间
#define User_code 0xFD02 //定义红外接收用户码
/************* 以下宏定义用户请勿修改 **************/
#define freq_base (MAIN_Fosc / 1200)
#define Timer0_Reload (65536 - (D_TIMER0 * freq_base / 10000))
/************* 本地变量声明 **************/
bit P_IR_RX_temp; //Last sample
bit B_IR_Sync; //已收到同步标志
uchar IR_SampleCnt; //采样计数
uchar IR_BitCnt; //编码位数
uchar IR_UserH; //用户码(地址)高字节
uchar IR_UserL; //用户码(地址)低字节
uchar IR_data; //数据原码
uchar IR_DataShit; //数据反码
bit B_IrUserErr; //User code error flag
bit B_IR_Press; //Key press flag,include repeat key.
uchar IR_code; //IR code 红外键码
sbit KG =P3^1;
sbit FMQ =P3^0;
sbit P_IR_RX =P3^2; //定义红外接收输入端口
uchar code SMG[11]={0x77,0x60,0x3b,0x7a,0x6c,0x5e,0x5f,0x70,0x7f,0x7e, 0x00};//0, 1, 2, 3,4, 5, 6, 7, 8, 9, off
uint i2,shi,fen,miao;
uint i1,ontime,offtime;
uchar k,data1=0x01;
bit b;
//uchar a[4]; //储存用户码、用户反码与键数据码、键数据反码
/************* 本地函数声明 **************/
void Tx1Send(uchar dat);
uchar HEX2ASCII(uchar dat);
void InitTimer(void);
void PrintString(unsigned char code *puts);
//////////////////////////////////////////
void delay1ms(void)
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++)
;
}
////////////////////////////////////////////
void delay(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
delay1ms();
}
////////////////////////////////////////////
void beep() //蜂鸣器响一声函数
{
FMQ=0;
delay(250); //延时
FMQ=1; //关闭蜂鸣器
}
////////////////////////////////////////////
void display(void) //显示定时时间。
{
if(shi==0) //当定时时间为0时,不显示
{
Ser_IN(SMG[10]);
Par_OUT();
}
else
{
Ser_IN(SMG[shi]);
Par_OUT();
}
}
/////////////////////////////////////////////////////////////
void Model_2(void)
{
ontime=40;
offtime=40;
}
/////////////////////////////////////////////////////////////
void Model_3(void)
{
ontime=100;
offtime=40;
}
/////////////////////////////////////////////////////////////
void Model_4(void)
{
ontime=200;
offtime=80;
}
/////////////////////////////////////////////////////////////
void Model_5(void)
{
ontime=700;
offtime=300;
}
/////////////////////////////////////////////////////////////
//判断接收的编码,执行相应功能
void _data_(void)
{
switch(IR_data)
{
case 0x01:k++;if(k==6) k=1;i1=0;b=1;beep();break; //开机及模式调节。蜂鸣器响一声 提示解码成功,模式切换时i1必须清零
case 0x05:shi++; if(shi==10) shi=0;beep();break; //定时时间1~9小时
case 0x09:break; //此功能暂时无效
case 0x0d:k=0;beep();break; //关机
}
IR_data=0x00; //必须清零
}
////////////////////////////////////////////
void main(void) //主函数
{
IE = 0x82; //开总中断和T0中断允许
IE2 = 0x04; //T2中断允许
TMOD = 0; //for STC15Fxxx系列 Timer0 as 16bit reload timer.
TH0 = Timer0_Reload / 256;
TL0 = Timer0_Reload % 256;
T2L = T50MS; //初始化计时值5ms中断一次
T2H = T50MS >> 8;
TR0 =1;
k =0;
while(1)
{
switch(k)
{
case 0: AUXR=0x00; //k=0时T2关闭中断
i1=0; //记得计时清零
shi=0; //关闭风扇时,定时时长也清零
KG=1; //关闭风扇
break;
case 1: AUXR=0x10; //k不等于0时打开中断
break;
case 2: AUXR=0x10; //k=2~5时启动相应模式
Model_2();
break;
case 3: AUXR=0x10;
Model_3();
break;
case 4: AUXR=0x10;
Model_4();
break;
case 5: AUXR=0x10;
Model_5();
break;
}
display();
}
}
/*************红外接收部分程序是引用STC官方的资料******************************/
/******************** 红外采样时间宏定义, 用户不要随意修改 *******************/
#if ((D_TIMER0 <= 250) && (D_TIMER0 >= 60))
#define D_IR_sample D_TIMER0 //定义采样时间,在60us~250us之间
#endif
#define D_IR_SYNC_MAX (15000/D_IR_sample) //SYNC max time
#define D_IR_SYNC_MIN (9700 /D_IR_sample) //SYNC min time
#define D_IR_SYNC_DIVIDE (12375/D_IR_sample) //decide data 0 or 1
#define D_IR_DATA_MAX (3000 /D_IR_sample) //data max time
#define D_IR_DATA_MIN (600 /D_IR_sample) //data min time
#define D_IR_DATA_DIVIDE (1687 /D_IR_sample) //decide data 0 or 1
#define D_IR_BIT_NUMBER 32 //bit number
//*******************************************************************************************
//**************************** IR RECEIVE MODULE ********************************************
void IR_RX_HT6121(void)
{
uchar SampleTime;
IR_SampleCnt++; //Sample + 1
F0 = P_IR_RX_temp; //Save Last sample status
P_IR_RX_temp = P_IR_RX; //Read current status
if(F0 && !P_IR_RX_temp) //Last sample is high,and current sample is low, so is fall edge
{
SampleTime = IR_SampleCnt; //get the sample time
IR_SampleCnt = 0; //Clear the sample counter
if(SampleTime > D_IR_SYNC_MAX) B_IR_Sync = 0; //large the Maxim SYNC time, then error
else if(SampleTime >= D_IR_SYNC_MIN) //SYNC
{
if(SampleTime >= D_IR_SYNC_DIVIDE)
{
B_IR_Sync = 1; //has received SYNC
IR_BitCnt = D_IR_BIT_NUMBER; //Load bit number
}
}
else if(B_IR_Sync) //has received SYNC
{
if(SampleTime > D_IR_DATA_MAX) B_IR_Sync=0; //data samlpe time to large
else
{
IR_DataShit >>= 1; //data shift right 1 bit
if(SampleTime >= D_IR_DATA_DIVIDE) IR_DataShit |= 0x80; //devide data 0 or 1
if(--IR_BitCnt == 0) //bit number is over?
{
B_IR_Sync = 0; //Clear SYNC
if(~IR_DataShit == IR_data) //判断数据正反码
{
if((IR_UserH == (User_code / 256)) &&
IR_UserL == (User_code % 256))
B_IrUserErr = 0; //User code is righe
else B_IrUserErr = 1; //user code is wrong
IR_code = IR_data;
B_IR_Press = 1; //数据有效
_data_();
}
}
else if((IR_BitCnt & 7)== 0) //one byte receive
{
IR_UserL = IR_UserH; //Save the User code high byte
IR_UserH = IR_data; //Save the User code low byte
IR_data = IR_DataShit; //Save the IR data byte
}
}
}
}
}
/********************** Timer0中断函数************************/
void timer0 (void) interrupt 1
{
IR_RX_HT6121();
}
复制代码
0.png
(7.54 KB, 下载次数: 39)
下载附件
2018-11-4 13:52 上传
全部资料51hei下载地址:
红外接收(遥控风扇).zip
(37.56 KB, 下载次数: 91)
2018-11-4 11:18 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
ssk7793
时间:
2018-11-22 07:44
非常好的程序,实测可用.怎么没用顶呢?
作者:
ceflsh
时间:
2020-1-9 22:23
学习了,谢谢楼主,
作者:
379225396
时间:
2020-5-2 16:56
直接下载进去好像不得用 太复杂也不利于学习
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1