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

1Hz PWM signal, any ideas?

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



Joined: 27 Jan 2006
Posts: 69
Location: Manchester, England

View user's profile Send private message

1Hz PWM signal, any ideas?
PostPosted: Thu Jun 19, 2008 1:12 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 19, 2008 1:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 19, 2008 7:16 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 19, 2008 7:47 pm     Reply with quote

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
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