?? main.c
字號:
//**********************************************************************************
//**********************************************************************************
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#include "w77e516.h"
extern char getbyte(void);//從串口取一個字節(jié)
extern putbyte(char c);//發(fā)送一個字節(jié)
extern putstring(uchar *puts);//發(fā)送一個字符串到串口
extern puthex(uchar c);//發(fā)送一個字節(jié)的hex碼,分成兩個字節(jié)發(fā)。
extern reputstring(uchar *puts);//發(fā)送一個字符串到串口
#define CR putstring("\r\n")//發(fā)送一個回車換行
extern void serial_init (void);
uchar code help[]="\r\n==============================================="
"\r\nISP of 77e516 by yuyuan"
"\r\n-----------------------------------------------"
"\r\n'r'-Read apflash"
"\r\n'e'-Chip erase"
"\r\n'w'-Write data to apflash"
"\r\n'v'-verify code with hexfile"
"\r\n'g'-Run w77e516\r\n"
"\r\n===============================================\r\n";
//**************************************
uchar timer0l,timer0h;
uchar xdata temp[1024];
void timer0_isr (void) interrupt 1
{
TR0=0;
// putstring("enter time interrupt1!\r\n");
TL0=timer0l;
TH0=timer0h;
}
closewatchdog()
{
TA=0xAA;
TA=0x55;
WDCON=0x00;
}
//片擦
void chiperase(void)
{
//UPDATE_64K:
P34=~P34;
timer0l=0x9f;//0xD0;
timer0h=0x0c;//0x8A;
TL0=timer0l; // 設(shè)置擦除操作的喚醒時間,時間一般取決于用戶系統(tǒng)時鐘,大約是15MS
TH0=timer0h;
//ERASE_P_4K:
SFRCN=0x22; //SFRCN = 22H, 擦除64K APFlash0
TCON=0x10; //TCON = 10H, TR0 = 1,GO
PCON =PCON | 0x01; //進(jìn)入空閑模式(進(jìn)行擦除操作
putstring("Erased__");
TMOD |= 0x21;
TR1 = 1;
TR0=0;
// serial_init();
putstring("Erased__");
}
//*********************************
//讀77e516flash中的內(nèi)容并輸出到串口
uint readflash()
{
uchar i;
uint m,n,n_all;
timer0l= 0xA0;//0xE9; ////為讀效驗設(shè)置定時器, 約1.5uS.
timer0h=0xFF;
TL0=timer0l;
TH0=timer0h;
SFRAH=0x00; //目標(biāo)地址低字節(jié)
SFRCN=0x00; //SFRCN = 00H, 讀APFlash0
SFRAL=0x00;
n_all=0;
for(i=0;i<1;i++)
{
//get the flashdata;
for(m=0;m<256;m++)
{
SFRAH=m;
for(n=0;n<256;n++)
{
SFRAL=n;
TCON=0x10; //TCON = 10H, TR0 = 1,GO
PCON=0x01; //
serial_init();
if(n%16==0){CR;puthex(n_all/256);puthex(n_all%256);putbyte(':');}
puthex(SFRFD);
n_all++;
if(RI){getbyte();return 1;} //putstring(help);
}
}
}
// putstring(help);
putstring("\r\nRead flash over!\r\n");
return 0;
}
//***********************************
//將兩個HEX字節(jié)轉(zhuǎn)換成一個字節(jié) (2 hexbytes = 1 byte)
uchar hextochar(uchar c0,c1)
{
uchar code hex_c[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0,10,11,12,13,14,15};
return((hex_c[c0-0x30]<<4)+hex_c[c1-0x30]);
}
//****************************************************
//從串口接收HEX文件并寫入單片機(jī)
downfile()
{
uchar c1,c0,i,n_of_line,gch;
uint counter,countertemp;
uchar cc,statusbit;
uchar receiveflag;
uchar failedcounter;
union {uint addrhl;struct {uchar h,l;}addr;} idata address; //定義現(xiàn)在寫入的地址
union {uint addrhl;struct {uchar h,l;}addr;} idata addressall; //定義已經(jīng)寫入的總數(shù)
unsigned char free[8]; //定義行首處理空間
chiperase();
counter=0;
failedcounter=0;
cc=0;
statusbit=0;
PMR=0x01;
// putstring("\r\nsend the file you will download!");
SFRAH=0x00;
SFRAL=0x00;
SFRCN=0x21;
timer0l=0xcd; //50us
timer0h=0xFF;
TL0=timer0l;
TH0=timer0h;
addressall.addrhl=0; //已經(jīng)寫入的總數(shù)清0
do{
resend:
counter=0x00;
receiveflag=0;
while (!(':'==(gch=getbyte())));
temp[counter]=gch;
counter++;
cc=0;
for(i=0;i!=8;i++) //讀取第一行的行首,8字節(jié)
{
free[i]=temp[counter]=getbyte();
counter++;
}
n_of_line=hextochar(free[0],free[1]); //分析行首,解出行字節(jié)總數(shù)及行首址
address.addr.h=hextochar(free[2],free[3]); //高位地址
address.addr.l=hextochar(free[4],free[5]); //底位地址
statusbit=hextochar(free[6],free[7]);
cc=n_of_line+address.addr.h+address.addr.l+statusbit;
do //大循環(huán),行總數(shù)為0時停止,此時文件結(jié)束
{
do //小循環(huán),每行一個循環(huán)
{
c1=temp[counter]=getbyte();
counter++;
c0=temp[counter]=getbyte();
counter++; //取兩個HEX數(shù)據(jù)
c0=hextochar(c1,c0);
cc+=c0;
n_of_line--; //該行剩下的字節(jié)減一
}while(n_of_line>0); //寫完一行退出該循
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -