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

Help! Code Compiles and Loads, but doesn't work!

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



Joined: 03 Oct 2006
Posts: 17

View user's profile Send private message

Help! Code Compiles and Loads, but doesn't work!
PostPosted: Tue Oct 03, 2006 8:10 pm     Reply with quote

I'm at my wits end. I've been crashing through PIC programming for the past two weeks and finally found the CCS compiler, now included in the latest version of MPLAB. I'm trying to work with the PIC16F54.

I've written a simple chunk of code. It compiles, and loads well using the PICKIT2. When I run it, it doesn't work.

Any ideas? Here's the entire program:

Code:


#include <16F54.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=2000000)

void main(void) {
    int i;
   output_low(PIN_B1);
   for (i=1;i<=2000;i++) {
     output_high(PIN_B1);
     delay_ms(500);
     output_low(PIN_B1);
     delay_ms(500);
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 03, 2006 8:22 pm     Reply with quote

I see two immediate problems.

1. Your #use delay() statement is set for 2 MHz. Is that accurate, or
do you really have a 20 MHz crystal ? If so, add another zero to
the statement.
2. In CCS, an 'int' is an unsigned 8-bit integer. It can hold a maximum
value of 255. To hold 2000, you need to declare it as a 'long'.

For people new to CCS, it's better to use the alternate data type
declarators: int1, int8, int16, int32. These are all unsigned.
Then you can immediately see the size of your variables.
Quote:

#include <16F54.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=2000000)

void main(void) {
int i;
output_low(PIN_B1);
for (i=1;i<=2000;i++) {
output_high(PIN_B1);
delay_ms(500);
output_low(PIN_B1);
delay_ms(500);
}
}
phased



Joined: 03 Oct 2006
Posts: 17

View user's profile Send private message

PostPosted: Tue Oct 03, 2006 8:28 pm     Reply with quote

I'll try that now. Actually, I don't have an external crystal? I was under the impression I didn't need one. Also, my problem existed before I changed the 20MHz to 2MHz. and before I put the whole thing in a for loop.

I'll let you know in 30 sec what happens.

Thank you VERY much!

Phased.
phased



Joined: 03 Oct 2006
Posts: 17

View user's profile Send private message

Here's the new code
PostPosted: Tue Oct 03, 2006 8:29 pm     Reply with quote

Code:

#include <16F54.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)

void main(void) {

   output_low(PIN_B1);
   while (TRUE)
   {
     output_high(PIN_B1);
     delay_ms(500);
     output_low(PIN_B1);
     delay_ms(500);
   }
}



Still nothing on pin B1. The oscilloscope shows a solid 0V!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 03, 2006 8:39 pm     Reply with quote

Quote:
Actually, I don't have an external crystal? I was under the impression I didn't need one.

You must have an external clock source for this PIC to operate.
The 16F54 doesn't have an internal oscillator, as for example
the 16F88 does. The 16F54 must have an external crystal,
resonator, RC circuit, or an external oscillator, in order to run.

Link to data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/41213C.pdf
See section 4.0 -- Oscillator Configurations
phased



Joined: 03 Oct 2006
Posts: 17

View user's profile Send private message

I feel like an idiot!
PostPosted: Tue Oct 03, 2006 8:52 pm     Reply with quote

I feel like an idiot! After PCM's first post I turned to page 21 of the datasheet and my face went pale. Thank's guys. I don't really care about the timing honestly. So I'm going to stick an RC circuit on here now. I'm sure I won't be able to make it work but as I have done for the past two weeks, I'm going to keep at it.

I'll let you know as soon as have something. Btw, I did a bit of math to find the R and C I'd need. Is the frequency F = 1/(RC) ?

So if R=1K and C=50pF, then F = 20Mhz.

Is this correct?

Thanks again guys!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 03, 2006 9:17 pm     Reply with quote

The maximum frequency for the RC oscillator is 4 MHz. To get that
frequency, you have to use a 3K resistor and a 20 pf capacitor.
You can't use values any smaller than those.

If you want to run at 20 MHz, then you need to get a crystal, and
also get two 22 pf capacitors.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 7:37 am     Reply with quote

If you're going to use a RC oscillator, make sure you change your #FUSES from HS to RC.

Ronald
phased



Joined: 03 Oct 2006
Posts: 17

View user's profile Send private message

PostPosted: Mon Oct 09, 2006 12:35 pm     Reply with quote

Smile THANK YOU ALL FOR THE HELP! Exclamation
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