I. Common Questions
1. The header files reg51.h and reg52.h are actually the same, and you can use both.
2. The void before main() can be added or not, anyway, they are all functions without return value.
3. Not every program needs to use the infinite loop while(1). For example, it is not necessary to light up a small LED light. As long as it is executed once, it will always be at a high level without looping.
4. Both bit operations and byte operations can be used to control the output level of the I/O port. Bit operations require the declaration of sbit, while byte operations do not.
5. There are two types of delay programs: a: loop delay; b: timer precise delay
6. If the defined variable is not initialized, the default initial value is zero.
7. When defining an array (such as: number[]={1, 2, 3, 4, 5, 6, };), a semicolon must be added at the end, and each element is separated by a comma. The number of elements can be added or not in [].
8. When the program is more complex, the program can be modularized, defined before the main function, and referenced in the main function, but pay attention to the difference between global variables and local variables.
9. Some arrays are preceded by code, which means that the array is written into FLASH, and the array content remains unchanged. If no code is added
, it is written into RAM. 10. Can P0^0 be assigned directly without bit operation? No, because there is an XOR symbol in P0^0, so bit operation must be performed before normal assignment.
11. The serial chip PL2303 is a domestic chip. The maximum baud rate during online simulation is 14400. Too high will cause program download failure.
12. The STC89C52 program storage space is 8K FLASH . When downloading Hex files, some students may notice that the file exceeds 8K and think that the storage space is not enough. This statement is wrong. The size of the space occupied by the program should be in the keil software. The number of bytes after code . 2. Header file
1. If the header file is not in the keil software library, you need to write it yourself and add it to the project, or directly copy a copy to the keil software C51——>INC folder, so that you can directly reference the header file. 3. Variables
Static variables are often used when modularizing functions.
4. AND &, OR |
Sometimes you will see such programming language in the program. A&=b; is equivalent to: a=a&b
Similarly, a|=b; is equivalent to: a=a|b
5. Interrupt program example: stopwatch
#include
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0, //code is written to flash
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xa7,0xa1,0x86,0x8e};
uint8 a[6]; //No code before, it is written to ram
uint16 counter = 0;
sbit ENLED = P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
{
TMOD |= 0x10; //Select timer mode
TMOD &= 0xdf;
TH1 = 0xFC; //Initial value of fixed time
TL1 = 0x67;
TR1 = 1; //Start timing
}
void int_init() //Interrupt function
{
ET1 = 1; //Select interrupt 1
EA = 1; //Enable interrupt function
}
void refresh_led() //Refresh function
{
static uint8 j = 0; //Static variable, the first definition is valid and the entire function retains
switch(j)
{
case 0: ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0 = number[a[0]];break;
case 1: ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;j++;P0 = number[a[1]];break;
case 2: ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;j++;P0 = number[a[2]];break;
case 3: ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;j++;P0 = number[a[3]];break;
case 4: ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;j++;P0 = number[a[4]];break;
case 5: ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;j = 0;P0 = number[a[5]];break;
default: break;
}
}
{
ENLED = 0; ADDR3 = 1; //Set the state of the 38 decoder
timer1_init(); //Timer function
int_init(); //Interrupt on
while(1); //Infinite loop to ensure that the program is in the running state
}
void interrupt_timer1() interrupt 3 //Interrupt function, each time the timer overflows, it will trigger an interrupt
{
static uint32 sec = 0;
TH1 = 0xFC;
TL1 = 0x67;
counter++;
if(counter == 1000)
{
sec++;
counter = 0;
a[0] = sec%10;
a[1] = sec/10%10;
a[2] = sec/100%10;
a[3] = sec/1000%10;
a[4] = sec/10000%10;
a[5] = sec/100000%10;
}
refresh_led();
}
VI. Writing header files
The format is: #ifndef _KEY_H_
#define _KEY_H_
…
#endif
The written header file needs to be placed under the project where the Keil software is located.
Previous article:Detailed steps for using keil software
Next article:Chip design: modelsim do script
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- Installation and sound source localization algorithm of a diamond microphone array
- How to start the design of adjustable voltage regulated power supply application
- Has anyone used the AT32F403 microcontroller? Is there any example program or something?
- A detail of MSP430 interrupt
- The upper limit for each mobile phone is US$2.5. How much 5G patent fees can Huawei collect?
- Schematic diagram of one-input and eight-output terminal block
- Tell us what information you need and we will serve you
- Why do we need to add an external EEPROM when using some MCUs?
- Tony teacher perfectly supports esp32-c3
- [Domestic RISC-V Linux Board Fang·Starlight VisionFive Trial Report] Unboxing