Last days to enter the BrewHardware Giveaway!

Home Brew Forums > Home Brewing Beer > Automated Brewing Forum > C code GCC countdown
LinkBack Thread Tools
Old 11-16-2013, 08:32 AM   #1
Feedback Score: 0 reviews
Join Date: Aug 2011
Location: Kokomo, Indiana
Posts: 630
Liked 14 Times on 13 Posts

Default C code GCC countdown

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

KuntzBrewing is offline
Reply With Quote Quick reply to this message
Old 11-16-2013, 02:58 PM   #2
Feedback Score: 1 reviews
Join Date: Apr 2012
Location: Philadelphia, PA
Posts: 1,235
Liked 65 Times on 58 Posts
Likes Given: 61


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

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

alien is offline
Reply With Quote Quick reply to this message
Old 11-17-2013, 02:55 AM   #3
Feedback Score: 0 reviews
Join Date: Jul 2011
Location: Richardson, Texas
Posts: 977
Liked 45 Times on 35 Posts
Likes Given: 3


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.


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.

BetterSense is offline
Reply With Quote Quick reply to this message

Quick Reply
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
2 day countdown till BREW! Scot_chale Recipes/Ingredients 2 08-27-2012 06:54 PM
CODE RED - Mountain Dew Code Red recipe AZOTH99 Soda Making 2 04-12-2012 09:25 PM
Countdown to Skunk FizorZed Bottling/Kegging 4 02-22-2011 11:49 PM
Rodgers overload--COUNTDOWN TO 421 olllllo General Chit Chat 6 03-05-2008 02:47 PM
Countdown to the Boil McKBrew General Beer Discussion 11 04-30-2007 04:59 AM

Newest Threads