Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Zero Cross Detection (50Hz) Coding help

Status
Not open for further replies.

Suraj143

Active Member
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?

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:
Thanks for sharing the code.

Very few people can have a program running at the first instance. Most will make either logical or silly mistakes along the way.

Just run it and tell us the results. Part of the fun of experimenting with PIC is to design a program, debug it when it not working and finally get it going.

Waiting for your good news.
 
Hi eblc1388 thanks for you response.I'll try to make that sooner.I asked about this code because this is dealing with 230V AC.

For me look likes ok but must make & see.

I'll inform you after checking.
 
Yes, 230V is not a good place to mess around. What electrical isolation are you using between PIC and 230V side?

If you don't use any MOC302x opto-triac to trigger the TRIAC, then I would suggest you experiments with low voltage AC such as 24V or 12V AC. The TRIAC will work the same if you use a low voltage lamp instead.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top