STC51 series MCU can automatically download programs without power failure

Publisher:dswecdLatest update time:2019-01-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the past, when programming microcontrollers, the most annoying thing was that every time you downloaded a program, you had to cut off the power, click download, and then power on to download the program to the microcontroller. If this operation happened once or twice, it was tolerable, but often we need to debug the code and perform download operations multiple times. Constant power off and on is undoubtedly a great damage to the microcontroller. 

According to the data sheet of stc12c5a60s2 (an enhanced 51 MCU, compatible with stc89c52 MCU), the MCU system can be soft reset to make the MCU run the system startup code (this part of the code is different from our user code). This is also the principle when we power off and then power on. The system is soft reset by writing 60H to IAP_CONTR (C7H), and then automatically downloading. There are two ways to start the MCU after resetting. One is to start from the user program area. We usually press the reset button to start in this way. The other is to start from the ISP program area. Power off startup belongs to this method. This startup method can be achieved by setting IAP_CONTR. We need to download the program, so we need to start from the ISP program area. For specific register settings, please refer to the official data sheet. 

The specific implementation steps are as follows: 

First, download the following code to the microcontroller using our old method.


IAP_CONTR EQU 0C7H


ORG 0000H

LJMP START


ORG 0023H

LJMP SERIAL


START:  

    LCALL SERIALINIT


    MainLoop:                               

    SJMP MainLoop   


SERIALINIT:

    MOV SP,#60H

    MOV SCON,#50H

    MOV TMOD,#20H

    MOV TH1,#0FDH

    MOV TL1,#0FDH

    SETB TR1        

    SETB ES

    SETB EA


SERIAL:

    PUSH ACC

    PUSH PSW


    JNB RI,RIGO

        CLR RI

        MOV A,SBUF

        CJNE A,#0AFH,RIGO;

        NOP

        NOP

        NOP

        MOV IAP_CONTR,#60H

    RIGO:


    POP PSW

    POP ACC

    RETI

END


Then set stc-isp as follows 

 Write the picture description here 

Custom commands can be modified according to your needs, and the code should also be modified accordingly.


The main principle of the above code is to open the microcontroller serial port interrupt. When using stc-isp to download the program, the software will first write the AFH command to the microcontroller. When the microcontroller detects this command through the serial port, it will perform a system soft reset. After the system soft reset, the microcontroller system code will download the user code to the microcontroller.


Every time you download the code, you need to add the above code to your own code.


Of course, there is more than one way to send a specific command through the serial port to perform a soft reset of the system. For example, you can also set a button and run MOV IAP_CONTR, #60H when it is detected that the button is pressed. The core is the command MOV IAP_CONTR, #60H, but under what circumstances to run this command needs to be designed according to your own ideas and actual use.

Reference address:STC51 series MCU can automatically download programs without power failure

Previous article:51 MCU timer and baud rate
Next article:8051-stc cold start automatic download line principle and DIY

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号