28X2 turning off PWM when using pwmout and hpwm and invoking an interrupt with hintsetup and setintflags


New Member
I need to use two independent PWM units at the same time to control two DC motors of the car track and a hardware interrupt for counting car laps and a button to switch functions. I have everything plugged in and it almost works. It just happens to me that one or the other pwm device turns off after an interrupt is triggered. This happens when the hall sensor triggers an interrupt. I have to restart it again with the command:
hpwm pwmdiv16, 0, 0, %0001, 124, valSpeedM1 ; car 1
pwmout pwmdiv16, motor2, 124, valSpeedM2 ; car 2

Speed control voltage measurement:
symbol speedM1 = 0
symbol speedM2 = 1
readadc10 speedM2, valSpeedM2
readadc10 speedM1, valSpeedM1

Car 1:
;symbol motor1 = C.2
hpwm pwmdiv16, 0, 0, %0001, 124, 173

Car 2:
symbol motor2 = C.1
pwmout pwmdiv16, motor2, 124, 173

To count laps, I have two hall sensors connected to a hinterrupt configured as follows:

symbol bINTMask = b21 ;w10
bINTMask = %00000111

setbit bINTMask, 6
hintsetup bINTMask
clearbit bINTMask, 6
setintflags or %00000111, bINTMask

; fix :) reset PWM's after STOP, after interrupt call. :(
pwmout pwmdiv16, motor2, 124, valSpeedM2
hpwm pwmdiv16, 0, 0, %0001, 124, valSpeedM1
; ---


track 1 ( car 1 ) = hint0flag
track 2 ( car 2 ) = hint1flag
game button = hint2flag

Please don't you know how to set an alternative mode so that both PWM devices work together with the set hardware interrupt? Thank you for your response.


Senior Member

It can be confusing because the "H" in HPWM... stands for "H-Bridge", not "Hardware" - ALL the PWM outputs (must) use Hardware. So I think you should find the information that you need in the Command Reference for PWMOUT, in particular note 9) onward and the linked hippy's code thread. Personally, I don't use X2 parts, but it looks as if you might be better off using the B.0 and/or B.5 PWM Outputs?

Cheers, Alan.


Senior Member
I successfully used all 4 available PWM modules on a 28X2 in a project back in 2020. The related code is as follows:

Rich (BB code):
   Symbol oRedLED       = B.0       '21      PWM Out to Red LED   *** *** *** *** Red   PWM *** ***
   Symbol oWhiteLED     = B.5       '26      PWM Out to White LED *** *** *** *** White PWM *** ***
   Symbol oBlueLED      = C.1       '12      PWM Out to Blue LED  *** *** *** *** Blue  PWM *** ***
   Symbol oGreenLED     = C.2       '13      PWM Out to Green LED *** *** *** *** Green PWM *** ***

   Symbol cPWMMin    = 0            'Minimum duty period (  0%) value for PWM
   Symbol cPWMPeriod = 255          'Period control of each cycle: lowest frequency possible
                                    '           (f = 488Hz with PWMDiv16 @ PICAXE Osc =  8MHz)

      PWMOut PWMDIV16, oRedLED, cPWMPeriod, cPWMMin   'Initialise 0% PWM for Red LED
      PWMOut PWMDIV16, oGreenLED, cPWMPeriod, cPWMMin 'Initialise 0% PWM for Green LED
      PWMOut PWMDIV16, oBlueLED, cPWMPeriod, cPWMMin  'Initialise 0% PWM for Blue LED
      PWMOut PWMDIV16, oWhiteLED, cPWMPeriod, cPWMMin 'Initialise 0% PWM for White LED