PDA

View Full Version : Pulse width modulation



manxman
- 14th February 2006, 15:02
I am attempting to use the 10 bit PWM facility on the PIC18F452. Following guidance in the data sheet I have written this code:

'Program to test use of PWM out PortC.2 (Pin 17, CCP1) on 18F452
'Does not work yet!

Include "modedefs.bas"
define LOADER_USED 1 'Bootloader
define OSC 20 'Sets clock to 20MHz


PR2=%11111111 'Set PWM period. Gives 1220.7Hz (0.82ms) with 20MHz clock
CCPR1L=%11110000 'Set PWM duty cycle in CCPR1L register
CCP1CON.4=0: CCP1CON.5=0 'Set PWM duty cycle in CCP1CON<5:4> bits (total of 0.2ms)
TRISC.2=0 'Make CCP1 pin an output
T2CON.0=0: T2CON.1=1 'Set TMR2 prescale value
T2CON.2=1 'Enable Timer2
CCP1CON.0=0: CCP1CON.1=0
CCP1CON.2=1: CCP1CON.3=1 'Configure the CCP1 module for PWM operation


Loop:

goto loop

End


However ... no luck!

Any suggestions?

sougata
- 17th February 2006, 07:25
Hi,

This link may help :

http://www.picbasic.co.uk/forum/attachment.php?attachmentid=466&d=1125842860

Regards

Sougata

manxman
- 2nd March 2006, 12:06
Sougata,

Thanks for your suggestion which I will test shortly.

In the meantime, I have found a way of producing an effective 16 bit PWM output on the 18F452 by combining the PWM outputs of the two CCP1 and CCP2 channels. Both channels are driven at the same frequency, as required by the PIC specification: this is 2KHz in my system and the outputs are used to drive each of the 2 bridges in an L293 chip. CCP1 is assigned to the lower byte of the 16bit Word (and then out to bridge 1) and CCP2 to the upper byte (then out to bridge 2). The maximum current from CCP2 is set to 1/256th of CCP1 by a series resistor. The summed current from the 2 bridges is then 16bit. Easy!

Two MOSFETs could be used instead of the L293 to produce a 16bit controlled DC supply. However, in my application I need to reverse the current to produce a controlled AC. Since the polarity is being switched in this instance, we effectively have 17bit current control!

The same method could be used on any other PIC with two CCP registers.