Skip to content

Instantly share code, notes, and snippets.

@Ilgrim
Forked from zerog2k/stc15f_prog.c
Created January 13, 2024 23:16
Show Gist options
  • Save Ilgrim/9b685eb6c5476f80c8ebf11a0011769c to your computer and use it in GitHub Desktop.
Save Ilgrim/9b685eb6c5476f80c8ebf11a0011769c to your computer and use it in GitHub Desktop.
stc mcu programmer sample code from stc website for stc15f series, with some google translate ;)
/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- To be downloaded from the ISP chip (limited STC15 series), for example using the main chip -----------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-755-82905966 -------------------------------------------*/
/* --- Tel: 86-755-82948412 -------------------------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* If you want to use this code in the program, in the program, using the information and procedures specified macro crystal technology */
/* If you want to quote this code in the article, please indicate in the article the use of information technology and procedures Wang Jing */
/*---------------------------------------------------------------------*/
// This sample in the Keil development environment select the Intel 8058 chip models compiled
// Assumed test chip operating frequency is 11.0592MHz
// Note: When using this code STC15 series of microcontrollers for download must be executed after Download code
// In order to power on the target chip, otherwise the chip will not be able to download the correct target
#include "reg51.h"
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
// Macro, constant definitions
#define FALSE 0
#define TRUE 1
#define LOBYTE(w) ((BYTE)(WORD)(w))
#define HIBYTE(w) ((BYTE)((WORD)(w) >> 8))
#define MINBAUD 2400L
#define MAXBAUD 115200L
#define FOSC 11059200L // Master chip operating frequency
#define BR(n) (65536 - FOSC/4/(n)) //Master chip baud rate calculation formula
#define T1MS (65536 - FOSC/1000) //Initial master chip 1ms Timing
//#define FUSER 11059200L //STC15系列目标芯片工作频率
//#define FUSER 12000000L //STC15系列目标芯片工作频率
//#define FUSER 18432000L //STC15系列目标芯片工作频率
//#define FUSER 22118400L //STC15系列目标芯片工作频率
#define FUSER 24000000L //STC15 series target chip operating frequency
#define RL(n) (65536 - FUSER/4/(n)) //STC15 series target chip baud rate calculation formula
//SFR
sfr AUXR = 0x8e;
//variables
BOOL f1ms; //1ms flag
BOOL UartBusy; //Serial transmit busy flag
BOOL UartReceived; //Serial data received flag
BYTE UartRecvStep; //Serial data reception control
BYTE TimeOut; //Serial communication timeout counter
BYTE xdata TxBuffer[256]; //Serial data transmit buffer
BYTE xdata RxBuffer[256]; //Serial data receive buffer
char code DEMO[256]; //Demo 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--;
}
}
//The serial interrupt service routine
void uart(void) interrupt 4 using 1
{
static WORD RecvSum;
static BYTE RecvIndex;
static BYTE RecvCount;
BYTE dat;
if (TI)
{
TI = 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 += dat;
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 data pattern must be 8-bit data + 1-bit even parity
AUXR = 0xc0;
TMOD = 0x00;
TH0 = HIBYTE(T1MS);
TL0 = LOBYTE(T1MS);
TR0 = 1;
TH1 = HIBYTE(BR(MINBAUD));
TL1 = LOBYTE(BR(MINBAUD));
TR1 = 1;
ET0 = 1;
ES = 1;
EA = 1;
}
//Xms Delay program
void DelayXms(WORD x)
{
do
{
f1ms = FALSE;
while (!f1ms);
} while (x--);
}
//Serial data transmission program
BYTE UartSend(BYTE dat)
{
while (UartBusy);
UartBusy = TRUE;
ACC = dat;
TB8 = P;
SBUF = ACC;
return dat;
}
//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<size; i++)
{
sum += UartSend(TxBuffer[i]);
}
UartSend(HIBYTE(sum));
UartSend(LOBYTE(sum));
UartSend(0x16);
while (UartBusy);
CommInit();
}
//STC15 series of chips for data download
BOOL Download(BYTE *pdat, long size)
{
BYTE arg;
BYTE fwver;
BYTE offset;
BYTE cnt;
WORD addr;
//handshake
CommInit();
while (1)
{
if (UartRecvStep == 0)
{
UartSend(0x7f);
DelayXms(10);
}
if (UartReceived)
{
arg = RxBuffer[4];
fwver = RxBuffer[17];
if (RxBuffer[0] == 0x50) break;
return FALSE;
}
}
//Set parameters (set from the chip using the highest baud rate and other parameters erasing wait time)
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;
if (fwver < 0x72)
{
CommSend(1);
}
else
{
TxBuffer[1] = 0x00;
TxBuffer[2] = 0x00;
TxBuffer[3] = 0x5a;
TxBuffer[4] = 0xa5;
CommSend(5);
}
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;
if (fwver < 0x72)
{
CommSend(2);
}
else
{
TxBuffer[2] = 0x00;
TxBuffer[3] = 0x5a;
TxBuffer[4] = 0xa5;
CommSend(5);
}
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;
if (fwver < 0x72)
{
offset = 3;
}
else
{
TxBuffer[3] = 0x5a;
TxBuffer[4] = 0xa5;
offset = 5;
}
while (addr < size)
{
TxBuffer[1] = HIBYTE(addr);
TxBuffer[2] = LOBYTE(addr);
cnt = 0;
while (addr < size)
{
TxBuffer[cnt+offset] = pdat[addr];
addr++;
cnt++;
if (cnt >= 128) break;
}
CommSend(cnt + offset);
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 modify hardware options can skip this step at this time all the hardware options
//// Remained unchanged, MCU frequency by adjusting the frequency to the last
//// If the write hardware options, MCU internal IRC frequency will be fixed written as 24M,
//// Suggestion: Good first use STC-ISP download software from the chip hardware option settings
//// Later using the main chip for chip program is downloaded from the hardware option does not write
//DelayXms(10);
//for (cnt=0; cnt<128; cnt++)
//{
// TxBuffer[cnt] = 0xff;
//}
//TxBuffer[0] = 0x04;
//TxBuffer[1] = 0x00;
//TxBuffer[2] = 0x00;
//if (fwver < 0x72)
//{
// TxBuffer[34] = 0xfd;
// TxBuffer[62] = arg;
// TxBuffer[63] = 0x7f;
// TxBuffer[64] = 0xf7;
// TxBuffer[65] = 0x7b;
// TxBuffer[66] = 0x1f;
// CommSend(67);
//}
//else
//{
// TxBuffer[3] = 0x5a;
// TxBuffer[4] = 0xa5;
// TxBuffer[36] = 0xfd;
// TxBuffer[64] = arg;
// TxBuffer[65] = 0x7f;
// TxBuffer[66] = 0xf7;
// TxBuffer[67] = 0x7b;
// TxBuffer[68] = 0x1f;
// CommSend(69);
//}
//while (1)
//{
// if (TimeOut == 0) return FALSE;
// if (UartReceived)
// {
// if ((RxBuffer[0] == 0x04) && (RxBuffer[1] == 'T')) break;
// return FALSE;
// }
//}
// The download is complete
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,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment