Page 1 of 1
Questions about PWM and ICD
Posted: Thu Feb 15, 2007 2:53 pm
by serstel
Hello
I am using the C command ccpr2l = FCV_SPEED; to output PWM from the PIC. This command alone however does not specify the frequency of the PWM. The CCPRxl register dfines the duty cycle. The period is set by the PR2 register in conjunction with the timer prescaler. How can i set the frequency of the PWM? In addition must the FCV_SPEED variable be a byte or can it be an integer?
Also how can I do in circuit debuging? I have the EB-006 multiprogrammer. Can I do debuging within flowcode?
Serstel
Posted: Thu Feb 15, 2007 4:58 pm
by Steve
No ICD from Flowcode yet, I'm afraid (it's on the list...).
In Flowcode, the variable should be called "Speed" and yes, it should be an integer.
To set the frequency of the PWM, you will need to write a couple of lines in a C icon that alter the values of the pr2 and t2con registers appropriately. You will need to calculate these values yourself as they are dependent on your clock frequency.
You will also need to turn the PWM on by writing to the ccp1con register. The datasheet for the PICmicro should have all the details you need.
Posted: Thu Feb 15, 2007 8:05 pm
by serstel
I am using a pic 18F4550. I have used the C commands
ccp1con=0x12;
pr2 = 0xff;
t2con = 0x7;
This is to have timer prescaler 16 and the minimum PWM frequency.
I still can't output PWM from the two outputs ccpr1l and ccpr2l. What else must I do?

Posted: Fri Feb 16, 2007 11:26 am
by Benj
Hello Serstel
This code is for the 18F4455 but I think it should be compatible with the 4550
C Code Block
Code: Select all
pr2 = 0xFF;
ccpr1l = FCV_DUTYR;
trisc = trisc & 0xF9;
t2con = 4;
ccp1con = 0x0C;
ccpr2l = FCV_DUTYL;
ccp2con = 0x0C;
Uses two flowcode variables for the duty cycle dutyr and dutyl.
This should get your PWMs going and then you can play around with the values to obtain the correct frequency of oscillation.
Posted: Fri Feb 16, 2007 12:22 pm
by serstel
Hello
I put this C code into a C block between begin and end and I compile just that
pr2 = 0xFF;
ccpr1l = 32767;
trisc = trisc & 0xF9;
t2con = 4;
ccp1con = 0x0C;
ccpr2l = 32767;
ccp2con = 0x0C;
Pin RC2 which is ccp1 seems to work but pin RC1 which is ccp2 does not. Not even when I configure it for output instead of RB3 does not work.
Serstel
Posted: Fri Feb 16, 2007 12:57 pm
by Steve
The values for ccpr1l and ccpr2l should be 8-bit values (i.e. between 0 and 255).
Also, there is a config setting for this chip that defines whether CCP2 is on RC1 or RB3.
Posted: Fri Feb 16, 2007 2:26 pm
by serstel
Helo
I use the config setting so that the pwm goes to RC1 instead of RB3 and used 8 bit values.
The problem however persists. No output on RC1.
Basically the whole project is a C code block between begin and end with this code inside it:
pr2 = 0xFF;
ccpr1l = 255;
trisc = trisc & 0xF9;
t2con = 4;
ccp1con = 0x0C;
ccpr2l = 255;
ccp2con = 0x0C;
Posted: Fri Feb 16, 2007 2:54 pm
by Steve
I've just tested this code here on an 18F4455 and it works fine on both outputs, although the duty cycle is 99%.
Try setting ccpr1l and ccpr2l to 128 instead of 255 - that should give a 50% duty cycle.
If you still can't get the code working, then I can only assume the chip is broken.