📄 main._c
字号:
//*****************************************************************************
//* BootLoader 3.1 for MegaLoad 3.0
//*
//* Devices supported at this time
//* ATMega8
//* ATMega16
//* ATMega32
//* ATMega64
//* ATMega128
//* ATMega162
//* ATMega169
//* ATMega8515
//* ATMega8535
//* ATMega2313 not tested, please give me feedback!
//*
//* Version 3.0 Dec 2003
//* 1.0 : First Release
//* 1.1 : Add auto baud rate, code smaller
//* 1.2 : Correct a bug with file larger than 65280 bytes.
//* 2.0 : All in one source file that fit in 512 Words
//* 3.0 : Add EEprom programming
//* Add RS485 half duplex support
//* Add LockBit programming
//* 3.1 : Correct a problem with interupt vector for M8/16/32/162/8515/8535
//* Change some #define to be more clear
//*
//* Sylvain.Bissonnette@MicroSyl.com
//*****************************************************************************
//
// To setup the bootloader for your project you must
// remove the comment below to fit with your hardware
// recompile it using ICCAVR setup for bootloader
// of 512 word for ONLY flash programming
// or 1024 word for flash and eeprom programming
//
//*****************************************************************************
// MCU selection
// -->Do the same thing in assembly.s<--
//*****************************************************************************
#define MEGATYPE 8
//#define MEGATYPE 16
//#define MEGATYPE 32
//#define MEGATYPE 64
//#define MEGATYPE 128
//#define MEGATYPE 162
//#define MEGATYPE 169
//#define MEGATYPE 8515
//#define MEGATYPE 8535
//#define MEGATYPE 2313
//*****************************************************************************
// Bootload on UART x
//*****************************************************************************
//#define UART 0
//#define UART 1
//*****************************************************************************
// BaudRate
// If you don't specify the baudrate divisor the bootloader
// will automaticaly be in AutoBaudRate mode
//*****************************************************************************
//#define BAUDRATE 87
//#define BAUDSETTING (unsigned int)((unsigned long)(fosc)/(16*(unsigned long)(BAUDRATE)))-1
//#define BAUD_H (unsigned char)(BAUDSETTING>>8)
//#define BAUD_L (unsigned char) BAUDSETTING
//*****************************************************************************
// Crystal speed
// frequancy of your MCU speed
// LOW -> Xtal < 8Mhz
// HIGH -> Xtal >= 8Mhz
//*****************************************************************************
//#define LOW
#define HIGH
//*****************************************************************************
// EEprom programming
// enable EEprom programing via bootloader
// YOU MUST SET TO 1024 WORD THE BOOTLOADER SIZE IF EEPROM
// PROGRAMMING IS USE
//*****************************************************************************
#define EEPROM
//*****************************************************************************
// LockBit programming
// enable LOCKBIT programing via bootloader
//*****************************************************************************
//#define LOCKBIT
//*****************************************************************************
// RS485
// if you use RS485 half duplex for bootloader
// make the appropriate change for RX/TX transceiver switch
//*****************************************************************************
#define RS485DDR DDRD
#define RS485PORT PORTD
#define RS485TXE 0x10
//*****************************************************************************
// HomeNet
// Only use for Sylvain Bissonnette personal projects
// For normal use don't define HOMENET
//*****************************************************************************
//#define HOMENET
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//!!!!!!!!!!!!!! DO NOT CHANGE ANYTHING BELOW THIS LINE !!!!!!!!!!!!!!!!!!!!!!!
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <macros.h>
#define Tiny2313 'O'
#define Tiny13 'P'
#define Mega8 'A'
#define Mega16 'B'
#define Mega32 'E'
#define Mega48 'L'
#define Mega64 'C'
#define Mega88 'M'
#define Mega128 'D'
#define Mega162 'F'
#define Mega163 'J'
#define Mega168 'N'
#define Mega169 'G'
#define Mega323 'K'
#define Mega8515 'H'
#define Mega8535 'I'
#define Flash1k 'g'
#define Flash2k 'h'
#define Flash4k 'i'
#define Flash8k 'l'
#define Flash16k 'm'
#define Flash32k 'n'
#define Flash64k 'o'
#define Flash128k 'p'
#define Flash256k 'q'
#define EEprom64 '.'
#define EEprom128 '/'
#define EEprom256 '0'
#define EEprom512 '1'
#define EEprom1024 '2'
#define EEprom2048 '3'
#define EEprom4096 '4'
#define Boot128 'a'
#define Boot256 'b'
#define Boot512 'c'
#define Boot1024 'd'
#define Boot2048 'e'
#define Boot4096 'f'
#define Page32 'Q'
#define Page64 'R'
#define Page128 'S'
#define Page256 'T'
#define Page512 'U'
#if !(defined MEGATYPE)
#error "Processor Type is Undefined"
#endif
#if (((MEGATYPE == 64) || (MEGATYPE == 128) || (MEGATYPE == 162)) && !(defined UART))
#error "UART ch must be defined for this part"
#endif
#ifdef EEPROM
#define BootSize Boot1024
#endif
#ifndef EEPROM
#define BootSize Boot512
#endif
#if (MEGATYPE == 8)
#include <iom8v.h>
#define DeviceID Mega8
#define FlashSize Flash8k
#define PageSize Page64
#define EEpromSize EEprom512
#define PageByte 64
#define NSHIFTPAGE 6
#define INTVECREG GICR
#define PULLUPPORT PORTD
#define PULLUPPIN 0x01
#endif
#if (MEGATYPE == 16)
#include "iom16v.h"
#define DeviceID Mega16
#define FlashSize Flash16k
#define PageSize Page128
#define EEpromSize EEprom512
#define PageByte 128
#define NSHIFTPAGE 7
#define INTVECREG GICR
#define PULLUPPORT PORTD
#define PULLUPPIN 0x01
#endif
#if (MEGATYPE == 32)
#include "iom32v.h"
#define DeviceID Mega32
#define FlashSize Flash32k
#define PageSize Page128
#define EEpromSize EEprom1024
#define PageByte 128
#define NSHIFTPAGE 7
#define INTVECREG GICR
#define PULLUPPORT PORTD
#define PULLUPPIN 0x01
#endif
#if (MEGATYPE == 64)
#include "iom64v.h"
#define DeviceID Mega64
#define FlashSize Flash64k
#define PageSize Page256
#define EEpromSize EEprom2048
#define PageByte 256
#define NSHIFTPAGE 8
#define INTVECREG MCUCR
#if (UART == 0)
#define PULLUPPORT PORTE
#define PULLUPPIN 0x01
#endif
#if (UART == 1)
#define PULLUPPORT PORTD
#define PULLUPPIN 0x04
#endif
#endif
#if (MEGATYPE == 128)
#include "iom128v.h"
#define DeviceID Mega128
#define FlashSize Flash128k
#define PageSize Page256
#define EEpromSize EEprom4096
#define PageByte 256
#define NSHIFTPAGE 8
#define INTVECREG MCUCR
#define RAMPZ_FLAG
#if (UART == 0)
#define PULLUPPORT PORTE
#define PULLUPPIN 0x01
#endif
#if (UART == 1)
#define PULLUPPORT PORTD
#define PULLUPPIN 0x04
#endif
#endif
#if (MEGATYPE == 162)
#include "iom162v.h"
#define DeviceID Mega162
#define FlashSize Flash16k
#define PageSize Page128
#define EEpromSize EEprom512
#define PageByte 128
#define NSHIFTPAGE 7
#define INTVECREG GICR
#if (UART == 0)
#define PULLUPPORT PORTD
#define PULLUPPIN 0x01
#endif
#if (UART == 1)
#define PULLUPPORT PORTB
#define PULLUPPIN 0x04
#endif
#endif
#if (MEGATYPE == 169)
#include "iom169v.h"
#define DeviceID Mega169
#define FlashSize Flash16k
#define PageSize Page128
#define EEpromSize EEprom512
#define PageByte 128
#define NSHIFTPAGE 7
#define INTVECREG MCUCR
#define PULLUPPORT PORTE
#define PULLUPPIN 0x01
#endif
#if (MEGATYPE == 8515)
#include "iom8515v.h"
#define DeviceID Mega8515
#define FlashSize Flash8k
#define PageSize Page64
#define EEpromSize EEprom512
#define PageByte 64
#define NSHIFTPAGE 6
#define INTVECREG GICR
#define PULLUPPORT PORTD
#define PULLUPPIN 0x01
#endif
#if (MEGATYPE == 8535)
#include "iom8515v.h"
#define DeviceID Mega8535
#define FlashSize Flash8k
#define PageSize Page64
#define EEpromSize EEprom512
#define PageByte 64
#define NSHIFTPAGE 6
#define INTVECREG GICR
#define PULLUPPORT PORTD
#define PULLUPPIN 0x01
#endif
#if (MEGATYPE == 2313)
#include "iot2313v.h"
#define DeviceID Tiny2313
#define FlashSize Flash2k
#define PageSize Page32
#define EEpromSize EEprom128
#define PageByte 32
#define NSHIFTPAGE 5
#define PULLUPPORT PORTD
#define PULLUPPIN 0x01
#endif
#ifndef UDR
#if (UART == 0)
#define UCSRA UCSR0A
#define UCSRB UCSR0B
#define UCSRC UCSR0C
#define UBRRL UBRR0L
#define UBRRH UBRR0H
#define UDR UDR0
#endif
#if (UART == 1)
#define UCSRA UCSR1A
#define UCSRB UCSR1B
#define UCSRC UCSR1C
#define UBRRL UBRR1L
#define UBRRH UBRR1H
#define UDR UDR1
#endif
#endif
#define FALSE 0
#define TRUE 1
/*********************************************************/
/* I N C L U D E */
/*********************************************************/
#include "assembly.h"
/*********************************************************/
/* P R O T O T Y P E */
/*********************************************************/
void FlashLoad(void);
void EEpromLoad(void);
void WriteEEprom(void);
char CheckEEprom(void);
void EEPROMwrite(int location, unsigned char byte);
unsigned char EEPROMread(int location);
void SendDeviceID(void);
void GetPageNumber(void);
void ExecCode(void);
char GetPage(void);
void WriteFlash(void);
char CheckFlash(void);
void LockBit(void);
unsigned char IsChar(void);
unsigned char RxChar(void);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -