View previous topic :: View next topic |
Author |
Message |
nicotec
Joined: 07 Nov 2008 Posts: 60
|
Hardware PWM on PIC24 |
Posted: Wed Nov 19, 2008 7:56 am |
|
|
Please help me to obtain hardware pwm 50Hz on pin B4; here is my code:
Code: |
#include <24FJ16GA002.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOJTAG //JTAG disabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#FUSES ICS3 //ICD communication channel 3
#FUSES NOIOL1WAY //Allows multiple reconfigurations of peripheral pins
#FUSES NOWINDIS //Watch Dog Timer in Window mode
#FUSES WPRES128 //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16 //Watch Dog Timer PostScalar 1:32768
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES FRC //Internal Fast RC Oscillator
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOOSCIO //OSC2 is general purpose output
#FUSES NOPR //Pimary oscillaotr disabled
#FUSES I2C1SELD
#use delay(clock=8000000)
#pin_select OC1=PIN_B4
void main()
{
//output_low(PIN_B4);
setup_compare(1,COMPARE_PWM | COMPARE_TIMER2 );
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8,10000 );
setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);
setup_wdt(WDT_ON);
setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
set_pwm_duty(1,9900);
output_low(PIN_B8);
do
{
output_high(PIN_B6);
delay_ms(500);
output_low(PIN_B6);
delay_ms(500);
} while(true);
} |
Tell me how to correct the above code.
Thanks |
|
|
sanddune008
Joined: 23 Oct 2008 Posts: 38
|
Re: Hardware PWM |
Posted: Wed Nov 19, 2008 8:21 am |
|
|
nicotec wrote: |
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8,10000 );
set_pwm_duty(1,9900);
Thanks |
check the following lines once more.......Remember that the PWM duty value should not be set greater value in setup_timer2() your case 10000
Please go through following link.........
http://www.ccsinfo.com/forum/viewtopic.php?t=17729&start=1 |
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
?? |
Posted: Wed Nov 19, 2008 8:51 am |
|
|
But my pin_B4 is always low; so I think that there is another error.
Thanks
Bye |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Nov 19, 2008 12:09 pm |
|
|
What's the PCD version? |
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
Last version |
Posted: Wed Nov 19, 2008 12:25 pm |
|
|
I use PCWHD version 4.081... Could You help me?
Thanks, bye |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Nov 19, 2008 3:08 pm |
|
|
I found, that the code doesn't work with V4.081, cause it is using wrong SFR registers for OC1. This bug is among the various ones silently removed with V4.082. You can use direct SFR access as a workaroun in V4.081:
Code: | #word OC1RS = 0x180
#word OC1CON = 0x184
OC1CON = 6; //setup_compare(1,COMPARE_PWM | COMPARE_TIMER2 );
OC1RS = 5000; //set_pwm_duty(1,5000); |
|
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
Good |
Posted: Thu Nov 20, 2008 2:03 am |
|
|
Thank You very much to give me the solution; now work correctly and I'll update my ccs compiler.
Thank You again
Bye |
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
Solved problem |
Posted: Thu Nov 20, 2008 3:32 am |
|
|
Thanks to FvM I solved problem updating compiler to 4.082 version my code work and with 4.081 work also using line suggested by FvM.
Thanks again
Regards |
|
|
|