Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

__delay_ms() is not working why ? using PIC16f877a and hitech compiler

Status
Not open for further replies.

erfan.khalilian69

Member level 4
Joined
Feb 7, 2011
Messages
71
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Kualalampur
Activity points
1,916
Hi everyone Im so confuse why the simple syntax gives me error . while I use __delay_ms(); it gives me error and i dun know the reason, is anyone can help me plz ?
here is my program :(once i deleted the __delay_ms() then it builds successful)

#include <pic.h>

#include <htc.h>
__CONFIG ( 0x3F32 ); //configuration for the microcontroller

// define
//==========================================================================

#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 20000000
#endif
#define BUTTON RB0 //bit 1 of PORTC
#define BUTTON1 RB1
#define BUTTON2 RB2
main(void)
{


TRISB = 0b00000111; /* all bits output */
//int x;

while(1)
{
if (BUTTON != 0)
{
PORTB = 0x80;


__delay_ms(1); // delay for 2 milliseconds
CLRWDT();

PORTB = 0x00;


__delay_ms(19); // delay for 2 milliseconds
CLRWDT();


}
if (BUTTON1 != 0)
{
PORTB = 0x40;


__delay_ms(1.5); // delay for 2 milliseconds
CLRWDT();

PORTB = 0x00;

__delay_ms(18.5); // delay for 2 milliseconds
CLRWDT();

}

if (BUTTON2 != 0)
{
PORTB = 0x20;


__delay_ms(2); // delay for 2 milliseconds
CLRWDT();

PORTB = 0x00;

__delay_ms(18); // delay for 2 milliseconds
CLRWDT();

} //else PORTB = 0x00;

}
}
 

Can you upload the error listing? Some advice I can give is to define _XTAL_FREQ in a config.h header file and include the header before htc.h.

Example:

config.h

#ifndef _CONFIG_H_
#define _CONFIG_H_


/* HTC.H Required Defines */

#define _XTAL_FREQ 4000000 //4MHz OSC



#endif

example.c

#include "config.h"
#include <htc.h>

void main (void)
{
PORTB = 0x00;
TRISB = 0x00;

while(1)
{

PORTB = 0x00;
__delay_ms(1000); // delay for 1 second

PORTB = 0xFF;
__delay_ms(1000); // delay for 1 second
}
}

There should also be no need to include picc.h, this is done by htc.h from version 7 on of the HiTech compiler.

Also in the future it's advantageous to just disable the watchdog timer, at least until final stages of coding.

Try these suggestions and report back your findings.

---------- Post added at 06:57 ---------- Previous post was at 06:26 ----------

I just notice a possible problem.

__delay_ms(1.5); // delay for 2 milliseconds
CLRWDT();

PORTB = 0x00;

__delay_ms(18.5); // delay for 2 milliseconds
CLRWDT();

You're passing a type float, 1.5 and 18.5, into a define macro parameter which is defined as unsigned long.

If you need finer granularity use __delay_us(1500) or __delay_us(1850) instead of __delay_ms(1.5) and __delay_ms(18.5).
 
Last edited:
thank you for your help, actually my main program is different form this program, anyway the problem is same.
the error is :
HI-TECH PICC-Lite COMPILER (Microchip PICmicro) V9.60
Copyright (C) 1984-2006 HI-TECH SOFTWARE
Error [499] ; . undefined symbol:
___delay_us (PR11.obj)

there are two warning also :
Warning [356] E:\study\level 4\MechatronicDesign\MainProgram\Source Code\PR11.c; 136.28 implicit conversion of float to integer
Warning [361] E:\study\level 4\MechatronicDesign\MainProgram\Source Code\PR11.c; 399.1 function declared implicit int

thanks for your advice , you mean Do I need to add file header in order to use this syntax for delay ????


the program is long but the parts which I need delay is in below :

#include<pic.h>
#include <htc.h>


#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 20000000
#endif
__CONFIG (0x3F32);
//#define _XTAL_FREQ 20000000
#define servo RC4// servo motor
void servomotor (void);




void servomotor (void){
while (t<20){
t++;

servo = 1;
for(e=1;e<=50; e++){
delayy(1500); // I wan to use this sysntax " __delay_us(1500);"

delayy(500);// __delay_us(500)

}
servo = 0;
delayy(2000); // __delay_ms(2);
//delay(500); // __delay_us(500)

break;
}
}

---------- Post added at 10:47 ---------- Previous post was at 10:44 ----------

this program must be able to rotate servo motor clockwise and after some delay then rotate in CCW (initial position), if you have any advice it will be great .
thanks
 

You're fine using __delay_ms(2), however this is not fine __delay_ms(1.5). Due to the fact that 1.5 is a type float and the define macro parameter and the actual function parameter passed by define macro requires a type unsigned long.

So instead of using __delay_ms(1.5) use __delay_us(1500) instead. You can use only integers, no floats, in the delay routines.

Passing a float to a unsigned long parameter is causing the following two compile time warnings:

Warning [356] E:\study\level 4\MechatronicDesign\MainProgram\Source Code\PR11.c; 136.28 implicit conversion of float to integer
Warning [361] E:\study\level 4\MechatronicDesign\MainProgram\Source Code\PR11.c; 399.1 function declared implicit int

Instead of the following code:

#include<pic.h>
#include <htc.h>


#ifndef _XTAL_FREQ
// Unless already defined assume 4MHz system frequency
// This definition is required to calibrate __delay_us() and __delay_ms()
#define _XTAL_FREQ 20000000
#endif

Use the following instead:

#include "config.h"
#include <htc.h>

Create a config.h header file in your project directory and ensure to include it before the htc.h.

So your using a 20MHz OSC in this project, is that correct?

Try these suggestions and let me know the resulting error listings.

---------- Post added at 07:33 ---------- Previous post was at 07:29 ----------

this program must be able to rotate servo motor clockwise and after some delay then rotate in CCW (initial position), if you have any advice it will be great .
thanks

Lets get the compile time errors and warnings taken care of first. At that point we'll work on the servo details.

By the way, what is delayy()? Is it your delay routine?
 

thanks , you mean I must write this delay in note file and then add this file to my program, like go source file and add the note file ? right?

yes the ocsillator is 20Mhz
and the delayy is the delayy which I wrote because still __delay_us() give me error.
void delayy(unsigned short i)
{
for(;i>0;i--);
}

but it is not precise , I need precise delay for servo motor as you know.
i'll try ur advice and tell you just a minute

---------- Post added at 11:18 ---------- Previous post was at 11:14 ----------

by the way how can I build the header ? does it need to be done in specific manner ?
can you tell me the steps?
thanks a lot
 

Ok, in order to clarify things I've uploaded the sample project so you can use it as a template. The project has been precompiled and ran so we know it compiles and functions correctly.

By the way, I'm using 9.80 Hi-Tech, but I don't think you'll have any problems opening it in your version.
 

Attachments

  • TestLed.zip
    32.1 KB · Views: 122
What PIC model are you using?

---------- Post added at 08:00 ---------- Previous post was at 07:58 ----------

Never mind, PIC16f877a I didn't see it in the title. My project was compiled for the PIC16f877a as well.
 

What PIC model are you using?

---------- Post added at 08:00 ---------- Previous post was at 07:58 ----------

Never mind, PIC16f877a I didn't see it in the title. My project was compiled for the PIC16f877a as well.

I really appreciate your help thank you so much
yeap my pic is pic16f877a
so once you upload the template I just need to add in header file ? right?
hope it will be working, thank you so much

---------- Post added at 12:07 ---------- Previous post was at 11:58 ----------

oh sorry i didn't see you upload it ? so should I add this templelet to header file ?
 

I've already upload the template. You don't need to add anything at the moment. I just want you to compile it again on your system, to make sure everything links correctly. Then you can modify it to might your needs.

What type of servos are you trying to drive? Do you have the datasheets for them? A schematic of the circuit you're designing?
 
I tried but it didn't work :( I add the config file to header and then run the program but it didn't work

here is the code :

#include "config.h"
#include <htc.h>

....main program...


the error is : Error [141] E:\study\level 4\MechatronicDesign\MainProgram\Source Code\PR11.c; 9.19 can't open include file "config.h": No such file or directory
 

Unzip a fresh copy and open the project file in MPLAB, then compile. Don't change or add a thing.
 

I dun know whats happened I did but it doesn't compile even :d

the servo is RC servo motor yeap I have the data sheet, the schematic is important ? because I just connect according to data sheet , one pin is vcc one is gnd and one must connected to the pic .
https://www.cytron.com.my/datasheet/RC Servo/RC_Servo_User's_Manual.pdf
here is data sheet

the program even it does not compile itself i dun know why maybe because of different version, how ever I just add the config to header file and then compile my program but still gives me error, I really confuse it is just simple syntax, actually now I believed that AVR is much better than PIC :D
 

Why don't you zip up your project directory and upload it. You'll have to use the "Go Advanced" button and use the paperclip icon to open the upload window.

I wouldn't get to frustated with the PIC, it seems to be more compiler related.

I'll clean it up over here an upload it back to you. I'll also read the datasheet on the Servos and give you some advice and examples.

---------- Post added at 09:08 ---------- Previous post was at 09:07 ----------

You'll need to use PWM/PCM for this servo. I have some designs and sample code which I'll upload as soon as I dig it up.
 
man I really appreciate your help your are really helpful , now I will upload my program then you can see whole things. thank you so much

---------- Post added at 12:46 ---------- Previous post was at 12:41 ----------

I want to upload it but it gives me error that it is invalid file , can I send it to your email ?
 

I got it! I fix the file not found error and now I'm working on float to integer conversions. You need to copy the config.h into the source directory you have and then tell the compiler where to find it.

Do you want to round temperature up to the nearest whole number? In other words 78.567 degrees becomes 79 degrees?
 

I got it! I fix the file not found error and now I'm working on float to integer conversions. You need to copy the config.h into the source directory you have and then tell the compiler where to find it.

Do you want to round temperature up to the nearest whole number? In other words 78.567 degrees becomes 79 degrees?

thank you ,, no actually temp and light working well , just servo part I got problem to fix it, actually I want to rotate servo motor 90 degree and then stop the motor for couple of seconds for example 5 seconds and then rotate the servo motor 90 degree CCW (back to the initial position) in order to open and close some door (just to show my lecture).
if it is working then my project is finish :D
thanks for your help
 

Beware, I made several assumptions, because I do not know all the project requirements. I round up the temperature to the nearest whole number and then passed the value to dismum(). Microchip has PWM features which makes managing servos relatively pain free. You should think about converting the float to ascii and then displaying it.

Attached is the new version:
 

Attachments

  • MainProgramV2.zip
    189.2 KB · Views: 106

I did not touch the servo routines. I just got the project to compile without any warnings or errors. I need to take a longer look at your code and ask you a few more questions before tampering with the servo routines. It about 3AM here, so I'm going to call it quits for the night shortly. Try running the hex file first, then unzip the project and see if you can recompile it without linkage problems.

---------- Post added at 10:07 ---------- Previous post was at 10:01 ----------

Do you have a schematic of the circuit under development? If so please upload a copy.

Also I forgot to change the value in config.h to 20000000.

---------- Post added at 10:25 ---------- Previous post was at 10:07 ----------

I work on the project some more, first thing in the morning. You'll probably be a sleep by then, so PM me when you're online.
 
Last edited:

thanks im waiting for ur reply then

---------- Post added at 15:02 ---------- Previous post was at 15:00 ----------

yes I already changed the 4000000 to 20000000 , the problem is from other side not this part ., thanks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top