tdelay.c File Reference

An attempt to make a usable delay routine. More...

#include <inttypes.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>

Include dependency graph for tdelay.c:


Functions

void tdelay (uint16_t ms)

Detailed Description

An attempt to make a usable delay routine.

Regular util/delay.h is weird, it's defined as __always_inline__ and it seems to make the text segment overflow too soon.

This is a fairly poor attempt at a delay routine. It uses Timer2 to measure intervals. Not tested at all.


Function Documentation

void tdelay ( uint16_t  ms  ) 

00017                          {
00018     uint16_t i;
00019     
00020     if (ms == 0) return;
00021     
00022     ms = ms * 12;
00023     
00024     uint8_t remainder = ms % 256;
00025     if (remainder) {
00026         TCCR2 = 0;
00027         TCNT2 = 0;
00028         OCR2 = remainder;
00029         TIFR |= _BV(OCF2);
00030         
00031         // prescaler = 1024, 0.128ms per cycle
00032         for (TCCR2 = _BV(CS22)|_BV(CS21)|_BV(CS20); (TIFR & _BV(OCF2)) == 0;);
00033     }
00034     
00035     OCR2 = 0;
00036     TCNT2 = 0;
00037     for (i = ms/256; i > 0; i--) {
00038         TCCR2 = 0;
00039         TIFR |= _BV(TOV2);
00040         for (TCCR2 = _BV(CS22)|_BV(CS21)|_BV(CS20); (TIFR & _BV(TOV2)) == 0;);
00041     }
00042     
00043     TCCR2 = 0;
00044 }


Generated on Fri Nov 13 04:21:26 2009 for [M]ouse by  doxygen 1.5.9