/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- 使用主芯片对从芯片(限STC15系列)进行ISP下载举例 -----------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-755-82905966 -------------------------------------------*/
/* --- Tel: 86-755-82948412 -------------------------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了宏晶科技的资料及程序 */
/* 如果要在文章中应用此代码,请在文章中注明使用了宏晶科技的资料及程序 */
/*---------------------------------------------------------------------*/
//本示例在Keil开发环境下请选择Intel的8058芯片型号进行编译
//假定测试芯片的工作频率为11.0592MHz
//注意:使用本代码对STC15系列的单片机进行下载时,必须要执行了Download代码之后,
//才能给目标芯片上电,否则目标芯片将无法正确下载
#include "reg51.h"
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
//宏、常量定义
#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 //主控芯片工作频率
#define BR(n) (65536 - FOSC/4/(n)) //主控芯片串口波特率计算公式
#define T1MS (65536 - FOSC/1000) //主控芯片1ms定时初值
#define FUSER 24000000L //15系列目标芯片工作频率
#define RL(n) (65536 - FUSER/4/(n)) //15系列目标芯片串口波特率计算公式
//SFR定义
sfr AUXR = 0x8e;
//变量定义
BOOL f1ms; //1ms标志位
BOOL UartBusy; //串口发送忙标志位
BOOL UartReceived; //串口数据接收完成标志位
BYTE UartRecvStep; //串口数据接收控制
BYTE TimeOut; //串口通讯超时计数器
BYTE xdata TxBuffer[256]; //串口数据发送缓冲区
BYTE xdata RxBuffer[256]; //串口数据接收缓冲区
char code DEMO[256]; //演示代码数据
//函数声明
void Initial(void);
void DelayXms(WORD x);
BYTE UartSend(BYTE dat);
void CommInit(void);
void CommSend(BYTE size);
BOOL Download(BYTE *pdat, long size);
//主函数入口
void main(void)
{
while (1)
{
Initial();
if (Download(DEMO, 0x0100))
{
//下载成功
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
{
//下载失败
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定时器中断服务程序
void tm0(void) interrupt 1 using 1
{
static BYTE Counter100;
f1ms = TRUE;
if (Counter100-- == 0)
{
Counter100 = 100;
if (TimeOut) TimeOut--;
}
}
//串口中断服务程序
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;
}
}
}
//系统初始化
void Initial(void)
{
UartBusy = FALSE;
SCON = 0xd0; //串口数据模式必须为8位数据+1位偶检验
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延时程序
void DelayXms(WORD x)
{
do
{
f1ms = FALSE;
while (!f1ms);
} while (x--);
}
//串口数据发送程序
BYTE UartSend(BYTE dat)
{
while (UartBusy);
UartBusy = TRUE;
ACC = dat;
TB8 = P;
SBUF = ACC;
return dat;
}
//串口通讯初始化
void CommInit(void)
{
UartRecvStep = 0;
TimeOut = 20;
UartReceived = FALSE;
}
//发送串口通讯数据包
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
1