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

📄 main._c

📁 AVR mage8单片机的bootload程序、完整版本、本人做项目一下在用、此程序为8m晶振版本、非常好用
💻 _C
📖 第 1 页 / 共 2 页
字号:
void TxChar(unsigned char ch);
void Wait(void);
char HomeNet(void);
void main(void);

/*
///////////////////////////////////////////////////////////////////////////////
#define WATCHDOG 4

void Watchdog_Feed( void );

unsigned char watchdog_feed_flag;
*/
///////////////////////////////////////////////////////////////////////////////

/*****************************************************************************/
/*           G L O B A L    V A R I A B L E S			                     */
/*****************************************************************************/	
unsigned char PageBuffer[PageByte];
unsigned int PageAddress;
unsigned int RealPageAddress;


/*
void Watchdog_Feed( void )
{
	  if ( watchdog_feed_flag == 0 )
	  {
	  	  watchdog_feed_flag = 1;
	  	  PORTC |= 1 << WATCHDOG;  
	  }
	  else
	  {
	  	  watchdog_feed_flag = 0;
	  	  PORTC &= ~( 1 << WATCHDOG );
	  }	  
}
*/
/*****************************************************************************/
/* Flash Programing Code								                     */
/*****************************************************************************/
void FlashLoad(void)
{
    TxChar('!');
    while (1)
    {
	      GetPageNumber();

	      if (RealPageAddress == 0xFFFF) return;
//		  Watchdog_Feed();

	      if (GetPage())
		    {
		        WriteFlash();
		        if (CheckFlash()) 
				    TxChar('!');
		        else TxChar('@');
	    	}
	      else TxChar('@');
	 }
}

/*****************************************************************************/

void GetPageNumber(void)
{
unsigned char PageAddressHigh;
unsigned char PageAddressLow;

while(!IsChar());
    WDR();
PageAddressHigh = RxChar();

while(!IsChar());
    WDR();
PageAddressLow = RxChar();

RealPageAddress = (int)((PageAddressHigh << 8) + PageAddressLow);
PageAddress = RealPageAddress << NSHIFTPAGE;

#ifdef RAMPZ_FLAG
if (PageAddressHigh) RAMPZ = 1; 
else RAMPZ = 0;
#endif
}

/*****************************************************************************/

char GetPage(void)
{
unsigned int i;
unsigned char LocalCheckSum = 0;
unsigned char CheckSum = 0;

for (i = 0 ; i < PageByte ; i++)
	{
	while(!IsChar());
	    WDR();
	PageBuffer[i] = RxChar();
	LocalCheckSum += PageBuffer[i];
   	WDR();
   	}
		   
while(!IsChar());
    WDR();
CheckSum = RxChar(); 
if (LocalCheckSum == CheckSum) return 1;
else return 0;
	
}

/*****************************************************************************/

void WriteFlash(void)
{
    unsigned int i;
    unsigned int TempInt;

    for(i = 0 ; i < PageByte ; i += 2)
   	{
   	    TempInt = PageBuffer[i] + (PageBuffer[i+1]<<8);
   	    fill_temp_buffer(TempInt,i);    //call asm routine.
   	}
	  write_page(PageAddress,0x03);       //Perform page ERASE
    write_page(PageAddress,0x05);       //Perform page write
    enableRWW();
    WDR();
}

/*****************************************************************************/

char CheckFlash(void)
{
    unsigned int i;							
    unsigned int TempInt;
    unsigned int TempInt2;
    WDR();
    for (i = 0 ; i < PageByte ; i += 2)
    {
	      TempInt = read_program_memory(PageAddress + i , 0x00);
	      TempInt2 = PageBuffer[i] +(PageBuffer[i+1] << 8);
	      if (TempInt != TempInt2) return 0;
    }
    return 1;
}

/*****************************************************************************/
/* EEprom Programing Code								                     */
/*****************************************************************************/

#ifdef EEPROM
void EEpromLoad()
{
unsigned char ByteAddressHigh;
unsigned char ByteAddressLow;
unsigned int ByteAddress;
unsigned char Data;
unsigned char LocalCheckSum;
unsigned char CheckSum;

TxChar(')');
TxChar('!');
while (1)
	  {
	  WDR();
	  LocalCheckSum = 0;
	  
	  while(!IsChar());
	  ByteAddressHigh = RxChar();
	  LocalCheckSum += ByteAddressHigh;
	  
	  while(!IsChar());
	  ByteAddressLow = RxChar();
	  LocalCheckSum += ByteAddressLow;
	  
	  ByteAddress = (ByteAddressHigh<<8)+ByteAddressLow;
	  
	  if (ByteAddress == 0xffff) return;
	  
	  while(!IsChar());
	  Data = RxChar();
	  LocalCheckSum += Data;
	  
	  WDR();
	  
	  while(!IsChar());
	  CheckSum = RxChar();	
	  
	  if (CheckSum == LocalCheckSum)	
	  	 {  
		 EEPROMwrite(ByteAddress, Data);
	  	 if (EEPROMread(ByteAddress) == Data) TxChar('!');
	  	 else TxChar('@');
		 }
	  else
	     {
		 TxChar('@');
		 }
	  }
}
#endif



#ifdef EEPROM
void EEPROMwrite( int location, unsigned char byte)
{
while (EECR & 0x02);   // Wait until any earlier write is done
EEAR = location;
EEDR = byte;
EECR |= 0x04;          // Set MASTER WRITE enable
EECR |= 0x02;          // Set WRITE strobe
WDR();
} 
#endif         



#ifdef EEPROM
unsigned char EEPROMread( int location)
{
//WDR();
while (EECR & 0x02);            
EEAR = location;
EECR |= 0x01;                       // Set READ strobe
return (EEDR);                      // Return byte
}
#endif 


/*****************************************************************************/
/* LockBit Code										                         */
/*****************************************************************************/


#ifdef LOCKBIT
void LockBit(void)
{
unsigned char Byte;
WDR();
TxChar('%');

while(!IsChar());
Byte = RxChar();

while(!IsChar());
if (Byte == ~RxChar()) write_lock_bits(~Byte);
}
#endif


/*****************************************************************************/
/* Serial Port Code										                     */
/*****************************************************************************/
unsigned char IsChar(void)
{
    if(UCSRA & 0x80) 
    	  return 1;
    else 
    	  return 0;
}

/*****************************************************************************/
unsigned char RxChar(void)
{
    return UDR;
}

/*****************************************************************************/

void TxChar(char ch)
{
    while(!(UCSRA & 0x20));  // wait for empty transmit buffer
	
	RS485PORT |= RS485TXE;
	UDR = ch;     	 		     // write char
	while(!(UCSRA & 0x40));
	UCSRA |= 0x40;
	RS485PORT &= ~RS485TXE;	
  WDR(); 
}

/*****************************************************************************/
/* Helpers Code											                     */
/*****************************************************************************/
void Wait()
{
int i;
#ifdef LOW 
for (i=0;i<32000;i++);
WDR();
#endif
#ifdef HIGH 
for (i=-32000;i<32000;i++);
WDR();
#endif
}

/*****************************************************************************/

void SendDeviceID(void)
{

    TxChar(DeviceID);
    TxChar(FlashSize);
    TxChar(BootSize);
    TxChar(PageSize);
    TxChar(EEpromSize);
    RxChar();
    WDR();
}

/*****************************************************************************/

void ExecCode(void)
{
#ifdef RAMPZ_FLAG 
    RAMPZ = 0;
#endif
    INTVECREG = 0x01;	   	 // Enable interrupt vector select
    INTVECREG = 0x00;	   	 // Move interrupt vector to flash
    asm("jmp 0x0000");       // Run application code   
}



void main(void)
{
    unsigned char i;
    CLI();          		      // disable all interrupts
    
    WDTCR |= ( 1 << WDCE ) | ( 1 << WDE ) ;
    WDTCR = 0x07 ;
    
	PULLUPPORT = PULLUPPIN;       // Pull up on RX line

    UCSRB = 0x00; 				         //disable while setting baud rate
    UCSRA = 0x00; 
    UCSRC = 0x86; 				         // Asyn,NoParity,1StopBit,8Bit,
    UBRRH = 0;     //set baud rate hi
    UCSRB = 0x18; 				         // Rx enable Tx Enable
#ifdef RS485DDR
	RS485DDR |= RS485TXE ;
    RS485PORT &= ~RS485TXE ;
#endif	
#ifdef BAUDRATE
    {
        UBRRL = BAUDRATE;
#else
for (i = 5; i < 20; ++i)
    {
    	  WDR();
        UBRRL = i;
#endif

        RxChar();
	    TxChar('>');
	    Wait();
	    if (RxChar() == '<') 
	    {
	        SendDeviceID();
	        FlashLoad();
//	        Watchdog_Feed();
       #ifdef EEPROM
	        EEpromLoad();
//	        Watchdog_Feed();
	   #endif
	   #ifdef LOCKBIT
	        LockBit();
	   #endif
	        ExecCode();
	    }
	}
    ExecCode();
}


//This program is ok,finished on 16:00 9/12/2006.

⌨️ 快捷键说明

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