go away spammer

Author Topic: atmel timer syntax  (Read 7062 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
atmel timer syntax
« on: February 15, 2007, 11:08:38 AM »
since im moving from PIC to AVR, im having issues with the syntax . . . below is the code i want to use on my atmega644, preferably with the timer.h library that comes with AVRlib . . .

can someone put in the correct syntax for me? im sure someone has some source they can quickly reference . . .

Code: [Select]
#include "timer.h"
//im not using a crystal on my atmega644

setup_timer //prescaling and stuff

while(1)
{
reset_timer //make timer go to zero

while(timer < 2ms) //i dont care for anything above 2ms so no need to track overflows
   { do_stuff(); }

do_other_stuff();
}

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: atmel timer syntax
« Reply #1 on: February 15, 2007, 01:28:36 PM »
I'm kinda confussed on what you want your timing to look like.

What you could do is attach your do_other_stuff() function to a timer and when that timer trips
(every 2ms or whatever you set it up to be) it will run with out fail.....

Does that help? void timerAttach(u08 interruptNum, void (*userFunc)(void) );

So  if you set up  timer0 to overflow every 2ms,  in your setup before
your main loop you would do this:
timerAttach(SIG_OVERFLOW0, do_other_stuff)

Code: [Select]
#include "timer.h"

void do_other_stuff(void);   //define this latter

void init()
{
    //setup the timer0 with appropriate bits
    timerAttach(SIG_OVERFLOW0, do_other_stuff);
    //other initialzation stuff
}

int main(void)
{
    init();
    while(1)
   {
        //just do_stuff() and the timer will automaticaly run do_other_stuff()
    }
}

void do_other_stuff(void)
{
    //the other stuff you want to do
}

You can also detach the function if you want to stop doing other things.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: atmel timer syntax
« Reply #2 on: February 15, 2007, 01:52:03 PM »
hmmmm ok perhaps I explained it poorly.

first, i want to set up the prescaler so that my timer does not overflow in less than 2ms.

second, i want to know what the timer value actually is. that way i can print out the exact time over serial for a datalogger, and do other things with that value.

this is the working PIC code I want converted to work on AVR:

Code: [Select]
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); //Set Timer1 prescaler to 8
set_timer1(0); //reset timer
while(get_timer1() < 2000) //~2ms has passed
{
timing=get_timer1();
do_stuff();
printf("Time passed: %d", get_timer()); //logs the time it took to do_stuff()
}

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: atmel timer syntax
« Reply #3 on: July 13, 2008, 11:09:41 PM »
ever figure this out?

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: atmel timer syntax
« Reply #4 on: July 14, 2008, 06:59:20 AM »
yeap

this post was back when i was developing the $50 Robot

(the solution is in the $50 Robot source code)

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: atmel timer syntax
« Reply #5 on: July 14, 2008, 11:23:10 AM »
All I see in the SoR_Utils header file is    

//timerInit(); // initialize the timer system


I don't see any commands for starting, stopping, reseting, etc...

So I started playing around with TCNT0, TCCR0, etc...

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: atmel timer syntax
« Reply #6 on: July 14, 2008, 11:30:14 AM »
oops sorry I wasn't thinking in my last post . . . :-X

This question was for a *very* early Axon prototype. Originally I was using the 644, and had no plans to make it a product.

The solution is in the current Axon code.

If you want to see the alpha Axon, its in the video here:
http://www.societyofrobots.com/sensors_accelerometer.shtml

 


Get Your Ad Here