Microcontroller Programming Experience

Publisher:快乐奇迹Latest update time:2015-07-09 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Processing of delay program:

  1. For the single-chip microcomputer that can set the system clock, the delay program uniformly sets the system clock frequency to the lowest first, and then writes the delay
     function according to this lowest clock frequency. The advantages of this are: first, the delay under different system clocks is unified; second, power consumption is reduced.
  2. For the single-chip microcomputer system, different external clocks have different corresponding times for the delay program. For ease of use, add the pre-compilation instructions #ifdef/#else/#
     endif. In this way, if a system clock is pre-defined, the corresponding delay parameters are selected for compilation, and the main body of the delay program remains unchanged, but there is
     a delay parameter in it, and the corresponding parameters are selected according to the pre-definition.

2. For loop processing:
   Use for(i=XX,i>0,i--) instead of for(i=0;i    For two or more layers of for loops, frequent loops are placed in the inner layer to reduce the time spent on jumping between loops.

3. Operation of peripherals: You can operate the peripherals as a file. For example, you can treat the LCD as a file. To print strings
   or , you can use fprintf() (51's C standard library does not support it, but you can write a file operation library yourself). As long as it is an input and output
   device, try to operate it with file operations.


4. Use of standard library functions:
  1. Standard input and output library (stdio.h):
   When a character string mixed with variable numbers (for example, send the nihao character string, and then send the value of the variable i) is sent to the LCD display or to the serial port, the format output function printf("nihao%d",i) can be used to
   send port; but it is not easy to display on the LCD. Usually, a function of converting numbers to strings is written,
   and then the original string is sent first, and then the string after digital conversion is sent. This is too troublesome. You can use the sprintf() function in the standard input and output library to complete it. Its
   prototype is: sprintf(char *buffer, const char *format, …), *buffer is the buffer to which the string and variable are written. You can
   use an array or a pointer. The subsequent format is the same as the printf() function; the corresponding function is the sscanf() function, which reads a string from the buffer
   , converts it to the corresponding type, and then assigns it to the specified variable.
   For example:
    #include
    void PrintToLcd(unsigned char *str)
    {
        ...
    }
    void main()
    {
        unsigned char *p;
        unsigned char i = 50;
        sprintf(p,"nihao%d",i);
        PrintToLcd(p);
    }
  2. String library (string.h)
    string concatenation (addition) char *strcat(char *dest,const char *stc)
   connects src to the end of the dest string and returns a pointer to dest
    String comparison int strcmp(char *str1,char *str2)
   Return value: less than 0: str1str2
    String copy 1 char *strcpy(char *dest,const char *src)
   Result Copy the contents of src into dest, the contents of the two strings are the same, and return a pointer to dest
    String copy 2 char *strdup(const char *src)
   src: source string to be copied, return value: pointer to the copied string
    String reversal char *strrev(char *s);
   returns a pointer to the reversed string
  3. Type conversion (math.h; stdlib.h)
   String to double precision (similar to StrToDouble in C++Builder) double atof(char *str)
   String to integer (similar to StrToInt) int atoi(char *str)
   String to long integer long atol(char *str)
   Floating point number to string char *ecvt(double value,int ndigit,int*dec,int *sign)
                   char *fcvt(double value,int ndigit,int*dec,int *sign)
    Input parameters: value: floating point number to be converted, ndigit: length of the converted string
    Output parameters: dec: decimal point position, sign: sign
    Returns the converted string pointer
   Integer to string char *itoa(int value,char *string,int radix)
    Input parameters: value: number to be converted, radix: Converted binary
    output parameter: string: The converted string
    returns a pointer to string.
   Long integer to string conversion char *ltoa(long value,char *string,int radix)

Keywords:MCU Reference address:Microcontroller Programming Experience

Previous article:MCU C language program (key pressing and debounce)
Next article:STC12C5A60S2 MCU Dual Serial Port Communication

Recommended ReadingLatest update time:2024-11-16 18:05

Realization of Audio Processing System Based on I2C Serial Bus Using Single Chip Microcomputer
At present, the diversified functions of consumer electronic products (such as color TVs, stereos, etc.) make the control circuit more complicated. The I2CBUS (Inter ICBUS) bus introduced by Philips is the most concise, effective and widely used bus format among many buses. The I2C bus is usually implemented in hard
[Industrial Control]
Realization of Audio Processing System Based on I2C Serial Bus Using Single Chip Microcomputer
Tips for Automotive Embedded System Developers to Choose the Right MCU Architecture
MCU is the basis of electronic control module (ECM), which is widely used in security system, comfort driving system, chassis system and driving information system. In the automotive electronics industry, the use of 8-bit and 16-bit microcontrollers (MCU) is still expanding. In the automotive electronic systems of u
[Microcontroller]
Homemade MCU XI…Analog-to-digital conversion IC ADC0809
Our focus is on the actual production, so I won’t go into too much detail and will only talk about the most refined knowledge related to production.    ADC0809 is a conversion IC that can convert the analog voltage signal we want to measure into a digital quantity so that it can be stored or displayed. The following
[Microcontroller]
A practical solution for high-precision measurement of displacement devices using microcontrollers and FPGAs
Displacement sensors are widely used in industrial and control fields, such as process detection, physical measurement and automatic control. Due to its low measurement accuracy, it often cannot meet social needs and also limits the application of sensors. Therefore, a displacement measurement device based on microcon
[Power Management]
A practical solution for high-precision measurement of displacement devices using microcontrollers and FPGAs
Design of ICSP Interface Circuit for PIC Microcontroller
ICSP interface circuit In-circuit serial programming (ICSP) is one of the features of PIC microcontrollers. It can burn programs directly into the microcontroller and perform in-circuit serial programming and debugging on the microcontroller. The ICSP interface circuit has only five wires, which are: VPP, VDD, VSS,
[Microcontroller]
Design of ICSP Interface Circuit for PIC Microcontroller
Analysis of two low-power power-saving working modes of AT89S51 microcontroller
AT89S51 has two low-power consumption power-saving operating modes: Idle Mode and PowerDown Mode. The purpose is to reduce system power consumption as much as possible. In the power-down retentive mode, VCC can be powered by the backup power supply. Figure 2-22 shows the internal control circuits of the two low-power
[Microcontroller]
Analysis of two low-power power-saving working modes of AT89S51 microcontroller
Using the MCU serial port to expand 16 LEDs
Title: Use the serial port of a single-chip microcomputer to expand 16 light-emitting diodes. Draw a circuit diagram and write a program to make the 16 light-emitting diodes emit light in different sequences (the time interval between light-emitting diodes is 1s). Answer:  MOV SCON,#00H CLR P1.1 MOV R7,#16 MOV
[Microcontroller]
Using the MCU serial port to expand 16 LEDs
38-Simple fan control based on 51 single chip microcomputer
Specific implementation functions The system consists of STC89C52 single-chip microcomputer + single digital tube + LED indicator light + LM298N chip + DC motor. Specific functions: (1) Simulate the rotation and shaking of the fan; (2) Use buttons to set the wind speed to strong (1st gear), medium (2nd gear), an
[Microcontroller]
38-Simple fan control based on 51 single chip microcomputer
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号