|
|
View previous topic :: View next topic |
Author |
Message |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
how to use PWM controls in PIC16F877a |
Posted: Fri Jan 03, 2014 12:35 am |
|
|
hey,
Is it possible to use PIC16F877A so that pin C2 (pin 17) and C1 (pin 16)
use different pulse width modulation ... maybe C2 on-time is 10 and off-time is 90, and C1 on-time is 50 and off-time is 50.
And if it is possible please tell me how to do it in practice.
I learned they both use timer2 interrupt.
Pin C2 is PWM 2 and pin C1 is PWM 1 ok??
But in practice I didn't find any pulses from C1 pin ... is it possible to use
those PWM channels in different pulse widths ?
Pin C2 works fine OK... I use it to control LCD display background light.
Happy new year for you all Smile.
I tried to make it work but no success.
Very happy and fun life for you all and I 'll be happy if you answer my question.
best regards from finland
-arto- |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Jan 03, 2014 2:13 am |
|
|
OK.
Some comments.
First look again at the data sheet. C2, is PWM1, not PWM2, and C1, is PWM2 not PWM1.
Little things like this will cause a lot of grief....
No, the PWM's, _don't_ use 'timer2 interrupt'. They just use timer2 as their count source. The interrupt is a separate thing entirely.
So:
Code: |
#include <16F877A.h>
#FUSES NOWDT, HS, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,UART1,bits=8,errors)
void main()
{
int16 width;
//Two PWM output demo
setup_timer_2(T2_DIV_BY_4,249,1);
//With the 20MHz clock, wil give PWM at 5KHz - 20000000/(4*4*(249+1))
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
//setup the two PWM channels
for (width=0;width<1000;width++)
{
//Now generate a pwm getting wider slowly on pwm1, and narrower
//at the same rate on PWM2
set_pwm1_duty(width);
set_pwm2_duty(1000-width);
delay_ms(10);
//each width will be output for 10mSec, so a total of just over
//ten seconds to ramp all the way.
}
//Now for your simpler 'fixed' widths
set_pwm1_duty(100L); //10% duty on C2
set_pwm2_duty(500L); //50% on C1
while(TRUE)
{
//and do nothing else....
}
}
|
Now I show how to vary the widths initially, ramping PWM1, from off to fully on, and PWM2 from on to off, over 10 seconds. Then stop and give the fixed ratios you describe.
On the timer, it counts from 0 to 'PR2' (the second number in the setup), so 0 to 249, which is 250 counts. The clock feeding all peripherals like this, is 'Fosc/4'. This then goes through a prescaler (/4 in the example), so the timer runs at 20000000/(4*4*(249+1)) = 5KHz.
Maximum value allowed for PR2, is 255 (256 counts).
Then the value for the 'on' time (which is what the 'duty' function sets), can go from 0 (off) to ((PR2+1)*4)-1. So '999' in the example.
Note the use of 'L', so the constant number is treated as an int16. The duty function has two different modes of operation. In one it only writes the top 8bits of the 10bit value allowed, while in the second it writes the whole value. The former is quicker, but doesn't give access to the full resolution of the PWM. Using int16, ensures the full access is done.
Best Wishes |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Fri Jan 03, 2014 8:39 am |
|
|
hello Tthelmah,
lot of thanks for your clear and good answer !
It was very confusing for me when I used :
set_pwm1_duty(100); //10% duty on C2
set_pwm2_duty(500); //50% on C1
and in my hardware PWM1 worked OK
it is connected to pin C2... and I did not use this define at all :
setup_timer_2(T2_DIV_BY_4,249,1);
(clock freq. #use delay(clock= 11059200) and pulse frequence is about
600 Hz measured with my good chinese oscilloscope siglent SDS D1072CNL )
but I solved the problem today morning I haven't use those at all:
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
thank's a lot have fun
-arto- |
|
|
freedom
Joined: 10 Jul 2011 Posts: 54
|
|
Posted: Mon Jun 15, 2015 3:10 am |
|
|
Quote: | First look again at the data sheet. C2, is PWM1, not PWM2, and C1, is PWM2 not PWM1.
|
Yes I have seen this in datasheet of PIC16F877A
If I want to use PWM_1 and PWM_2 at any other pin ( as example PWM_1 at pin_A0 and PWM_2 at pin_E1),
Does it possible ?
Any software/coding solution?
Any example ? please
I want to do it because , I used all pin of port "C" as input and used Port "A" and "E" as output.
Pin_A0 will be used for stepper pulse generator and Pin_E1 will be used for another stepper pulse generator
I am using compiler version 4.124 |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Mon Jun 15, 2015 5:53 am |
|
|
freedom wrote: | Quote: | First look again at the data sheet. C2, is PWM1, not PWM2, and C1, is PWM2 not PWM1.
|
Yes I have seen this in datasheet of PIC16F877A
If I want to use PWM_1 and PWM_2 at any other pin ( as example PWM_1 at pin_A0 and PWM_2 at pin_E1),
Does it possible ?
Any software/coding solution?
Any example ? please
I want to do it because , I used all pin of port "C" as input and used Port "A" and "E" as output.
Pin_A0 will be used for stepper pulse generator and Pin_E1 will be used for another stepper pulse generator
I am using compiler version 4.124 |
Quote: |
Hello,
if I get your email I'll send an example how to control RGB led with 2 PWM
and interrupts ...an BLUE led uses interrupts ... RED and GREEN uses PWM
best regards
arto
my email:
arto.hautala@pp1.inet.fi
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 15, 2015 2:01 pm |
|
|
Quote: | Yes I have seen this in datasheet of PIC16F877A
If I want to use PWM_1 and PWM_2 at any other pin ( as example PWM_1 at pin_A0 and PWM_2 at pin_E1),
Does it possible ?
Any software/coding solution?
Any example ? please |
No, you can't move the output pins for the CCP modules with the 16F877A.
If you want PWM on other pins, then you have to do software PWM. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|