View previous topic :: View next topic |
Author |
Message |
ratgod
Joined: 27 Jan 2006 Posts: 69 Location: Manchester, England
|
1Hz PWM signal, any ideas? |
Posted: Thu Jun 19, 2008 1:12 pm |
|
|
Hi There,
I am trying to generate a 1Hz PWM signal, I'm not sure about duty cycle and that sort of stuff.
I want it to have the LED on for the same length of time as its off, is that 50% duty?
I would also like to be able to vary the frequency with a Pot, I have a 10K pot on my PIC (18F252 for now) working well, I can get it to make the led become brighter and dimmer via ADC and PWM but I'm not fully understanding what I am doing with PWM.
note: a 20MHz clock is being used.
Code: | void pwm_mode()
{
long duty;
byte value=0;
setup_adc_ports(AN0); //_VREF_VREF);
setup_adc(ADC_CLOCK_DIV_64);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16,255,1);
while(true)
{
set_adc_channel(0);
delay_us(10);
value = read_adc();
set_pwm1_duty((long)value*2);
delay_ms(250);
}
} |
any ideas?
thanks |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Jun 19, 2008 1:38 pm |
|
|
The term PWM refers to Pulse Width Modulation, which is a means of controlling the average power by controlling the duty cycle. If you want 50% duty cycle you can not use PWM.
On the other hand you might be able to use the PWM hardware of the PIC in an odd way as an LED flasher by setting a fixed 50% PWM value and adjusting the clock speed.
But at real slow speeds like 1Hz you can do it easier all in software. Is this an educational quest to understand the PWM hardware of the PIC, or is there some product requirement you are trying to meet? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
ratgod
Joined: 27 Jan 2006 Posts: 69 Location: Manchester, England
|
|
Posted: Thu Jun 19, 2008 7:16 pm |
|
|
Hi There,
This is pretty much part of research for work.
we are intending to be pulsing transistor or FET based devices to allow them to handle more current, and I was asked to research PWM and try to make a led flash every second with it then we were going to soak test the final device (when its been decided which one). I assume it will be going much faster than 1Hz in the end product, I think they wanted me to do 1Hz just to prove the concept of hardware PWM in a visual way.
Do you have a simple explaination on how to configure the PWM as I'm somewhat lost with all the information I have seen.
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 19, 2008 7:47 pm |
|
|
This thread has the formula to find the lowest PWM frequency for the
hardware PWM module. You'll find that you can't go as low as 1 Hz.
http://www.ccsinfo.com/forum/viewtopic.php?t=17993
For a software routine that can be modified to work at 1 Hz, see my
post near the start of this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=20050
See my comments at the end of this thread for instructions on how
to reduce the PWM frequency with that software routine:
http://www.ccsinfo.com/forum/viewtopic.php?t=30175
I suggest that you change the RTCC divisor to 64 or 128, and
then increase the LOOPCNT value as required. These are the
two lines that you will need to change:
Code: |
#define LOOPCNT 39
setup_counters(RTCC_INTERNAL, RTCC_DIV_1);
|
-------
Edited to fix one of the links.
Last edited by PCM programmer on Thu Sep 11, 2008 3:51 pm; edited 1 time in total |
|
|
|