Just as a footnote to this, going the longhand way with HPWM & setting the registers manually (vs copping out & using the easy HPWM command), has rescued back a fair bit of program space ...and my LEDS are fading a bit smoother too.
So to sum up, this will get your HPWM running on CCP2 (Pin 7) on a 16f1824....
Code:
DEFINE CCP2_REG PORTC 'route CCP2 output to PortC.3 Pin7 (16f1824)
DEFINE CCP2_BIT 3 'route CCP2 ouput to PortC.3 Pin7 (16f1824)
duty var byte
CCP2CON = %00001100 ' Turn HPWM on on CCP2
TRISC.3 = 1 'Disable the CCP2 pin output driver by setting the bit
CCPR2L.6 = 0 'I'm only using 8 bit PWM so clear the top two bits (these would be using for 9/10 bit HPWM)
CCPR2L.7 = 0 'I'm only using 8 bit PWM so clear the top two bits (these would be using for 9/10 bit HPWM)
PR2 = 63 'This sets the PWM period (frequency) to 15.625KHZ @4MHZ (I hope so at least - I've not scoped it!) and yields the full 255 bits of resolution
CCPTMRS0 = %11110011 'This is the CCP Timer select register - these bits select 'CCP2 USING TIMER2'
T2CON = %00000100 'TIMER2 ON 1:1 PRESCALER 1:1 POSTSCALER
TRISC.3 = 0 'Enable the CCP2 pin output driver by clearing the bit
Main:
duty = 255
gosub Change_PWM
pause 500
duty = 0
gosub Change_PWM
pause 500
goto Main
Change_PWM: 'the following three lines for 8 bit PWM
CCP2CON.4 = Duty.0 'Bit 0
CCP2CON.5 = Duty.1 'Bit 1
CCPR2L = Duty >> 2 'Bit 2-7
return
the above code will give you some LED blinkage (you can never have enough LED blinkage) to prove it's working ....all you have to do is change the variable "Duty" & then a Gosub to change the HPWM duty cycle.
Many thanks to all who helped
Bookmarks