标题:
使用STC15F104E单片机做的红外遥控解码调光程序 PWM输出频率65Hz
[打印本页]
作者:
luohui0819
时间:
2017-9-8 14:37
标题:
使用STC15F104E单片机做的红外遥控解码调光程序 PWM输出频率65Hz
单片机源程序如下:
/******************************************************************************************
遥控调光,PWM输出频率65Hz.
******************************************************************************************/
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define BAUD 0xfb00 // 原先是0xef00
//#define BAUD 0xFE80
#define IAP_ADDRESS 0x800
//#define ENABLE_IAP 0x82 //if SYSCLK<20MHz
/*Declare SFR associated with the IAP */
sfr IAP_DATA = 0xC2; //Flash data register
sfr IAP_ADDRH = 0xC3; //Flash address HIGH
sfr IAP_ADDRL = 0xC4; //Flash address LOW
sfr IAP_CMD = 0xC5; //Flash command register
sfr IAP_TRIG = 0xC6; //Flash command trigger
sfr IAP_CONTR = 0xC7; //Flash control register
sfr AUXR = 0x8E;
sbit RXB = P3^0; //define UART TX/RX port
sbit TXB = P3^1;
sbit k1=P3^0;
sbit k3=P3^2;
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^1;
sbit key4=P3^3;
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE t, r;
BYTE buf[16];
BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;
//uchar test;
uchar j=0;
uchar last=0;
uchar b=0;
uchar count2=0;
uchar fang=0;
uchar power=0;
uint k;
uchar i;
uchar a,b;
uint count=0;
uchar count_close=0;
uchar read_buff[6];
//void IapIdle(); //摸拟串口要用到
//-----------------------------------------------------
void delay(unsigned int t) // 延时函数
{
for(;t!=0;t--) ;
}
uchar read()
{
count=0;
j=0;
while(k3==0) //低 9ms
{
count++;
}
if(count<850) //9ms 应该大于6400
{
goto pp2;
}
count=0;
while(k3==1) //高 4.5ms
{
count++;
if(count>4000)
{
goto pp2;
}
}
if(count<2000)
{
goto pp2;
}
for(b=0;b<4;b++) ////接收来自遥控器的3个码
{
for(a=0;a<8;a++)
{
delay(50); //低
while(k3==0)
{
count++;
if(count>10000)
{
goto pp2;
}
}
j>>=1;
count=0;
while(k3==1)//高
{
count++;
if(count>10000)
{
goto pp2;
}
}
if(count>500)
{
j|=0x80;
}
}
read_buff[b]=j;
}
pp1:if(read_buff[2]<28)
{
j=fang=read_buff[2];
}
else
{
j=0xff;
}
delay(50000);
pp2:delay(50000);
return j;
}
/********************************************************************************/
void tm0() interrupt 1 using 2
{
count2++;
if(count2>49)
count2=0;
if(count2<count_close)
{
k1=1;
// k2=1;
// k4=1;
}
else
{
k1=0;
// k2=0;
// k4=0;
}
/*
if(i==1)//用于识别遥控器是否长按下
{
k++;
if(k>15000)
{
k=15500;
i=0;
}
} */
}
/*******************************************************************************/
main()
{
//P3M0=0X08;
TMOD = 0x00; //timer0 in 16-bit auto reload mode
AUXR = 0x80; //timer0 working at 1T mode
TL0 = BAUD;
TH0 = BAUD>>8; //initial timer0 and set reload value
TR0 = 1; //tiemr0 start running
ET0 = 1; //enable timer0 interrupt
PT0 = 1; //improve timer0 interrupt priority
TING = 1;
RING = 0;
TEND = 0;
REND = 0;
TCNT = 0;
RCNT = 0;
EX0=0;
PX0=1;
EA = 1;
power=1;
count_close=0;
while(1)
{
if(k3==0)
{
last=read();
}
if(last==11) //亮度增加
{
if(power==1)
{
last=0xff;
i=1;
if(count_close>50) // 原先是40
{
count_close=50;
}
if(count_close>0)
{
count_close--;
}
if(count_close<1)
count_close=0;
}
}
if(last==19) //亮度减少
{
if(power==1)
{
last=0xff;
i=1;
if(count_close>50) // 原先是40
{
count_close=50;
}
count_close++;
}
}
if(last==23) //熄灭
{
last=0xff;
power=0;
count_close=50; // 原先是40
}
if(last==27) //开灯
{
last=0xff;
power=1;
count_close=0;
}
if(last==0x04)
{
if(power==1)
{
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
红外解码调光.rar
(25.58 KB, 下载次数: 99)
2017-9-8 14:37 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
cycserver
时间:
2024-11-8 10:02
感谢了,学习中
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1