View previous topic :: View next topic |
Author |
Message |
a.fava
Joined: 25 Jun 2004 Posts: 17 Location: Cremona (Italy)
|
pwm code on PIC24 |
Posted: Mon Nov 17, 2008 1:47 pm |
|
|
Hello
Ineed to use the output pin OC1 -2 -3 in PWM mode of the micro PIC24FJ128GA010.
I tried several times as shown in the code list, but it didn't work.
My compiler is PCWHD version 4.081.
Anyone can help me ?
Thanks.
Alessandro
Code: | #include <24FJ128GA010.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOJTAG //JTAG disabled
#FUSES NOCOE //Device will reset into operational mode
#FUSES NODEBUG //No Debug mode for ICD
#FUSES WPOSTS16 //Watch Dog Timer PostScalar 1:32768
#FUSES WPRES128 //Watch Dog Timer PreScalar 1:128
#FUSES WINDIS //Watch Dog Timer in non-Window mode
#FUSES HS //High speed Osc (> 4mhz)
#FUSES FRC //Internal Fast RC Oscillator
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES NOWRT //Program memory not write protected
#FUSES NOPROTECT //Code not protected from reading
#use delay(clock=8000000)
void main()
{
setup_adc_ports(sAN5);
setup_adc(adc_clock_internal);
set_adc_channel(5);
setup_compare(1,COMPARE_PWM | COMPARE_TIMER3 );
setup_timer3(TMR_INTERNAL | TMR_DIV_BY_1,0 );
setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
enable_interrupts(INT_TIMER3);
while(1)
{
int value;
int pw_val;
value=read_adc();
pw_val = value * 64;
set_pwm_duty(1, pw_val);
}
} |
_________________ Alessandro |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Nov 17, 2008 4:13 pm |
|
|
I think, you should preset the timer to 0xFFFF rather than 0 to see any PWM action. Also I don't see the purpose of enabling timer interrupt without defining a handler, but fortunately global interrupts are still disabled. |
|
|
a.fava
Joined: 25 Jun 2004 Posts: 17 Location: Cremona (Italy)
|
pwm code |
Posted: Tue Nov 18, 2008 2:16 pm |
|
|
Thank you FvM
I tried to load the timer 3 with 0xFFFF and I didn't use the timer3 interrupt as I think it's not necessary.
But it doesn't work.
Have you got any idea ?
Alessandro _________________ Alessandro |
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
pwm |
Posted: Wed Nov 19, 2008 5:15 am |
|
|
I have similar problem with pic24fj16ga002 using this code:
Code: |
void main()
{
#pin_select OC1=PIN_B4
setup_compare(1,COMPARE_PWM | COMPARE_TIMER2 );
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1);
setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);
//set_pwm_duty(1,30000);
output_low(PIN_B8);
do
{
output_high(PIN_B6);
delay_ms(500);
output_low(PIN_B6);
delay_ms(500);
} while(true);
} |
How do You think to solve the problem? |
|
|
a.fava
Joined: 25 Jun 2004 Posts: 17 Location: Cremona (Italy)
|
|
Posted: Wed Nov 19, 2008 1:07 pm |
|
|
I thank you NICOTEC for the answer.
But the problem is that the function #pin_select...... the CIP 24FJ128GA010 there is and does not work.
For other types of microcontrollers the function #pin_select.... works and I have no problem of programming or operation. _________________ Alessandro |
|
|
|