Sunday, June 19, 2022

Making a Simple Free Running Timer with Multiplexing Display using Atmega32

In previous post, I made a simple multiplexing display example using a two-digit SSD. In this example, I gonna make a free running timer two a two-digit multiplexing seven-segment display. It will start counting from 0 to 999 seconds before it reset by itself.

Making a Simple Free Running Timer with Multiplexing Display using Atmega32
Program simulation

The display is driven by delay function. Each digits are activated for only 5ms. I use Timer/Counter 0 to create timing tick.

The CPU clock is 4MHz. Clock period is,

1/4000000MHz =  250 nano seconds, or 0.00000025 second.

Timer 0 prescaler is Fosc/1024. So its Timer/Counter 0 period is,

0.00000025 second * 1024 = 256 micro seconds, or 0.000256 second.

The 8-bit TCNT0 register can store counting value up to 256. So the TOV0 occurs every,

256*0.000256 second = 0.065536 second.

To get a one second timing, I need to make a calculation of one second count. That is,

1 second count = (1 second)/(0.065536 second) = 15 counts.

Finally after the 15 counts, I will get a one-second time. Then I will need to reset the counter variable.

  1. /*
  2.  * m32Mux2Counting.c
  3.  *
  4.  * Created: 6/19/2022 3:36:14 PM
  5.  * Author : aki-technical
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. /*Delay function*/
  11. #define F_CPU 4000000UL
  12. #include <util/delay.h>
  13.  
  14. int main(void)
  15. {
  16. unsigned char temp=0,cnt=0;
  17. /*Common Cathode Display*/
  18. unsigned char displayData[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  19. /*PortB and PortD Output*/
  20. DDRC=0xFF;
  21. DDRD=0xFF;
  22. /*Select Fclk/1024*/
  23. TCCR0|=(1<<CS02)|(1<<CS00);
  24. /*Clear TOV0 flag*/
  25. TIFR|=(1<<TOV0);
  26. /*Clear Counter*/
  27. TCNT0=0;
  28. while (1)
  29. {
  30. /*Multiplexing Display Process*/
  31. PORTD=0x00;
  32. PORTC=displayData[temp/10];
  33. /*Turn On Digit 1*/
  34. PORTD=0x01;
  35. /*Activate for 5ms*/
  36. _delay_ms(5);
  37.  
  38. PORTD=0x00;
  39. PORTC=displayData[temp%10];
  40. /*Turn On Digit 2*/
  41. PORTD=0x02;
  42. /*Activate for 5ms*/
  43. _delay_ms(5);
  44.  
  45. /*TOV0 occurs for every 0.065536 second*/
  46. if ((TIFR)&&(1<<TOV0))
  47. {
  48. cnt++;
  49. TIFR|=(1<<TOV0);
  50. }
  51.  
  52. /*One second is equal to 15 counts*/
  53. if (cnt>=15)
  54. {
  55. temp++;
  56. cnt=0;
  57. }
  58. /*Check if it is greater than 99 seconds*/
  59. if (temp>99)
  60. {
  61. temp=0;
  62. }
  63. }
  64. }
  65.  
  66.  

Click here to download its source file.

Making a Simple Free Running Timer with Multiplexing Display using Atmega32
Schematic Diagram
 

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)