Saturday, May 23, 2020

Using timer 0 of ATMega32 to make a retentive timer low

In an MCU application, when you pressed a button accidentally or by mistake, the related process will activated. To prevent this, we could use a timer to count the duration of the button that being pressed.

Timer 0 is very common to most of microcontroller. In this example, I use timer 0 to count the duration of any pressed button. When it is pressed (shot to ground) the program sub routine start counting the duration. If the duration exceeds 3 second, it will activate a corresponding LED. Otherwise it is exempt. 


Using timer 0 of ATMega32 to detect long pressed buttons
Timing diagram.
When any pin become low, timer 0 start counting
duration until 3 seconds to activate the corresponding
output.

Using timer 0 of ATMega32 to detect long pressed buttons
Schematic diagram. SW2 used for toggling D1.
SW3 used for toggling D2. Both buttons implement a pressing
duration counting with timer 0.

The MCU clock is 4 MHz. So the MCU clock period is 1/(4000000) = 250 nano seconds. I set the prescaler to 1:1024 giving the timer 0 period of 1024 x 250 nano seconds = 256 micro seconds.

I want to create 10 milli tick. So 10 milli seconds / 256 micro seconds = 39 counts.

The preload value of TCNT0 is 256 - 39 = 217 or -39 .

In the program, when the TOV0 is set to '1' we preload the TCNT0 to -39 or 217.


C source code; 


#include <avr/io.h>

unsigned int tenMillis=0;
unsigned int tenMilliSecond(void){
//Check if TIFR is set
if(TIFR&0x01){
//Preload TCNT0
TCNT0=-39;
//Clear flag
TIFR=0x01;
tenMillis+=1;
}
return tenMillis;
}

int main(void)
{
    //Set PortC to Output
DDRC=0xC0;
//Clear PortC
PORTC=0x03;
//Set 1:1024 prescaler
TCCR0=0x05;
//Clear overflow flag
TIFR=0x01;
    while (1) 
    {
while((PINC&0x01)==0){
if (tenMilliSecond()>300)
{
PORTC^=0x80;
break;
}
}
tenMillis=0;
while((PINC&0x02)==0){
if (tenMilliSecond()>300)
{
PORTC^=0x40;
break;
}
}
tenMillis=0;
    }
}

A screen shot of this example:

Using timer 0 of ATMega32 to detect long pressed buttons
Simulation screen shot.
D1 is toggled high after SW2 had been press
for more than 3 seconds.
Back to main tutorial page ATMega32 tutorials in C with Atmel Studio 7.







No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)