1. 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.
    Dismiss Notice

Zero Cross Detection (50Hz) Coding help

Discussion in 'Microcontrollers' started by Suraj143, Jan 30, 2008.

  1. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,567
    Likes:
    2
    Location:
    South Mald Isld
    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 (text):
        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: Jan 30, 2008
  2. eblc1388

    eblc1388 Active Member

    Joined:
    Jan 25, 2005
    Messages:
    2,228
    Likes:
    18
    Location:
    UK
    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.
     
  3. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,567
    Likes:
    2
    Location:
    South Mald Isld
    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.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. eblc1388

    eblc1388 Active Member

    Joined:
    Jan 25, 2005
    Messages:
    2,228
    Likes:
    18
    Location:
    UK

    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.
     
  6. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,234
    Likes:
    641
    Location:
    Derbyshire, UK
    I would suggest you're confusing the issue by calling it PWM? (which is really a DC technique), what you're doing is phase shift power control.
     

Share This Page