NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » compiling a .c file

February 27, 2011
by dave789
dave789's Avatar

i need to write a program with both a millisecond delay and a microsecond delay. i figured out with a changed value in the provided millisecond delay function of the libnerdkits i could get the desired microsecond behavior. i hoped i could then just save this .c file with a new name ( udelay )and it would create a corresponding udelay.o ( object ) file. i read in another thread that the normal compile of a program automatically runs the make file in the libnerdkits. when i do this i get, however, no created udelay.o file created. can anyone help?

February 27, 2011
by Rick_S
Rick_S's Avatar

Or you could just use delay_us(); instead of delay_ms();

Rick

February 27, 2011
by dave789
dave789's Avatar

sure. well, it looks to me that the ms delay is built on the us delay function anyway, but that does not solve my problem. i need this to put this seperate function in the libnerdkits folder. this is what i do not know how to do.

February 28, 2011
by Rick_S
Rick_S's Avatar

You are correct, delay_ms is built on delay_us. But you lost me as to what exactly you are trying to achieve. If you want to use delay_us or delay_ms, all you have to do is include ../libnerdkits/delay.h in your programs and both would be accessable.

If you are having problems compiling a library from a copy you modified, that would require a change in the makefile.

For instatnce. Say you make a change inside one of the funtions in the delay library (without adding or changing names of functions) then save it to a file called mydelay.c You would also need to copy delay.h to mydelay.h. Then in the makefile, change the reference of ../libnerdkits/delay.o in the LINKOBJECTS to ../libnerdkits/mydelay.o

Then your file will compile with your program. Keep in mind, you will also need to change the include in your program for your custom delay file.

Rick

March 03, 2011
by dave789
dave789's Avatar

just to explain the reason for two delays. i need a 30 micro second hi output. with the provided millisecond delay, i was unable to achieve such a short time. after changing a variable in the millisecond delay's .c file, i was able to get my 30 microseconds ( i am checking with an oscilliscope). between the high outputs i need it to stay low in the millisecond's range. the altered .c file did not cooperate, however. it appears less than 1 millisecond in the millisecond delay is a problem and more than 1000 microseconds in the microsecond delay is a problem.
i read something on .h files and thought it would not be necessary for what i was trying to do. i will digest your info. and let you know the my results... good or bad. thanks -dave

March 03, 2011
by bretm
bretm's Avatar

A little while back I had need for an improvement over delay_ms and delay_us and I put together this web page to create accurate timing loops. To use it in C, surround the results with "asm volatile". For example, to generate a 30us loop, go to the web page, enter 14.7456 as the CPU speed (for NerdKits crystal), 30 as the delay, press the "us" button, and it generates this:

; Delay 442 cycles
; 29us 975 25/576 ns
; at 14.7456 MHz

    ldi  r18, 147
L1: dec  r18
    brne L1
    nop

To turn this into C code, make some minor tweaks to get it to work with the "asm" statement syntax:

asm volatile (
  "   ldi  r18, 147"  "\n"
  "1: dec  r18"       "\n"
  "   brne 1b"        "\n"
  "   nop"            "\n"
);

I'm planning on having the web page do that automatically at some point.

It handles long delays quite easily. Here's a one-second delay for example:

asm volatile(
"    ldi  r18, 75 "  "\n"
"    ldi  r19, 206"  "\n"
"    ldi  r20, 238"  "\n"
"1:  dec  r20     "  "\n"
"    brne 1b      "  "\n"
"    dec  r19     "  "\n"
"    brne 1b      "  "\n"
"    dec  r18     "  "\n"
"    brne 1b      "  "\n"
"    rjmp 2f      "  "\n"
"2:               "  "\n"
);
April 06, 2011
by dave789
dave789's Avatar

hey bretm,

i have been away from my nerdkit for a while, but just plugged in your delay code. works! awesome. thanks man.

April 06, 2011
by Noter
Noter's Avatar

I have found the delay functions that are included with avr/gcc are quite accurate and as long as you define F_CPU to reflect your clock frequency, they self-adjust. I wonder why the nerdkit folks wrote their own in libnerdkits?

They're in the util directory ...

#include <util/delay.h>

functions are _delay_us(?) and _delay_ms(?).

Post a Reply

Please log in to post a reply.

Did you know that our USB NerdKit works on Windows, Linux, and Mac OS X? Learn more...