|
同上附上程序帮忙修改
谢谢·
主程序/*********************************************************************************************/
#include <STC12C2052AD.H> //头文件
#include "Infrared.c"
extern bit irflag;
extern unsigned char ircode[4];
extern void InitInfrared(void);
sbit dj11 = P1^7;
sbit dj12 = P1^6;
sbit dj21 = P1^5;
sbit dj22 = P1^4;
sbit LED1 = P1^3;
sbit LED2 = P1^2;
sbit LED3 = P1^1;
/*********************************************************************************************/
/*********************************************************************************************/
void delay (unsigned int a){
unsigned int i;
while( --a != 0){
for(i = 0; i < 600; i++);
}
}
/*********************************************************************************************/
/*********************************************************************************************
函数名:PWM初始化函数
调 用:PWM_init();
参 数:无
返回值:无
结 果:将PCA初始化为PWM模式,初始占空比为0
备 注:需要更多路PWM输出直接插入CCAPnH和CCAPnL即可
/**********************************************************************************************/
void PWM_init (void){
CMOD=0x02; //设置PCA定时器
CL=0x00;
CH=0x00;
CCAPM0=0x42; //PWM0设置PCA工作方式为PWM方式(0100 0010)
CCAP0L=0x00; //设置PWM0初始值与CCAP0H相同
CCAP0H=0x00; // PWM0初始时为0
CCAPM1=0x42; //PWM1设置PCA工作方式为PWM方式(使用时删除//)
CCAP1L=0x00; //设置PWM1初始值与CCAP0H相同
CCAP1H=0x00; // PWM1初始时为0
//CCAPM2=0x42; //PWM2设置PCA工作方式为PWM方式
//CCAP2L=0x00; //设置PWM2初始值与CCAP0H相同
//CCAP2H=0x00; // PWM2初始时为0
//CCAPM3=0x42; //PWM3设置PCA工作方式为PWM方式
//CCAP3L=0x00; //设置PWM3初始值与CCAP0H相同
//CCAP3H=0x00; // PWM3初始时为0
CR=1; //启动PCA定时器
}
/**********************************************************************************************/
/*********************************************************************************************
函数名:PWM0占空比设置函数
调 用:PWM0_set();
参 数:0x00~0xFF(亦可用0~255)
返回值:无
结 果:设置PWM模式占空比,为0时全部高电平,为1时全部低电平
备 注:如果需要PWM1的设置函数,只要把CCAP0L和CCAP0H中的0改为1即可
/**********************************************************************************************/
void PWM0_set (unsigned char a){
CCAP0L= a; //设置值直接写入CCAP0L
CCAP0H= a; //设置值直接写入CCAP0H
}
/**********************************************************************************************/
/**********************************************************************************************/
void PWM1_set (unsigned char a){
CCAP1L= a; //设置值直接写入CCAP0L
CCAP1H= a; //设置值直接写入CCAP0H
}
void zz (void){
dj11=0;
dj21=0;
dj12=1;
dj22=1;
}
void fz (void){
dj11=1;
dj21=1;
dj12=0;
dj22=0;}
void zzw (void){
dj11=1;
dj22=1;
dj12=0;
dj21=0;}
void yzw (void){
dj11=0;
dj22=0;
dj12=1;
dj21=1;}
void dz (void){
dj11=0;
dj22=0;
dj12=0;
dj21=0;}
/***********************************************************************************************/
void main(void){
unsigned char a;
PWM_init();
dz();
InitInfrared();
while (1) {
PWM0_set(a);
PWM1_set(a);
if (irflag){ //接收到红外数据时刷新显示
if(ircode[2]==0x40)zz();
if(ircode[2]==0x44)fz();
if(ircode[2]==0x43)dz();
if(ircode[2]==0x0c)a=100;
if(ircode[2]==0x18)a=80;
if(ircode[2]==0x5e)a=60;
if(ircode[2]==0x08)a=40;
if(ircode[2]==0x1c)a=20;
if(ircode[2]==0x5a)a=10;
if(ircode[2]==0x42)a=5;
if(ircode[2]==0x52)a=3;
}}}
红外接收程序
sbit IR_INPUT = P3^2; //红外接收引脚 38K载波
bit irflag = 0; //红外接收标志,收到一帧正确数据后置1
unsigned char ircode[4]; //红外代码接收缓冲区
void InitInfrared()
{ EA=1;
IR_INPUT = 1;
TMOD= 0XF1;
TR0 = 0;
ET0 = 0;
IT0 = 1;
EX0 = 1;
}
unsigned int GetHighTime()
{
TH0 = 0;
TL0 = 0;
TR0 = 1;
while(IR_INPUT)
{
if(TH0 > 0x40)
{
break;
}
}
TR0 = 0;
return(TH0 * 256 + TL0);
}
unsigned int GetLowTime()
{
TH0 = 0;
TL0 = 0;
TR0 = 1;
while(!IR_INPUT)
{
if(TH0 > 0x40)
{
break;
}
}
TR0 = 0;
return(TH0 * 256 + TL0);
}
void EXINT1_ISR() interrupt 0
{
unsigned char i, j;
unsigned int time;
unsigned char byt;
time = GetLowTime();
if((time <8500) || (time > 9500))//引导吗9ms低电平
{
IE0 = 0;
return;
}
time = GetHighTime();
if((time<4000) || (time > 5000)) //引导吗4.5ms高电平
{
IE0 = 0;
return;
}
for(i=0; i<4; i++)
{
for(j=0; j<8; j++)
{
time = GetLowTime();
if((time<360) ||(time >760))
{
IE0 = 0;
return;
}
time = GetHighTime();
if((time>360) && (time <760)) //大约560US左右取样 低电平”0“
{
byt >>= 1;
}
else if((time>1490) && (time<1890)) //大约1690US左右取样 高电平"1"
{
byt >>= 1;
byt |= 0x80;
}
else
{
IE0 = 0;
return;
}
}
ircode[i] = byt;}
if(ircode[2]!=~ircode[3]) //反码检查
{
return;
}
irflag = 1;
IE0 = 0;
}
|
|