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

📄 avricp.c

📁 AVR ICP 支持AVR器件编程
💻 C
📖 第 1 页 / 共 4 页
字号:

#define CPU         __AVR_ATmega16__
#define F_CPU       7372800L             /* CLK = 7.372800MHz       */ 
#define __OPTIMIZE__

#define _CV(bit)      (~(1 << (bit)))
#define TRUE          (1==1) 
#define FALSE         (1==0) 

#include <avr/io.h>
#include <string.h>
#include <util/delay.h>

#include <avr/pgmspace.h>

typedef unsigned char uchar;
typedef unsigned int  uint;

/*--------------------------------------------------------------------------*/ 
/* I/O port setting                                                         */ 
/* ---- ----- ------------------------------------------------------------- */ 
/* PD2: RST                                                                 */
/* PB5: MOSI                                                                */
/* PB6: MISO                                                                */
/* PB7: SCK                                                                 */
/* ---- ----- ------------------------------------------------------------- */ 
/* PA4: OUT    GREEN LED                                                    */
/* PA5: OUT    RED   LED                                                    */
/* ---- ----- ------------------------------------------------------------- */ 
/* PD0: IN     UART  RxD                                                    */ 
/* PD1: OUT    UART  TxD                                                    */ 
/* ---- ----- ------------------------------------------------------------- */ 

#define TARGET_RESET     PD2 
#define TARGET_MISO      PB6 
#define TARGET_MOSI      PB5 
#define TARGET_SCK       PB7

#define HOST_LED         PA5
#define MON_LED          PA4


//FOR AT89C51
#define  RST             PA6
#define  HIGHVP          PA0
#define  VP              PA1
#define  PROG            PA2
#define  PSEN            PA3

#define  P26             PD6
#define  P27             PD7
#define  P36             PD6
#define  P37             PD7

#define  RDY51           PA7
#define  A14             P3^0 


/*--------------------------------------------------------------------------*/ 
/* revision information                                                     */ 
/*--------------------------------------------------------------------------*/ 
#define SW_MAJOR      '2'   /* Major Software revision number   */ 
#define SW_MINOR      '8'   /* Minot Software revision number   */ 
#define HW_MAJOR      '1'   /* Major Hardware revision number   */ 
#define HW_MINOR      '8'   /* Minot Hardware revision number   */ 

/*--------------------------------------------------------------------------*/ 
/* device information                                                       */ 
/*--------------------------------------------------------------------------*/ 
#define AT89C1051			0x80 
#define AT89C2051           0x81 
#define AT89C51             0x82	  //自定义	
#define AT89C52             0x83	  //自定义	
#define AT89S51             0x84	  //自定义	
#define AT89S52             0x85	  //自定义	
#define AT89S53             0x87 
#define AT89S8252           0x86 
#define AT90C8534           0x70
#define AT90C8544           0x71
#define AT90S1200     		0x13
#define AT90S1200REVA       0x10
#define AT90S1200REVB       0x11
#define AT90S1200REVC       0x12 
#define AT90S2313           0x20
#define AT90S2323           0x48
#define AT90S2333           0x34
#define AT90S2343           0x4C
#define AT90S4414			0x28
#define AT90S4433           0x30
#define AT90S4434           0x6C
#define AT90S8515           0x38
#define AT90S8535           0x68
#define ATMEGA103           0x41
#define ATMEGA128           0x43
#define ATMEGA128BOOT       0x44
#define ATMEGA16			0x74
#define ATMEGA16BOOT        0x75
#define ATMEGA161           0x60
#define ATMEGA161BOOT       0x61
#define ATMEGA163           0x64
#define ATMEGA163BOOT       0x66
#define ATMEGA169           0x78
#define ATMEGA169BOOT       0x79
#define ATMEGA32			0x72
#define ATMEGA32BOOT        0x73
#define ATMEGA603           0x42
#define ATMEGA64            0x45
#define ATMEGA64BOOT        0x46
#define ATMEGA8             0x76
#define ATMEGA8BOOT         0x77
#define ATMEGA83            0x65
#define ATMEGA83BOOT		0x67
#define ATMEGA8515          0x3A
#define ATMEGA8515BOOT      0x3B
#define ATMEGA8535          0x69
#define ATTINY10            0x51
#define ATTINY11            0x50
#define ATTINY12            0x55
#define ATTINY15            0x56
#define ATTINY19			0x58
#define ATTINY26            0x5E
#define ATTINY28            0x5C

/* Device List    */ 
/* Device List                                                              */ 
/* +---------------+----------+------+-------+------+------+------+-------+ */ 
/* | Device        |Signature | Code | Flash |EEProm| Lock | Fuse | PMode | */ 
/* +---------------+----------+------+-------+------+------+------+-------+ */ 
/* | tiny12        | 1E 90 05 | 0x55 |  R/W  | R/W  | R/W  | R/W  | Byte  | */ 
/* | tiny15        | 1E 90 06 | 0x66 |  R/W  | R/W  | R/W  | R/W  | Byte  | */ 
/* |               |          |      |       |      |      |      |       | */ 
/* | S1200         | 1E 90 01 | 0x13 |  R/W  | R/W  |  W   | NA   | Byte  | */ 
/* |               |          |      |       |      |      |      |       | */ 
/* | S2313         | 1E 91 01 | 0x20 |  R/W  | R/W  |  W   | NA   | Byte  | */ 
/* | S2323         | 1E 91 02 | 0x48 |  R/W  | R/W  | R/W  | R/W  | Byte  | */ 
/* | S2333         | 1E 91 05 | 0x34 |  R/W  | R/W  | R/W  | R/W  | Byte  | */ 
/* | S2343         | 1E 91 03 | 0x4C |  R/W  | R/W  | R/W  | R/W  | Byte  | */ 
/* |               |          |      |       |      |      |      |       | */ 
/* | S4414         | 1E 92 01 | 0x28 |  R/W  | R/W  |  W   | NA   | Byte  | */ 
/* | S4433         | 1E 92 03 | 0x30 |  R/W  | R/W  | R/W  | R/W  | Byte  | */ 
/* | S4434         | 1E 92 02 | 0x6C |  R/W  | R/W  | R/W  | R/W  | Byte  | */ 
/* |               |          |      |       |      |      |      |       | */ 
/* | S8515         | 1E 93 01 | 0x38 |  R/W  | R/W  |  W   | NA   | Byte  | */ 
/* | S8535         | 1E 93 03 | 0x68 |  R/W  | R/W  | R/W  | R/W  | Byte  | */ 
/* |               |          |      |       |      |      |      |       | */ 
/* |mega83         | 1E 93 05 | 0x65 |  R/W  | R/W  | R/W  | R/W  | Page  | */ 
/* |mega103        | 1E 97 01 | 0x41 |  R/W  | R/W  | R/W  | R/W  | Page  | */ 
/* |mega161        | 1E 94 01 | 0x60 |  R/W  | R/W  | R/W  | R/W  | Page  | */ 
/* |mega163        | 1E 94 02 | 0x64 |  R/W  | R/W  | R/W  | R/W  | Page  | */ 
/* |               |          |      |       |      |      |      |       | */ 
/* |mega8          | 1E 93 07 | 0x76 |  R/W  | R/W  | R/W  | R/W  | Page  | */ 
/* |mega16         | 1E 94 03 | 0x74 |  R/W  | R/W  | R/W  | R/W  | Page  | */ 
/* |mega32         | 1E 95 02 | 0x72 |  R/W  | R/W  | R/W  | R/W  | Page  | */ 
/* |mega128        | 1E 97 02 | 0x43 |  R/W  | R/W  | R/W  | R/W  | Page  | */ 
/* |mega8515       | 1E 93 06 | 0x3A |  R/W  | R/W  | R/W  | R/W  | Page  | */ 
/* +---------------+----------+------+-------+------+------+------+-------+ */ 
//ATMEGA8     , 0x1E,0x93,0x07,
//ATMEGA16    , 0x1E,0x94,0x03,
//ATMEGA32    , 0x1E,0x95,0x02,
//ATMEGA64    , 0x1E,0x96,0x02,
//ATMEGA128   , 0x1E,0x97,0x02,
//ATMEGA8515  , 0x1E,0x93,0x06,
//ATMEGA8535  , 0x1E,0x93,0x08,

typedef struct _devices_info { 
	unsigned char   code; 
	
	unsigned int    wPageSize;		// Flash page size
	unsigned char   ucEepromPageSize;// Eeprom page size (extended parameter)
	
	unsigned char   twd_fuse; 
	unsigned char   twd_flash; 
	unsigned char   twd_eeprom; 
	unsigned char   twd_erase; 
	
} devices_info; 



const devices_info /*__ATTR_PROGMEM__*/  g_deviceSupported[] = 
{ 
	// code         , FPage,EPage,twd fuse lock,flash,eeprom,erase 
	
	{  ATTINY12     ,    0 ,   0 ,         2 ,   4 ,    7 ,   7 },
	{  ATTINY15     ,    0 ,   0 ,         2 ,   5 ,    9 ,   9 },   
                                     
	{  AT90S1200    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },   
                                     
	{  AT90S2313    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },   
	{  AT90S2323    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },  
	{  AT90S2333    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },   
	{  AT90S2343    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },   
                                     
	{  AT90S4414    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },   
	{  AT90S4433    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },   
	{  AT90S4434    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },  
                                     
	{  AT90S8515    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },   
	{  AT90S8535    ,    0 ,   0 ,         2 ,   9 ,    9 ,  18 },   
                                     
	{  ATMEGA103    ,  256 ,   0 ,         2 ,  56 ,    9 ,  56 },    
	{  ATMEGA161    ,  128 ,   0 ,         2 ,  14 ,    4 ,  28 },    
	{  ATMEGA163    ,  128 ,   0 ,         2 ,  16 ,    4 ,  32 },    
                                     
	{  ATMEGA8      ,   64 ,   4 ,         5 ,   5 ,    9 ,   9 },
	{  ATMEGA16     ,  128 ,   4 ,         5 ,   5 ,    9 ,   9 },
	{  ATMEGA32     ,  128 ,   4 ,         5 ,   5 ,    9 ,   9 },
	{  ATMEGA64     ,  128 ,   8 ,         5 ,   5 ,    9 ,   9 },
	{  ATMEGA128    ,  256 ,   8 ,         5 ,   5 ,    9 ,   9 },
	{  ATMEGA8515   ,   64 ,   4 ,         5 ,   5 ,    9 ,   9 },
	{  ATMEGA8535   ,   64 ,   4 ,         5 ,   5 ,    9 ,   9 },
	                                 
	{  ATMEGA16BOOT ,  128 ,   4 ,         5 ,   5 ,    9 ,   9 },
//自定义	                             
	{  AT89C51      ,    0 ,   0 ,         0 ,   2 ,    0 ,  10 },
	{  AT89C52      ,    0 ,   0 ,         0 ,   2 ,    0 ,  10 },
	{  AT89S51      ,    0 ,   0 ,         2 ,   2 ,    0 , 250 }, //twd_erase = 250*2
	{  AT89S52      ,    0 ,   0 ,         2 ,   2 ,    0 , 250 }, //twd_erase = 250*2
	{  0x00 ,0x00, 0x00    }        
};





unsigned char G_device;
unsigned char G_index;
unsigned char G_pgmode;
unsigned char G_pgaddrh;
unsigned char G_pgaddrl;

unsigned char G_P2;
unsigned char G_P3;
unsigned char G_CON;


/*--------------------------------------------------------------------------*/ 
/* Port direction setting                                                   */ 
/*--------------------------------------------------------------------------*/ 
void InitPortForIdle(void)
{
	DDRA = _BV(PA4)|_BV(PA5); 
	DDRB = 0; 
	DDRC = 0; 
	DDRD = _BV(PD2); 
}

void InitPortForSPI(void)
{
	DDRA = _BV(PA4)|_BV(PA5);
	DDRB = 0xFF ^ _BV(TARGET_MISO);
	DDRC = 0;            
	DDRD = _BV(PD2);   
}

void InitPortForParallel(void)
{
	DDRA = _BV(PA0)|_BV(PA1)|_BV(PA2)|_BV(PA3)|_BV(PA4)|_BV(PA5)|_BV(PA6);
	DDRB = 0;
	DDRC = 0; 
	PORTC = 0xff; 
	DDRD = _BV(PD3)|_BV(PD4)|_BV(PD5)|_BV(PD6)|_BV(PD7);
	PORTD = 0; 
}

/*--------------------------------------------------------------------------*/ 
/* Port operation                                                           */ 
/*--------------------------------------------------------------------------*/ 
void OutP0(unsigned char data)
{
	DDRC  = 0xff;
	PORTC = data;
}
void OutP1(unsigned char data)
{
	DDRB  = 0xff;
	PORTB = data;
}

void OutP2(unsigned char data)
{
	G_P2 = data;
	PORTA = (PORTA & 0xc0)| (data & 0x3f);
	unsigned char tmp = (data & 0xc0);
	PORTD = tmp;
	asm("nop");
	PORTD = tmp | _BV(PD5);
	asm("nop");
	PORTD = tmp;
}
void OutP3(unsigned char data)
{
	G_P3 = data;
	PORTA = (PORTA & 0xc0)| (data & 0x3f);
	unsigned char tmp = (data & 0xc0);
	PORTD = tmp;
	asm("nop");
	PORTD = tmp | _BV(PD4);
	asm("nop");
	PORTD = tmp;
}
void OutCON(unsigned char data)
{
	G_CON = data;
	PORTA = (PORTA & 0xc0)| (data & 0x3f);
	unsigned char tmp = (data & 0xc0);
	PORTD = tmp;
	asm("nop");
	PORTD = tmp | _BV(PD3);
	asm("nop");
	PORTD = tmp;
}
void OutP2Bit(unsigned char pos,unsigned char b)
{
	unsigned char data;
	data =(G_P2 & _CV(pos)) | ( b << pos);
	OutP2(data);
}
void OutP3Bit(unsigned char pos,unsigned char b)
{
	unsigned char data;
	data =(G_P3 & _CV(pos)) | ( b << pos);
	OutP3(data);
}
void OutCONBit(unsigned char pos,unsigned char b)
{
	unsigned char data;
	data =(G_CON & _CV(pos)) | ( b << pos);
	OutCON(data);
}

#define  SetVPP12V()   OutCONBit(HIGHVP,1)
#define  SetVPP5V()    OutCONBit(HIGHVP,0)

#define  VPP(b)        OutCONBit(VP,b)



void OutAddress(unsigned char addrh,unsigned char addrl)
{
    
    OutP2( (G_P2 & 0xC0) | (addrh & 0x3f));
    OutP1(addrl);
    return;
}




/*--------------------------------------------------------------------------*/ 
/* Reset to High                                                            */ 
/*--------------------------------------------------------------------------*/ 
inline void SetReset(void) 
{ 
	PORTD|=_BV(TARGET_RESET); 
} 

/*--------------------------------------------------------------------------*/ 
/* Reset to low                                                             */ 
/*--------------------------------------------------------------------------*/ 
inline void ClrReset(void) 
{ 
	PORTD&=_CV(TARGET_RESET); 

⌨️ 快捷键说明

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