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 support@ccsinfo.com

How to generate PWM signal?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
winson



Joined: 03 Dec 2008
Posts: 24

View user's profile Send private message

How to generate PWM signal?
PostPosted: Sun Feb 08, 2009 12:17 pm     Reply with quote

Hi,

Anybody can point me to a right direction with some sample code regarding how to generate a PWM signal? Actually i was trying to generate a 38kHz PWM signal as the carrier for my UART IR transmission, then when need to transmit data i will make the 38KHz PWM signal ON/OFF to drive an infrared LED and communicate with the IR receiver.

I was using PIC16F877A, and i haved read through the PWM section of the data sheet but just not so understand about how to set the register.

Anybody can help?

Thanks.
Ttelmah
Guest







PostPosted: Sun Feb 08, 2009 3:16 pm     Reply with quote

Start by calculating the divisor needed to give your frequency. Take (for example), if you have a 8MHz clock. Then the timers run off this divided by 4, so the total divisor, becomes:

(8000000/4)/38000 = 52.63

Now a number of things apply. If the divisor is above 256, you will need to use a prescaler. Three are available, up to /16. If the value is above 4096, then the required frequency is not possible with that crystal.
Then, you can only use integers, so the nearest will be /53, giving 37735Hz.

So then you setup timer2, with:

setup_timer2(T2_DIV_BY_1,52,1);

The first value is a constant, selecting the prescaler (1, 4, 16), the second number, is the required divisor minus one. The third doesn't matter for this.

Then to enable the PWM, use:

setup_ccp1(CCP_PWM);

This for the first PWM, or ccp2 for the second.

Then to output a squarewave, use:

set_pwm1_duty(106L);

The value here should be the divisor * 2. You must include the 'L'.

To set the output low, with no pulse, use:

set_pwm1_duty(0);


Best Wishes
winson



Joined: 03 Dec 2008
Posts: 24

View user's profile Send private message

PostPosted: Mon Feb 09, 2009 10:58 am     Reply with quote

Thanks for reply,

This explanation really very clear. So just have a simple question, if i want to produce a PWM signal with 50% duty cycle then i just need to write like below?

Code:

set_pwm1_duty(53L);
Ttelmah
Guest







PostPosted: Mon Feb 09, 2009 11:43 am     Reply with quote

106L, was for the 50% duty cycle (squarewave).
The maximum count useable, is the divisor*4 (because the PWM, actually uses the master clock at it's 'heart', though the timer uses this /4). So 50%, is half this.

Best Wishes
winson



Joined: 03 Dec 2008
Posts: 24

View user's profile Send private message

PostPosted: Wed Feb 11, 2009 1:23 am     Reply with quote

Oh I see, thank you very much for the explanation. Very Happy
winson



Joined: 03 Dec 2008
Posts: 24

View user's profile Send private message

PostPosted: Wed Feb 18, 2009 11:03 pm     Reply with quote

Hi everybody,

I have succesfull generate the 38KHz PWM square wave and it look nice, but after i add in the PWM code to my program, the LCD which is control by the same PIC was not working correctly and it is ok without the PWM code added. The LCD was display the same character two times like "good" it will become "ggooood".

So anybody know why this is happening? It look like affect by the PWM.

thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 18, 2009 11:30 pm     Reply with quote

In this post, you explain that you use a 9v battery with a current
limiting resistor to supply power to the board. This causes problems
with the voltage every time you change the load on battery:
http://www.ccsinfo.com/forum/viewtopic.php?t=37598&start=3
Have you fixed this problem by adding a 5v voltage regulator I.C.
to your board ? If not, you should do so.
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 1:56 am     Reply with quote

I second the clear explanation Ttelmah provided. But if you want to double check you calculations you can check here: http://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator

Also you can look for Mr.e's multicalc program. You can find it here: http://www.picbasic.org/forum/attachment.php?attachmentid=3283&d=1234787673
Guest








PostPosted: Thu Feb 19, 2009 9:20 am     Reply with quote

PCM programmer wrote:

Have you fixed this problem by adding a 5v voltage regulator I.C.
to your board ? If not, you should do so.


Thanks for reply, yes i'm using a LM7805 IC to regulate the power supply to the entire board(this is what i forget to tell in my previous post). For the LM7805 IC it just able to limit it's output current to a range of 700mA to 1000mA, and this value of current is consider very large for the Vdd of an LCD which is in the range of 1.5mA to 3mA and no choice for me to put a current limiting resistor to the Vdd pin.

I was thinking is it possible just fed a 700mA to the Vdd pin of an LCD(to avoid using current limiting resistor). But i think this may solve the problem, since the LCD will become unstable when a PWM code is added.
winson



Joined: 03 Dec 2008
Posts: 24

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 9:26 am     Reply with quote

Anonymous wrote:
PCM programmer wrote:

Have you fixed this problem by adding a 5v voltage regulator I.C.
to your board ? If not, you should do so.


Thanks for reply, yes i'm using a LM7805 IC to regulate the power supply to the entire board(this is what i forget to tell in my previous post). For the LM7805 IC it just able to limit it's output current to a range of 700mA to 1000mA, and this value of current is consider very large for the Vdd of an LCD which is in the range of 1.5mA to 3mA and no choice for me to put a current limiting resistor to the Vdd pin.

I was thinking is it possible just fed a 700mA to the Vdd pin of an LCD(to avoid using current limiting resistor). But i think this may solve the problem, since the LCD will become unstable when a PWM code is added.


Hey, i'm not aware that i not login. Just to tell this is my message.


Quote:

But i think this may solve the problem, since the LCD will become unstable when a PWM code is added.

Typo here, i should say "i think this may NOT solve the problem"
winson



Joined: 03 Dec 2008
Posts: 24

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 11:41 am     Reply with quote

languer wrote:
I second the clear explanation Ttelmah provided. But if you want to double check you calculations you can check here: http://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator

Also you can look for Mr.e's multicalc program. You can find it here: http://www.picbasic.org/forum/attachment.php?attachmentid=3283&d=1234787673


I have try the PWM calculator from micro-example.com to calculate the value needed for each register, and it is work, the PWM signal was also able to generate out but this time the LCD was sometime not able to display except one line of grey box and sometime work correctly.

Will a PIC with PWM running draw more current?
I have think that may be the PWM enabled PIC will "fight" with the LCD in order to get more current. Thus making the LCD cant obtain enough current then not able to initialize and display correctly.
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 11:58 am     Reply with quote

So are you using 9V through a limiting resistor to power the LCD?
Or are you using 5V (from regulator) to power LCD?
Or 5V (from regulator) to power LCD through limiting resistor?

In any case you should drop the limiting resistor alltogether. The LCD wants to see a voltage, once you supply the voltage it will sink (i.e. use) as much current as it needs. If the LCD datasheet says it sinks 3mA, then that is what it will sink (no matter how much is available).

The regulator is there to provide a constant voltage (5V for the 7805). It will do so under load conditions, up to a maximum of 700mA (based on your numbers). So it will provide a stable 5V for anything that draws (i.e. uses) anywhere from 0mA to 700mA (there are thermal issues to consider, but I will keep this simple). I believe you are thinkin the regulator will try to source 700mA no matter what, and that is not the case.

So for your application, you should power the LCD directly from the regulator (no resistor of any kind required). Now you should have a 0.1uF (rough value) on the LCD supply line to filter any noise which may be getting to it. You should have a similar thing on the PIC's supply line.

Since the LCD does not draw much current, you could also use a zener diode, but the regulator is the best approach by far.
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 12:12 pm     Reply with quote

If I follow PCM programmer logic here, and this is usually a good thing, this post and the one here (http://www.ccsinfo.com/forum/viewtopic.php?t=37598) are for the same thing. Correct?

It appears you are using the PWM/CCP pins (RC1 and RC2) for both PWM and LCD functions:
Code:
#bit RS = PORTC.0
#bit RW = PORTC.1
#bit E  = PORTC.2
#bit BF = PORTD.7


Why?

If this is the case, you do not want to do this. You cannot re-assign the PWM outputs (those are PIC-specific). But you can re-assign the LCD pins (those are SW specific). I would re-assign PORTC.2 (and possibly PORTC.1) to other pins.
winson



Joined: 03 Dec 2008
Posts: 24

View user's profile Send private message

PostPosted: Sat Feb 21, 2009 11:05 pm     Reply with quote

languer wrote:

It appears you are using the PWM/CCP pins (RC1 and RC2) for both PWM and LCD functions:
Code:
#bit RS = PORTC.0
#bit RW = PORTC.1
#bit E  = PORTC.2
#bit BF = PORTD.7


Why?


Sorry for late reply. This is my old program that purposely write to test the LCD and once it is able to run i was adapt it to my main program which is consist of UART transmission, Keypad scanning, LCD display, PWM generation, and Infrared signal PCM modulation. For the new program the RS, RW and E signal was assign to PORTE and PORTD remain the same as the data pin. During the first time when the LCD program section was added to the main program it was run nicely but when add in the PWM and infrared PCM modulation then only that time the LCD was operate in not stable condition.

Do i need to post my code? I think it is very long, and is about 1400lines.
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Tue Feb 24, 2009 11:57 pm     Reply with quote

If you still need help with this, why don't you post a small (not 1400 lines) program which includes the LCD and PWM program sections which demonstrate the problem.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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