View previous topic :: View next topic |
Author |
Message |
matheuslps
Joined: 29 Sep 2010 Posts: 73 Location: Brazil
|
Simple PWM with ECCP in D7 PIN |
Posted: Sat Dec 29, 2012 9:38 am |
|
|
Hi guys, I am tring to learn how to use the ECCP module with the PIC 18F45K20.
I was able to configure it to show me a singnal on the CCP1 (P1A) with the code:
Code: | #include <18F45K20.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#use delay (clock=4000000)
void main ()
{
setup_timer_2(T2_DIV_BY_16,207,1);
setup_ccp1(CCP_PWM_HALF_BRIDGE|CCP_PWM_H_H);
set_pwm1_duty(416L); //Should be 50% duty cycle
while (true);
} |
I need this signal on pin D7 (P1D). Is it possible to run the signal only on that pin?
Version: 4.114 and 4.057
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Sat Dec 29, 2012 12:20 pm |
|
|
You don't want half bridge mode.
Just start with the single PWM mode (CCP_PWM).
This then defaults to P1A.
You then have to change the pulse steering register to direct this output to P1D (CCP_PULSE_STEERING_D).
So:
Code: |
#include <18F45K20.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#use delay (clock=4000000)
void main ()
{
setup_timer_2(T2_DIV_BY_16,207,1);
setup_ccp1(CCP_PWM|CCP_PULSE_STEERING_D);
//on some compiler versions you may need to set TRIS here
output_drive(PIN_D7);
set_pwm1_duty(416L); //Should be 50% duty cycle
while (true);
}
|
Should work with 4.114 (would _not_ trust 4.057 - this was before V4 started to really work).
Best Wishes |
|
|
matheuslps
Joined: 29 Sep 2010 Posts: 73 Location: Brazil
|
|
Posted: Sat Dec 29, 2012 5:10 pm |
|
|
Thanks man, working like a charm! |
|
|
|