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.
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.
/* * m32Mux2Counting.c * * Created: 6/19/2022 3:36:14 PM * Author : aki-technical */ #include <avr/io.h> /*Delay function*/ #define F_CPU 4000000UL #include <util/delay.h> int main(void) { unsigned char temp=0,cnt=0; /*Common Cathode Display*/ unsigned char displayData[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; /*PortB and PortD Output*/ DDRC=0xFF; DDRD=0xFF; /*Select Fclk/1024*/ TCCR0|=(1<<CS02)|(1<<CS00); /*Clear TOV0 flag*/ TIFR|=(1<<TOV0); /*Clear Counter*/ TCNT0=0; while (1) { /*Multiplexing Display Process*/ PORTD=0x00; PORTC=displayData[temp/10]; /*Turn On Digit 1*/ PORTD=0x01; /*Activate for 5ms*/ _delay_ms(5); PORTD=0x00; PORTC=displayData[temp%10]; /*Turn On Digit 2*/ PORTD=0x02; /*Activate for 5ms*/ _delay_ms(5); /*TOV0 occurs for every 0.065536 second*/ if ((TIFR)&&(1<<TOV0)) { cnt++; TIFR|=(1<<TOV0); } /*One second is equal to 15 counts*/ if (cnt>=15) { temp++; cnt=0; } /*Check if it is greater than 99 seconds*/ if (temp>99) { temp=0; } } }
Click here to download its source file.
Schematic Diagram |
No comments:
Post a Comment