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

PWM spikes

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



Joined: 15 Apr 2013
Posts: 6
Location: India

View user's profile Send private message

PWM spikes
PostPosted: Sun Sep 15, 2013 11:50 pm     Reply with quote

Hi,

I am trying to generate PWM on 16F690. This is the code below.

Code:

#include <16F690.h>
#FUSES NOWDT, HS, NOPROTECT, BROWNOUT, MCLR, NOCPD, NOPUT, NOIESO, NOFCMEN
#use delay(clock=40000000)


void main()
{
   setup_timer_2(T2_DIV_BY_1,250,1);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty(0); /// 0 - 255
}


I have set the duty cycle to zero. I tried simulating this using Proteus. It shows spikes now and then. Is there an error in my code or is it a bug of Proteus?

Here is a Screenshot of Proteus
Ttelmah



Joined: 11 Mar 2010
Posts: 19366

View user's profile Send private message

PostPosted: Mon Sep 16, 2013 12:39 am     Reply with quote

With the code as posted, the chip will actually be asleep, and the oscillator stopped. No anything....
I'd say Proteus has problems with this, and keeps trying to reawaken the chip.

Proteus also does not check other things for being 'possible'. What is the maximum clock rate that HS supports?. What is the maximum clock rate the chip supports?. OS01 in table 17-1. Is 40MHz ever going to work?.
Then in a real chip, you need:
1) MCLR pulled up (since you have it enabled).
2) Decoupling close to the chip.
3) You really should not leave the other pins floating. Either drive them (set as output), or have a pullup resistor on them. Floating pins _will_ lead to erratic power consumption and behaviour - again Proteus misses this.
4) Clock. For HS, you need a crystal, and loading capacitors. Proteus will 'believe' you if you say you have a 40MHz crystal, and it is working, even if the real chip won't....

This is why you will learn a lot more especially about problems, using a real chip.

Code:

#include <16F690.h>
#FUSES NOWDT, HS, NOPROTECT, BROWNOUT, MCLR, NOCPD, NOPUT, NOIESO, NOFCMEN
#use delay(clock=40000000)

void main()
{
   setup_timer_2(T2_DIV_BY_1,250,1);
   setup_ccp1(CCP_PWM_L_L); //Note the change
   set_pwm1_duty(0); /// 0 - 255
   while (TRUE); //stop the chip 'dropping off the end'....
}


The other thing is that using an eight bit value to set the duty, leaves two bits of the PWM 'unprogrammed'. These can be set in the setup_CCP1, using the syntax I show. Without this, the actual result can be 'indeterminate'. However on the real chip, given it'd be asleep, you would see nothing. However if you keep the chip awake (while etc..), you might see continuous steady pulses, depending what is in these bits. They are meant to be zero on boot, but I have in the past seen these containing values.

Best Wishes
artohautala



Joined: 17 Nov 2011
Posts: 187

View user's profile Send private message Send e-mail

PWM works fine now
PostPosted: Mon Nov 25, 2013 7:51 am     Reply with quote

Lot of thanks Ttelmah

I'm using this setup now and everything is OK
My clock frequency is

Code:

#use delay(clock= 11059200)

//init PWM1
setup_timer_2(T2_DIV_BY_16,255,16);
setup_ccp1(CCP_PWM);             
     
//set_pwm1_duty(100); /// 0 - 255

I measured PWM pulse in practice with oscilloscope frequency is 675 Hz
period is 1.48 ms.
Ontime is 0.57 ms and offtime is 0.91 ms.

all the best

Laughing Very Happy
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