Hi I just wrote a sample program to do a PWM in 230V/ 50Hz.
Operation is like this
* Wait for zero crosses
* When it does start the timer0
* timer0 set for 39uS
* In every tmr0 overflow increment the delay.
* When delay completed fire the triac
* Wait for next zero crosses
I’m going to fade the 230V bulb using 256 steps.
So when it is fully dim 39 X 256 = 9984 uS less than 10mS.
I just need to check whether its ok or not?
Operation is like this
* Wait for zero crosses
* When it does start the timer0
* timer0 set for 39uS
* In every tmr0 overflow increment the delay.
* When delay completed fire the triac
* Wait for next zero crosses
I’m going to fade the 230V bulb using 256 steps.
So when it is fully dim 39 X 256 = 9984 uS less than 10mS.
I just need to check whether its ok or not?
Code:
org 0000h
clrf STATUS
goto Init
org 0004h
;*************************************
;detecting Zero cross or TMR0 overflow
;*************************************
btfsc INTCON,T0IF ;is it a TMR0 overflow? ;2+1
goto Load ;2
btfsc INTCON,INTF ;is it a zero cross?
goto Start_T ;yes,
goto Exit
Load movlw 100h-d'255' ;load 1 to TMR0 ;1
movwf TMR0 ;1 = 39uS
;********************************************
;W_count controls the dim level - (256 steps)
;********************************************
256_T decfsz W_Count,F
goto Exit
incf S_Count,F ;increment the delay
movf S_Count,W
movwf W_Count
Fire bsf PORTB,1 ;start firing the triac
bcf INTCON,T0IE ;disable TMR0 interrupt
goto Exit
;***********************************************
;turning on the timer, 1(TMR0) X 32(PS) = 32 uS
;***********************************************
Start_T bsf INTCON,T0IE ;enable TMR0 interrupt
movlw 100h-d'255' ;load 1 to TMR0
movwf TMR0
bcf PORTB,1 ;turn off firing
Invert movlw b'00100000' ;invert the edge selection-
bsf STATUS,RP0 ;-for next zero cross
xorwf OPTION_REG,F
bcf STATUS,RP0
goto Exit
Exit bcf INTCON,T0IF
bcf INTCON,INTF
retfie
;******************
;initializing ports
;******************
Init bsf STATUS,RP0 ;bank1
clrf TRISB
movlw b'00000100' ;set pre scaler 1:32
movwf OPTION_REG
bcf STATUS,RP0 ;bank0
bsf INTCON,GIE
bcf PORTB,1 ;turn off the TRIAC
Main nop
goto Main ;wait until zero crosses
End
Last edited: