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

Hardware PWM 50Hz using PIC24FJ16GA002

 
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 50Hz using PIC24FJ16GA002
PostPosted: Thu Nov 13, 2008 1:18 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Nov 13, 2008 5:10 pm     Reply with quote

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

View user's profile Send private message

clock
PostPosted: Fri Nov 14, 2008 1:24 am     Reply with quote

Internal FRC 8Mhz; could you help me?
Thanks
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Nov 14, 2008 1:20 pm     Reply with quote

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

View user's profile Send private message

PWM
PostPosted: Fri Nov 14, 2008 2:52 pm     Reply with quote

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

View user's profile Send private message

??
PostPosted: Mon Nov 17, 2008 1:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 17, 2008 3:55 pm     Reply with quote

With PMW duty cycle set to a value higher than timer2 register, PWM should be expected to stay permanently high.
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