Code: Select all
#define EMC0 4
#define EMC1 6
#define EMC2 8
sub setupPWM (cycleTime, highTime) ' times in micro-seconds
DIM prescale as integer
prescale = 48 ' as the peripherals run at 48 MHz
while (cycleTime >= &H10000)
cycleTime = cycleTime >> 1
highTime = highTime >> 1
prescale = prescale << 1
loop
prescale = prescale - 1 ' counts to n-1
cycleTime = cycleTime - 1 ' counts to n-1
' enable counters CT16B0 and CT16B1
SYSCON_SYSAHBCLKCTRL = SYSCON_SYSAHBCLKCTRL or (1<<7) or (1<<8)
T3_TCR = 0
IOCON_PIO0_21 = IOCON_PIO0_21 or &H00000001 'P0.21
T3_MR0 = cycleTime - highTime + 1
T3_EMR = T3_EMR or ((1<<EMC0) or 1)
T3_PWMC = T3_PWMC or &H09
T3_PR = prescale
T3_TC = 0
T3_MCR = 1<<10 'Reset on MR3
T3_MR3 = cycleTime
T3_TCR = 1' Enable counter and PWM
end sub
main:
setupPWM(100,2) ' near full bright for now
Will need to add variable PWM which without prescale (which is not changed currently) just needs to change T3_MR0