CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PWM for PIC24FJ128GC006 and CCS 5.077

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
adcor



Joined: 21 Feb 2008
Posts: 31

View user's profile Send private message

PWM for PIC24FJ128GC006 and CCS 5.077
PostPosted: Mon Jun 18, 2018 1:18 am     Reply with quote

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: 19540

View user's profile Send private message

PostPosted: Mon Jun 18, 2018 3:51 am     Reply with quote

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!... Smile
adcor



Joined: 21 Feb 2008
Posts: 31

View user's profile Send private message

PostPosted: Mon Jun 18, 2018 5:24 am     Reply with quote

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: 19540

View user's profile Send private message

PostPosted: Mon Jun 18, 2018 7:56 am     Reply with quote

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

View user's profile Send private message

SOLVED! PWM for PIC24FJ128GC006 and CCS 5.077
PostPosted: Mon Jun 18, 2018 4:08 pm     Reply with quote

Thank you, it worked!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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