Code:
Phase VAR BYTE[3] ' Array used as pointers into lookuptable.
DutyCycle VAR WORD[3] ' Array storing the dutycycles retreived from the table
Temp VAR WORD ' Temporary variable to avoid hard to understand array indexing
i VAR BYTE ' General purpose counter
' The lookup table has 186 entries long, to get 120° phase shift we need to
' "start" the second phase at 1/3 of the cycle and the third phase at 2/3
' of the table. 186/3=62 so first phase starts at 0, second phase at 62
' and third phase at 123.
' Initilise pointers.
Phase[0] = 0 : Phase[1] = 61 : Phase[2] = 123
Main:
Gosub GetDuty ' Retrieve the dutycycle values for all three phases
GOSUB SetDutyCycle ' Set the three PWM modules accordingly
' Now increment the individual table pointers and make sure they wrap
' around to zero when they reach then end of the table. That way they
' will always stay 62 "steps" (120°) from each other.
For i = 0 to 2
Phase[i] = Phase[i] + 1
If Phase[i] > 185 then Phase[i] = 0 'When pointer is > 185 we need to wrap around to 0.
NEXT
PauseUs 300
Goto Main
SetDutyCycle:
' Get value from the array of dutycycles and put it in the dutycycle
' registers of the 3 PWM modules. We could have used the array directly
' but this way (using a Temp variable) is easier to understand.
Temp = DutyCycle[0] ' Get dutycyle for phase 1 from the array and store in temp.
CCP1CON.4 = Temp.0 ' Set the LSB's
CCP1CON.5 = Temp.1
CCPR1L = Temp >> 2 ' Set the 8 high bits
Temp = DutyCycle[1] ' Same procedure.
CCP2CON.4 = Temp.0
CCP2CON.5 = Temp.1
CCPR2L = Temp >> 2
Temp = DutyCyle[2] ' Same procedure.
CCP3CON.4 = Temp.0
CCP3CON.5 = Temp.1
CCPR3L = Temp >> 2
RETURN
' ------------------------------------------------------------------------------
' ---- Subroutine to retreive the three dutycycle values from the table.
' ---- Values will be stored in the DutyCycle array.
' ------------------------------------------------------------------------------
GetDuty:
' This For-Next loop runs three times. Each time it gets the value from the lookuptable
' that Phase[i] is pointing at. The Phase array pointers are incremented in the main loop
' and are always 62 "steps" appart so that a 120° phase shift is preserved.
For i = 0 to 2
LookUp2 Phase[i], [0,18,34,52,68,85,102,119,137,152,170,187,203,220,236,253,267,285,302,_
318,334,350,367,382,397,413,429,443,459,474,489,504,518,533,547,562,575,589,603,616,_
630,643,655,669,681,693,706,718,729,740,752,764,774,785,795,806,815,825,835,844,853,_
861,870,879,887,895,901,909,916,923,929,935,941,946,952,957,962,967,971,974,978,_
981,984,987,990,992,994,996,997,998,999,999,1000,999,999,998,997,997,996,994,992,_
990,987,984,981,978,974,971,967,962,957,952,946,941,935,929,923,916,909,901,895,_
887,879,870,861,853,844,835,825,815,806,795,785,774,764,752,740,729,718,706,693,_
681,669,655,643,630,616,603,589,575,562,547,533,518,504,489,474,459,443,429,413,_
397,382,367,350,334,318,302,285,267,253,220,203,187,170,152,137,119,102,85,68,_
52,34,18,0],Temp
' Lookup2 can't handle an array as the designator so we need to
' put the value in a temporary variable first and then move
' it to the array of dutycycles.
DutyCycle[i] = Temp
NEXT
RETURN
/Henrik.
Bookmarks