Hi Does anyone know where to find any example of implementing PWM using CAPCOM especially in XC161 (16 bit microcontroller)? MJ
Ok, I arbitrarily selected a duty cycle of 50%. A 50% DC is simply half of the period which I calculated before to be 0x7A12. So take 0x7A12/2 = 0x3D09, but remember that the timer only counts up so again you need to subtract this from 0x10000. Then the compare value for CC0 would be 0x10000 – 0x3D09 = 0xC2F7. I said that the channel is running in compare mode 3 so when the timer (T0) matches the value in the CC0 register the port pin associated with CC0 (P6.0) is set high (to 1). When the timer overflows the port associated with CC0 (P6.0) is cleared (to 0). You can achieve a 0% DC by programming the CC0 value less than period value. You can not achieve 100% DC without directly controlling the pin since the timer overflow will always reset (clear) the output. Some examples to write to the CC0 register for different DC values would be: 0x85EF = 99.9968% 0xC2F7 = 50.0000% 0xFFFF = 0.0032% 0x0 thru 0x85EE = 0.0000% The ports can be used for general purpose I/O or for different functions. To internally map the port pin (P6.0) to the CAPCOM then you need to have a 1 in the zero bit of the ALTSELOP6 register (It Assumes that NO external Bus is enabled, thus no chip selects). Since I only need to make sure that I have a one in the register a simple OR to the register can be made.
ALTSEL0P6 |= 0x0001u;
DP6 |= 0x0001u;