1. Preparation of startup documents
$ NOMOD51 ; Ax51 macro assembler control command: disable predefined 8051
; Customize the storage area that needs to be initialized after power-on
; The starting address of the indirect addressing area IDATA is fixed to 0
IDATALEN EQU 8OH; specifies the length of the IDATA area to be initialized (in bytes)
XDATASTART EQU OH ;The starting address of the external direct addressing area XDATA that needs to be initialized
XDATALEN EQU OH ;Specify the length of the XDATA area to be initialized (in bytes)
PDATASTART EQU OH; Specify the starting address of the page addressing area PDATA to be initialized
PDATALEN EQU OH; Specify the length of the PDATA area to be initialized (in bytes)
; Reentrant stack initialization, the following EQU statement defines and initializes the stack pointer of the reentrant function
; Stack space for reentrant functions in SMALL mode
IBPSTACK EQU O ; set to 1 if reentrancy is used in SMALL mode
EBPSTACKTOP EQU OFFH+1 ; Set the top of the stack to the highest position +1
; Stack space for reentrant functions in LARGE mode
XBPSTACK EQU O ; set to 1 if reentrancy is used in LARGE mode
XBPSTACKTOP EQU OFFFFH+1 ; Set the top of the stack to the highest position +1
; Stack space for reentrant functions in COMPACT mode
PBPSTACK EQU O ; set to 1 when using reentry in COMPACT mode
PBPSTACKTOP EQU OFFFFH+1 ; Set the top of the stack to the highest position +1
; Use COMTACT mode to define pages for 64KB of XDATA RAM
PPAGEENABLE EQU O; also set to 1 when using PDATA
PPAGE EQU O ; define page number
PPAGE_SFR DATAOAOH; the highest address byte of SFR
; Standard SFR symbol
ACC DATA OEOH
B DATA OFOH
SP DATA 81H
DPL DATA 82H
DPH DATA 83H
;****************************************************** *******************************
NAME ? C_STARTUP
C_C51STARTUP SEGMENT CODE
STACK SEGMENT IDATA
RSEG ? STACK
DS 1
EXTRN CODE (? C_STAR; external code (this label will represent the starting address of the user program)
PUBLIC ? STARTUP ; symbols for external use
CSEG AT O; Place the following code at address 0 of the code segment (absolute address positioning)
? C_STARTUP:
LJMP STARTUP1
RSEG ? C_C51STARTUP
STARTUP1:
IF IDATALEN <> ;If the length is greater than 1, initialize IDATA
MOV RO, #IDATALEN-1
CLR A
IDATALOOP:
MOV@RO,A
DJNZ RO, IDATALOOP
ENDIF
IF XDATALEN <> 0; If the length is greater than 1, initialize XDATA
MOV DPTR, #XDATASTART
MOV R7; # LOW (XDATALEN)
IF (LOW(XDATALEN)) <> 0; Preset the number of external loops for initialization to R6
MOV R6;# (HIGH (XDATALEN))+1
ELSE MOV R6; #HIGH (XDATALEN)
ENDIF
CLR A
XDATALOOP:
MOVX@DPTA,A
INC DPTR
DJNZ R7, XDATALOOP
DJNZ R6, XDATALOOP
ENDIF
IF PPAGEENABLE <> 0
MOV PPAGE_SFR, #PPAGE
ENDIF
IF PDATALEN <> 0; If the length is greater than 1, initialize PDATA
MOV R0, # LOW (PDATASTART)
MOV R7, # LOW (PDATALEN)
CLR A
PDATALOOP:
MOVX @R0, A
INC R0
DJNZ R7, PDATALOOP
ENDIF
IF IBPSTACK <> 0; In SMALL mode, the stack must be set when using reentrant functions
EXTRN DATA (? C_IBP)
MOV ? C_IBP , # LOW IBPSTACKTOP
ENDIF
IF XBPSTACK <> 0; When using reentrant functions in COMPACT mode, the stack must be set
EXTRN DATA (? C_XBP)
MOV ? C_XBP, #HIGH XBPSTACKTOP
MOV ? C_XBP + 1, # HIGH XBPSTACKTOP
ENDIF
IF PBPSTACK <> 0; In LARGE mode, the stack is set when using reentrant functions.
EXTRN DATA (? C_PBP)
MOV ? C_PBP, # LOW PBPSTACKTOP
ENDIF
MOV SP, # ? STACK – 1
; If you use Mode 4 program grouping technology (BANKING), please enable the following program code
; CALL ? B_SWITCHO ; The program starts executing from the first block (bank0)
LJMP ? C_START; Jump to the C program entry from here
END
2. The file structure of the main program:
#include "main.h"
void Project_Init(void) / This function initializes the microcontroller
{
USART_vInit(); //Initialize asynchronous serial interface device
T01__vInit(); //Initialize timers T0 and T1
INT__vInit(); // Initialize external trigger interrupts ET0 and ET1
IP = 0x15; //Set interrupt priority: T2, T1, T0 are low, serial port, ET1, ET1 are high
//Users can join with the code
EA = 1; //Open global interrupt
}
//Users can add custom functions
void main (void) //Main program function, called by Keil PK51 internal function C_START
{
//Custom variables that users can add
Project_Init(); //Initialize the project core microcontroller
//Global initial variable assignment that users can add
while(1) // infinite loop, complete background transaction processing
{
//Users can join with the code
}
}
3. The structure of the interrupt processing file
The framework format of the interrupt processing file interrupt.c is as follows:
#include "interrupt.h
//External interrupt 0 service handler
void INT_viIsrEx0(void)interrupt EXOINT
{
//Users can join with the code
}
//Timer T1 service handler
void T1_viIsrT1(void) interrupt T1 INT
{
//Users can join with the code
}
//Serial transceiver interrupt service handler
void USART_ viIsr (void)interrupt USARTINT
{
USART_Interrupt(); //Serial transceiver interrupt processing function in the serial interface device driver
//Users can join with the code
}
Previous article:Answers to Common Exercises on MCS-51 MCU Timer/Counter
Next article:最后一页
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- Breaking through the intelligent competition, Changan Automobile opens the "God's perspective"
- The world's first fully digital chassis, looking forward to the debut of the U7 PHEV and EV versions
- Design of automotive LIN communication simulator based on Renesas MCU
- When will solid-state batteries become popular?
- Adding solid-state batteries, CATL wants to continue to be the "King of Ning"
- The agency predicts that my country's public electric vehicle charging piles will reach 3.6 million this year, accounting for nearly 70% of the world
- U.S. senators urge NHTSA to issue new vehicle safety rules
- Giants step up investment, accelerating the application of solid-state batteries
- Guangzhou Auto Show: End-to-end competition accelerates, autonomous driving fully impacts luxury...
- Lotus launches ultra-900V hybrid technology "Luyao" to accelerate the "Win26" plan
- Get "10 Things You Need to Know About PCIe" for free to keep up with the latest PCIe trends
- Applications of mentor graphics
- Taiwanese media: Raising prices to grab wafer foundry capacity will become a new normal
- 【TI recommended course】#Ultrasonic water and gas meter design challenges and solutions#
- Why are more and more FPGAs used in smart cars?
- Voltage list of different countries
- Can SensorTile.Box connect to wifi? If so, how?
- Solve the problem that IAR cannot jump to function definition
- Technical parameters of the best 6G vector network analyzer AV3620 in China!
- TMS320 DSP series and OMAP series