Friday, April 29, 2022

ATMega16 two-digit multiplexing display and counting example

Display multiplexing is very common for most microcontroller programming and interfacing. In usual, a controller could drive up to 8 digits before we can see its display flickering. In this programming example, I use only two digits of 7-Segment display. Because it's easy to build the circuit with a  little of source codes.

ATMega16 two-digit multiplexing display example
Program simulation in Proteus

Controller keeps track of counting an external pulse generated by SW1 switch pressing. The maximum press counting is 60 before it rolls down to 0. The display will become flicker whenever the input switch has a longer press duration, but it's not longer than 250ms.

  1. /*
  2.  * mux2Ssd.c
  3.  *
  4.  * Created: 5/25/2022 7:20:40 PM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #define F_CPU 4000000UL
  11. #include <util/delay.h>
  12.  
  13. int main(void)
  14. {
  15. //common cathode
  16. unsigned char SSD[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  17. unsigned char cnt=0;
  18. //PortC output
  19. DDRC=0xFF;
  20. //PD7 input
  21. DDRD&=~(1<<7);
  22. //Turn on PD7
  23. PORTD|=(1<<7);
  24. while (1)
  25. {
  26. //Display processing
  27. PORTD&=~(1<<0);
  28. PORTD&=~(1<<1);
  29. PORTC=SSD[cnt/10];
  30. PORTD|=(1<<0);
  31. _delay_ms(3);
  32.  
  33. PORTD&=~(1<<0);
  34. PORTD&=~(1<<1);
  35. PORTC=SSD[cnt%10];
  36. PORTD|=(1<<1);
  37. _delay_ms(3);
  38.  
  39. //Input Testing
  40. if ((PIND&0x80)==0)
  41. {
  42. //while((PIND&0x80)==0);
  43. cnt++;
  44. _delay_ms(250);
  45. }
  46. if(cnt>=60) cnt=0;
  47. }
  48. }
  49.  
  50.  

Click here to download source file.

ATMega16 two-digit multiplexing display and counting example
Schematic diagram

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)