We will not start a software or microcontroller lesson but:
The following example creates PWM at 50Hz and uses 2 CPU timers.
PWM is defined by T = t1 + t2.
Times_0 givs an interrupt every 20mSec, 1/0.02 = 50
T=50Hz.
Timer_2 counts the time t1 and then sets PWM output to '0'.
At Timer_0 interrupt, PWM output rise to '1' and Timer_2 starts to count.
At Timer_2 interrupt PWM output is set to '0' and Timer_2 stops counting.
- קוד: בחר הכל
TF0_VECTOR: ; Timer0 interrupt
MOV TL0, #LOW (RTC_20MSEC)
MOV TH0, #HIGH (RTC_20MSEC) ;Load Timer0 Val for 20mSec Time.
SETB RtcFlag ; Tells the system that 20mSec has Passed.
MOV TL2, PWM_TIMER_VAL+1
MOV TH2, PWM_TIMER_VAL ;Load Timer2 with the val you want (t1)
SETB TR2 ; Timer2 - start Running
RETI ; Exit interrupt
TF2_VECTOR: Timer2 interrupt
CLR TF2 ; Clear OverFlow flag
CLR TR2 ; Stop Running
CLR PWM_OUT ; Set output to 0v
RETI ; Exit interrupt