51 single chip microcomputer realizes digital tube as instrument panel to display the direction, speed and movement of the marquee

Publisher:asa1670Latest update time:2022-01-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

one

2. Programming


/********************************************************************************************************************

---- @Project: LED-74HC595

---- @File: main.c

---- @Edit: ZHQ

---- @Version: V1.0

---- @CreationTime: 20200702

---- @ModifiedTime: 20200703

---- @Description: Use the S1 key as the direction key to control the marquee, and the S5 key as the direction key to control the marquee.

---- The S9 key is used as the deceleration key to control the direction of the marquee, and the S13 key is used as the speed key to control the direction of the marquee.

---- To the start or pause button.

---- Functions implemented:

---- Running the marquee: The 1st to 8th LED lights are off. The 9th to 16th LED lights light up one by one and

---- And only one light can be on at a time. Each time you press the independent button S13, the running marquee will pause.

---- The marquee will run. Use S1 to change the direction. Use S5 and S9 to change the speed. Each press of the button increases or decreases the speed.

---- Decrease in units of 10.

---- Digital tube display: This program has only one window, which is divided into three partial displays. 8, 7, 6-digit digital tube display

---- Running status, "on" is displayed when starting, and "oFF" is displayed when stopping. The 5-digit digital tube displays the direction of the digital tube, and the positive direction displays

---- Display "n" in the reverse direction, "U" in the reverse direction. The 4, 3, 2, 1 digit digital tubes display the speed. The larger the value, the slower the speed. The slowest speed is

---- 550, the fastest speed is 50.

---- Microcontroller: AT89C52

****************************************************************************************************************************/

#include "reg52.h"

/*——————Macro definition——————*/

#define FOSC 11059200L

#define T1MS (65536-FOSC/12/500) /*0.5ms timer calculation method in 12Tmode*/

 

#define const_voice_short 40 /*Duration of the buzzer short call*/

 

#define const_key_time1 9 /*Key debounce delay time*/

#define const_key_time2 9 /*Key debounce delay time*/

#define const_key_time3 9 /*Key debounce delay time*/

#define const_key_time4 9 /*Key debounce delay time*/

 

//#define const_1s 96 /*Generates a time base of approximately one second*/

 

/*——————Variable function definition and declaration——————*/

/*Define the 74HC595 digital tube*/

sbit Dig_Hc595_Sh = P2^0;

sbit Dig_Hc595_St = P2^1;

sbit Dig_Hc595_Ds = P2^2;

 

/*74HC595 program for LED light*/

sbit Hc595_Sh = P2^3;

sbit Hc595_St = P2^4;

sbit Hc595_Ds = P2^5;

 

/*define buzzer*/

sbit Beep = P2^7;

 

/*When the pause indicator light is on, it means pause*/

sbit LED = P3^5;

 

/*Define buttons*/

sbit Key_S1 = P0^0; /*First line input*/

sbit Key_S2 = P0^1; /*Second line input*/

sbit Key_S3 = P0^2; /*Third line input*/

sbit Key_S4 = P0^3; /*Fourth line input*/

 

sbit Key_Gnd = P0^4;

 

unsigned int uiKeyTimeCnt1 = 0; /*Key debounce delay counter*/

unsigned char ucKeyLock1 = 0; /*Variable flag for self-locking after key triggering*/

unsigned int uiKeyTimeCnt2 = 0; /*Key debounce delay counter*/

unsigned char ucKeyLock2 = 0; /*Variable flag for self-locking after key triggering*/

unsigned int uiKeyTimeCnt3 = 0; /*Key debounce delay counter*/

unsigned char ucKeyLock3 = 0; /*Variable flag for self-locking after key triggering*/

unsigned int uiKeyTimeCnt4 = 0; /*Key debounce delay counter*/

unsigned char ucKeyLock4 = 0; /*Variable flag for self-locking after key triggering*/

 

unsigned char ucKeySec = 0; /*The number of the key being triggered*/

 

unsigned char ucLED1 = 0; /*Represents the on/off status of 16 lights, 0 represents off, 1 represents on*/

unsigned char ucLED2 = 0;

unsigned char ucLED3 = 0;

unsigned char ucLED4 = 0;

unsigned char ucLED5 = 0;

unsigned char ucLED6 = 0;

unsigned char ucLED7 = 0;

unsigned char ucLED8 = 0;

unsigned char ucLED9 = 0;

unsigned char ucLED10 = 0;

unsigned char ucLED11 = 0;

unsigned char ucLED12 = 0;

unsigned char ucLED13 = 0;

unsigned char ucLED14 = 0;

unsigned char ucLED15 = 0;

unsigned char ucLED16 = 0;

 

unsigned char ucLed_update = 0; /*Refresh the variable. Update it every time the LED status is changed. */

unsigned char ucLedStep_09_16 = 0; /*Step variables for the 9th to 16th LED marquee*/

unsigned int uiTimeCnt_09_16 = 0; /*Delay counter for counting the number of timing interrupts from the 9th to the 16th LED marquee*/

unsigned char ucLedStatus16_09 = 0; /*Intermediate variable representing the output status of the underlying 74HC595*/

unsigned char ucLedStatus08_01 = 0; /*Intermediate variable representing the output status of the underlying 74HC595*/

 

unsigned char ucLedDirFlag = 0; /*Direction variable, the core variable that associates the button with the marquee, 0 represents the positive direction, 1 represents the reverse direction*/

unsigned int uiSetTimeLevel_09_16 = 100; /*Speed ​​variable, the larger the value, the slower the speed, the smaller the value, the faster the speed. */

unsigned char ucLedStartFlag = 1; /*Variables for start and pause, 0 represents pause, 1 represents start*/

 

unsigned char ucDigShow8; /*The content to be displayed by the 8th digital tube*/

unsigned char ucDigShow7; /*The content to be displayed by the 7th digital tube*/

unsigned char ucDigShow6; /*The content to be displayed on the 6th digital tube*/

unsigned char ucDigShow5; /*The content to be displayed on the 5th digital tube*/

unsigned char ucDigShow4; /*The content to be displayed on the 4th digital tube*/

unsigned char ucDigShow3; /*The content to be displayed on the third digital tube*/

unsigned char ucDigShow2; /*The content to be displayed on the second digital tube*/

unsigned char ucDigShow1; /*The content to be displayed on the first digital tube*/

 

unsigned char ucDigDot1;

unsigned char ucDigDot2;

unsigned char ucDigDot3;

unsigned char ucDigDot4;

unsigned char ucDigDot5;

unsigned char ucDigDot6;

unsigned char ucDigDot7;

unsigned char ucDigDot8;

 

unsigned char ucDigShowTemp = 0; /*temporary intermediate variable*/

unsigned char ucDisplayDriveStep = 1; /*Step variable for dynamic scanning of digital tube*/

 

unsigned char ucWd1Part1Update = 1; /*Part 1 of window 1 updates the display variable*/

unsigned char ucWd1Part2Update = 1; /*Part 2 of window 1 updates the display variable*/

unsigned char ucWd1Part3Update = 1; /*Part 3 of window 1 updates the display variable*/

 

unsigned int uiVoiceCnt = 0; /*Buzzer beeping duration counter*/

 

void Dig_Hc595_Drive(unsigned char, unsigned char);

 

/*Common cathode digital tube character table obtained based on the schematic diagram*/

code unsigned char Dig_Table[] =

{

0x3f, /*0 sequence number 0*/

0x06, /*1 Sequence number 1*/

0x5b, /*2 Sequence number 2*/

0x4f, /*3 sequence number 3*/

0x66, /*4 Sequence number 4*/

0x6d, /*5 sequence number 5*/

0x7d, /*6 Sequence number 6*/

0x07, /*7 Sequence number 7*/

0x7f, /*8 sequence number 8*/

0x6f, /*9 sequence number 9*/

0x00, /*Do not display serial number 10*/

0x40, /*-    Serial number 11*/

0x73, /*P sequence number 12*/

0x5c, /*o Sequence number 13*/

0x71, /*F sequence number 14*/

0x3e, /*U sequence number 15*/

0x37, /*n sequence number 16*/

};

 

/**

* @brief Timer 0 initialization function

* @param None

* @retval initialize T0

**/

void Init_T0(void)

{

TMOD = 0x01; /*set timer0 as mode1 (16-bit)*/

TL0 = T1MS; /*initial timer0 low byte*/

TH0 = T1MS >> 8; /*initial timer0 high byte*/

}

/**

* @brief peripheral initialization function

* @param None

* @retval Initialize peripheral

* Transfer the contents displayed by the digital tube to the following variable interfaces to facilitate the writing of higher-level window programs in the future.

* Simply change the content of the corresponding variables below to display the numbers you want.

**/

void Init_Peripheral(void)

{

ucDigDot8 = 0; /*No decimal points are displayed*/

ucDigDot7 = 0;  

ucDigDot6 = 0; 

ucDigDot5 = 0;  

ucDigDot4 = 0; 

ucDigDot3 = 0;  

ucDigDot2 = 0;

ucDigDot1 = 0;

 

ET0 = 1;/*Enable timer interrupt*/

TR0 = 1;/*Start timing interrupt*/

EA = 1;/*Open the general interrupt*/  

}

 

/**

* @brief initialization function

* @param None

* @retval initialize the MCU

**/

void Init(void)

{

LED = 0;

Beep = 1;

Key_Gnd = 0;

 

Init_T0();

}

/**

* @brief delay function

* @param None

* @retval None

**/

void Delay_Long(unsigned int uiDelayLong)

{

   unsigned int i;

   unsigned int j;

   for(i=0;i   {

      for(j=0;j<500;j++) /*Number of empty instructions in the embedded loop*/

          {

             ; /*A semicolon is equivalent to executing an empty statement*/

          }

   }

}

/**

* @brief delay function

* @param None

* @retval None

**/

void Delay_Short(unsigned int uiDelayShort)

{

   unsigned int i;

   for(i=0;i   {

; /*A semicolon is equivalent to executing an empty statement*/

   }

}

 

 

/**

* @brief Driver function for displaying digital tube fonts

* @param None

* @retval The principle of dynamically driving the digital tube

* In the eight-digit digital tube, at any moment, only one of the digital tubes is displayed at a time, and the other seven digital tubes are

* By setting its common bit com to a high level to turn off the display, as long as the speed of switching the screen is fast enough, human vision cannot distinguish it, and it feels like eight digital tubes

* are lit at the same time. In the following dig_hc595_drive(xx,yy) function, the first parameter xx is the pin that drives the digital tube segment seg, and the second parameter yy is the driver

* The pin of the common position com of the digital tube.

**/

void Display_Drive(void)

{

switch(ucDisplayDriveStep)

{

case 1: /*Display the first position*/

ucDigShowTemp = Dig_Table[ucDigShow1];

if(ucDigDot1 == 1)

{

ucDigShowTemp = ucDigShowTemp | 0x80; /*Display decimal point*/

}

Dig_Hc595_Drive(ucDigShowTemp, 0xfe);

break;

case 2: /*Display the second position*/

ucDigShowTemp = Dig_Table[ucDigShow2];

if(ucDigDot2 == 1)

{

ucDigShowTemp = ucDigShowTemp | 0x80; /*Display decimal point*/

}

Dig_Hc595_Drive(ucDigShowTemp, 0xfd);

break;

case 3: /*Display the 3rd digit*/

ucDigShowTemp = Dig_Table[ucDigShow3];

[1] [2] [3]
Reference address:51 single chip microcomputer realizes digital tube as instrument panel to display the direction, speed and movement of the marquee

Previous article:Microcontroller learning notes————Proteus realizes virtual serial port
Next article:51 single chip microcomputer realizes simple addition calculator with digital tube display

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号