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)
IfinverterOn=0ThenPORTC.0=1' L/H activePORTB.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 IfElse PORTC.0 = 0 PORTB.4 = 0 HPWM 1, pwmFreq, 0End 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
@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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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)
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
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
https://github.com/GreatCowBASIC/Demonstration_Sources?search=1
A great place to start. Do a search for
sine
.thank you for respond , ok let me look into it and see what i can do then am running out off time
@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
Many errors.
I f you load the code below into GCCODE then all the errors and serious comments will be in RED.
Evan
Last edit: Anobium 2025-06-25