Setting up Timers

From $1

  • You do not have permissions to view this page - please try logging in.
Table of contents
No headers

The first step in getting the AVR-MT-128 to do something useful Vespa-wize is to get a timer happening. Everything we want to do (for now) is time based and first on the agenda is measuring speed from a magnetic pick-up. So we want a timer that gives us seconds to start with.

A combination of reading the manual and the Newbie's Guide to AVR Timers gives me the following successful code. We are using CTC to enable us to check for the timeout via a timeout register (TIFR). We "prescale" down the timer 1 from 16Mhz to 16Mhz/1024 and set the compare value (OCR1A) to 1 second (15625 which is how many ticks per second in 16Mhz/1024)...

int main(void) {    

 //Ports
 InitPorts();

 TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
 TCCR1B |= ((1 << CS22) | (1 << CS20)); // Start timer at Fcpu/1024 
 OCR1A   = 15625; // Set CTC compare value to 1Hz at 16MHz AVR clock, with a prescaler of 1024
...

 if (TIFR & (1 << OCF1A)) 
 { 
 // do some calc on second tick   
 TIFR = (1 << OCF1A); // clear the CTC flag (writing a logic one to the set flag clears it) 
 }

 

Tags: (Edit tags)
  • No tags
 
Comments (0)
You must login to post a comment.

 
Powered by MindTouch Core

Disclaimer: Vespa Labs contains information that is VERY likely to wreck your scooter and possibly yourself both intentionally (i.e. gaining more peformance while sacrificing reliability + safety) and unintentionally (i.e. misleading or incorrect information). Vespa Labs is only a wiki and intended as a dumping ground for information and not as a properly reviewed source. The same disclaimers that wikipedia.org use apply to Vespa Labs. The short version is use information at your own risk, both information on the main wiki and in the user areas are intended to be used only as "thought provoking" for someone that knows what they are doing. If you try to implement a "thought" Vespa Labs is not reponsible and if something goes wrong we hope that you live and update the offending information with corrections to warn others.

Vespa Labs is an international site and therefore may contain information that is not road legal in some countries/states and may also invalidate insurance policies. Treat all information as experimental and for "race use only" (i.e. not for road use - even if it is implied or explicitly stated). Refer to and adhere to your local road and legal rules, as well as the manufacturers recommendations.