Menu

help me

samco
2025-06-23
2025-06-25
  • samco

    samco - 2025-06-23

    greetings! i m on my 2nd year project om do design inverter using pic16F, family microcontroller but
    am having a challenge here am trying to archive a sine wave trough cpp1 as the spwm signal and use C1 and C2 as complementary for pwm of 50hz to drive with HCF4081 quad gate to drive my h bridge but am still not getting any pulse at the pins here is my code so

    'federal polytechnic nedede '
    ' code 144, '
    'Soft Start, 50Hz H-Bridge Logic, SPWM (RC2), with Voltage Feedback'

    chip 16f887, 8

    option explicit

    ' ----- I/O Directions -----
    Dir PORTC.0 Out ' L/H Enable Output (inverter active state)
    Dir PORTC.1 Out ' H-Bridge Logic A (50Hz)
    Dir PORTC.2 Out ' PWM output (SPWM )
    Dir PORTC.3 Out ' H-Bridge Logic B (50Hz)
    Dir PORTB.1 In ' Inverter ON/OFF switch (with pull-up)
    Dir PORTB.4 Out ' Inverter Indicator LED

    ' ----- Variables -----
    Dim sineIndex As Byte
    Dim direction As Byte
    Dim dutyValue As Word
    Dim inverterOn As Bit
    Dim softStartDone As Bit
    Dim pwmFreq As Word
    Dim pwmStepFreq As Word

    ' ----- Setup -----
    direction = 0
    sineIndex = 0
    softStartDone = 0
    pwmFreq = 1566 ' ~Start soft at 1.5kHz for 8MHz (half of 3132-200)
    pwmStepFreq = 40 ' Lower ramp step due to slower base clock

    ' ----- PWM Init -----
    HPWM 1, pwmFreq, 0

    ' ----- Timer2 for stepping sine -----
    T2CON = 0b00000101 ' 1:4 prescaler
    PR2 = 99 ' ~20kHz at 8MHz
    PIE1.1 = 1 ' Enable TMR2 match
    INTCON.6 = 1
    INTCON.7 = 1
    On Interrupt Timer2Match Call HandleSPWM

    ' ----- Main Loop -----
    Do
    inverterOn =(PORTB.1 = 0) ' Button pressed (active low)

    If inverterOn =0 Then
        PORTC.0 = 1     ' L/H active
        PORTB.4 = 1     ' Inverter LED
    
        ' Soft Start: gradually increase PWM frequency
        If Not softStartDone Then
            If pwmFreq < 20000 Then
                pwmFreq = pwmFreq + pwmStepFreq
                HPWM 1, pwmFreq, 20
                Wait 100 ms
            Else
                softStartDone = 1
            End If
        End If
    Else
        PORTC.0 = 0
        PORTB.4 = 0
        HPWM 1, pwmFreq, 0
    End If
    

    Loop

    ' ----- Interrupt Handler for 50Hz Logic Switching and SPWM -----
    Sub HandleSPWM
    If softStartDone Then
    ' Toggle H-Bridge polarity every half-cycle
    If sineIndex = 0 Then
    direction = 1 - direction
    If direction = 1 Then
    PORTC.1 = 1 : PORTC.3 = 0
    Else
    PORTC.1 = 0 : PORTC.3 = 1
    End If
    End If

        ' Read sine value and scale it to 10-bit range for 8MHz
        ReadTable sineTable, sineIndex, dutyValue
        dutyValue = dutyValue * 4     ' Scale 8-bit sine to 10-bit duty (0–1023)
        HPWM 1, pwmFreq, dutyValue    ' Set 10-bit PWM duty
    
        sineIndex = sineIndex + 1
        If sineIndex >= 77 Then sineIndex = 0
    End If
    

    CCPR1L = dutyValue
    ;TMR2IF = 0
    PIR1.1 = 0 ' Clear Timer2 interrupt flag
    End Sub
    ' ----- Sine Table (77-step half-cycle) -----
    Table sineTable
    0,8,17,26,35,44,53,62,71,80,89,97,106,115,123,132,140,148,157,165,173,181,189,196,204,212,219,226,233,240,247,254,261,267,274,280,286,292,298,303,309,314,319,324,328,333,337,341,344,348,351,354,356,359,361,363,365,366,368,369,370,371,372,372,373,373,373,373,372,372,371,370,369,368,366,365,363,361
    End Table

     
  • samco

    samco - 2025-06-23

    thank you for respond , ok let me look into it and see what i can do then am running out off time

     
  • samco

    samco - 2025-06-24

    @anobium honestly i have checked down the link, i couldn't understand as a newbie in coding it seems more professional without commenting on them while the inverter full code i saw was using deep memory language not for beginners like me, if someone can easily correct my code i will be much happy. i even trying using updatepwm function which work on ccp1 without outputting frq, and control on it, please mr @anobium if you can help me out i will glad

     
  • Anobium

    Anobium - 2025-06-25

    Many errors.

    I f you load the code below into GCCODE then all the errors and serious comments will be in RED.

    Evan

    ' ==== Pure Sine Wave Inverter (PIC16F72 - 16MHz) ====
    ' Includes: Soft Start, 50Hz H-Bridge Logic, SPWM (RC2), Voltage Feedback, Protections
    
    #chip 16f887, 16
    #option explicit
    
    ' ----- I/O Directions -----
    Dir PORTC.0 Out  ' L/H Enable Output (inverter active state)
    Dir PORTC.1 Out  ' H-Bridge Logic A (50Hz)
    Dir PORTC.2 Out  ' PWM output (SPWM)
    Dir PORTC.3 Out  ' H-Bridge Logic B (50Hz)
    Dir PORTC.5 Out  ' Buzzer
    Dir PORTC.6 Out  ' Fan
    Dir PORTC.7 Out  ' Relay
    Dir PORTB.1 In   ' Inverter ON/OFF switch (with pull-up)
    Dir PORTB.3 Out  ' Grid Indicator LED
    Dir PORTB.4 Out  ' Inverter Indicator LED
    Dir PORTB.5 Out  ' Battery Low Indicator
    Dir PORTB.6 Out  ' Overload Indicator
    Dir PORTB.7 Out  ' Charging Indicator
    
    ' ----- ADC Channels -----
    ' AN0: Grid Sense
    ' AN1: AC Voltage Feedback
    ' AN2: Battery Level
    ' AN3: Charge Current
    ' AN4: Overload Detection
    
    ' ----- Variables -----
    Dim sineIndex As Byte
    Dim direction As Byte
    Dim dutyValue As Word
    Dim acFeedback As Word
    Dim batteryLevel As Word
    Dim gridLevel As Word
    Dim overloadValue As Word
    Dim inverterOn As Bit
    Dim softStartDone As Bit
    Dim pwmFreq As Word
    Dim pwmStepFreq As Word
    
    
    
    ' ----- Setup -----
    direction = 0
    sineIndex = 0
    softStartDone = 0
    pwmFreq = 3132   ' Start soft at ~3.1kHz
    pwmStepFreq = 80 ' Soft start ramp step
    
    ' ----- PWM Init -----
    HPWM 1, pwmFreq, 0
    HPWMOff
    ' ----- Timer2 for stepping sine -----
    
    //! All this is overwriting the T2CON, PR2 setup up in HPWM 1,xx,xx
    /* 
    T2CON = 0b00000101      ' 1:4 prescaler
    PR2 = 99                ' ~20kHz at 16MHz
    PIE1.1 = 1              ' Enable TMR2 match
    */
    
    //! All this is overwriten by On Interrupt Timer2Match Call HandleSPWM
    /*
    INTCON.6 = 1
    INTCON.7 = 1
     */
    
    On Interrupt Timer2Match Call HandleSPWM
    
    ' ----- Main Loop -----
    Do
        inverterOn = (PORTB.1 = 0) ' Button pressed (active low)
    
        ' Grid detection
        gridLevel = ReadAD10(AN0)
        If gridLevel > 300 Then
            PORTC.0 = 0
            PORTB.3 = 1 ' Grid LED
            PORTB.4 = 0
            HPWM 1, pwmFreq, 0
           ' Continue
        Else
            PORTB.3 = 0
        End If
    
        If inverterOn Then
            PORTC.0 = 1     ' L/H active
            PORTB.4 = 1     ' Inverter LED
    
    
    
            ' Soft Start: gradually increase PWM frequency
            If   Not softStartDone Then
    
                If pwmFreq < 20000 Then
                    pwmFreq = pwmFreq + pwmStepFreq
                    HPWM 1, pwmFreq, 20
                   ; Wait 100 ms
                Else
                    softStartDone = 1
    
            End If
            End If
        Else
            PORTC.0 = 0
            PORTB.4 = 0
            HPWM 1, pwmFreq, 0
        End If
    
    Loop
    
    ' ----- Interrupt Handler for 50Hz Logic Switching and SPWM -----
    Sub HandleSPWM
        If softStartDone Then
            ' Toggle H-Bridge polarity every half-cycle
            If sineIndex = 0 Then
                direction = direction -1 
                If direction = 1 Then
                    PORTC.1 = 1 : PORTC.3 = 0
                Else
                    PORTC.1 = 0 : PORTC.3 = 1
                End If
            End If
    
            ' Read sine value and scale it to 10-bit range for 16MHz
            ReadTable sineTable, sineIndex, dutyValue
            //! Slow!!!   Use 4 rotates rather than maths
            dutyValue = dutyValue * 4     ' Scale 8-bit sine to 10-bit duty (0–1023)
    
            //! This is an 8-bit PWM duty. See the Help.
            //!  See demo - C:\GCstudio\gcbasic\demos\PWM_Solutions\8_9_10_bit_PWMCPP_Examples\PWM_Variable_mode_8bit_10bit.gcb - to leverage 10-bit PWM
            HPWM 1, pwmFreq, dutyValue    ' Set 10-bit PWM duty
    
            sineIndex = sineIndex + 1
            If sineIndex >= 77 Then sineIndex = 0
        End If
    
        //! This is messing up the Timer2 controlled by HPWM
        // TMR2IF = 0
    End Sub
    ' ----- Sine Table (77-step half-cycle) -----
    //! Table needs to be a WORD table as your data is greater than a Byte
    Table sineTable As Word
    0,8,17,26,35,44,53,62,71,80,89,97,106,115,123,132,140,148,157,165,173,181,189,196,204,212,219,226,233,240,247,254,261,267,274,280,286,292,298,303,309,314,319,324,328,333,337,341,344,348,351,354,356,359,361,363,365,366,368,369,370,371,372,372,373,373,373,373,372,372,371,370,369,368,366,365,363,361
    End Table
    
     

    Last edit: Anobium 2025-06-25

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.