728x90

728x90

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
 

320x50

Search This Blog

tyro-728x90