Atmega16 Timer1 Output Control!

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello guys I want a suggestion on how to control outputs using the timer1 of the atmega16.

I was able to turn on the led for a second.I want to do a couple more things from this timer1:-

 

1. I want to light up the led for 8mins and then turn that off(long delay which is new for me), I was able to learn more about my problem on the internet people we saying that the Watchdog timer can do this job well (It can produce long delays fairly easily)If that is the case someone can explain that to me.

 

2.All this led blinking I want with a button(I hadn't used the interrupts as I don't require that milisecond response , a not so noticeable lag will also suffice). I was actually successful with the button but lost to the long delays .

 

3. Also please check that is my code for avoiding button debounce is correct or not ,with correct i mean that it works well but is it the right way to do this ??

 

Can someone advice me ?

Below is the simple code that I had written:-

#include <avr/io.h>

#include <avr/interrupt.h>
#include <util/delay.h>

void timer1_init()

{

	TCCR1B |= (1 << WGM12) | (1 << CS12);

	TCNT1 = 0;

	OCR1A = 31250;

	TIMSK |= (1 << OCIE1A);

	sei();

}

ISR (TIMER1_COMPA_vect)
{

		PORTD ^= (1<<PD6);
		/*I want this led to be ON for 8 mins when the button is pressed
		 and then turn off until button is pressed again*/

}

int main(void)
{

	DDRD = 0xff;
	PORTD = 0x00;
	DDRA = 0x00;
	PORTA = 0xff;

	while(1)  {

		if(!(PINA & (1<<PA0)))
		{
			timer1_init();
			_delay_ms(200);//debounce delay
		}

	}
}

Sorry I am just new here please don't be harsh on me for asking so many questions on a single thread.

This topic has a solution.

anshumaan kumar

Last Edited: Sat. Dec 14, 2019 - 02:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Remember the multitasking tutorial? Your answer is there.
Your debouncing is poor. You have already been given the answer in your previous thread.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Set your timer to 100ms using ctc mode, then when the overflow flag is set, increment a long counter, 10 times is one second, so when the counter reached the desired time out value, do what is needed.

 

Jim

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Remember the multitasking tutorial? Your answer is there.

Ok. I too firmly believe that the my answer is there but it's in a way hidden from me let me try to solve my problem using the multitasking code you have provided and I will be back with something, and again thanks for the help.

 

ki0bk wrote:
Set your timer to 100ms using ctc mode, then when the overflow flag is set, increment a long counter, 10 times is one second, so when the counter reached the desired time out value, do what is needed.

Thank you for your reply, let me test this.

anshumaan kumar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
Set your timer to 100ms using ctc mode, then when the overflow flag is set, increment a long counter, 10 times is one second, so when the counter reached the desired time out value, do what is needed.

I didn't set the timer to 100ms I had set to count in 1 sec. Well I was a little bit confused on your answer then patiently read your answer a couple of times and then I have done this. This works flawlessly I required a delay of 3 minutes and this code fulfills that, please check this for any suggestions.smiley

 

Thanks Jim I have now fully understood the concept you are talking about and I am happy about it and now I will try to implement it in the Multitasking code written by Kartman. But before that let me check my debouncing.wink

Very much pleasured to be in a kind and helping community.

#include <avr/io.h>
#include <avr/interrupt.h>

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

 

anshumaan kumar

Last Edited: Sat. Dec 14, 2019 - 02:05 AM