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

Generating PWM signal

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



Joined: 31 May 2012
Posts: 10

View user's profile Send private message

Generating PWM signal
PostPosted: Fri Sep 12, 2014 6:20 am     Reply with quote

Firstly, I do asking this question to learn (apologize for taking your time).
Secondly I want to generate a pwm signal (which's duty cycle will changes w.r.t. data) from any output port. Is there any shortest method to get PWM signal? Can you write to ways of generating PWM signals?

Code:
For ex;
// I want light a led with PWM
//data is variable between 0- 100.
//sample wanted code is below
 output_bit(pin_b0,data); //not working as PWM. working as 0-1.

//Should I always define Setup_timer and setup_ccp1 to get PWM?
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Fri Sep 12, 2014 9:06 am     Reply with quote

Start with what PIC?.

You can only generate genuine hardware (high frequency) PWM, on pins that support PWM. Most basic chips only have 2 PWM channels, but then some of the more sophisticated PIC's have 6 or more.

Any pin. Not unless you switch to a PIC with relocatable peripherals (some of the larger PIC18's, and PIC24's).

You can generate a 'software' PWM, using a timer interrupt. Look in the code forum, where there is an example for drive radio control servos. However this has limited frequency and resolution.

Look at ex_pwm.c
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Fri Sep 12, 2014 10:06 am     Reply with quote

Quote:
//Should I always define Setup_timer and setup_ccp1 to get PWM?

It depends on the PIC chip....best to look at the device.h files and see what is needed
EX a 24HJ256GP210A with hardware PWM would have these lines
Code:

setup_timer3(TMR_INTERNAL | TMR_DIV_BY_1, 0x00ff);
 /// TMR_DIV_BY_1 0x00ff are chosen depending on xtal and the application
setup_compare(1, COMPARE_PWM | COMPARE_TIMER3);
set_pwm_duty(1,value);
 

1 represents the hardware compare unit OC1 aka D0 make sure to check the data sheet for the PWM hardware pins
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