View previous topic :: View next topic |
Author |
Message |
cengav4r
Joined: 31 May 2012 Posts: 10
|
Generating PWM signal |
Posted: Fri Sep 12, 2014 6:20 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19365
|
|
Posted: Fri Sep 12, 2014 9:06 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Fri Sep 12, 2014 10:06 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|