Home > other >  Infrared remote control led
Infrared remote control led

Time:10-24

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 timeinit ()//for PWM timer
{
TMOD=0 x02;//timer 0, work mode 2 (0000001), eight timing mode
TH0=0 x06;//initial 6 to write preset timer 0, 250 microseconds overflow a (12 MHZ)
TL0=0 x06;//writing preset values
TR0=1;//start the timer
ET0=1;//allow 0 timer interrupt
EA=1;//allow always interrupt
}

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,

CodePudding user response:

The
reference 1/f, a tree may reply:
used in the building Lord function of infrared receiving and PWM control LED brightness function debugged separately? Interrupt can be nested, as long as it's not always hold the remote control, timer interrupt is able to run normally,

Good thank you, I didn't try the interrupt nesting

CodePudding user response:

See only temporarily delay function that is not good, and the main function is to initialize, no function implementation, can't analysis
/=================================================
//first of all, I think to achieve the function of the time delay function you say can't write
//should use a timer method
//this will cause the CPU waiting and LED state unchanged for a long time cannot be achieved
//control LED brightness effect
//=================================================
Void delay (int XMS)//delay function
{
For (I=XMS; I> 0; I -)
For (j=110; J> 0; J -)
;
}

Void timeinit ()//for PWM timer
{
TMOD=0 x02;//timer 0, work mode 2 (0000001), eight timing mode
TH0=0 x06;//initial 6 to write preset timer 0, 250 microseconds overflow a (12 MHZ)
TL0=0 x06;//writing preset values
TR0=1;//start the timer
ET0=1;//allow 0 timer interrupt
EA=1;//allow always interrupt
}

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;
}

//=================================================
//the main function part after initialization, there is no function implementation
//may be the code is not complete, can't continue to analyze the
//=================================================
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 following analysis for their own humble opinion, if there are any as welcome exchange,
//VX: 395233700
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related