Section 88: MCU quickly intercepts valid data strings by using keywords

Publisher:MysticalWhisperLatest update time:2016-03-11 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Opening remarks:
Most of my previous serial port programs rely on time to identify whether each string of data has been received. The protocols of some serial port projects are fixed and do not require any response signals from the slave. Such projects only need to quickly identify whether the data string has been received based on specific keywords. For example, there is now an electronic scale with a measurement range of 0.00 grams to 500.00 grams. It continuously sends the current weight data to the outside through the serial port. Each string of data has a fixed length of 26 bytes. The last two bytes are carriage return and line feed characters 0x0d 0x0a. The 9th, 10th, 11th, 12th, 13th, and 14th from the bottom are valid ASCII code numbers, of which the 11th from the bottom is a fixed decimal point, and the other data can be ignored. The idea of ​​this type of serial port framework is to determine whether the data string is valid based on whether there is 0x0d 0x0a at the end of the data. Once this keyword is found, it is determined whether the total data length is equal to or greater than the fixed length of a string of data. If it is satisfied, the relevant flag is set to notify the serial port service program in the main function to process it. At the same time, the serial port interrupt is also closed in time to avoid being disturbed by the interruption of serial port data during the processing of serial port data, and it will be opened again after the serial port service program has completed the processing.
 
For details, please see the source code.
 
(1) Hardware platform:
Based on Zhu Zhaoqi 51 single-chip microcomputer learning board.
 
(2) Functions to be implemented:
The baud rate is: 9600. Display the current weight data of the electronic scale on the digital tube, and use the serial port assistant software on the computer to simulate the electronic scale to send 3 strings of data in the following format protocol. Its protocol is very simple. Each string of data has a fixed length of 26 bytes. The last two bytes are carriage return and line feed characters 0x0d 0x0a. The 9th, 10th, 11th, 12th, 13th, and 14th from the bottom are valid ASCII code numbers, of which the 11th from the bottom is a fixed decimal point, and the other data can be ignored.
(a) The characters are:
ST,GS,+ 0.77 g
Converted to hexadecimal:
20 53 54 2C 47 53 2C 2B 20 20 20 20 20 20 30 2E 37 37 20 2020 20 20 67 0D 0A
Digital tube display: 0.77
(b)
The characters are:
ST, GS, + 136.39 g
Converted to hexadecimal:
20 53 54 2C 47 53 2C 2B 20 20 20 20 31 33 36 2E 33 39 20 2020 20 20 67 0D 0A
Digital tube display: 136.39
(c)
The characters are:
ST,GS,+ 0.00 g
Converted to hexadecimal:
20 53 54 2C 47 53 2C 2B 20 20 20 20 20 20 30 2E 30 30 20 2020 20 20 67 0D 0A
Digital tube display: 0.00

(3) The source code is explained as follows:
  1. #include "REG52.H"
  2.  
  3.  
  4. #define const_rc_size 36 //Buffer array size for receiving serial port interrupt data
  5.  
  6. #define const_least_size 26 //The size of a string of standard data
  7.  
  8. void initial_myself();    
  9. void initial_peripheral();
  10. void delay_short(unsigned int uiDelayShort); 
  11. void delay_long(unsigned int uiDelaylong);
  12. //74HC595 driving the digital tube
  13. void dig_hc595_drive(unsigned char ucDigStatusTemp16_09, unsigned char ucDigStatusTemp08_01);  
  14. void display_drive(); //Drive function for displaying digital tube fonts
  15. void display_service(); //Display window menu service program
  16.  
  17. //74HC595 driving LED
  18. void hc595_drive(unsigned char ucLedStatusTemp16_09, unsigned char ucLedStatusTemp08_01);
  19.  
  20. void usart_service(void); //Serial port receiving service program, in the main function
  21. void usart_receive(void); //Serial port receive interrupt function
  22.  
  23. void T0_time(); //Timer interrupt function
  24.  
  25.  
  26. sbit dig_hc595_sh_dr=P2^0; //74HC595 program of digital tube
  27. sbit dig_hc595_st_dr=P2^1;  
  28. sbit dig_hc595_ds_dr=P2^2;  
  29.  
  30.  
  31. sbit hc595_sh_dr=P2^3; //74HC595 program for LED light
  32. sbit hc595_st_dr=P2^4;  
  33. sbit hc595_ds_dr=P2^5; 
  34.  
  35. sbit beep_dr=P2^7; //Buzzer driver IO port
  36.  
  37. sbit led_dr=P3^5; //Independent LED light
  38.  
  39.  
  40. //Common cathode digital tube character table obtained based on the schematic diagram
  41. code unsigned char dig_table[]=
  42. {
  43. 0x3f, //0 Sequence number 0
  44. 0x06, //1 Sequence number 1
  45. 0x5b, //2 Sequence number 2
  46. 0x4f, //3 Sequence number 3
  47. 0x66, //4 Sequence number 4
  48. 0x6d, //5 Sequence number 5
  49. 0x7d, //6 Sequence number 6
  50. 0x07, //7 Serial number 7
  51. 0x7f, //8 Sequence number 8
  52. 0x6f, //9 Sequence number 9
  53. 0x00, //No sequence number 10
  54. 0x40, //- Sequence number 11
  55. 0x73, //P serial number 12
  56. };
  57.  
  58.  
  59. unsigned int uiRcregTotal=0; // represents how much data the current buffer has received
  60. unsigned int uiRcregTotalTemp=0; //Intermediate variable representing how much data the current buffer has received
  61. unsigned char ucRcregBuf[const_rc_size]; //Buffer array for receiving serial port interrupt data
  62. unsigned char ucReceiveFlag=0; //Receive success flag
  63.  
  64.  
  65. unsigned char ucDigShow8; //The content to be displayed on the 8th digital tube
  66. unsigned char ucDigShow7; //The content to be displayed on the 7th digital tube
  67. unsigned char ucDigShow6; //The content to be displayed on the 6th digital tube
  68. unsigned char ucDigShow5; //The content to be displayed on the 5th digital tube
  69. unsigned char ucDigShow4; //The content to be displayed on the 4th digital tube
  70. unsigned char ucDigShow3; //The content to be displayed on the third digital tube
  71. unsigned char ucDigShow2; //The content to be displayed on the second digital tube
  72. unsigned char ucDigShow1; //The content to be displayed on the first digital tube
  73.  
  74. unsigned char ucDigDot8; //Whether the decimal point of digital tube 8 is displayed
  75. unsigned char ucDigDot7; //Whether the decimal point of digital tube 7 is displayed
  76. unsigned char ucDigDot6; //Whether the decimal point of digital tube 6 is displayed
  77. unsigned char ucDigDot5; //Whether the decimal point of digital tube 5 is displayed
  78. unsigned char ucDigDot4; //Whether the decimal point of digital tube 4 is displayed
  79. unsigned char ucDigDot3; //Whether the decimal point of digital tube 3 is displayed
  80. unsigned char ucDigDot2; //Whether the decimal point of digital tube 2 is displayed
  81. unsigned char ucDigDot1; //Whether the decimal point of digital tube 1 is displayed
  82. unsigned char ucDigShowTemp=0; //temporary intermediate variable
  83. unsigned char ucDisplayDriveStep=1; //Dynamic scanning digital tube step variable
  84.  
  85. unsigned char ucWd1Part1Update=1; //8-bit digital tube update display flag
  86.  
  87. unsigned long ulWeightCurrent=12345; //Display the current actual weight
  88.  
  89. void main() 
  90.   {
  91.    initial_myself();  
  92.    delay_long(100);   
  93.    initial_peripheral(); 
  94.    while(1)  
  95.    { 
  96.       usart_service(); //Serial port receiving service program
  97.       display_service(); //Display window menu service program
  98.    }
  99. }
  100.  
  101. /* Note 1:
  102. * This section processes serial port data based on whether the data string is valid based on whether the data tail has 0x0d 0x0a. Once this keyword is found,
  103. * Then determine whether the total data length is equal to or greater than the fixed length of a string of data. If it is satisfied, set the relevant flag to notify the main function
  104. * The serial port service program is used for processing. At the same time, the serial port interrupt is also closed in time to avoid interruption of serial port data during the processing of serial port data.
  105. * Wait until the serial port service program has finished processing before opening it.
  106. */
  107. void usart_receive(void) interrupt 4 //Serial port receives data interrupt function      
  108. {        
  109.  
  110.    if(RI==1)  
  111.    {
  112.         RI = 0;
  113.  
  114.         ++uiRcregTotal;
  115.         ucRcregBuf[uiRcregTotal-1]=SBUF; //Cache the data received by the serial port into the receiving buffer
  116.         if(uiRcregTotal>=2&&ucRcregBuf[uiRcregTotal-2]==0x0d&&ucRcregBuf[uiRcregTotal-1]==0x0a) //Once the suffix is ​​found to be 0x0d 0x0a, it will be processed and judged
  117.         {
  118.            if(uiRcregTotal
  119.            {
  120.                uiRcregTotal=0;
  121.            }
  122.            else
  123.            {
  124.                uiRcregTotalTemp=uiRcregTotal; //Pass the total received data to an intermediate variable and process the intermediate variable in the main function
  125.                ucReceiveFlag=1; //Notify the main program of successful reception
  126.                            ES=0; // Disable receiving interrupt, and enable serial port interrupt after the main function has processed the received data, to avoid interrupt interference from serial port data during processing of serial port data. 
  127.            }
  128.         }
  129.         else if(uiRcregTotal>=const_rc_size) //Exceeds the buffer
  130.         {
  131.            uiRcregTotal=0;   
  132.         }  
  133.  
  134.  
  135.      
  136.     
  137.    }
  138.    else //If it is not a serial port receiving interrupt, then it must be a serial port sending interrupt. Clear the send interrupt flag in time, otherwise the send interrupt will continue.
  139.    {
  140.         TI = 0; 
  141.    }
  142.                                                          
  143. }  
  144.  
  145. void usart_service(void) //Serial port receiving service program, in the main function
  146. {
  147.   //After adding the static keyword, this local variable will not be initialized every time the function comes in, which may reduce the time consumed by instructions.
  148.     static unsigned long ulReceiveData10000; //Defined as long type to facilitate the subsequent multiplication operation so that it will not overflow and cause errors.
  149.     static unsigned long ulReceiveData1000;
  150.     static unsigned long ulReceiveData100;
  151.     static unsigned long ulReceiveData10;
  152.     static unsigned long ulReceiveData1;
  153.  
  154.  
  155.     if(ucReceiveFlag==1) //Indicates that data has been received successfully and enters data processing and analysis
  156.     {
  157.        ulReceiveData10000=0;
  158.        ulReceiveData1000=0;
  159.        ulReceiveData100=0;
  160.        ulReceiveData10=0;
  161.        ulReceiveData1=0;
  162.  
  163. /* Note 2:
  164. * According to the protocol, the 9th, 10th, 11th, 12th, 13th, and 14th digits from the bottom are valid ASCII code numbers, and the 11th digit from the bottom is a fixed decimal point, so it is omitted.
  165. */
  166.  
  167.        if(ucRcregBuf[uiRcregTotalTemp-9]>=0x30)  
  168.        {
  169.           ulReceiveData1=ucRcregBuf[uiRcregTotalTemp-9]-0x30; //The received ASCII code number minus 0x30 becomes the actual value.
  170.        }
  171.  
  172.        if(ucRcregBuf[uiRcregTotalTemp-10]>=0x30)  
  173.        {
  174.           ulReceiveData10=ucRcregBuf[uiRcregTotalTemp-10]-0x30;
  175.           ulReceiveData10=ulReceiveData10*10;
  176.        }
  177.  
  178.        if(ucRcregBuf[uiRcregTotalTemp-12]>=0x30)  
  179.        {
  180.           ulReceiveData100=ucRcregBuf[uiRcregTotalTemp-12]-0x30;
  181.           ulReceiveData100=ulReceiveData100*100;
  182.        }
  183.  
  184.        if(ucRcregBuf[uiRcregTotalTemp-13]>=0x30)  
  185.        {
  186.           ulReceiveData1000=ucRcregBuf[uiRcregTotalTemp-13]-0x30;
  187.           ulReceiveData1000=ulReceiveData1000*1000;
  188.        }
  189.  
  190.        if(ucRcregBuf[uiRcregTotalTemp-14]>=0x30)  
  191.        {
  192.           ulReceiveData10000=ucRcregBuf[uiRcregTotalTemp-14]-0x30;
  193.           ulReceiveData10000=ulReceiveData10000*10000;
  194.        }
  195.  
  196.  
  197.        ulWeightCurrent=ulReceiveData10000+ulReceiveData1000+ulReceiveData100+ulReceiveData10+ulReceiveData1;
  198.        ucWd1Part1Update=1; //Update display
  199.  
  200.        uiRcregTotalTemp=0; //Clear the intermediate variable of the actual number of bytes received
  201.        uiRcregTotal=0; // Clear the number of bytes actually received
  202.        ucReceiveFlag=0; //Clear completion flag
  203.  
  204.            ES = 1; // Enable receive interrupt
  205.     }            
  206. }
  207.  
  208.  
  209.  
  210. void display_service() //Display window menu service program
  211. {
  212.   //After adding the static keyword, this local variable will not be initialized every time the function comes in, which may reduce the time consumed by instructions.
  213.             static unsigned char ucTemp5; //intermediate transition variable
  214.             static unsigned char ucTemp4; //intermediate transition variable
  215.             static unsigned char ucTemp3; //intermediate transition variable
  216.             static unsigned char ucTemp2; //intermediate transition variable
  217.             static unsigned char ucTemp1; //intermediate transition variable
  218.  
  219.  
  220.             if(ucWd1Part1Update==1) //Update display
  221.             {
  222.                ucWd1Part1Update=0; //Clear the flag in time to avoid continuous scanning
  223.  
  224.               //First decompose the data to display each bit
  225.                ucTemp5=ulWeightCurrent%100000/10000;  
  226.                ucTemp4=ulWeightCurrent%10000/1000;     
  227.                ucTemp3=ulWeightCurrent%1000/100;
  228.                ucTemp2=ulWeightCurrent%100/10;
  229.                ucTemp1=ulWeightCurrent%10;
  230.  
  231.                ucDigDot3=1; //Display the decimal point of the 3rd digital tube, the actual data has 2 decimal points.
  232.  
  233.                ucDigShow8=10; //The 8th digital tube is not used, so the display is none. 10 means the display is empty.
  234.                ucDigShow7=10; //The 7th digital tube is not used, so the display is none. 10 means the display is empty.
  235.                ucDigShow6=10; //The sixth digital tube is not used, so the display is none. 10 means the display is empty.
  236.  
  237.                if(ulWeightCurrent<10000)   
  238.                {
  239.                   ucDigShow5=10; //If it is less than 1000, the thousands place will be displayed
  240.                }
  241.                else
  242.                {
  243.                   ucDigShow5=ucTemp5; //The content to be displayed on the 5th digital tube
  244.                }
  245.   
  246.  
  247.                if(ulWeightCurrent<1000)   
  248.                {
  249.                   ucDigShow4=10; //If less than 1000, thousands will be displayed
  250.                }
  251.                else
  252.                {
  253.                   ucDigShow4=ucTemp4; //The content to be displayed on the 4th digital tube
  254.                }
  255.  
  256.                 // Because there are 2 decimal places, the first 3 digits are valid numbers and must be displayed. Do not judge and remove the blank display processing of 0.
  257.                 ucDigShow3=ucTemp3; //The content to be displayed on the third digital tube
  258.                 ucDigShow2=ucTemp2; //The content to be displayed on the second digital tube
  259.                 ucDigShow1=ucTemp1; //The content to be displayed on the first digital tube
  260.             }
  261.  
  262.      
  263. }
  264.  
  265.  
  266.  
  267.  
  268. void display_drive()  
  269. {
  270.    // The following program can also compress the capacity if some arrays and shifted elements are added. However, what Hongge pursues is not capacity, but clear explanation ideas.
  271.    switch(ucDisplayDriveStep)
  272.    { 
  273.       case 1: //Display the first position
  274.            ucDigShowTemp=dig_table[ucDigShow1];
  275.                    if(ucDigDot1==1)
  276.                    {
  277.                       ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point
  278.                    }
  279.            dig_hc595_drive(ucDigShowTemp,0xfe);
  280.                break;
  281.       case 2: //Display the second digit
  282.            ucDigShowTemp=dig_table[ucDigShow2];
  283.                    if(ucDigDot2==1)
  284.                    {
  285.                       ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point
  286.                    }
  287.            dig_hc595_drive(ucDigShowTemp,0xfd);
  288.                break;
  289.       case 3: //Display the third digit
  290.            ucDigShowTemp=dig_table[ucDigShow3];
  291.                    if(ucDigDot3==1)
  292.                    {
  293.                       ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point
  294.                    }
  295.            dig_hc595_drive(ucDigShowTemp,0xfb);
  296.                break;
  297.       case 4: //Display the 4th digit
  298.            ucDigShowTemp=dig_table[ucDigShow4];
  299.                    if(ucDigDot4==1)
  300.                    {
  301.                       ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point
  302.                    }
  303.            dig_hc595_drive(ucDigShowTemp,0xf7);
  304.                break;
  305.       case 5: //Display the 5th digit
  306.            ucDigShowTemp=dig_table[ucDigShow5];
  307.                    if(ucDigDot5==1)
  308.                    {
  309.                       ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point
  310.                    }
  311.            dig_hc595_drive(ucDigShowTemp,0xef);
  312.                break;
  313.       case 6: //Display the 6th digit
  314.            ucDigShowTemp=dig_table[ucDigShow6];
  315.                    if(ucDigDot6==1)
  316.                    {
  317.                       ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point
  318.                    }
  319.            dig_hc595_drive(ucDigShowTemp,0xdf);
  320.                break;
  321.       case 7: //Display the 7th position
  322.            ucDigShowTemp=dig_table[ucDigShow7];
  323.                    if(ucDigDot7==1)
  324.                    {
  325.                       ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point
  326.            }
  327.            dig_hc595_drive(ucDigShowTemp,0xbf);
  328.                break;
  329.       case 8: //Display the 8th digit
  330.            ucDigShowTemp=dig_table[ucDigShow8];
  331.                    if(ucDigDot8==1)
  332.                    {
  333.                       ucDigShowTemp=ucDigShowTemp|0x80; //Display decimal point
  334.                    }
  335.            dig_hc595_drive(ucDigShowTemp,0x7f);
  336.                break;
  337.    }
  338.    ucDisplayDriveStep++;
  339.    if(ucDisplayDriveStep>8) //After scanning 8 digital tubes, restart scanning from the first one
  340.    {
  341.      ucDisplayDriveStep=1;
  342.    }
  343.  
  344. }
  345.  
  346. //74HC595 driver function of digital tube
  347. void dig_hc595_drive(unsigned char ucDigStatusTemp16_09, unsigned char ucDigStatusTemp08_01)
  348. {
  349.    unsigned char i;
  350.    unsigned char ucTempData;
  351.    dig_hc595_sh_dr=0;
  352.    dig_hc595_st_dr=0;
  353.    ucTempData=ucDigStatusTemp16_09; //Send the high 8 bits first
  354.    for(i=0;i<8;i++)
  355.    { 
  356.          if(ucTempData>=0x80)dig_hc595_ds_dr=1;
  357.          else dig_hc595_ds_dr=0;
  358.          dig_hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register
  359.          delay_short(1); 
  360.          dig_hc595_sh_dr=1;
  361.          delay_short(1);
  362.          ucTempData=ucTempData<<1;
  363.    }
  364.    ucTempData=ucDigStatusTemp08_01; //Send the lower 8 bits first
  365.    for(i=0;i<8;i++)
  366.    { 
  367.          if(ucTempData>=0x80)dig_hc595_ds_dr=1;
  368.          else dig_hc595_ds_dr=0;
  369.          dig_hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register
  370.          delay_short(1); 
  371.          dig_hc595_sh_dr=1;
  372.          delay_short(1);
  373.          ucTempData=ucTempData<<1;
  374.    }
  375.    dig_hc595_st_dr=0; //The ST pin updates the data of the two registers and outputs them to the output pin of 74HC595 and latches them
  376.    delay_short(1); 
  377.    dig_hc595_st_dr=1;
  378.    delay_short(1);
  379.    dig_hc595_sh_dr=0; //Pull low to enhance anti-interference
  380.    dig_hc595_st_dr=0;
  381.    dig_hc595_ds_dr=0;
  382. }
  383.  
  384. //74HC595 driver function for LED lamp
  385. void hc595_drive(unsigned char ucLedStatusTemp16_09, unsigned char ucLedStatusTemp08_01)
  386. {
  387.    unsigned char i;
  388.    unsigned char ucTempData;
  389.    hc595_sh_dr=0;
  390.    hc595_st_dr=0;
  391.    ucTempData=ucLedStatusTemp16_09; //Send the high 8 bits first
  392.    for(i=0;i<8;i++)
  393.    { 
  394.          if(ucTempData>=0x80)hc595_ds_dr=1;
  395.          else hc595_ds_dr=0;
  396.          hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register
  397.          delay_short(1); 
  398.          hc595_sh_dr=1;
  399.          delay_short(1);
  400.          ucTempData=ucTempData<<1;
  401.    }
  402.    ucTempData=ucLedStatusTemp08_01; //Send the lower 8 bits first
  403.    for(i=0;i<8;i++)
  404.    { 
  405.          if(ucTempData>=0x80)hc595_ds_dr=1;
  406.          else hc595_ds_dr=0;
  407.          hc595_sh_dr=0; //The rising edge of the SH pin sends data to the register
  408.          delay_short(1); 
  409.          hc595_sh_dr=1;
  410.          delay_short(1);
  411.          ucTempData=ucTempData<<1;
  412.    }
  413.    hc595_st_dr=0; //The ST pin updates the data of the two registers and outputs them to the output pin of 74HC595 and latches them
  414.    delay_short(1); 
  415.    hc595_st_dr=1;
  416.    delay_short(1);
  417.    hc595_sh_dr=0; //Pull low to enhance anti-interference
  418.    hc595_st_dr=0;
  419.    hc595_ds_dr=0;
  420. }
  421.  
  422.  
  423.  
  424. void T0_time() interrupt 1
  425. {
  426.   TF0=0; //Clear interrupt flag
  427.   TR0=0; //Disable interrupt
  428.  
  429.  
  430.   display_drive(); //Drive function of digital tube font
  431.  
  432.   TH0=0xfe; //Reinstall initial value (65535-500)=65035=0xfe0b
  433.   TL0=0x0b;
  434.   TR0=1; //Open interrupt
  435. }
  436.  
  437.  
  438. void delay_short(unsigned int uiDelayShort) 
  439. {
  440.    unsigned int i;  
  441.    for(i=0;i
  442.    {
  443.      ; //A semicolon is equivalent to executing an empty statement
  444.    }
  445. }
  446.  
  447. void delay_long(unsigned int uiDelayLong)
  448. {
  449.    unsigned int i;
  450.    unsigned int j;
  451.    for(i=0;i
  452.    {
  453.       for(j=0;j<500;j++) //Number of empty instructions in the embedded loop
  454.           {
  455.              ; //A semicolon is equivalent to executing an empty statement
  456.           }
  457.    }
  458. }
  459.  
  460. void initial_myself() //Initialize the MCU in the first zone
  461. {
  462.  
  463.   beep_dr=1; //Use PNP transistor to control the buzzer, it will not sound when the output is high level.
  464.   led_dr=0; //Turn off the independent LED light 
  465.   hc595_drive(0x00,0x00); //Turn off all LED lights driven by the other two 74HC595
  466.  
  467.   TMOD=0x01; //Set timer 0 to working mode 1
  468.   TH0=0xfe; //Reinstall initial value (65535-500)=65035=0xfe0b
  469.   TL0=0x0b;
  470.  
  471.   //Configure the serial port
  472.   SCON=0x50;
  473.   TMOD=0X21;
  474.  
  475. /* Comment 3:
  476. * In order to ensure that the data received by the serial port interrupt is not lost, you must set IP = 0x10, which is equivalent to setting the serial port interrupt to the highest priority.
  477. * At this time, the serial port interrupt can interrupt any other interrupt service function to achieve nesting,
  478. */
  479.   IP =0x10; //Set the serial port interrupt to the highest priority, required.
  480.  
  481.   TH1=TL1=-(11059200L/12/32/9600); //The serial port baud rate is 9600.
  482.   TR1=1;
  483. }
  484.  
  485. void initial_peripheral() // Initialize the periphery of the second zone
  486. {
  487.  
  488.    ucDigDot8=0; //Initialize all decimal points without displaying them
  489.    ucDigDot7=0;  
  490.    ucDigDot6=0; 
  491.    ucDigDot5=0;  
  492.    ucDigDot4=0; 
  493.    ucDigDot3=0;  
  494.    ucDigDot2=0;
  495.    ucDigDot1=0;
  496.  
  497.    EA=1; //Open the general interrupt
  498.    ES=1; //Enable serial port interrupt
  499.    ET0=1; //Enable timer interrupt
  500.    TR0=1; //Start timing interrupt
  501. }
  502.  
 
Closing remarks:
    I have talked about the clock program made with ds1302 in Section 48, but later many netizens suggested that in order to make it easier for beginners to learn programming ideas, I should use the microcontroller timer to make a clock program. Therefore, I decided to talk about this in the next section. For more details, please listen to the next analysis - using the internal timer of the microcontroller to make a clock.
Keywords:MCU Reference address:Section 88: MCU quickly intercepts valid data strings by using keywords

Previous article:Section 89: Using the internal timer of a microcontroller to make a clock
Next article:Section 87: Source code of industrial control project donated by Zheng Wenxian

Recommended ReadingLatest update time:2024-11-15 15:42

Design of Hall Effect Water Flow Meter Based on 51 Single Chip Microcomputer
1. Development preparation (1) One YF-B1 flow sensor (2) One 51 development board   2. Basic knowledge (1) The YF-B1 flow sensor has only three wires. They are the data line, VCC, and GND. The data line outputs a square wave with a duty cycle of 50%. When water flows through the water flow rotor assembly, the magne
[Microcontroller]
Some Experiences in 51 Single Chip Microcomputer Development
1. The default level of the MCU IO port is high after power-on. (P1, P2, and P3 ports are internally pulled up. If the P0 port is not connected to a pull-up resistor, the default is high impedance.) 2. The operating frequency of the microcontroller = crystal oscillator frequency/12, that is, using a 12MHz crystal
[Microcontroller]
[MCU Notes] STM8S series MCU FLASH operation
    1. Modify the comments of stm8s_conf.h to allow #include "stm8s_flash.h" to compile     2. Modify the injection of stm8s.h to enable the Flash function to run from RAM #if !defined (RAM_EXECUTION)     #define RAM_EXECUTION (1) // When writing to a block, you must enable injection here #endif /* RAM_EXECUTION *
[Microcontroller]
51 MCU timer and interrupt
Timer, Interrupt ############################## EX0(IE.0), external interrupt 0 enable bit; ET0(IE.1), timer/counter T0 interrupt enable bit;  EX1(IE.2), external interrupt 0 enable bit; ET1(IE.3), timer/counter T1 interrupt enable bit; ES(IE.4), serial port interrupt enable bit; EA (IE.7), CPU interrupt enable
[Microcontroller]
Detailed analysis of CY and OV in MCU
CY (Carry): Used to indicate the carry in addition and the borrow in subtraction. If there is a carry in addition or a borrow in subtraction, the CY position is 1, otherwise it is 0 OV: Indicates whether overflow occurs during the operation. If the result of the operation exceeds the range of data that can be repres
[Microcontroller]
Detailed analysis of CY and OV in MCU
Design of timing acquisition and storage system based on microcontroller C8051F021 and clock chip
With the rapid development of science and technology, many applications in the field of modern industrial measurement and control require the regular collection and storage of large amounts of data. Taking the ocean current data acquisition and storage interface circuit designed for the ocean current meter as an examp
[Microcontroller]
Design of timing acquisition and storage system based on microcontroller C8051F021 and clock chip
Detailed analysis of the types of various IO ports of the microcontroller
Open collector The output structure is shown in Figure 1. The collector of the transistor on the right is not connected to anything, so it is called an open collector. The transistor on the left is used for inversion, so that when the input is "0", the output is also "0". For Figure 1, when the input on the left end
[Microcontroller]
51 MCU pull-up resistor
1. In order to achieve quasi-3-state, the P0 port of 51 single-chip microcomputer adopts OC output, that is, collector floating output, also called totem pole output. This circuit structure has only pull-down capability, and there is no current in high-level output, which shows high-impedance state at high level; with
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号