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 support@ccsinfo.com

Hardware PWM on PIC24

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



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

Hardware PWM on PIC24
PostPosted: Wed Nov 19, 2008 7:56 am     Reply with quote

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

View user's profile Send private message

Re: Hardware PWM
PostPosted: Wed Nov 19, 2008 8:21 am     Reply with quote

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

View user's profile Send private message

??
PostPosted: Wed Nov 19, 2008 8:51 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 19, 2008 12:09 pm     Reply with quote

What's the PCD version?
nicotec



Joined: 07 Nov 2008
Posts: 60

View user's profile Send private message

Last version
PostPosted: Wed Nov 19, 2008 12:25 pm     Reply with quote

I use PCWHD version 4.081... Could You help me?
Thanks, bye
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Nov 19, 2008 3:08 pm     Reply with quote

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

View user's profile Send private message

Good
PostPosted: Thu Nov 20, 2008 2:03 am     Reply with quote

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

View user's profile Send private message

Solved problem
PostPosted: Thu Nov 20, 2008 3:32 am     Reply with quote

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
Smile Regards Very Happy
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