Incrementing timer with push button

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

Hi, I'm trying to have two timers which values could be incremented by half their value each time a button is pressed, but will such a code will even work:

 

unsigned int on1;
unsigned int off1;

TCCR0A = 0; // set entire TCCR0A register to 0
	TCCR0B = 0; // same for TCCR0B
	TCNT0  = 0; // initialize counter value to 0 // set compare match register for 120.1923076923077 Hz increments
	OCR0A = 172; // = 16000000 / (1024 * 90.3179190751445) - 1 (must be <256)
	TCCR0B |= (1 << WGM01); // Set CS02, CS01 and CS00 bits for 1024 prescaler
	TCCR0B |= (1 << CS02) | (0 << CS01) | (1 << CS00); // enable timer compare interrupt
	TIMSK0 |= (1 << OCIE0A);
	
	TCCR1A = 0; // set entire TCCR1A register to 0
	TCCR1B = 0; // same for TCCR1B
	TCNT1  = 0; // initialize counter value to 0 // set compare match register for 60.00060000600006 Hz increments
	OCR1A = 33332; // = 16000000 / (8 * 60.00060000600006) - 1 (must be <65536) // turn on CTC mode
	TCCR1B |= (1 << WGM12); // Set CS12, CS11 and CS10 bits for 8 prescaler
	TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10); // enable timer compare interrupt
	TIMSK1 |= (1 << OCIE1A);
	sei(); // allow interrupts
	
	
	
	{
		if(button(&PINB, PB2)){ //when PB3 button is pressed
			on1=OCR1A+OCR1A/2;
			_delay_ms(10);
			
		}
		if(button(&PINC, PC4)){ //when PC4 button is pressed
			off1=OCR0A+OCR0A/2;
			_delay_ms(10);
			
			
			
ISR(TIMER1_COMPA_vect){
	//interrupt commands for TIMER 1 here
}

ISR(TIMER0_COMPA_vect){
	//interrupt commands for TIMER 0 here
}

And even if this would work I don't know how to set it up to run at a specific time.

I know I want to run "on" timer when PD2 pin value is high and run "off" timer when PD2 value is low. I know I should be using last lines of code for that but I have no idea how to write that.

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

Take a step back and tell us what you want to achieve. In terms of time, minimum, maximum, resolution etc?
Using a delay for debounce is rather simplistic and may not achieve what you’d like.

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

Kartman wrote:
Take a step back and tell us what you want to achieve. In terms of time, minimum, maximum, resolution etc? Using a delay for debounce is rather simplistic and may not achieve what you’d like.

 

From my code I imagine that have timer0 which will run for 90 seconds and with each button press a 45 seconds will be added to that value. Timer1 runs for 60 seconds and each button press will increment it by 30 seconds.

At the end of the day I would like my timer0 to run every time PD2 pin value is low and timer1 to run when PD2 pin value is high.

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

You still haven't answered my questions. Forget the implementation - concentrate instead on describing the application. What are the maximum times? We can assume a minimum of 90 seconds and 45 seconds. What happens if you press the button too many times? How do you reset the times?

 

You might only need one hardware timer to do both and debounce your inputs.

 

Let's assume you want a time resolution of 1 second. We can set up a timer in ctc mode to interrupt every 10ms. We can count those 10ms ticks to get one second. We can then count those one seconds to get each of your times.

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

Agree with Kartman's questions.  And what happens when the timers run?  And what happens when they time down to zero?  Are they controlling something?  Are you displaying the values?  Describe the desired operation of the system.