View previous topic :: View next topic |
Author |
Message |
phased
Joined: 03 Oct 2006 Posts: 17
|
Help! Code Compiles and Loads, but doesn't work! |
Posted: Tue Oct 03, 2006 8:10 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 8:22 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 8:28 pm |
|
|
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
|
Here's the new code |
Posted: Tue Oct 03, 2006 8:29 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 8:39 pm |
|
|
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
|
I feel like an idiot! |
Posted: Tue Oct 03, 2006 8:52 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 9:17 pm |
|
|
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
|
|
Posted: Wed Oct 04, 2006 7:37 am |
|
|
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
|
|
Posted: Mon Oct 09, 2006 12:35 pm |
|
|
THANK YOU ALL FOR THE HELP! |
|
|
|