用stc89c52的单片机设计红外遥控,控制pwm波的占空比 附上遥控器的码值
时间:10-02
整理:3721RD
点击:
#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit pwm0=P2^0;sbit hwx=P3^2;uchar lsd=0xfe;uchar n=0,pwm=1,q=50;uchar recv[4]; /*****************************产生PWM波*************************************/void delay140us(uchar x){ unsigned char k; while(x--) { for(k=0;k<13;k++){} }}void tset(){ TMOD=0x01; TH0=(65536-10)/256; TL0=(65536-10)%256; EA=1; ET0=1; TR0=1;}void PWMout() interrupt 1 {
TH0=(65536-10)/256; TL0=(65536-10)%256; n++; if(n<=q) { pwm=1; } else { pwm=0; } if(n==101) { n=0; }} /*****************************产生PWM波***************************************/ /*********************************** 红外线接收*******************************************/void Irecv() interrupt 0{ unsigned char i,j,c=0; EX0=0; delay140us(15); if(hwx==1) { EX0=1; return; } while(!hwx) { delay140us(1); } for(i=0;i<4;i++) { for(j=0;j<8;j++) { while(hwx) { delay140us(1); } while(!hwx) { delay140us(1); } while(hwx) { c++; if(c>=30) { EX0=1; return; } } recv=recv>>1; if(c>=8) { recv=recv|0x80; } c=0; } } if(recv[2]!=~recv[3]) { EX0=1; return; } switch(recv[2]) { case 0x07:q++;break; case 0x15:q--;break; } EX0=1;}/***************************************红外线接收************************************/void main(){ EA=1; EX0=1; IT1=1; tset(); hwx=1; while(1) { pwm0=pwm; }}
TH0=(65536-10)/256; TL0=(65536-10)%256; n++; if(n<=q) { pwm=1; } else { pwm=0; } if(n==101) { n=0; }} /*****************************产生PWM波***************************************/ /*********************************** 红外线接收*******************************************/void Irecv() interrupt 0{ unsigned char i,j,c=0; EX0=0; delay140us(15); if(hwx==1) { EX0=1; return; } while(!hwx) { delay140us(1); } for(i=0;i<4;i++) { for(j=0;j<8;j++) { while(hwx) { delay140us(1); } while(!hwx) { delay140us(1); } while(hwx) { c++; if(c>=30) { EX0=1; return; } } recv=recv>>1; if(c>=8) { recv=recv|0x80; } c=0; } } if(recv[2]!=~recv[3]) { EX0=1; return; } switch(recv[2]) { case 0x07:q++;break; case 0x15:q--;break; } EX0=1;}/***************************************红外线接收************************************/void main(){ EA=1; EX0=1; IT1=1; tset(); hwx=1; while(1) { pwm0=pwm; }}
see see
学习了
帮你顶置的,回帖