-1

I am trying to make a countdown timer using an ATmega32 clocked at 8 MHz.

I want to use the ATmega timer to start a countdown for 8 minutes when the switch is pressed and the output should turn on for 8 minutes and when the countdown hits zero the output should turn off and the countdown returning back to zero. (All this I want to show on a 16*2 LCD). I know that I have to use a timer for that, but I am really not sure on how to do it. Any suggestions?

Is creating this much long delay from an AVR timer possible or not?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • [ATmega32](https://www.microchip.com/wwwproducts/en/ATmega32) - what board is it sitting in? Your own? – Peter Mortensen Dec 12 '19 at 17:01
  • This can't be that uncommon. What did you find in your Internet search? There must be something similar out there. For instance, at [Instructables](https://en.wikipedia.org/wiki/Instructables). Please add the documentation for the research to your question (by [editing your question](https://stackoverflow.com/posts/59309312/edit)), not here in comments. – Peter Mortensen Dec 12 '19 at 17:05
  • For the length, using a timer alone (software can always be used to count overflows, thus enabling arbitrary length of time), take the clock frequency, and find the highest prescaler and highest-bit timer in [the datasheet](https://www.microchip.com/wwwproducts/en/ATmega32). For example, ATmega328 at 16 MHz ([Arduino Uno](http://arduino.cc/en/Main/ArduinoBoardUno), the single 16-bit timer (Timer1) and the highest [prescaler](https://en.wikipedia.org/wiki/Prescaler) 1:256): 1 / 16,000,000 s * 256 * 65536 = 1.04 seconds. A 32-bit timer would extend that to about 19 hours. – Peter Mortensen Dec 12 '19 at 17:22
  • 1
    This is Stack Overflow, [not a forum](http://meta.stackexchange.com/a/92115). It also operates on a time scale of minutes, not hours or days. You are expected to respond promptly. – Peter Mortensen Dec 12 '19 at 17:24
  • I am really sorry for that I am still a stranger to the site's way of operation and I am really sorry for that please forgive me this time I will take care next time. – user9728254 Dec 14 '19 at 14:56
  • Am I blacklisted already?? – user9728254 Dec 15 '19 at 17:37

1 Answers1

0

Please don't be angry with me(I don't want to disrespect you people by answering my own question).I had tried to solve my problem myself and I had achieved this, this code works thanks for your valuable time to answer my questions. I have posted the code so that new people can benefit from it. Thanks for your time.

volatile uint16_t second_count = 0;

void init_timer1(void);

void init_timer1()
{
    TCCR1B |= (1<<WGM12);//CTC mode
    TCNT1 = 0;//initializing timer
    OCR1A = 31249;//preloading timer so that it can count with 1 sec 
    TIMSK |= (1<<OCIE1A);//Timer compare A Match interrupt
    TCCR1B |= (1<<CS12);//Starting timer and clock prescaler(256)
}

ISR(TIMER1_COMPA_vect)
{
    second_count++;
    if(second_count == 180)//turing the output on for 180 seconds on power on
    {
        PORTD |= (1<<PD6);
    }   
    else
    {
        PORTD &= ~(1<<PD6);
        second_count = 0;
    }

}
int main(void)
{
    init_timer1();
    sei();
    DDRD = 0xff;
    PORTD = 0x00;
    DDRB = 0x00;
    PORTB = 0xff;
    while(1)
    {

    }
}