⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bootloader.c

📁 W78E516B的BOOT程序
💻 C
字号:
#include "reg54.h"
#include "serint.h"
#include "bootloader.h"
#include "sysWatchDog.h"
#include "stdio.h"

bit nTimeCountFlag = 0;
bit bEndProgFlag = 0;
extern union add
{
	unsigned int nCounter;		//address is correct?
	struct {unsigned char ah;unsigned char al;}half;
}ADDR;

main()
{
	unsigned char nTL0Buf,nTH0Buf;
//Main_4K: if CHPCON.7 = 1, the CPU is work on F04Kmode, 
//if CHPCON.7 = 0, it is switch to LDROM from APROM
//if is F04KMode, the CPU can't reset correctly by software, yun must take a note

	if(CHPCON&0x80)
	{
		CHPENR = 0x87;
		CHPENR = 0x59;
		CHPCON	= 0x03;
	}
	CHPENR	= 0x00;
	TCON	= 0x00;
	TMOD	= 0x01;
	IP		= 0x00;
	ET0		= 1;			//enable timer 0 interrupt
	EA		= 1;
	nTL0Buf = 0xF0;
	nTH0Buf = 0xFF;
	TL0		= nTL0Buf;
	TH0		= nTH0Buf;
	TR0 	= 1;		//TR=1, start timer0
	PCON 	= 0x01;		//enter idle mode

//	while(1)P1=~P1;
	CleanupWatchDog();
	SerInitialize();
	Timer0_Init();

//	erase operation...
	CHPENR = 0x00;		//disable chpcon write-attribute
//	TCON = 0x00;
	IP = 0x00;
	ET0 = 1;
	EA = 1;
//	TMOD = 0x01;
	nTL0Buf = 0;//0x3c;		//set wake-up time for erase operation, about 15ms at 40MHz
	nTH0Buf = 0;//0xB0;
	TL0 = nTL0Buf;
	TH0 = nTH0Buf;
	TR0 = 1;		//TR=1, start timer0
	SFRCN = 0x22;		//SFRCN(C7H)=22H, Erase 64K
	PCON |= 0x01;		//enter idle mode
	TR0 = 0;
	TI = 1;
	putstr("Erase OK !\n\r");
	//Blank Check
	SFRCN	= 0x00;
	SFRAH	= 0x00;
	SFRAL	= 0x00;
//	nTL0Buf = 0xFB;
//	nTH0Buf = 0xFF;
	//Blank check loop
	do
	{
	TL0 	= 0xfb;//nTL0Buf;
	TH0 	= 0xff;//nTH0Buf;
		TR0 = 1;
		PCON 	|= 0x01;		//Enter to idle mode
		TR0 	= 0;			//enable Timer 0 
		if(SFRFD!=0xff)			//blank check error
		{
			putchar(SFRFD+0x30);
			putstr("Blank Check error, Maybe the flash Rom is bad or Reset to retry! Press any key to reboot \n\r");
			RI = 0;
			while(RI);
			CHPENR = 0x87;		//Reset CPU
			CHPENR = 0x59;
			CHPCON = 0x83;
			TR0	   = 0;

			break;
		}
		else
		{
			SFRAL++;
			if(!SFRAL)SFRAH++;
		}
	}while(SFRAH|SFRAL);
	if(!(SFRAH|SFRAL))putstr("Blank Check OK !\n\r");
//display "You can program your routine now !"
//	TI = 1;
	putstr("You can program your routine now !\n\r");
//Enable all interrupts, waiting for program...
	nTimeCountFlag = 1;
	ES	= 1;
	ET0 = 1;
	EA 	= 1;
	while(1)
	{
/*		if(bEndProgFlag)
		{
			while(ADDR.nCounter)
			{
				SFRAH = ADDR.half.ah;
				SFRAL = ADDR.half.al;
				ADDR.nCounter++;
				SFRCN = 0x21;
				TR0   = 1;
				SFRFD = 0;
				PCON |=0x01;
			}
			ES = 0;
			TI = 1;
			putstr("Program End, You should manual reset CPU...");
			RI = 0;
			while(!RI);
			CHPENR = 0x87;		//Reset CPU
			CHPENR = 0x59;
			CHPCON = 0x83;
			TR0	   = 0;

		}
*/		PCON |= 0x01;	//enter idle mode...
	}

}
//you should call this subroutine when you need to download your program.
//At first, you must burn this isp-program to 4K LDROM.
void Boot2LDROM()
{
	CHPENR = 0x87;
	CHPENR = 0x59;
	CHPCON = 0x03;
	TCON   = 0;
	TMOD   = 1;
	IP	   = 0;
	IE	   = 0x82;
	TL0	   = 0xF0;
	TH0    = 0xFF;
	TR0    = 1;
	PCON   = 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -