MSP430 MCU Example 10-8-bit Digital Tube Display Clock
[Copy link]
The P4 port of the single-chip microcomputer controls the segment code of the 8-bit integrated digital tube, using 8 common cathode LED digital tubes. The P5 port of the single-chip microcomputer is used as the bit scan control port, and P5.0~P5.7 are connected to the common COM pins of LED0~LED7 respectively.
Compared with the static digital tube driving circuit, the circuit shown in the figure below occupies 16 I/O port lines. The dynamic display of 8 digital tubes only requires two 8-bit ports, of which P4 controls the segment code and P5 controls the bit selection line. The principle of dynamic scanning: the P5 port outputs the scanning signal to each bit in turn, so that only one digital tube is selected at each moment (low-level selection for the common cathode and high-level selection for the common anode), and then the glyph code to be displayed in the bit is sent from the P4 port to light up the glyph displayed by the glyph segment of the bit. In this way, under the control of the segment code sent by P4 and the bit selection line sent by P5, each digital tube can be lit up in turn to display its own character. Although the digital tubes of the display are lit up in sequence, as long as the lighting time of each bit exceeds 1ms, it will be displayed again after a period of time. In this way, the scanning is repeated continuously. As long as the scanning frequency is fast enough (greater than 50Hz), the flickering cannot be seen due to human visual temporary storage. Dynamic scanning is generally implemented by software, which can be implemented by software delay or timer delay.
MSP430 MCU simulation example based on Proteus 10-8 digital tube display clock
Programming
//main.c
#include "msp430f247.h"
#include "stdlib.h"
#include "string.h"
/************************************************Software delay, main frequency 1M*******************/
#define CPU_F1 ((double)1000000)
#define delay_us1M(x) __delay_cycles((long)(CPU_F1*(double)x/1000000.0))
#define delay_ms1M(x) __delay_cycles((long)(CPU_F1*(double)x/1000.0))
/****************************************************** ***************************/
//Common anode digital tube segment code table
unsigned char const Led_Tab1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
//Common cathode digital tube segment code table
unsigned char const Led_Tab2[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x40};
unsigned char time_counter; //1S counter
unsigned char time[3]; // hours, minutes, seconds count
unsigned char dis_buff[8]; //Display buffer, storing the 6-character segment code value and two separators to be displayed
void display(void); //Scan display function
void time_to_disbuffer(void); //Send time value to display buffer function
/************************************************
Function name: main function
Function: 8-bit digital tube dynamic display clock
Entry parameters: None
Export parameters: None
************************************************/
main()
{
_EINT(); // Enable interrupt and respond to key presses
WDTCTL = WDTPW + WDTHOLD; // Turn off the watchdog
P4DIR=0xff; //Segment code port initialization
P5DIR=0xff; //bit select port initialization
time[2]=22; //initial value of time
time[1]=51;
time[0]=55;
time_to_disbuffer();
dis_buff[2]=10;//
dis_buff[5]=10;//
while(1)
{
display(); //Display scan
if(++time_counter>=40)//Update time
{
time_counter=0;
if(++time[0]>=60)
{
time[0]=0;
if(++time[1]>=60)
{
time[1]=0;
if(++time[2]>=24) time[2]=0;
}
}
time_to_disbuffer(); //Modify the display buffer
}
}
}
void display(void)
{
static char i;
P5OUT=0xff;
P4OUT=80;
P4OUT=Led_Tab2[dis_buff[i]]; //Time value sent to display buffer function
P5OUT=~(1<<i); //bit select port selection
if(++i==8) i=0;
delay_ms1M(2);
}
void time_to_disbuffer(void)
{
dis_buff[0]=time[2]/10;//hours
dis_buff[1]=time[2]%10;
dis_buff[3]=time[1]/10;//minutes
dis_buff[4]=time[1]%10;
dis_buff[6]=time[0]/10;//seconds
dis_buff[7]=time[0]%10;
//dis_buff[2]=0x04;//
//dis_buff[5]=0x04;//
}
Program description
According to the hardware circuit, at any time, only one I/O port in P5.0~P5.7 can output a low level, that is, only one digital tube is lit, and the P4 port must output the corresponding segment code value of that bit. Even if the displayed content does not change, the cyclic scanning process must be performed continuously.
The software design should ensure that the digital tube display effect is continuous, uniform in brightness, and without tailing. In order to ensure that the display effect of each digital tube does not flicker and make the digital tubes look like all lit, the microcontroller must first scan the 8 digital tubes more than 25 times in 1S. This is to use the visual temporary memory effect of the human eye. The second thing to consider is that in the 25ms time interval, the 8 digital tubes must be lit one by one, so the duration of each digital tube lighting must be the same, so that the brightness can be uniform. The third thing to consider is the duration of each digital tube lighting. If this time is longer, the brightness of the digital tube is higher, otherwise it is darker. Usually, the duration of each digital tube lighting is 1~2ms.
|