51 single chip microcomputer uses timer to perform dynamic scanning program of digital tube

Publisher:平静的33号Latest update time:2016-05-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Digital tube is a relatively basic module in the study of 51 single-chip microcomputer. In order to simplify circuit connection, improve system reliability and reduce manufacturing cost, multi-digit digital tube widely adopts dynamic scanning for display. If the program is not written properly, the dynamic scanning of digital tube is prone to uneven brightness, low brightness, ghosting and other phenomena. Many beginners will use while (1) loop in the main function, combined with delay function to scan continuously, but this requires continuous calling of display scanning. If the single-chip microcomputer needs to execute other programs, it will cause various problems due to untimely scanning. In addition, if the program is not appropriate in the order of selecting the control segment position, it will cause problems such as display ghosting. Here is a stable and reliable program solution.

 

Circuit connection: P1 port is connected to the segment select and chip select of the eight-bit digital tube through two 74HC573. The segment select and bit select are controlled by P3.4 and P3.5. The low level latch

 

#include 
 
 #define DIGI_PORT P1 // Macro definition of digital tube port. If the port changes, just modify this sentence
 #define DIGI_NUM 8 // Macro definition of the number of digital tubes
 
 sbit DULA = P3^4;
 sbit WELA = P3^5;
 
 unsigned char digiBuf[DIGI_NUM]; //Digital tube buffer
 
 unsigned char code DigiTable[] = {
 	0x3F,/*0*/
 	0x06,/*1*/
 	0x5B,/*2*/
 	0x4F,/*3*/
 	0x66,/*4*/
 	0x6D,/*5*/
 	0x7D,/*6*/
 	0x07,/*7*/
 	0x7F,/*8*/
 	0x6F,/*9*/
 	0x00,/* */
 };
 
 /* 
 Initialize timer 0, mode 1, 11.0592MHz crystal oscillator, interrupt every 5ms,
 If there are only 4 digital tubes, 10ms is enough, and the interval length can be adjusted according to the actual situation.
 While ensuring no flickering, increase the interval as much as possible to reduce CPU resource consumption
 */
 void initDigi()
 {
     TMOD = 0x01;
     TH0 = 0xEE;
     TL0 = 0x00;
     EA = 1;
     ET0 = 1;
     TR0 = 1;
 }
 
 void main()
 {
 	digiBuf[0] = 10; // If it is 10, this bit will not be displayed
 	digiBuf[1] = 1;
 	digiBuf[2] = 2;
 	digiBuf[3] = 3;
 	digiBuf[4] = 4;
 	digiBuf[5] = 5;
 	digiBuf[6] = 6;
 	digiBuf[7] = 7;
 	initDigi();
 	while(1);
 }
 
 // Timer 0 function runs once every certain period of time
 void displayTimer0() interrupt 1
 {
 	static unsigned char digiPos = 0; // The digital tube that needs to be displayed currently. Use static local variables or global variables here. Do not use default local variables.
     TH0 = 0xEE;
     TL0 = 0x00;
 	// Turn off all digital tubes by bit selection, otherwise ghosting may occur (after the segment selection is changed and before the bit selection is changed, the digital tubes that were last turned on by bit selection will display wrong information)
 	DIGI_PORT = 0xFF; //If this sentence is swapped with the next sentence, it may cause ghosting. For details, see http://blog.csdn.net/jzj1993/article/details/8563337
 	HOT = 1;
 	TEMPERATURE = 0;
 	// Perform segment selection here (all digital tubes are turned off at this time, and no error message will be displayed)
 	DIGI_PORT = DigiTable[digiBuf[digiPos]];
 	GAME = 1;
 	GAME = 0;
 	// Here, the position is selected according to digiPos
 	switch(digiPos) {
 		case 0: DIGI_PORT = ~(1 << 0); break; // Turn on the 0th digital tube
 		case 1: DIGI_PORT = ~(1 << 1); break; // Turn on the first digital tube
 		case 2: DIGI_PORT = ~(1 << 2); break; // Turn on the second digital tube
 		case 3: DIGI_PORT = ~(1 << 3); break; // Turn on the third digital tube
 		case 4: DIGI_PORT = ~(1 << 4); break; // Turn on the 4th digital tube
 		case 5: DIGI_PORT = ~(1 << 5); break; // Turn on the 5th digital tube
 		case 6: DIGI_PORT = ~(1 << 6); break; // Turn on the 6th digital tube
 		case 7: DIGI_PORT = ~(1 << 7); break; // Turn on the 7th digital tube
 	}
 	HOT = 1;
 	TEMPERATURE = 0;
 	// Change the digiPos value to prepare for the next entry into this function
 	digiPos++;
 	if(digiPos == DIGI_NUM)
 		digiPos = 0;
 }
 
Reference address:51 single chip microcomputer uses timer to perform dynamic scanning program of digital tube

Previous article:Basic knowledge of 51 MCU program development
Next article:Basic Analysis of 51 Single Chip Microcomputer (Based on C Language)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号