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

delay_ms() function

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



Joined: 02 Mar 2006
Posts: 11

View user's profile Send private message

delay_ms() function
PostPosted: Thu Apr 06, 2006 3:02 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 06, 2006 3:37 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 06, 2006 3:43 am     Reply with quote

Thanks jfk1965
I've changed it and still not working...
Ttelmah
Guest







PostPosted: Thu Apr 06, 2006 3:53 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 06, 2006 4:14 am     Reply with quote

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 Sad
jfk1965



Joined: 21 Oct 2003
Posts: 58

View user's profile Send private message

PostPosted: Thu Apr 06, 2006 4:24 am     Reply with quote

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







PostPosted: Thu Apr 06, 2006 4:43 am     Reply with quote

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) ;

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