|
|
View previous topic :: View next topic |
Author |
Message |
adcor
Joined: 21 Feb 2008 Posts: 31
|
PWM for PIC24FJ128GC006 and CCS 5.077 |
Posted: Mon Jun 18, 2018 1:18 am |
|
|
I cannot get the PWM working using the code below, maybe someone will spot the issue:
Code: |
#include <24FJ128GC006.h>
#DEVICE CONST=READ_ONLY
#device ADC=12
#FUSES FRC_PLL, PLL2, NOPROTECT, NOWDT, NOWRT, NODEBUG
#USE delay(internal=8MHz, clock=32Mhz)
#pin_select OC1 = PIN_B4 // PWM out 4khz
void main()
{
setup_compare( 1, COMPARE_PWM_EDGE | COMPARE_TRIG_SYNC_TIMER3 | COMPARE_TIMER3 );
setup_timer3(TMR_INTERNAL | TMR_DIV_BY_8, 500);//250uS
set_pwm_duty(1,251);
while (TRUE);
|
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19541
|
|
Posted: Mon Jun 18, 2018 3:51 am |
|
|
One reason, and a comment:
Reason: COMPARE_CONT_PULSE needed.
Comment:
You do realise on these you don't have to use a timer?.
These have an internal timer on each channel that can be used instead. Use 'COMPARE_SYSTEM_CLOCK', and setting the RS register (set_compare_time). No prescaler though. However does mean you can save lots of timers!... |
|
|
adcor
Joined: 21 Feb 2008 Posts: 31
|
|
Posted: Mon Jun 18, 2018 5:24 am |
|
|
thanks for your answer. I tried both suggestion but no luck yet. (even COMPARE_TOGGLE)
Code: |
setup_compare(1,COMPARE_CONT_PULSE | COMPARE_TRIG_SYNC_TIMER3 | COMPARE_TIMER3 );
setup_timer3(TMR_INTERNAL | TMR_DIV_BY_8, 500);//250uS
set_pwm_duty(1,251);
//Second option
setup_compare(1,COMPARE_CONT_PULSE | COMPARE_SYSTEM_CLOCK );
set_compare_time(1, 1819);
set_pwm_duty(1,900); // set 50% duty |
You also helped me a few days ago with a CN24 compiler error for this processor....I am having doubts about this PIC/compiler combo.
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19541
|
|
Posted: Mon Jun 18, 2018 7:56 am |
|
|
Are you sure your CPU is clocking?.
Having 'CLOCK', set to a different frequency than 'INTERNAL' is very odd. Using:
Code: |
#include <24FJ128GC006.h>
#DEVICE CONST=READ_ONLY
#device ADC=12
#FUSES FRC_PLL, PLL2, NOPROTECT, NOWDT, NOWRT, NODEBUG
#USE delay(internal=32MHZ)
#pin_select OC1 = PIN_B4 // PWM out 4khz
void main()
{
setup_compare( 1, COMPARE_FAULT_CLEAR);
setup_compare( 1, COMPARE_PWM_EDGE | COMPARE_TRIG_SYNC_TIMER3 | COMPARE_CONT_PULSE | COMPARE_TIMER3 );
setup_timer3(TMR_INTERNAL | TMR_DIV_BY_8,499);//250uS
set_pwm_duty(1,249);
|
It is merrily outputting a pulse for me |
|
|
adcor
Joined: 21 Feb 2008 Posts: 31
|
SOLVED! PWM for PIC24FJ128GC006 and CCS 5.077 |
Posted: Mon Jun 18, 2018 4:08 pm |
|
|
Thank you, it worked! |
|
|
|
|
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
|