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

PIC16F877: PWM with less than 250Hz possible?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 31, 2010 3:01 pm     Reply with quote

I didn't actually write the inner loop code. Someone else Smile on the
forum wrote that as a fragment. I incorporated it into a test program
just to be my helpful old self. But I'll try to explain it.

The 'loop' variable is initialized to 100 (Set in LOOPCNT).

The first if() statement decrements the loop counter once per pass.
It will decrement to 99, then 98, etc., down to 0 (after 100 interrupts).
When it goes to 0, the if() statement will reload the loop counter with
the initial value of 100.

I think there's a bug with the 'pulse' variable. It's not initialized.
One quick fix would be to initialize it to 0 in the static declaration:
Quote:
#INT_RTCC
void tick_interrupt(void)
{
static int8 loop = LOOPCNT;
static int8 pulse = 0;
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Aug 31, 2010 4:56 pm     Reply with quote

PCM programmer wrote:
I think there's a bug with the 'pulse' variable. It's not initialized.
In ANSI C all static and global variables will be initialized to zero before the program starts, except when the programmer specified another value.

To avoid the confusion about the variable being initialized or not I often explicitly add the initial value.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 31, 2010 5:17 pm     Reply with quote

You're right, and I looked for that in the .LST file but I missed it.
It is there (with no explicit init of pulse in the code):
Quote:
012C: CLRF pulse
Brian



Joined: 29 Jun 2010
Posts: 10

View user's profile Send private message

PostPosted: Wed Sep 01, 2010 11:57 am     Reply with quote

Everything is working now and makes sense.

Thanks!
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 Previous  1, 2
Page 2 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