/*单片机型号:STC12C5202AD-PWM
单片机振荡:外部晶振12MHZ,
遥控接收器,明暗变化
日期:2009.8.1
版本:V1.0
红外线用户码:S0~S15 0x00ff
作者:廖_shaoh
D7=0
*/
#include <STC_NEW_8051.H>
#include <stdio.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
volatile uint pwm_count=0;
volatile uchar pwm_wide=31;
bit Power="1"; //电路工作否判断,初始化上电工作
/****************IO端口定义*************/
sbit green="P1"^1;
sbit pwm1=P1^5; //调光通道1
sbit ir="P3"^3; //外部中断1采样口
sbit rec_led=P1^7; //指示正在接收编码
/***********函数列表*************/
//void safeguard_init();
void system_init();
void decode(uchar);
void delay_10us(unsigned char y);
void PCA_init();
/*******系统初始化******/
void system_init() //工作初始化设置
{
TMOD="0x12";
TCON=0x04;
TH1=0x00;
TL1=0x00;
TH0=0XF0;
TL0=0XF0;
EA=1;
ET0=0;
ET1=0;
EX1=1;
P3=0x08;
}
/******PCA初始化*******/
void PCA_init()
{
CMOD="0x84"; 62.5KHZ
CCON="0";
CL="0";
CH="0";
CCAPM0=0x42;
}
void decode(uchar keycode)
{
switch(keycode)
{
case 0:
Power=~Power;
//if(!Power) //待机
// {PCA_PWM0&=0xfd;CCAP0H=0;}
else
{PCA_PWM0|=0x02;CCAP0H=0xff;}
break;
case 1:
if(Power)
{
PCA_PWM0&=0xfd;
if(pwm_wide>217)
{
pwm_wide=248;
PCA_PWM0|=0x02;
CCAP0H=0xff;
}
else
{
CCAP0H=pwm_wide;
pwm_wide+=31;
}
}
break;
case 2:
if(Power)
{
PCA_PWM0&=0xfd;
pwm_wide-=31;
if(pwm_wide==0)
{
pwm_wide=31;
CCAP0H=0;
}
else
{
CCAP0H=pwm_wide;
}
}
break;
default: break;
}
}
void main()
{
PCA_init();
system_init();
PCA_PWM0=0x03;
TR0=1;
CR="1";
while(1) {};
}
/************定时器1中断服务测程序**************/
/*void timer1(void) interrupt 3
{
TH1=0xCD;
TL1=0x38;
}*/
/*void delay_10us(unsigned char y)
{
unsigned char x;
for(x=y;x>0;x--);
} */
/*******************定时器0中断服务程序****************/
/*
void timer0(void) interrupt 1
{
}
*/
/*******************外部中断1服务程序******************/
void int1(void) interrupt 2
{ uint time_us;
uchar i,j,k=0,mid,timeh,timel;
uchar buf[4];
EA="0";
green="0";
for (i=0;i<9;i++)
{
//Delay882uS();
//TH1=0xfc;
// TL1=0x8e;
TR1=1;
while(TF1==0);
TF1=0;TR1=0;
// if((P3&0x08)!=0) /
{IE1=0;
EA=1;
return;
}
}
while ((P3&0x08)=
TH1=0;
TL1=0;
TR1=1;
while ((P3&0x08)!=0); /
TR1=0;
timel="TL0";
timeh="TH0";
//time_us=((uint)timeh)<<8|((uint)timel);
if(time_us<2600)
{
// IE1=0;
EA="0";
return;
}
rec_led=0;
for (j=0;j<4;j++)
{
k="0";
for (i=0;i<8;i++)
{
while((P3&0x08)==0);
TH1=0xfc;
TL1=0x8e;
TR1=1;
while(TF1==0);
TF1=0;TR1=0;
mid = P3&0x08;
if (mid!=0)
{
TH1=0xfc;
TL1=0x18;
TR1=1;
while(TF1==0);
TF1=0;TR1=0;
//Delay1000uS();
k|=1<<i;
}
else
{
k&=~(1<<i);
}
}
buf[j] = k ;
}
if ((buf[2]|buf[3]) != 0xff)
{ IE1=0;
EA=1;
return;
}
decode(buf[2]);
EA="1";
IE1=0;
green="1";
rec_led=1;
}
/***********串口中断陷进*************/
void cuan(void) interrupt 4
{
_nop_();
_nop_();
}
文章评论(0条评论)
登录后参与讨论