View previous topic :: View next topic |
Author |
Message |
nicotec
Joined: 07 Nov 2008 Posts: 60
|
Hardware PWM 50Hz using PIC24FJ16GA002 |
Posted: Thu Nov 13, 2008 1:18 pm |
|
|
Hi, I'm new to use CCS compiler and I have some difficulty to obtain through pic24-wizard the hardware pwm in subject.
Here is the code:
Code: | void main()
{
#pin_select OC1 = PIN_B4
setup_compare(1,COMPARE_PWM | COMPARE_TIMER2 );
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_64,20 );
setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);
setup_wdt(WDT_ON);
setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
// TODO: USER CODE!!
} |
Through pic24-wizard appear that the following line:
Code: |
setup_compare(1,COMPARE_PWM | COMPARE_TIMER2 );
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_64,20 ); |
It should be enough to obtain hardware pwm, but the frequency specified during wizard setup does not appear in the code.
Thank You in advance and Regards |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Nov 13, 2008 5:10 pm |
|
|
The PWM period of 20 seems a bit too short (and doesn't allow any usable PWM operation). You didn't tell the system clock frequency, |
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
clock |
Posted: Fri Nov 14, 2008 1:24 am |
|
|
Internal FRC 8Mhz; could you help me?
Thanks |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Fri Nov 14, 2008 1:20 pm |
|
|
According to documentation, setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8,10000) should give 50 Hz PWM frequenc. |
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
PWM |
Posted: Fri Nov 14, 2008 2:52 pm |
|
|
Thank You very much, I calculated a PR2 of 9999 using data sheet formula; I'll try on monday the following setting to obtain a 50Hz 50% PWM:
Code: |
#pin_select OC1 = PIN_B4
setup_compare(1,COMPARE_PWM | COMPARE_TIMER2 );
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8,9999);
set_pwm_duty(1,32768);
|
after I will post result here;
I hope it will work fine.
Thanks
Bye |
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
?? |
Posted: Mon Nov 17, 2008 1:20 am |
|
|
I tried the above line but still not working; here is my code:
Code: | void main()
{
#pin_select OC1 = PIN_B4
setup_compare(1,COMPARE_PWM | COMPARE_TIMER2 );
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8,9999 );
setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);
setup_wdt(WDT_ON);
setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
output_low(PIN_B8);
do
{
set_pwm_duty(1,32768);
output_high(PIN_B6);
delay_ms(500);
output_low(PIN_B6);
output_high(PIN_B7);
delay_ms(500);
output_low(PIN_B7);
} while(true);
} |
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Nov 17, 2008 3:55 pm |
|
|
With PMW duty cycle set to a value higher than timer2 register, PWM should be expected to stay permanently high. |
|
|
|