[mw_shl_code=c,true]/*------------------------------------------------------------------*/
/* --- STC MCU International Limited -------------------------------*/
/* --- STC IAP ?????????ú???????§ISP ???????ò ----------------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-755-82944243 ----------------------------------------*/
/* --- Tel: 86-755-82948412 ----------------------------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------*/
/* ???????????ò???????ò???????????????????ò,???????ò???ò??????×??÷ */
/* ???????ê?§??????×????ò???ò */
/*------------------------------------------------------------------*/
#include<STC12C5A.h>
#include "absacc.h"
sbit en=P3^2; //MAX485????????IO
sbit led1=P4^1;
sbit seg5=P2^4;
sbit seg6=P2^5;
sbit seg7=P2^6;
#define seg_duan P0
char code seg_yang[]={0x40, 0xf9, 0x24, 0xa0, 0x91, 0x82, 0x02, 0xf8, 0x00, 0x80};//????
/*?¨??????*/
#define FOSC 11059200L //?????±????
#define BAUD (256 - FOSC/32/19200) //?¨???®???¨????
#define MAX_SIZE 118 //???§???ò×??ó????????????
#define ENABLE_IAP 0x82 //?????¤×÷????<20MHz
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef struct
{
BYTE cmd;
WORD addr;
WORD len;
BYTE chk;
} CBW;
void Isp_Check(BYTE *p);
BYTE Isp_RecvUart();
void Isp_RecvBlock(BYTE *p, BYTE n);
void Isp_SendUart(BYTE dat);
void Isp_SoftReset();
BYTE cnt7f; //Isp_Check??????????±???,????7F???????÷,?±??????????16??7F?ó????ISP????????
CBW cbw; //?®???ü???é
BYTE sum; //???é??±???
BYTE buf[64]; //??????????
/*------------------------------------------------
?®??????·??????ò
------------------------------------------------*/
void uart() interrupt 4
{
if (TI) TI = 0; //·????ê??????
if (RI) //?????ê??????
{
Isp_Check(&cnt7f); //ISP?ì??
RI = 0; //???????ê??±ê??
}
}
void delayms(int xms)//???±????
{
for(;xms>0;xms--);
}
void main()
{
SCON = 0x50; //?¨???®????????8bit??±?,?????é??
AUXR = 0x15; //?¨????·??ú?÷12±???,???????¨????·??ú?÷?¨?±?÷
BRT = BAUD; //???????¨????·??ú?÷?¨?±?÷???¨?±????
ES = 1; //?????®??????
EA = 1; //?ò??????????????
en=0;
while (1)
{
led1=~led1;
delayms(5000);
}
}
/*------------------------------------------------
?®??ISP?ü???ò???ì?????é
------------------------------------------------*/
void Isp_Check(BYTE *p)
{
BYTE i;
WORD j;
if (SBUF != 0x7f) //?ì???®????????·???7F
{
*p = 0; //??????7F,?ò??7F??????
// Isp_SoftReset(); //??????·?·¨?ü???±,????????
}
else
{
(*p)++; //????7F,?ò7F??????+1
if (*p>=16) //????7F??·?????????????16??
{ //??>=16??,?ò????ISP????????
IE = 0; //??±??ù??????
PSW = 0; //ISP???é??????0×é?????÷
SP = 0x5f; //????ISP???é??????????
RI = 0; //?????®??????±ê??
TI = 0; //???®??·???±ê??
Isp_SendUart(0x5a); //·???5A 69??PC,±í??ISP???é??×?±????÷
Isp_SendUart(0x69); //·???5A 69??PC,±í??ISP???é??×?±????÷
while (1) //ISP????????,?÷???·
{
sum = 0; //?????é?? //???ü?????í,?ò???ò????
if((Isp_RecvUart()==0x5a)&&(Isp_RecvUart()==0x69))
{
Isp_RecvBlock((BYTE *)&cbw, 6); //????6×??????ü???ò??
if (sum != 0) //?????ü???ò????·????·
{
Isp_SoftReset(); //???ü?????í,?ò???ò????
}
switch (cbw.cmd)
{
case 0: //0???ü?????????ü??
IAP_ADDRL = 0; //????0????????????
IAP_ADDRH = 0;
IAP_CONTR = ENABLE_IAP; //????IAP????
IAP_CMD = 3; //?????ü??
if (cbw.len > MAX_SIZE) //????????????????·?????·??§
{
cbw.len = MAX_SIZE;
}
while (cbw.len--) //??????·??????ê??
{
WDT_CONTR = 0x17; //???????·
IAP_TRIG = 0x5a; //??·?ISP?ü??
IAP_TRIG = 0xa5;
IAP_ADDRH += 2; //??±ê???·+512
}
Isp_SendUart(0); //???··???
break;
case 1: //1???ü????±à???ü??
sum = 0; //???????é????
Isp_RecvBlock(buf, 64); //????64×?????±à??????
Isp_RecvUart(); //???????é??
if (sum != 0) //??????????·????·
{
Isp_SoftReset(); //?????????í,?ò???ò????
}
IAP_CONTR = ENABLE_IAP; //????IAP????
IAP_CMD = 2; //±à???ü??
j = cbw.addr; //±à????±ê???·
for (i=0; i<64; i++) //±à??64×???????
{
WDT_CONTR = 0x17; //???????·
IAP_DATA = buf; //???±?°??????IAP?????????÷
IAP_ADDRL = j; //??±ê???·??IAP???·?????÷
IAP_ADDRH = j >> 8;
IAP_TRIG = 0x5a; //??·?ISP?ü??
IAP_TRIG = 0xa5;
j++; //??±ê???·+1
}
j = cbw.addr; //???é??±ê???·
for (i=0; i<64; i++) //???é64×???????
{
WDT_CONTR = 0x17; //???????·
if (buf != CBYTE[j]) //??????????±ê????????±???
break; //???à??,?ò±à?????í
j++; //???é??????×???
}
Isp_SendUart(!(i == 64)); //???é????·???0; ·??ò·???1
break;
default:
Isp_SoftReset(); //??????·?·¨?ü???±,????????
break;
}
}
}
}
}
}
/*------------------------------------------------
???????é?®??????
????????: R0 (?????????????·)
R7 (???????¤??)
------------------------------------------------*/
void Isp_RecvBlock(BYTE *p, BYTE n)
{
while (n--) //?ì???¤??
{
*p = Isp_RecvUart(); //????1×???,??±???????????
p++; //?????????·+1
}
}
/*------------------------------------------------
????1×????®??????
????????: ACC (????????????)
------------------------------------------------*/
BYTE Isp_RecvUart()
{
BYTE dat;
RI = 0; //?????®??????±ê??
en=0;
while (!RI) //?????????ê??
{
WDT_CONTR = 0x17; //???????·
}
dat = SBUF; //?????®??????
RI = 0; //????±ê??
sum += dat; //???????é??
return dat; //·????????????®??????
}
/*------------------------------------------------
·???1×????®??????
????????: ACC (??·?????????)
------------------------------------------------*/
void Isp_SendUart(BYTE dat)
{
en=1;
TI = 0; //????±ê??
SBUF = dat; //·????±?°????
while (!TI) //?????°????????·????ê??
{
WDT_CONTR = 0x17; //???????·
}
en=0;
}
/*------------------------------------------------
?í??????
------------------------------------------------*/
void Isp_SoftReset()
{
IAP_CONTR = 0x20; //???§???ò??????
}
[/mw_shl_code]
请问大神们,这个ISP在线升级程序的通讯协议怎么看?没有经验,求指教啊。。。 |