News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Zero crossing With PIC

Started by m.kaviani, Jan 20, 2022, 02:45 PM

Previous topic - Next topic

m.kaviani

hi all
a simple zero-crossing circuit that PIC took and make on pin-change interrupt on RB0. in every crossing zero line
here is the code


Device = 18F66K80

Config_Start
  RETEN = OFF    ;Ultra low-power regulator is Disabled (Controlled by REGSLP bit)
  INTOSCSEL = Low    ;LF-INTOSC in Low-power mode during Sleep
  SOSCSEL = Dig    ;High Power SOSC circuit selected
  XINST = Off    ;Enabled
  FOSC = HS1    ;HIGH SPEED oscillator
  PLLCFG = On    ;Enabled
  FCMEN = OFF    ;Disabled
  IESO = OFF    ;Disabled
  PWRTEN = OFF    ;Disabled
  BOREN = SBORDIS    ;Enabled in hardware, SBOREN disabled
  BORV = 3    ;1.8V
  BORPWR = ZPBORMV    ;ZPBORMV instead of BORMV is selected
  WDTEN = OFF    ;WDT disabled
  WDTPS = 1048576    ;1:1048576
  CANMX = PORTB    ;ECAN TX and RX pins are located on RB2 and RB3, respectively
  T0CKMX = PORTB    ;Timer0 gets its clock input from the RB5/T0CKI pin on 64-pin packages
  T3CKMX = PORTG    ;Timer3 gets its clock input from the RG2/T3CKI pin on 64-pin packages
  MSSPMSK = MSK7    ;7 Bit address masking mode
  MCLRE = On    ;MCLR Enabled, RE3 Disabled
  STVREN = On    ;Enabled
  BBSIZ = BB1K    ;2K word Boot Block size
  Cp0 = OFF    ;Disabled
  CP1 = OFF    ;Disabled
  CP2 = OFF    ;Disabled
  CP3 = OFF    ;Disabled
  CPB = OFF    ;Disabled
  CPD = OFF    ;Disabled
  WRT0 = OFF    ;Disabled
  WRT1 = OFF    ;Disabled
  WRT2 = OFF    ;Disabled
  WRT3 = OFF    ;Disabled
  WRTC = OFF    ;Disabled
  WRTB = OFF    ;Disabled
  WRTD = OFF    ;Disabled
  EBTR0 = OFF    ;Disabled
  EBTR1 = OFF    ;Disabled
  EBTR2 = OFF    ;Disabled
  EBTR3 = OFF    ;Disabled
  EBTRB = OFF    ;Disabled
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
    Declare Xtal 64            ' 16 Mhz x 4xPLL
    Declare Optimiser_Level = 2
    Declare Dead_Code_Remove = 1

    Include "hpwm_2.bas"
    Include "lcd_dev_board.bas"
    Include "i2c_dev_board.bas"

    Clear ADCON0.0         ' TURN OF THE A/D CONVERTER
    ADCON1 = 0
    ADCON2 = 0
    ANCON0 = 0
    ANCON1 = 0
   
    Clear TRISE.5      ' SET AS OUTPUT
    Clear TRISB.5      ' SET AS OUTPUT
    Clear TRISE.4      ' SET AS OUTPUT
    Clear TRISC.3
    Clear TRISD.7      ' TO FIRE SCRs

    Set   TRISC.0      ' SET AS INPUT
    Set   TRISF.5      ' SET AS INPUT
    Set   TRISF.4      ' SET AS INPUT
    Set   TRISB.0      ' SET AS INPUT FOR INT0

    Symbol LED1          =  PORTE.5
    Symbol LED2          =  PORTB.5
    Symbol LED3          =  PORTE.4

    Symbol PB1           =  PORTC.0  ' PUSH BOTTON 1
    Symbol PB2           =  PORTF.5  ' PUSH BOTTON 2
    Symbol PB3           =  PORTF.4  ' PUSH BOTTON 3
   
    Symbol GIE           = INTCON.7   ' GLOBAL INTERRUPT ENABLE
    Symbol PEIE          = INTCON.6   ' PREPHERAL INTERRUPT ENABLE
    Symbol IPEN          = RCON.7     ' Interrupt Priority Enable bit
    Symbol INT0IE        = INTCON.4   ' RB0 INTERRUPT ENABLE
    Symbol INT0IF        = INTCON.1   ' RB0 INTERRUPT FLAG
    Symbol INTEDG0       = INTCON2.6  ' RB0 INTERRUP EDGE SELECT 1=RISING 0=FALLING     

    Symbol DAC_WRITE        = %11000010 ' digital to analog write command
    Symbol DAC_COMMAND      = %01000000 ' 64 HEX
   
    Symbol SCR_FIRE         = PORTD.7

    Clear LED1 : Clear LED2 : Clear LED3

    Dim B1         As Byte
    B1 = 0
   
    Set GIE
    Set PEIE
    Set IPEN
    Set INT0IE
    Clear SCR_FIRE
    Clear INTCON2.7

On_Hardware_Interrupt GoTo ISR_HIGH

MAIN:

    Nop

GoTo MAIN


ISR_HIGH:
    Context Save
   
    Toggle INTEDG0 ' CHANGE THE INTERRUPT EDGE AFTER INTERRUPT 0 TO 1 OR 1 TO 0
    Clear INT0IF
   
    Set SCR_FIRE
    DelayUS 100
    Clear SCR_FIRE
   
   Context Restore
Stop

shantanu@india

Good work for clearing the basic concepts of thyristor firing based on zero crossing detector.
I have used pwm signals with zero crossing optos(MOC) to control heating elements where duty cycle control is not an issue.
Zero crossing detection of your kind is more required for motor speed control.
Regards
Shantanu

trastikata

I would rather use a diode bridge and optocoupler, like 4N35, for full isolation - something like this:



 

m.kaviani

the accuracy is very important when are controlling a spot welding machine with about 200kw during welding. your circuit works, But it has 680us tolerance
between zero-cross point output signal.
that machine may weld an 8mm nut or set to weld two pieces of steel sheets.
in this situation, I use a simple little freet transformer that is controlled by PIC PWM to make isolated auxiliary voltage for another circuit.
   

trastikata

Quote from: m.kaviani on Jan 20, 2022, 03:56 PMthe accuracy is very important when are controlling a spot welding machine with about 200kw during welding. your circuit works, But it has 680us tolerance
between zero-cross point output signal.
that machine may weld an 8mm nut or set to weld two pieces of steel sheets.
in this situation, I use a simple little freet transformer that is controlled by PIC PWM to make isolated auxiliary voltage for another circuit.

The necessary delay is easily calculated based on the mains frequency.

200kW at 220v is 900 Amps, down to 1.2v on the spot-welder's secondary gives about 160 000 Amps secondary ;), are you sure you are not mistaken?

Anyway why not using the low voltage side of the transformer to feed the zero-crossing circuit? Having a galvanic isolation from the mains is a must on any industrial or end-user power tool or machine. 

John Lawton

It'll be a challenge to get an actual zero-crossing point due to the noise on the supply line induced by the welder :)

top204

I think there are some PIC devices with a Zero Cross detection peripheral within them somehow. I've never investigated them, but I've seen the ZCD bits and pieces in the data files I created for the compilers.

shantanu@india

Quote from: top204 on Jan 20, 2022, 04:47 PMI think there are some PIC devices with a Zero Cross detection peripheral within them somehow. I've never investigated them, but I've seen the ZCD bits and pieces in the data files I created for the compilers.
PICs are almost indestructible provided you do not cross the voltage limit. I had once built a triac based fan speed controller by connecting the live 230V A. C line directly to a 12F675 via a 2.2M resistance for zero crossing based on the internal protection diode. The speed was controlled by an RC5 remote through a 38kHz detector.  The PIC was powered from the live line directly through a capacitive divider connected to a zener bridge regulator... no isolation at all. We have sold thousands of such small circuits without a single failure ever. Now that market has been taken over by the Chinese!!
Regards
Shantanu

Giuseppe MPO

Quote from: top204 on Jan 20, 2022, 04:47 PMI think there are some PIC devices with a Zero Cross detection peripheral within them somehow. I've never investigated them, but I've seen the ZCD bits and pieces in the data files I created for the compilers.


The peripherals integrated in the PICs are very interesting, at the time I inserted, precisely as regards the driving of a TRIAC using the internal peripheral of a PIC16F1619 plus the timers to calculate the phase shift and obtain the pulse calculation without any line of code . I talked about it some time ago at this discussion

https://protoncompiler.com/index.php/topic,409.msg2577.html#msg2577

m.kaviani

yes, I use a 400vac for zero crossing and two 1000A thyristors will be triggered with a PIC firring circuit with an isolated pulse transformer.
the spot welder draws current from primary just for 0.05 to 0.2 second

m.kaviani

Quote from: John Lawton on Jan 20, 2022, 04:39 PMIt'll be a challenge to get an actual zero-crossing point due to the noise on the supply line induced by the welder :)
it could be done by 400/12vac 12va transformer. i did use this one on an old project.

TimB

#11
I cannot remember how I in the past detected Zero cross. From memory it was a few high value resistors. It triggered an interrupt. When I serviced the interrupt I loaded into a timer a value for the time to turn on the Triac. When that timer rolled over it triggered its interrupt and I turned on the Triac. The calculations on the time and hence the period to load into the tmr were done in the main code.

Nothing that special but it effectively ran in the background.I was able to add in a 7 seg display and Timers etc etc. It ran to start with at 4mhz but when I had to calculate RMS Amps I turned on PPL and pushed it to 16mhz. It was back about 16+ years ago. If it's of interest I can dig it out.

Just an Edit. Serious stuff m.kaviani. All above my pay grade...