只用到了延时子程序,在亮度调节和频率的调节上花了点时间
程序如下:没图啊啊啊啊 图在另一部手机 这部手机在女朋友那。。。。悲催了。。。。
#include <stc.h>
#include <intrins.h>
sbit IRIN =P1^0;
sbit led=P1^7;
bit onoff_enable;
#define Freq 24
#define TIMER1 0xf3 //baud rate 4800
#define uart
#define uchar unsigned char
#define TIMEDELAY 1 //中断时间1ms
#define th0_para (uchar)((65536-(Freq/12)*1000*TIMEDELAY)>>8)
#define tl0_para (uchar)(65536-(Freq/12)*1000*TIMEDELAY)
#define pwm_total 30
#define pwm_default 15
unsigned char buff1[8];
uchar jump_table[7] ={0xfe,0xfd,0xfb,0xfc,0xfa,0xf9,0xf8};
uchar led_counter =0;
uchar counter =0;
uchar INDEX;
uchar pwm;
uchar pw_on;
/*void delayus(unsigned char dlycnt) //延时80*us
{
unsigned char i;
unsigned char k =dlycnt;
while(k--)
{
i =100;
while(--i);
}
}*/
void delayus(unsigned char t) //delay 50us(90)
{
unsigned char i;
for(i=0;i<t;i++);
}
/*void timer0_init(void) //定时器0,用于设置占空比,达到调节led亮度的作用
{
TMOD |=0X01;
TH0 =th0_para;
TL0 =tl0_para;
}*/
#ifdef uart
void Uart_Init(void)
{
ET1 =0;
ES =0;
SCON = 0x50; //串口方式1,允许接收
TMOD |= 0x20; //定时器1工作方式2,定时器0工作方式1
PCON |=0X80; //4800 *2
TH1 = TIMER1;
TL1 = TIMER1;
TR1 = 1; //启动定时器1
}
void uart_send(unsigned char ch)
{
TI =0;
SBUF =ch;
while(!TI);
TI =0;
}
#endif
unsigned char receive(unsigned char *receivebuff) //receive Low Level first, if skip, data no meaning
{
unsigned char bytecount;
unsigned char bitcount;
unsigned char counter1,counter2;//c0,c1,byt,bt;
unsigned char *pchar =receivebuff;
// unsigned char max =0;
// lowflag =0;
// highflag =0;
pchar[0] =0x00;
pchar[1] =0x00;
pchar[2] =0x00;
pchar[3] =0x00; //clear buffer first
pchar[4] =0x00;
pchar[5] =0x00;
for(bytecount =0; bytecount <6;bytecount ++)
{
// counter1 =0; //clear counter
for(bitcount =0; bitcount<8;bitcount++)
{
counter1 =0;
while(!IRIN)
{
delayus(90); //delay 50us
counter1++;
// if(counter1>19){*pchar<<=1;}
if(counter1 > 50) return 0; //overtime
// lowflag =1;
// if(counter1>6) break;
}
if(counter1 <6) //if <500us, is jam
{
// lowflag =0; //pulse too short,clear flag and return
return 0;
}
// if(lowflag ==0) return 0; //if no low level first, jump out directly to save time
counter2 =0;
while(IRIN)
{
delayus(90); //delay 50us
counter2 ++;
// if(counter2 >19) {*pchar |=0x01; *pchar <<=1;}
if(counter2 > 50) return 0;
}
if(counter2 < 6)
{
return 0;
}
*pchar <<=1;
if(counter2 >19) *pchar |=0x01; //counter2 > 18 or 19 read as '1'
// if(max <=counter2) max =counter2;
}
pchar ++;
}
// #ifdef uart
// uart_send(max);
// #endif
return 1;
// delayus(1800); //delay 1ms
}
void int_init(void)
{
ET0 =1;
EA =1;
TR0 =1;
}
void timer0_isr(void) interrupt 1
{
if(!onoff_enable)
{
pwm++;
if(pwm <= pwm_total)
{
if(pwm<=pw_on)
led=1;
// P1 =jump_table[INDEX];
// else P1 =0xff;
else led=0;
}
else pwm =0;
// if(pwm == pwm_total) pwm =0;
/* if(++counter ==10)
{
counter =0;
if(++led_counter ==200)
{
led_counter =0;
if(++INDEX ==7)INDEX =0;
}
}*/
}
else led=1;
TH0 = th0_para;
TL0 += tl0_para;
// TL0 = tl0_para;
}
void io_init(void)
{
P1M0 &=~(1<<7);
P1M1 |=(1<<7);
}
void main(void)
{
#ifdef uart
Uart_Init();
#endif
// timer0_init();
// io_init();
int_init();
pwm=0;
pw_on =pwm_default;
INDEX =0;
// led=0;
// uart_send(0x55);
while(1)
{
if(receive(&buff1)==1)
{
// if((buff1[0]==0x11)&&(buff1[1]==0x11))
// {
// if((buff1[2]==0x81)&&(buff1[3]==0x88))
// {
// if((buff1[4]==0x88)&&(buff1[5]==0x88))
// {
// if((buff1[6]==0x88)&&(buff1[7]==0x18))
// {
// onoff_enable =!onoff_enable;
// return;
// led=1;
// }
// else led=0;
// }
// }
// if((buff1[2]==0xc0)&&(buff1[3]==0x3f))
// {
// pwm_on++;
// if(pwm_on >=pwm_total)pwm_on =pwm_total;
// return;
// }
// if((buff1[2]==0x40)&&(buff1[3]==0xbf))
// {
// pwm_on--;
// if(pwm_on <=pwm_default)pwm_on =pwm_default;
// return;
// }
//
// }
if((buff1[2]==0x0b)&&(buff1[3]==0xfd)){if(pw_on==0)pw_on=0;else pw_on=pw_on-5;} //up
if((buff1[2]==0x0d)&&(buff1[3]==0xfd)){if(pw_on==pwm_total)pw_on=pwm_total;else pw_on=pw_on+5;} //down
if((buff1[2]==0x0f)&&(buff1[3]==0xbd)){;} //left
if((buff1[2]==0x0f)&&(buff1[3]==0x7d)){;} //right
if((buff1[2]==0x0e)&&(buff1[3]==0xfd)){onoff_enable=~onoff_enable;pw_on=pwm_default;} //power
if((buff1[2]==0x0f)&&(buff1[3]==0xdd)){;} //cam1
if((buff1[2]==0x0f)&&(buff1[3]==0xf9)){;} //cam2
#ifdef uart
// uart_send(buff1[0]);
// uart_send(buff1[1]);
uart_send(buff1[2]); //transfer by serial use 1ms every byte
uart_send(buff1[3]);
// uart_send(buff1[4]);
// uart_send(buff1[5]);
#endif
}
}
}