Direct software architecture for 8-bit MCU systems (C51 series)

Publisher:AngelicJourneyLatest update time:2024-11-08 Source: cnblogs Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

}


Reference address:Direct software architecture for 8-bit MCU systems (C51 series)

Previous article:Answers to Common Exercises on MCS-51 MCU Timer/Counter
Next article:最后一页

Latest Microcontroller Articles
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号