asking
Full Member level 5
- Joined
- Sep 21, 2010
- Messages
- 279
- Helped
- 6
- Reputation
- 12
- Reaction score
- 6
- Trophy points
- 1,298
- Activity points
- 3,377
Hi,
I am trying to make simple Increment counter based on push switch. When i press switch the counter increments on LCD(16x2) but varialble(Incrementing) again goes to zero in next cycle. I have define that variable as Volatile so it won't be affected by Optimization of GCC compiler.
My Code and Proteus File Attached. Just when we press button counter increments. But again in next cycle it gets earsed. I mean it gets initialize to zero. It should not be initialize...what should i do keep the counter value as it is ?
I am trying to make simple Increment counter based on push switch. When i press switch the counter increments on LCD(16x2) but varialble(Incrementing) again goes to zero in next cycle. I have define that variable as Volatile so it won't be affected by Optimization of GCC compiler.
My Code and Proteus File Attached. Just when we press button counter increments. But again in next cycle it gets earsed. I mean it gets initialize to zero. It should not be initialize...what should i do keep the counter value as it is ?
Code:
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <lcd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
char buffer[10];
volatile uint32_t counter = 0;
//volatile uint8_t k = 0;
void InitTimer1(){
TIFR |= 0x01; // clear interrupt flag
TCCR1B |= (1 << WGM12)|(1<<CS12)|(0<<CS11)|(0<<CS10); // REFER PAGE 106
OCR1AH = 0x7A;
OCR1AL = 0x12; // 1 SECOND DELAY USING 16 BIT TIMER 1 COMPARE VALUE
TCCR1A = 0x00; // DISABLE TCCR CONTROL ON I/O PINS OF PORTA
TCNT1H = 0; // INITIALIZE COUNTER TO 0
TCNT1L = 0;
TIMSK |= (1<<OCIE1A); // COMPARE OUTPUT INTERRUPT ENABLE
sei(); // global interrupt enable
}
void keypress()
{
if (bit_is_clear(PINA, 0))
{
counter = counter + 1;
_delay_ms(50);
}
else
{
//counter = counter;
}
}
int main(void)
{
DDRA |= (0<<PINA0); // PORTA0 IS NOW INPUT
PORTA = 0XFF; // ENABLE PULL UP FOR ALL INPUT PINS
DDRB = 0XFF; // ALL PORTB I/O ARE OUTPUT
InitTimer1();
lcd_init(LCD_DISP_ON);
while(1)
{
keypress();
lcd_gotoxy(0,0);
itoa(counter, buffer, 10);
lcd_puts(buffer);
lcd_gotoxy(0,1); // ZERO POINT STARTING SECOND ROW
lcd_puts("TIMER1 TEST");
}
}