I have designed a single-chip microcomputer, which needs to be implemented using both C language and assembly language. Now I write down my feelings and gains from this design, as well as the problems I encountered. Friends who are interested are welcome to exchange ideas and make suggestions.
Single chip microcomputer design: 99 code table design based on 51 single chip microcomputer
Software environment: Proteus8.0 + Keil4
Requirements: 1. Press the switch once, and the digital tube starts timing. 2. Press it twice, and the digital tube display is still. 3. Press it three times, and the digital tube value is reset to zero.
The C language program is as follows:
#include
#define uint unsigned int
#define uchar unsigned char
uchar shi,ge,aa,keycount=0,temp;
sbit anjian=P1^7;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void display(shi,ge);
void key ();
void init();
void delay(uint z);
/*-----Main program-----*/
void main()
{
init(); //initialization
while(1)
{
key();
if(keycount==1)
TR0=1; //Open interrupt
if(keycount==2)
TR0=0;
if(keycount==3)
{
temp=0;
keycount=0;
}
if(aa==10){aa=0;
if(temp<=99)
{
temp++;display(shi,ge);
}
else
temp=0;}
}
}
/*------Initialization procedure-------*/
void init()
{
keycount=0;
temp=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
//TR0=0;
}
/*-----Timer Interrupt-----*/
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
}
/*-----Display subroutine-----*/
void display(shi,ge)
{
shi=temp/10;
ge=temp%10;
P0=table[shi];;delay(70);
P2=table[ge]; ;delay(70);
}
/*-----Key detection subroutine-----*/
void key ()
{
if(anjian==0)
{
delay(5); //debounce
if(anjian==0)
keycount++;
}
//while(anjian==0);
//display(shi,ge); //Wait for the button to pop up
}
/*-----Delay subroutine-----*/
void delay(uint z) //delay about 1ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
The circuit simulation results are as follows:
Okay, then let's start the journey from C language to assembly language^_^
(1) Change lines 1-10 of the C language to
ORG 0000H //Assembly start pseudo instruction, which specifies the starting address of the source program or data block in the program memory
ajmp STAR //ajmp unconditional jump instruction
ORG 000bh
ajmp timer0
anjian equ P1.7 //bit definition
keycount equal 40h
shi equ 41h
42h
aa equ 43h
Temp 44h
tab: db 3fh,6h,5bh,4fh,66h //Create table
db 6dh,7dh,7h,7fh,6fh
(2) Change lines 12-14 and 39-49 of the initialization function in C language to
1 STAR: 2 acall init //Subroutine short-range call instruction, the function is that the main program calls the subroutine, the range of calling the subroutine is 2kb
init:
mov keycount,#0 //keycount=0
mov temp,#0 //temp=1
mov tmod,#01h //TMOD=0x01
mov TH0,#60
mov TL0,#176
setb EA //position instruction, set the bit indicated by the operand to 1
setb ET0
setb TR0
ret
acall is a subroutine short-range call instruction, and ret is used to return.
(3) Lines 15-35 in C language are a while loop with complicated logic. Pay attention!
START:
acall display
inc temp //add 1 instruction, add 1 to the content of the unit or register specified by the operand
acall delay70 // short-range call delay70
x8: mov r0,keycount
cjne r0,#2,F1 //cjne comparison jump instruction, if r0=2, jump to x8, otherwise jump to F1.
ajmp x8
F1: mov r0,temp
cjne r0,#99,START //If r0<99, repeat the loop, otherwise temp=0
mov temp,#0
ajmp START
F9:
acall key
mov r0,keycount
cjne r0,#0,F2 //keycount=0 execute sequentially, otherwise jump to F1
CLR P1.3 //Clear
SETB TR0
F2: mov r0,keycount //Second key press
cjne r0,#2,F2
clr TR0
reti
mov r0,keycount // third key press
cjne r0,#3,F3
mov temp,#0
mov keycount,#0
The inc increment instruction adds 1 to the content of the unit or register specified by the operand, and returns the result back to the original operand unit.
The clr bit is reset, and its function is to clear the bit indicated by the operand to "0".
Or in the interrupt function
timer0:
w1:
acall key
mov TH0,#60
mov TL0,#176
cpl p1.0
JB keycount,x2
ajmp x3
x2:
ajmp START
clr p1.0
ajmp w1
ajmp w1
x3: mov r0,keycount
cjne r0,#3,w1 //If r0=3, execute sequentially, otherwise jump to w1
mov temp,#0
mov keycount,#0
ret
(4) Change the display function in lines 58-64 of the C language to
display:
mov a,temp
mov b,#10
div ab //Division instruction, implements the division operation of two eight-bit unsigned numbers.
mov r2,A
mov r3,B
mov dptr,#tab //16-bit data transfer usage
mov a,r2
movc a,@a+dptr //Look up the table, first add the contents of accumulator A to the contents of data pointer register DPTR, then use the result as the address and send the result of the address to A
mov P0,a
acall delay70
nop //empty instruction
mov a,r3
movc a,@a+dptr
mov P2,a
nop
acall delay70
ret
Div is a division instruction, which is used to implement the division operation of two 8-bit unsigned numbers. Generally, the dividend is placed in accumulator A and the divisor is placed in register B. After the instruction is executed, the quotient is placed in A and the remainder is placed in B.
movc is a table lookup instruction. It first adds the contents of the accumulator A to the contents of the data pointer register DPTR, and then uses the result as the address to send the contents of the address into A.
nop is a no-operation instruction. It does not perform any operation, but takes up one machine cycle (i.e. 12 oscillation cycles) and is often used for delay or waiting. (The effect of some program execution cannot be clearly recognized by the human eye due to the short delay time)
The purpose of this program is to display a two-digit number on a digital tube at the tens place and a digital tube at the ones place.
(5) The key function in lines 66-76 of the C language is changed to
1 key:2 jb anjian,F6 //If anjian=0, execute sequentially, otherwise jump to F63 ACALL delay54 inc keycount //keycount++5 F6: 6 ret
jb is a bit-conditional transfer instruction. Its function is that if the directly addressed bit = 1, the program will transfer to the specified target address for execution. If the bit = 0, the program will be executed sequentially.
(6) Change the delay function in lines 78-83 of the C language to
delay70:
mov r6,#70
D2: mov R7,#248
d1: djnz R7,d1 //248*70 times
DJnz R6,D2
ret
delay5:
mov r6,#5 // eliminate jitter.
F7: mov R7,#248
F8: djnz r7,F8 //248*5 times
djnz r6,F7
ret
Note: 248 = 2 8 , which is approximately equal to 1ms. delay is the delay program.
Warm reminder: The case of the program code is not affected in assembly language, but it is affected in C language.
Question 1: Both ret and reti are program return instructions, what is the difference?
My answer: ret is the subroutine return instruction, and reti is the interrupt subroutine return instruction. The difference is that if the subroutine is called by the acall or lcall instruction, the return instruction is ret; if the subroutine is called by the address 0003, 0013, 000B, 001B, or 0023, the return instruction is reti.
Question 2: mov 20h, #0h and setb 20h all add 1, what is the difference?
My answer: 20h in mov instruction refers to byte, 20h in setb refers to bit.
The journey ends!
I still remember that I was struggling with the syntax and functions of various instructions in assembly language some time ago. Until one sunny afternoon, I took the two and a half pages of C language code that I had written in one hand and a single-chip assembly instruction query manual in the other hand, and began to translate line by line. The assembly code may have some errors in debugging, but the basic logic is correct. Moreover, this time C -> assembly, I have a deeper understanding of the storage and call of data in the computer. During this period, the head teacher and classmates also answered my questions. I believe that in the future, I will have a deeper understanding of computers. The harder you work, the luckier you are!
Previous article:51 single chip microcomputer stepper motor control, forward and reverse rotation, etc.
Next article:STC12C5A60S2 MCU realizes ISP automatic download
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- CMD file allocation method
- LSM6DS3 3D Accelerometer and 3D Gyroscope PCB Package and Code
- Two power modules with the same voltage connected in parallel?
- cubemx sets TF to 4bit, but the file shows 1bit mode
- How to choose the inductor of BOOST boost power supply?
- "Invite you to disassemble" Episode 1 --- Xiaomi 45W charging head disassembly
- The experience of debugging L-band RF power amplifier is in "2019.1.1" for your reference
- 2021 ON Semiconductor Avnet RSL10 Bluetooth SoC Development and Design Competition 4th Post (Bluetooth Current)
- MSP430FR5969 Remote Upgrade
- Circuit help, how do you analyze this circuit?