Recently, the company has to modify the smart car. I am very ashamed of myself. I have been studying electronics for four years in college, and I have never played single-chip computers twice in the end.
We plan to spend some time playing single-chip microcomputer and start with the simplest led.
The development environment of this series of learning notes is:
Development Board: hc800em3 is a very good development board with many external interfaces. The Board also provides a "conversion interface" between single-chip microcomputer and arm ".
Single-Chip Microcomputer model: stc90c516rd +
Arm: stm32
Development Tool: Keil 3 (Single-Chip)
I. Schematic diagram
As there are many tutorials on the Internet, the use of Keil and the circuit analysis of the Board are not described in detail. (Fortunately, the schematic diagram of the board can still be understood, and we need to make up for it in the future ~).
It is easy to understand, set 1 pin to 0, the lamp D8 will be on.
Ii. MCU latencyProgramSuperficial Analysis
In order to make the LED light have a variety of effects (such as flashing), it is necessary to use a delay program. Some netizens said that the delay program of single-chip microcomputer is not accurate, and it is recommended to use a timer. But after all, it's an entry. Study the timer later.
The following program is a C program with a 1 ms delay at a 12 MHz crystal source.
Void delay_ms (void) {unsigned char a, B, c; For (C = 1; C> 0; c --) for (B = 142; B> 0; B --) for (A = 2; a> 0; --);}
1. machine cycle
A machine cycle contains 12 clock cycles. The formula for calculating the clock cycle is (1/crystal oscillator frequency) seconds.
For example, the crystal oscillator is 12 MHz.
The Single-Chip Microcomputer Clock cycle = (1/12) microseconds, thus the machine cycle can be 1 us.
Here, the machine cycle can be understood as the instruction cycle, that is, the time it takes for the microcontroller to execute an instruction. Note: The commands here are not in C language, and machine commands can be viewed through disassembly.
2. Disassembly
We need to calculate the execution time of a program. After all, we can view its corresponding disassembly.Code. The disassembly code of the above program is attached here. Keil can call up the disassembly code display window.
54: void delay (void) 55: {56: unsigned char a, B, c; 57: For (C = 1; C> 0; c --) C: 0x0045 7f01 mov R7, #0x01 58: for (B = 142; B> 0; B --) C: 0x0047 7e8e mov R6, # 0x8e 59: for (A = 2; a> 0; A --); C: 0x0049 7d02 mov R5, # 0x02c: 0x004b ddfe djnz R5, C: 004bc: 0x004d DEFA djnz R6, c: 0049c: 0x004f dff6 djnz R7, C: 0047 60:} C: 0x0051 22 RET
3. disassembly Analysis
The machine cycle corresponding to each instruction of delay_ms () is listed below.
The command periodic table of 8051 is attached here.
Command machine cycle C: 0x0045 7f01 mov R7, # 0x011c: 0x0047 7e8e mov R6, # 0x8e1c: 0x0049 7d02 mov R5, # 0x021c: 0x004b ddfe djnz R5, c: 004b2c: 0x004d DEFA djnz R6, C: 00366c: 0x004f dff6 djnz R7, C: 00472c: 0x0051 22 RET 2
Next we will calculate how much time the program will take. (If you do not understand assembly instructions, you can view the 8051 instruction periodic table)
Djnz R5, C: 004b, the number of machine cycles used to execute this cycle is 2*2 = 4.
From the cycle of the third instruction to the fifth instruction, the number of machine cycles used for executing the cycle is (1 + 2*2 + 2) * 142
From the cycle of the second command to the sixth command, the number of machine cycles used to execute the cycle is (1 + (1 + 2*2 + 2) * 142 + 2) * 1
Delay_time = 1 + (1 + (1 + 2*2 + 2) * 142 + 2) * 1 + 2 seconds
A good Program Computing Software for latency is provided on the Internet. MCU genie Software
3. Main Program
int main (void) {P2 = 0xfe; while (1) {delay_ms (100); // delay 100msp2 ^ = (1 <0); // returns 0 ;} the execution result of the program is that D1 flashes once every Ms
Interested friends, you can modify the main program to display different effects on LED lights. Very interesting single-chip microcomputer ~~ This is what we wrote today.
the above is purely a personal study note. If something is wrong, I hope to raise it. We hope to study and make progress together. My email address is: xzy@yingzhi8.com