I want to use the infrared to control the brightness of led, infrared with external interrupt, adjust the led brightness PWM with timer, but the code down to the board after I press the remote control, the brightness of the led is changeless, I wonder whether because of external interrupt interrupted timing interruption caused?? If that is how to solve? If not why?? I am using 51 boards, thank you! The code is as follows:
# include Typedef unsigned int under-16; typedef unsigned char u8;
Sbit led=P2 ^ 5; Sbit IRIN highlights=P3 ^ 2;
Under-16 I, j; U8 IrValue [6], Time, scale=0;
Void delay (int XMS)//delay function { For (I=XMS; I> 0; I -) For (j=110; J> 0; J -) ; }
Void Irinit ()//infrared communication interrupt { IT0=1;//drop along the trigger EX0=1;//open the interrupt 0 allows EA=1;//open the total interruption allows
IRIN highlights=1; }
Void main () { Timeinit (); Irinit (); While (1) { If (IrValue [2]==0 x09) { Scale=scale + 1; } If (scale==2) { Scale=0; } } }
Void ReadIr () interrupt 0//infrared data read { U8 j, k; Under-16 err; Time=0; Delay (700); If (IRIN highlights==0)//confirm whether receive the correct signal { Err=1000; While ((IRIN highlights==0) & amp; & (err> 0)) { Delay (1); Err,; } If (IRIN highlights==1) { Err=500; While ((IRIN highlights==1) & amp; & (err> 0)) { Delay (1); Err,; } for(k=0; k<4. K++)//a total of four groups of data { for(j=0; j<8; J++)//receiving a set of data { Err=60; While ((IRIN highlights==0) & amp; & (err> 0)) { Delay (1); Err,; } Err=500; While ((IRIN highlights==1) & amp; & (err> 0)) { Delay (10); Time++; Err,; If (Time> 30) { return; } } IrValue [k] & gt;>=1; If (Time>=8) { IrValue [k]=0 x80; } Time=0; } } } If (IrValue [2].=~ IrValue [3]) { return; } } }
Void timer0 () interrupt 1/0/timer interrupt service routines { The static unsigned int t; Used to save the current time//t ratio in a second position T++;//1 per 250 microseconds If (t==2) { T=0;//t=0, start a new PWM cycle Led=0; } If (scale==t)//in accordance with the current duty ratio switch output for the high level Led=1; }
CodePudding user response:
The building Lord function of infrared receiving and PWM control LED brightness function debugged separately? The interrupt can be nested, as long as it's not always hold the remote control, timing interrupt is able to run normally,