### Author Topic: Check my thoughts/math here - MCU frequency cycle times  (Read 5576 times)

0 Members and 1 Guest are viewing this topic.

#### HDL_CinC_Dragon

• Supreme Robot
• Posts: 1,261
##### Check my thoughts/math here - MCU frequency cycle times
« on: March 15, 2008, 04:54:31 PM »
16MHz = 16,000,000Hz(cycles per second)
16MHz = 16,000 cycles per millisecond
so in theory, to have my MCU delay by 1ms, I would need to have it wait 16,000 cycles right?
so "while(del > 0) { del--; }" where del equals 16,000, should output a frequency of 1000Hz or 1Khz give or take like 20Hz right?
So why does it output a frequency closer to about 4Hz or 5Hz. What am I not accounting for?

Is it that it needs those many many other clock cycles to interpret and execute the commands?

Code: [Select]
`#include <avr/io.h>int del = 0; //create integer "del"int val = 16000; //create integer "val"int main(void) { DDRB = 0xFF; //Set B ports output DDRC = 0x00; //Set all C ports input DDRD = 0xFF; //Set all D ports output PORTB = 0b00000000; //Set all B ports low PORTC = 0x00; //Set all C ports low PORTD = 0b00010000; //Set D4 high while(1) { del = val; //Set "del" equal to "val" PORTB = 0b00000001; //Set B0 high PORTD = 0b00000000; //Set all D ports low while(del > 0) { del--; // These two waits should create the frequency } del = val; PORTB = 0b00000000; //Set all B ports low PORTD = 0b00010000; //Set D4 high while(del > 0) { del--; // These two waits should create the frequency } } }`
« Last Edit: March 15, 2008, 07:27:30 PM by HDL_CinC_Dragon »
United States Marine Corps
Infantry
Returns to society: 2014JAN11

#### dunk

• Expert Roboticist
• Supreme Robot
• Posts: 1,086
##### Re: Check my thoughts/math here
« Reply #1 on: March 15, 2008, 06:56:00 PM »
since your program is written in C, which is an interpreted language you would have to compile your program to convert it to machine code.
the AVRs datasheet will tell you how many clock cycles each machine code instruction takes.

dunk.

#### HDL_CinC_Dragon

• Supreme Robot
• Posts: 1,261
##### Re: Check my thoughts/math here
« Reply #2 on: March 15, 2008, 07:08:51 PM »
yea but doesnt AVR translate it into low-level language before sending it the the MCU? It converts it into Hexidecimal right?
United States Marine Corps
Infantry
Returns to society: 2014JAN11

#### dunk

• Expert Roboticist
• Supreme Robot
• Posts: 1,086
##### Re: Check my thoughts/math here
« Reply #3 on: March 15, 2008, 07:26:05 PM »
Quote
yea but doesnt AVR translate it into low-level language before sending it the the MCU? It converts it into Hexidecimal right?
exactly.
if you were to look each one of those Hex instructions up in the datasheet you would see how long each one takes.

"while(del > 0) { del--; }" looks like one command in C but it translates to many individual instructions when compiled.

dunk.

#### HDL_CinC_Dragon

• Supreme Robot
• Posts: 1,261
##### Re: Check my thoughts/math here
« Reply #4 on: March 15, 2008, 07:27:15 PM »
ah yeah thats a good point... Ill check the data sheet out and see how many cycles each instruction uses. Thanks dunk
United States Marine Corps
Infantry
Returns to society: 2014JAN11

#### HDL_CinC_Dragon

• Supreme Robot
• Posts: 1,261
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #5 on: March 17, 2008, 01:06:15 PM »
I looked in the data sheet for my MCU but I didnt see anything in the sheet saying how long it takes what hex to execute how long... any ideas?
United States Marine Corps
Infantry
Returns to society: 2014JAN11

#### superchiku

• Supreme Robot
• Posts: 953
• cooll
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #6 on: March 17, 2008, 01:17:13 PM »
well u are using the delay cycles technic and thats the problem the servo speed can be controlled that way but if u are giving out frequencies then i suggest try using the mcu's timer or use IC555 timer ckt
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

#### HDL_CinC_Dragon

• Supreme Robot
• Posts: 1,261
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #7 on: March 17, 2008, 01:20:02 PM »
Its not controlling a servo, its controlling a piezo disc...
is there a tutorial on the SoR for using the timer?
United States Marine Corps
Infantry
Returns to society: 2014JAN11

#### superchiku

• Supreme Robot
• Posts: 953
• cooll
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #8 on: March 17, 2008, 01:25:16 PM »
it is not controlling a servo and thats the problem that kinda code will work on a servo but not on a piezo crystal which i guess u must be using for ultrasonic transducer ,i told ya try using either IC 555 or the mcu's inbuilt timer to produce signal of the required frequency
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

#### HDL_CinC_Dragon

• Supreme Robot
• Posts: 1,261
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #9 on: March 17, 2008, 01:36:43 PM »
Nope, not even an ultrasonic transducer. But a 555 timer has a constant frequency unless I put a pot on it controlled by a servo... which is just a waste of electronics and space for something like that. Im wondering if you could point me to a tutorial on using the timer or even a small tidbit of source code I can look at?... would there be one in the data sheet? Ill check. Thanks for your help thus far.
United States Marine Corps
Infantry
Returns to society: 2014JAN11

#### superchiku

• Supreme Robot
• Posts: 953
• cooll
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #10 on: March 17, 2008, 01:46:05 PM »
u see using IC 555 is most reliable  but if u are using mcu's timer then plzz specify wat kinda mcu ur using
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

#### HDL_CinC_Dragon

• Supreme Robot
• Posts: 1,261
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #11 on: March 17, 2008, 01:57:05 PM »
ah crap sorry, Im using the ATMega8 16Mhz clock speed.

And yeah I know the 555 would be most reliable but I doubt id be able to have much fine control over it without having a bulky system to control a potentiometer or something... unless theres something Im not thinking of at the moment...
United States Marine Corps
Infantry
Returns to society: 2014JAN11

#### superchiku

• Supreme Robot
• Posts: 953
• cooll
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #12 on: March 17, 2008, 02:00:35 PM »
can i know how many timers are there in the atmega 8 ? then  will tell u wat to do
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

#### HDL_CinC_Dragon

• Supreme Robot
• Posts: 1,261
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #13 on: March 17, 2008, 02:07:49 PM »
"Two 8-bit Timer/Counters with Separate Prescaler, one Compare Mode"
"One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode"
"Real Time Counter with Separate Oscillator"

Thats whats in the Datasheet for it
United States Marine Corps
Infantry
Returns to society: 2014JAN11

#### superchiku

• Supreme Robot
• Posts: 953
• cooll
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #14 on: March 17, 2008, 02:20:06 PM »
ok u said u are using 16mhz clock is it internal or external crystal???

have u changed the fuse bits???

if yes then the algorithm can be like this

set the tccr1b register in ur mcu to a prescaler of 8 then if u need to give a delay of i ms then check ur tcnt1 register value for 239 and switch ur make ur output switch on and off when u get a value of 239 that should nearly give u a delay of 1ms==1000hz for  the output
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

#### hazzer123

• Supreme Robot
• Posts: 460
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #15 on: March 17, 2008, 03:42:03 PM »
If you can check your compilers manual, it will tell you how to place assembly into your c code. It will require tags before and after the code. This gives you lots of control of the timing of commands.

for example in Microchip C18 compiler to do inline assembly you type :

Code: [Select]
`_asmASSEMBLY GOES HERE_endasm`
Thats another route you can choose if timers are a bit advanced for you at the moment.

Also, there will definately be some user friendly delay functions that you could use.
Try finding a header file called "delays.h" or something and check its instructions, ill bet there's a function called delay_ms(); or something similar. That would solve your problem easily.
Imperial College Robotics Society
www.icrobotics.co.uk

#### HDL_CinC_Dragon

• Supreme Robot
• Posts: 1,261
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #16 on: March 17, 2008, 08:37:41 PM »
Thanks for the replies guys. Based on what you said, Chiku, I think timers might be a little out of my league right now
Hazzer, what you suggested is probably more for me at this point in my roboticist "career". Thanks for the help guys.
United States Marine Corps
Infantry
Returns to society: 2014JAN11

#### superchiku

• Supreme Robot
• Posts: 953
• cooll
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #17 on: March 17, 2008, 10:13:25 PM »
delay ms will give delay in mili seconds i have never used it but i guess maz delay u can give is  32 miliseconds,i dont remember actually

well certainly u can use that but since i have never tested it i dont know if it will work or not
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

• Supreme Robot
• Posts: 11,680
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #18 on: March 23, 2008, 03:09:46 PM »
My timer tutorial was written for PIC's . . .

So this is what I did to get my timer working on my Axon, but I think you can get it to work without me explaining the details:

Add this line with the other initializations (probably in main of photovore.c):
timer0Init(); // initialize the timer system

In sor_utils.h, make sure timer is uncommented:
#include "timerx8.h"   // include timer function library (timing, PWM, etc)

In the makefile, make sure you have the timer file listed (something like this):
# List C source files here. (C dependencies are automatically generated.)
SRC = a2d.c buffer.c uart4.c timerx8.c rprintf.c \$(TARGET).c

Now to reset the timer, add this function at the bottom of sor_utils:
Code: [Select]
`//**************RESET TIMER**************void reset_timer(void) { //restart timer count TCNT0=0x00;//clear timer timer0ClearOverflowCount();//clear timer0's overflow counter. }//***************************************`So to reset in main, just call this function:
reset_timer();

To get the elapsed time, do this:

For a list of more functions, read timer.h:
http://hubbard.engr.scu.edu/embedded/avr/avrlib/docs/html/timerx8_8h.html

Oh and if you get it to work, feel free to write a tutorial on it

#### superchiku

• Supreme Robot
• Posts: 953
• cooll
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #19 on: March 25, 2008, 04:29:13 AM »
ill use that and see if it works
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

#### Soeren

• Supreme Robot
• Posts: 4,672
##### Re: Check my thoughts/math here
« Reply #20 on: March 25, 2008, 09:35:35 AM »
Hi,

since your program is written in C, which is an interpreted language
C is NOT an interpreted language, but rather a compiled language (i.e. "translated" to MC).

An interpreted language is interpreted (usually one line at a time) at run time (eg. some old variants of BASIC, JavaScript and AFAIK the BASIC stamp although it may be tokenized, which is still run time interpreted, but saves memory space, like some other old BASIC variants).
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Engineering is based on numbers - not adjectives

#### bukowski

• Robot Overlord
• Posts: 219
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #21 on: March 25, 2008, 10:25:01 AM »
Dragon,
There are a bunch of ways to electrically vary the freq of the 555. Mebbe a transistor between pins 6 & 7 on the timer with the gate controlled by an analog output on your mega8?
I love the way everyone is so secretive about their projects.

#### AndrewM

• Robot Overlord
• Posts: 255
##### Re: Check my thoughts/math here - MCU frequency cycle times
« Reply #22 on: March 25, 2008, 09:34:57 PM »
Just incase you decide to take a look at it to determine clock cycles for each command, here's the link to the AVR instruction set:  http://www.atmel.com/dyn/resources/prod_documents/doc0856.pdf

At the bottom of each instruction command is a listing for the number of cycles used.
blog: www.iamwhen.com
Saving the world from humanity one robot at a time.

#### benji

• Supreme Robot
• Posts: 832