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.

having problem with delay_ms and delay_us

Status
Not open for further replies.

yanami

Newbie level 4
Joined
Dec 5, 2011
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,328
Code:
void main()
{
int i;
ANSEL;                                    // Configure AN pins as digital I/O
ANSELH;
PORTD;
TRISD;
RD2;
while(1)
{
//Move Anti Clockwise direction
for(i=1;i<=500; i++)
{
PORTD=(1<<RD2); //output_high(PIN_D2);
delay_us(1000); //want servo to move to 0 degrees.
PORTD=(0<<RD2);  //output_low(PIN_D2);
delay_ms(20);

}
//Move Clockwise Direction
for(i=1;i<=500; i++)
{
PORTD=(1<<RD2); //output_high(PIN_D2);
delay_us(2000); //want servo to move to 180 degrees.
PORTD=(0<<RD2);  //output_low(PIN_D2);
delay_ms(20);

}
}
}
 

i dont know. i just found this code and kinda wishing i could use this with my study. im not even sure if this is useful. anyway, it says that
ANSEL, ANSELH, PORTD, TRISD, RD2 are undeclared so i tried putting int each like this
int ANSEL;
int ANSELH;
int PORTD;
int TRISD;
int RD2;

then it says that delay_ms and delay_us are undefined reference or whatever it is

---------- Post added at 15:40 ---------- Previous post was at 15:30 ----------

im using servo in proteus and i wanted to make it rotate clockwise and then counter using a switch so i started on this. i just found this code and kinda thinking if i could use this and find for another code for the switch.
 

Your code seems to be from a PIC microcontroller.
PORTD, TRISD etc are internal registers that should be declared in a header that contains the info for the specific mcu, these are not variables and there is no point to declare them as variables.
The delay is also a function defined in a library file so you have to include that too.
Which compiler have you used?

Alex
 
  • Like
Reactions: yanami

    yanami

    Points: 2
    Helpful Answer Positive Rating
i dont know. i just found this code and kinda wishing i could use this with my study....
This code is written for "MikroC for PIC" compiler from Mikroelectronika
ANSELH, PORTD, TRISD are registers in PIC microcontroller

then it says that delay_ms and delay_us are undefined reference or whatever it is
delay_ms() and delay_us() are built-in library functions of mikroC compiler.
 
  • Like
Reactions: yanami

    yanami

    Points: 2
    Helpful Answer Positive Rating
Ah yeah i do remember now. I tried it on code blocks since i don't have MikroC and that i couldn't find free downloads for that.
 

Those functions are part of the Microchip C30 compiler and presumably others. To use them you need to include a file delay.h which simply has in it:

Code:
#ifndef __DELAY_H

	#define FOSC		80000000LL		// clock-frequecy in Hz with suffix LL (64-bit-long), eg. 32000000LL for 32MHz
	#define FCY      	(FOSC/2)		// MCU is running at FCY MIPS

	#define delay_us(x)	__delay32(((x*FCY)/1000000L))	// delays x us
	#define delay_ms(x)	__delay32(((x*FCY)/1000L))		// delays x ms

	#define __DELAY_H	1
	#include <libpic30.h>

#endif

I think in reality it is simply the two lines #define delay... that you need where the __delay32(... part defines the clock division necessary

Keith.
 
  • Like
Reactions: yanami

    yanami

    Points: 2
    Helpful Answer Positive Rating
whats with the clock division? i have this post.problem similar to this. about bluetooth and servo.
I wanna use bluetooth module and servo on proteus and then rotate the servo clockwise/counterclockwise controlled by a switch in proteus. The switch would only represent the position of the servo, high or low, depends. is this possible? Any source code for this? I don't know what kind of Bluetooth to use as well as the pic i guess i still have to find something that is cheaper and can be found here in Philippines.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top