View previous topic :: View next topic |
Author |
Message |
SuMa
Joined: 02 Mar 2006 Posts: 11
|
delay_ms() function |
Posted: Thu Apr 06, 2006 3:02 am |
|
|
Hi, everybody.
I am programming a PIC16f891 and I have a little problem with the command delay_ms() beacause I cannot make it works and I don't know why...
This is a little program to check it:
Code: |
#include <16F819.h>
#device adc=10
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#fuses INTRC, NOBROWNOUT,NOWDT
#use delay(clock=4000000)
void main() {
while(true){
output_high(PIN_A0);
delay_ms(3); // tempa variable 0-255 or a constant 0-65535
output_low(PIN_A0);
delay_ms(3);
}
|
My problem is this:
When I use delay_ms() and I check it on the A0 pin the delay measured is completely different from my programming intentions.
This are my measurements:
with delay_ms(0) I check a delay of 0,5 ms
with delay_ms(1) I get-> 125ms
with delay_ms(2) I get -> 250ms
with delay_ms(3) I get ->380ms
All this have non sense, and I am trying to get the solution but I don't know what to do.
The help tells:
delay_ms (time)
time - a variable 0-255 or a constant 0-65535
This function will create code to perform a delay of the specified length. Time is specified in milliseconds.
Could you help me? Maybe my problem is on the configuration but I don't know.
Thanks a lot |
|
|
jfk1965
Joined: 21 Oct 2003 Posts: 58
|
|
Posted: Thu Apr 06, 2006 3:37 am |
|
|
Not sure if Port A is set as I/O as default or as analog inputs.
To be sure the PIC isn't set as Analog add the following line into your main function.
Code: | setup_adc_ports(NO_ANALOG); |
JFK |
|
|
SuMa
Joined: 02 Mar 2006 Posts: 11
|
|
Posted: Thu Apr 06, 2006 3:43 am |
|
|
Thanks jfk1965
I've changed it and still not working... |
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 06, 2006 3:53 am |
|
|
The problem is your oscillator. You don't say what compiler version you have, but on some, you _must_ add the 'setup_oscillator(OSC_4MHz)' line, to actually program the oscillator to 4MHz. Most of the latter compilers will do this automatically, but it is safer not to rely on this. Also, move the 'use delay' statement, in front of the includes for stdlib, stdio, and stddef (putting it latter, can prevent the automatic behaviour working...).
What is happening, is that the code is being told that the chip is running at 4MHz, but the processor is actually running at 32KHz (the default wake-up speed). Hence the timings are in error by 4000000/32768 = 122.07:1
Best wishes |
|
|
SuMa
Joined: 02 Mar 2006 Posts: 11
|
|
Posted: Thu Apr 06, 2006 4:14 am |
|
|
Thanks Ttelmah
my version is IDE 3.28, PCB,PCM,PCH 3.150
And this is my new heading as you suggest
Code: |
#include <16F819.h>
#device adc=10
#use delay(clock=4000000)
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#fuses INTRC, NOBROWNOUT,NOWDT //INTRC_IO
|
The setup_oscillator(OSC_4MHz) command doesn't work in my version so I cannot use it.
But still not working |
|
|
jfk1965
Joined: 21 Oct 2003 Posts: 58
|
|
Posted: Thu Apr 06, 2006 4:24 am |
|
|
Is the Pic actually Oscillating at 4Mhz
Try using delay_us instead of delay_ms and see if that works.
I had it before when us worked but ms didn't i'll try and find out what the solution was.
JFK |
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 06, 2006 4:43 am |
|
|
If you are on 3.150, then you definately 'predate' the automatic oscillator setup. You need to generate your own 'setup_oscilator', or the unit will only run at the default 32KHz.
The solution is to write the oscillator configuration yourself.
Seriously, the compiler you have, is very early for support for this chip, and you are probably going to find other problems, so it might well be worth considering upgrading.
The 'DIY' solution, is:
Code: |
#byte OSCCON = 0x8F
#bit ICSF = OSCCON.2
//Then at the start of yuour main
OSCCON=0x60;
while(ICSF==0) ;
|
|
|
|
|