Spike Brewing 12.5 Conical Fermenter Giveaway!

Home Brew Forums > Home Brewing Beer > Automated Brewing Forum > C code GCC countdown

Reply
 
LinkBack Thread Tools
Old 11-16-2013, 07:32 AM   #1
KuntzBrewing
Feedback Score: 0 reviews
Recipes 
 
Join Date: Aug 2011
Location: Kokomo, Indiana
Posts: 630
Liked 12 Times on 11 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, 01:58 PM   #2
alien
Feedback Score: 1 reviews
Recipes 
 
Join Date: Apr 2012
Location: Philadelphia, PA
Posts: 1,234
Liked 65 Times on 58 Posts
Likes Given: 61

Default

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
}
}

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

Default

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.

__________________
BetterSense is offline
 
Reply With Quote Quick reply to this message
Reply


Quick Reply
Message:
Options
Thread Tools


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