|
|
View previous topic :: View next topic |
Author |
Message |
jvalencia
Joined: 18 Mar 2005 Posts: 5
|
PCWH 3.221 and delay problem |
Posted: Fri Mar 18, 2005 11:38 am |
|
|
I'm having some issues with delay_us function. I have this simple code:
Code: | #include <16F819.h>
#device adc=8
#use delay(clock=8000000)
#fuses NOWDT,INTRC_IO, PUT, NOMCLR, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG, NOPROTECT
void main()
{
while(TRUE)
{
output_b(0xff);
delay_us(2000);
output_b(0x00);
delay_us(18000);
}
}
|
The output is a led being lighted one time each 2-3 seconds, that it's not the desired output. |
|
|
Guest
|
|
Posted: Fri Mar 18, 2005 12:09 pm |
|
|
I've verified that it's not the delay function, as i created the "mydelay" function and get the same fault output.
I don't know what's happening. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 18, 2005 12:16 pm |
|
|
If you look in the 16F819 data sheet, you'll see that this chip has got
multiple frequencies for the internal oscillator. So that should be a
clue right away, that maybe one of the lower frequencies is selected.
The data sheet shows that the frequency is selected by the OSCCON
register. It also shows that the power-reset state of that register is all
0's, which selects 31.25 KHz.
There are a few ways to set OSCCON for 8 MHz operation.
If you put the #use delay() statement below the #fuses INTRC_IO
statement, then the compiler will insert code to configure OSCCON
for 8 MHz operation into the start-up code.
Code: |
#include <16F819.h>
#device adc=8
#fuses NOWDT,INTRC_IO, PUT, NOMCLR // (etc.)
#use delay(clock=8000000) // Put this line below the #fuses statement |
But that's probably a little hard to remember, so a better way would
be to use the setup_oscillator() function right at the start of main().
Code: | #include <16F819.h>
#device adc=8
#use delay(clock=8000000)
#fuses NOWDT,INTRC_IO, PUT, NOMCLR // (etc.)
void main()
{
setup_oscillator(OSC_8MHZ); // Add this line.
while(TRUE)
{
output_b(0xff);
delay_us(2000);
output_b(0x00);
delay_us(18000);
}
} |
setup_oscillator() works OK in your version. In some earlier versions
it may not work correctly, so then you would set OSCCON manually,
after defining the register address with a #byte statement. Example:
#include <16F819.h>
#device adc=8
#use delay(clock=8000000)
#fuses NOWDT,INTRC_IO, PUT, NOMCLR // (etc.)
#byte OSCCON = 0x8F
void main()
{
OSCCON = 0x70; // Select 8 MHz internal oscillator
while(TRUE)
{
output_b(0xff);
delay_us(2000);
output_b(0x00);
delay_us(18000);
}
} |
|
|
jvalencia
Joined: 18 Mar 2005 Posts: 5
|
|
Posted: Fri Mar 18, 2005 12:39 pm |
|
|
Yeah it worked!
Thanks a lot. |
|
|
|
|
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
|