Example of using other MCUs to download serial port ISP to STC15 series microcontrollers

Publisher:WhisperingWavesLatest update time:2014-12-29 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Example of using other MCUs to perform serial port ISP download on STC15 series microcontrollers (equivalent to opening the STC ISP download protocol)


 

Example of using other MCUs to perform serial port ISP download on STC15 series microcontrollers (equivalent to opening the ISP download protocol of STC microcontrollers)

//In this example, please select Intel's 8058 chip model for compilation in the Keil development environment

//Assume that the operating frequency of the test chip is 11.0592MHz

//Note: When using this code to download the STC15 series MCU, you must execute the Download code first.

//To power on the target chip, otherwise the target chip will not be able to download correctly

#include "reg51.h"

typedef bit BOOL;

typedef unsigned char BYTE;

typedef unsigned short WORD;

typedef unsigned long DWORD;

//Macro and constant definitions

#define FALSE               0

#define TRUE                1

#define LOBYTE(in) ((BYTE)(WORD)(in))

#define HIBYTE(w)           ((BYTE)((WORD)(w) >> 8))

 

#define MINBAUD             2400L

#define MAXBAUD             115200L

 

#define FOSC 11059200L //main control chip operating frequency

#define BR(n) (65536 - FOSC/4/(n)) // Calculation formula for the baud rate of the main control chip serial port

#define T1MS (65536 - FOSC/1000) // 1ms timing initial value of the main control chip

 

#define FUSER 24000000L //15 series target chip operating frequency

#define RL(n) (65536 - FUSER/4/(n)) //15 series target chip serial port baud rate calculation formula

 

//SFR definition

sfr AUXR = 0x8e;

 

//Variable definitions

BOOL f1ms; //1ms flag

BOOL UartBusy; //Serial port sending busy flag

BOOL UartReceived; //Serial port data reception completed flag

BYTE UartRecvStep; //Serial port data receiving control

BYTE TimeOut; //Serial communication timeout counter

BYTE xdata TxBuffer[256]; //Serial port data sending buffer

BYTE xdata RxBuffer[256]; //Serial port data receiving buffer

char code DEMO[256]; //demonstration code data

 

// Function declaration

void Initial(void);

void DelayXms(WORD x);

BYTE UartSend(BYTE dat);

void CommInit(void);

void CommSend(BYTE size);

BOOL Download(BYTE *pdat, long size);

 

//Main function entry

void main(void)

{

    while (1)

    {

        Initial();

        if (Download(DEMO, 0x0100))

        {

            //download successful

            P3 = 0xff;

            DelayXms(500);

            P3 = 0x00;

            DelayXms(500);

            P3 = 0xff;

            DelayXms(500);

            P3 = 0x00;

            DelayXms(500);

            P3 = 0xff;

            DelayXms(500);

            P3 = 0x00;

            DelayXms(500);

            P3 = 0xff;

        }

        else

        {

            //download failed

            P3 = 0xff;

            DelayXms(500);

            P3 = 0xf3;

            DelayXms(500);

            P3 = 0xff;

            DelayXms(500);

            P3 = 0xf3;

            DelayXms(500);

            P3 = 0xff;

            DelayXms(500);

            P3 = 0xf3;

            DelayXms(500);

            P3 = 0xff;

        }

    }

}

 

//1ms timer interrupt service routine

void tm0(void) interrupt 1 using 1

{

    static BYTE Counter100;

   

    f1ms = TRUE;

    if (Counter100-- == 0)

    {

        Counter100 = 100;

        if (TimeOut) TimeOut--;

    }

}

 

//Serial port interrupt service routine

void uart(void) interrupt 4 using 1

{

    static WORD RecvSum;

    static BYTE RecvIndex;

    static BYTE RecvCount;

    BITE that;

 

    if (TI)

    {  

        IF = 0;

        UartBusy = FALSE;

    }

   

    if (RI)

    {

        RI = 0;

        dat = SBUF;

        switch (UartRecvStep)

        {

        case 1:

            if (dat != 0xb9) goto L_CheckFirst;

            UartRecvStep++;

            break;

        case 2:

            if (dat != 0x68) goto L_CheckFirst;

            UartRecvStep++;

            break;

        case 3:

            if (dat != 0x00) goto L_CheckFirst;

            UartRecvStep++;

            break;

        case 4:

            RecvSum = 0x68 + dat;

            RecvCount = dat - 6;

            RecvIndex = 0;

            UartRecvStep++;

            break;

        case 5:

            RecvSum += that;

            RxBuffer[RecvIndex++] = dat;

            if (RecvIndex == RecvCount) UartRecvStep++;

            break;

        case 6:

            if (dat != HIBYTE(RecvSum)) goto L_CheckFirst;

            UartRecvStep++;

            break;

        case 7:

            if (dat != LOBYTE(RecvSum)) goto L_CheckFirst;

            UartRecvStep++;

            break;

        case 8:

            if (dat != 0x16) goto L_CheckFirst;

            UartReceived = TRUE;

            UartRecvStep++;

            break;

L_CheckFirst:

        case 0:

        default:

            CommInit();

            UartRecvStep = (dat == 0x46 ? 1 : 0);

            break;

        }

    }

}

 

//system initialization

void Initial(void)

{

    UartBusy = FALSE;

 

    SCON = 0xd0; //Serial port data mode must be 8-bit data + 1-bit even check

    AUXR = 0xc0;

    TMOD = 0x00;

    TH0 = HIBYTE(T1MS);

    TL0 = LOBYTE(T1MS);

    TR0 = 1;

    TH1 = HIBYTE(BR(MINBAUD));

    TL1 = LOBYTE(BR(MINBAUD));

    TR1 = 1;

    ET0 = 1;

    EN = 1;

    EA = 1;

}

 

//Xms delay program

void DelayXms(WORD x)

{

    do

    {

        f1ms = FALSE;

        while (!f1ms);

    } while (x--);

}

 

//Serial data sending program

BYTE UartSend(BYTE dat)

{

    while (UartBusy);

   

    UartBusy = TRUE;

    ACC = that;

    TB8 = P;

    SBUF = ACC;

   

    return that;

}

 

//Serial communication initialization

void CommInit(void)

{

    UartRecvStep = 0;

    TimeOut = 20;

    UartReceived = FALSE;

}

 

//Send serial communication data packet

void CommSend(BYTE size)

{

 WORD sum;

    BYTE i;

   

    UartSend(0x46);

    UartSend(0xb9);

    UartSend(0x6a);

    UartSend(0x00);

    sum = size + 6 + 0x6a;

    UartSend(size + 6);

    for (i=0; i

    {

        sum += UartSend(TxBuffer[i]);

    }

    UartSend(HIBYTE(sum));

    UartSend(LOBYTE(sum));

    UartSend(0x16);

    while (UartBusy);

 

    CommInit();

}

 

//Data download program for STC15 series chips

BOOL Download(BYTE *pdat, long size)

{

    BYTE angry;

    BYTE cnt;

    WORD addr;

   

    //Handshake

    CommInit();

    while (1)

    {

        if (UartRecvStep == 0)

        {

            UartSend(0x7f);

            DelayXms(10);

        }

        if (UartReceived)

        {

            arg = RxBuffer[4];

            if (RxBuffer[0] == 0x50) break;

            return FALSE;

        }

    }

 

    //Setting parameters

    TxBuffer[0] = 0x01;

    TxBuffer[1] = arg;

    TxBuffer[2] = 0x40;

 TxBuffer[3] = HIBYTE(RL(MAXBAUD));

 TxBuffer[4] = LOBYTE(RL(MAXBAUD));

 TxBuffer[5] = 0x00;

 TxBuffer[6] = 0x00;

 TxBuffer[7] = 0xc3;

    CommSend(8);

 while (1)

 {

        if (TimeOut == 0) return FALSE;

        if (UartReceived)

        {

            if (RxBuffer[0] == 0x01) break;

            return FALSE;

        }

 }

 

    //Prepare

    TH1 = HIBYTE(BR(MAXBAUD));

    TL1 = LOBYTE(BR(MAXBAUD));

    DelayXms(10);

 TxBuffer[0] = 0x05;

 CommSend(1);

 while (1)

 {

        if (TimeOut == 0) return FALSE;

        if (UartReceived)

        {

            if (RxBuffer[0] == 0x05) break;

            return FALSE;

        }

 }

   

    // Erase

    DelayXms(10);

 TxBuffer[0] = 0x03;

 TxBuffer[1] = 0x00;

 CommSend(2);

    TimeOut = 100;

    while (1)

 {

        if (TimeOut == 0) return FALSE;

        if (UartReceived)

        {

            if (RxBuffer[0] == 0x03) break;

            return FALSE;

        }

 }

 

    //Write user code

    DelayXms(10);

    addr = 0;

 TxBuffer[0] = 0x22;

 while (addr < size)

 {

        TxBuffer[1] = HIBYTE(addr);

        TxBuffer[2] = LOBYTE(addr);

        cnt = 0;

        while (addr < size)

        {

            TxBuffer[cnt+3] = pdat[addr];

            addr++;

            cnt++;

            if (cnt >= 128) break;

        }

        CommSend(cnt + 3);

  while (1)

  {

            if (TimeOut == 0) return FALSE;

            if (UartReceived)

            {

                if ((RxBuffer[0] == 0x02) && (RxBuffer[1] == 'T')) break;

                return FALSE;

            }

  }

  TxBuffer[0] = 0x02;

 }

 

    //Write hardware options (if you do not need to modify hardware options, this step can be skipped)

    DelayXms(10);

    for (cnt=0; cnt<128; cnt++)

    {

        TxBuffer[cnt] = 0xff;

 }

    TxBuffer[0] = 0x04;

 TxBuffer[1] = 0x00;

 TxBuffer[2] = 0x00;

 TxBuffer[34] = 0xfd;

 TxBuffer[62] = arg;

 TxBuffer[63] = 0x7f;

 TxBuffer[64] = 0xf7;

 TxBuffer[65] = 0x7b;

 TxBuffer[66] = 0x1f;

 CommSend(67);

 while (1)

 {

        if (TimeOut == 0) return FALSE;

        if (UartReceived)

        {

            if ((RxBuffer[0] == 0x04) && (RxBuffer[1] == 'T')) break;

            return FALSE;

        }

 }

 

    //Download completed

    return TRUE;

}

 

char code DEMO[256] =

{

    0x02,0x00,0x5E,0x12,0x00,0x4B,0x75,0xB0,

    0xEF,0x12,0x00,0x2C,0x75,0xB0,0xDF,0x12,

    0x00,0x2C,0x75,0xB0,0xFE,0x12,0x00,0x2C,

    0x75,0xB0,0xFD,0x12,0x00,0x2C,0x75,0xB0,

    0xFB,0x12,0x00,0x2C,0x75,0xB0,0xF7,0x12,

    0x00,0x2C,0x80,0xDA,0xE4,0xFF,0xFE,0xE4,

    0xFD,0xFC,0x0D,0xBD,0x00,0x01,0x0C,0xBC,

    0x01,0xF8,0xBD,0xF4,0xF5,0x0F,0xBF,0x00,

    0x01,0x0E,0xBE,0x03,0xEA,0xBF,0xE8,0xE7,

    0x02,0x00,0x4B,0x75,0x80,0xFF,0x75,0x90,

    0xFF,0x75,0xA0,0xFF,0x75,0xB0,0xFF,0x75,

    0xC0,0xFF,0x75,0xC8,0xFF,0x22,0x78,0x7F,

    0xE4,0xF6,0xD8,0xFD,0x75,0x81,0x07,0x02,

    0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

};

Keywords:MCU Reference address:Example of using other MCUs to download serial port ISP to STC15 series microcontrollers

Previous article:LCD12864 with Chinese character library graphic display-MCU program
Next article:STC MCU PCA generates PWM program

Recommended ReadingLatest update time:2024-11-16 15:01

Introduction to AT Series 51 MCU
51 MCU AT89S series supporting ISP       ATMEL's newly launched programmable MCS51 compatible microcontroller AT89S51/52 will completely replace the AT89C51/52 microcontroller.    AT89S series microcontrollers have the following features:       Compatible with MCS51 microcontroller       4/8K bytes FLASH memory suppor
[Microcontroller]
Introduction to Ethernet connection application of MC9S12NE64 microcontroller
1 Introduction With the emergence of the Internet and the rapid development of Ethernet, the number of Ethernet-based device controls is increasing and the development is also getting faster and faster. At present, Ethernet (EtImmet) has been widely used in various computer networks. Through Ethernet and
[Microcontroller]
Introduction to Ethernet connection application of MC9S12NE64 microcontroller
The function of the microcontroller external interrupt line
This figure is a schematic diagram of an external interrupt line or external event line. In the figure, there is a slash on the signal line and a note with the word 19 next to it, indicating that there are 19 sets of such lines. The blue dotted arrow in the figure marks the transmission path of the external interrupt
[Microcontroller]
The function of the microcontroller external interrupt line
Temperature control system circuit design based on ATmega16L microcontroller
Introduction: Design a temperature control system based on ATmega16L microcontroller, and explain the hardware design of the system. Adopt modular design method and use incremental PID algorithm to make the temperature value of the controlled object tend to the given value. main controller The main controller of t
[Microcontroller]
Temperature control system circuit design based on ATmega16L microcontroller
Software Reset Method of MCS-51 Series Microcontroller
In the application of single-chip microcomputer system, we often need to use reset technology to achieve anti-interference. Some single-chip microcomputers (such as 8098) have special reset instructions. Although some enhanced MCS-51 series single-chip microcomputers do not have reset instructions, the WATCHDOG circ
[Microcontroller]
Design of universal industrial wireless remote control system based on single chip microcomputer and PLC
     1. Introduction     Industrial wireless remote control system can digitally process the control instructions of the operator or machine, transmit them to the remote receiving system through the transmitting system, and then convert them into control instructions through decoding to realize the control of variou
[Microcontroller]
Design of universal industrial wireless remote control system based on single chip microcomputer and PLC
Single chip microcomputer design to measure frequency
1. Circuit Design In the single-chip microcomputer application system, it is often necessary to measure the frequency of a continuous pulse wave. In practical applications, the measurement of physical quantities such as rotation speed, displacement, speed, flow, etc. is generally converted into a pulse electrical sign
[Microcontroller]
Single chip microcomputer design to measure frequency
51 single chip assembly language electronic keyboard
I have written a C language before: http://hi.baidu.com/do_sermon/item/c5e55bc1fc2dc30ec710b2f9 In response to the request of netizens, I wrote another compilation. In addition, the function of displaying the key number was added. Question link: http://zhidao.baidu.com/question/1818901024900190348.html The cir
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号