C code GCC countdown

Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum

Help Support Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

KuntzBrewing

Well-Known Member
Joined
Aug 22, 2011
Messages
630
Reaction score
13
Location
Kokomo
I've got a rather long program. I'm reading adc values and using 2 ds18b20s
I've got my code for that wirrten. I'm using an ATmega328p chip, coding in c on atmel studio 6.

My program will have time dependent outputs, when to switch from one mode to another, displaying a countdown, and such. I want to countdown every second. I want to know how many milliseconds my code takes to go from cycle to cycle so I can create a delay that accurately makes 1 second one second(1000ms)

I don't seem to have access to time.h

I also can't simulate my code, because I'm using sensors that use inputs that cannot be simulated.

I'm aware that time.h has a function where in the beginning of the loop you call a start count function and at the end a stopcount function then compare the two.

Are there any other ways, or do you have a way to download the .h file
 
Check out avrfreaks, there is lots of information there.

Try setting up an interrupt that increments every millisecond and use that as a timer. Something like this, if you have a 16 MHz crystal like on an Arduino:

volatile unsigned long millis = 0; // timer

ISR (TIMER2_COMPA_vect)
{
millis++; // increment timer
}

void main()
{
cli(); // disable interrupts
OCR2A = 124; // set up timer2 CTC interrupts for buzzer
TCCR2A |= (1 << WGM21); // CTC Mode
TIMSK2 |= (1 << OCIE2A); // set interrupt on compare match
GTCCR |= (1 << PSRASY); // reset timer2 prescaler
TCCR2B |= (5 << CS20); // prescaler 128
sei(); // enable interrupts

for(;; )
{
// main loop
}
}
 
This is what the timers are for. If you are not using the 16bit timer just set it up with a nice slow frequency and use as an asynchronous time source.

Example:

If you chip is running at 1MHz, divide the timer by 64 or whatever the max is and then you will know exactly how long each timer tick is.

Keep in mind the internal oscillator is only +/- 10% or so. Use a crystal if you want accuracy.

Also I have never come across a brewing application that required strong real-time control. Typically I write a finite state machine that evaluates on some slow main loop, like every 100ms or something.
 

Latest posts

Back
Top