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

📄 avricp.c

📁 AVR ICP 支持AVR器件编程
💻 C
📖 第 1 页 / 共 4 页
字号:
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
void Set_address(void)
{
    //if(G_pgmode != TRUE)return;
    G_pgaddrh = getc(); 
    G_pgaddrl = getc(); 
    putc(13); 
	return;
}
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* |                                   | ID  | host  | programer  | Note |  */ 
/* | Write program memory, low byte    | 'c' |    dd |      | 13d |   3  |  */ 
/* | Write program memory, high byte   | 'C' |    dd |      | 13d |   3  |  */ 
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* | Write program memory  byte   ,command=40 or 48                      |  */ 
/* +---------------------------------------------------------------------+  */ 
//多功能函数1=======写入========
void Write_program_memory_byte(unsigned char command)
{
    //if(G_pgmode != TRUE)return;
	unsigned char data;
	data = getc();
	if(G_device == AT89C51 || G_device == AT89C52)
	{
		
		VPP(1);
		SetVPP5V();
		PORTA|= _BV(PA6);   //RST=H
		OutCONBit(PSEN,0);  //PSEN=L
		
		OutP2Bit(P26,0);
		OutP3Bit(P36,1);
		OutP3Bit(P37,1);
		OutP2Bit(P27,1);

		Time48Tclcl();

		unsigned int  addr;
		
		addr = G_pgaddrh;
		addr <<= 8;
		addr |= G_pgaddrl;
		addr <<= 1;
		
		if(command == 0x48)	addr++;
		OutAddress(addr >> 8,addr & 0xff);

		OutP0(data);

		SetVPP12V();
		wait_1us(10);
		
		OutCONBit(PROG,0);
		wait_1us(10);
		OutCONBit(PROG,1);
		//Wait51(g_deviceSupported[G_index].twd_flash);
		wait_1ms(g_deviceSupported[G_index].twd_flash);
		SetVPP5V();

	}
	else
	{
		SPIWrite(command); 
		SPIWrite(G_pgaddrh); 
		SPIWrite(G_pgaddrl); 
		SPIWrite(data); 
		//no delay if m103,m83,m603,m161,m163
		if (g_deviceSupported[G_index].wPageSize == 0)
		{ 
			unsigned char waittime = g_deviceSupported[G_index].twd_flash;
			do
			{
				wait_1ms(1);
				SPIWrite((command & 0x0f) | 0x20); 
				SPIWrite(G_pgaddrh); 
				SPIWrite(G_pgaddrl);
				if(data == SPIRead())break;
			}while(waittime--); 
			
		} 
	}
	return;
}
/* +---------------------------------------------------------------------+  */ 
void Write_program_memory_low_byte(void)
{
	Write_program_memory_byte(0x40);
	putc(13); 
	return;
}
/* +---------------------------------------------------------------------+  */ 
void Write_program_memory_high_byte(void)
{
	Write_program_memory_byte(0x48);
	if(!(++G_pgaddrl))G_pgaddrh++;
	putc(13); 
	return;
}


/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* |                                   | ID  | host  | programer  | Note |  */ 
/* | Issue Page Write                  | 'm' |       |      | 13d |      |  */ 
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
void Issue_Page_Write(void)
{
    SPIWrite(0x4c); 
    SPIWrite(G_pgaddrh); 
    SPIWrite(G_pgaddrl); 
    SPIWrite(0x00); 
	wait_1ms(g_deviceSupported[G_index].twd_flash);

    putc(13); 
	return;
}
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* |                                   | ID  | host  | programer  | Note |  */ 
/* | Read program memory               | 'R' |       |dd(dd)|     |   4  |  */ 
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
//多功能函数2
unsigned char ParaReadByte(unsigned char ah,unsigned char al)
{
	DDRC = 0;
	OutAddress(ah,al);
	Time48Tclcl();
	return PINC;
}
void Read_program_memory(void)
{
	if(G_device == AT89C51 || G_device == AT89C52)
	{
		SetVPP5V();
		VPP(1);
		PORTA|= _BV(PA6);   //RST=H
		OutCONBit(PSEN,0);  //PSEN=L
		OutCONBit(PROG,1);
		
		//OutP2Bit(P27,0);
		OutP3Bit(P36,1);
		OutP3Bit(P37,1);

		OutP2Bit(P26,0);
		OutP2Bit(P27,0);
		//Time48Tclcl();


		unsigned int  addr;
		char highbyte,lowbyte;
		addr = G_pgaddrh;
		addr <<= 8;
		addr |= G_pgaddrl;
		addr <<= 1;
		
		//写程序后校验时第一个字节出错,但是单独校验时不出错,
		//所以加入下面这行。
		if(addr == 0) ParaReadByte(0,0);


		lowbyte = ParaReadByte(addr >> 8,addr & 0xff);
		addr++;
		highbyte = ParaReadByte(addr >> 8,addr & 0xff);
		putc(highbyte);
		putc(lowbyte);
	}
	else
	{
		SPIWrite(0x28); 
		SPIWrite(G_pgaddrh); 
		SPIWrite(G_pgaddrl); 
		putc(SPIRead()); 
		SPIWrite(0x20); 
		SPIWrite(G_pgaddrh); 
		SPIWrite(G_pgaddrl); 
		putc(SPIRead()); 
	}
	if(!(++G_pgaddrl))G_pgaddrh++;
	return;
}



/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* |                                   | ID  | host  | programer  | Note |  */ 
/* | Write data memory                 | 'D' |    dd |      | 13d |      |  */ 
/* | Read data memory                  | 'd' |       |   dd |     |      |  */ 
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* | Read data memory byte                                               |  */ 
/* +---------------------------------------------------------------------+  */ 
unsigned char Read_data_memory_byte(void)
{
    SPIWrite(0xA0); 
    SPIWrite(G_pgaddrh); 
    SPIWrite(G_pgaddrl); 
    return(SPIRead()); 
}
/* +---------------------------------------------------------------------+  */ 
void Write_data_memory(void)
{
	unsigned char data;
    data = getc(); 
    SPIWrite(0xC0); 
    SPIWrite(G_pgaddrh); 
    SPIWrite(G_pgaddrl); 
    SPIWrite(data); 
	unsigned char waittime = g_deviceSupported[G_index].twd_eeprom;
	do
	{
		wait_1ms(1);
		if(data != 0xff)if(data == Read_data_memory_byte())break;
    }while(waittime--); 
    if(!(++G_pgaddrl))G_pgaddrh++; 
    putc(0x0D); 
	return;
}
/* +---------------------------------------------------------------------+  */ 
void Read_data_memory(void)
{
    putc(Read_data_memory_byte()); 
    if(!(++G_pgaddrl))G_pgaddrh++; 
	return;
}
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* |                                   | ID  | host  | programer  | Note |  */ 
/* | Chip erase                        | 'e' |       |      | 13d |      |  */ 
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
//多功能函数3
void Chip_erase(void)
{
	if(G_device == AT89C51 || G_device == AT89C52)
	{
		OutCONBit(PROG,0);  //PROG=L

		VPP(1);
		PORTA|= _BV(PA6);   //RST=H
		OutCONBit(PSEN,0);  //PSEN=L
		
		OutP2Bit(P26,1);
		OutP3Bit(P36,0);
		OutP3Bit(P37,0);
		OutP2Bit(P27,0);
		
		Time48Tclcl();
		
		SetVPP12V();
		OutCONBit(PROG,0);  
		
		wait_1ms(g_deviceSupported[G_index].twd_erase);
		OutCONBit(PROG,1);
		SetVPP5V();



	}
	else
	{
		SPIWrite(0xAC); 
		SPIWrite(0x80); 
		SPIWrite(0x04); 
		SPIWrite(0x00); 
		wait_1ms(g_deviceSupported[G_index].twd_erase);  
		if(G_device == AT89S51 || G_device == AT89S52)
			wait_1ms(g_deviceSupported[G_index].twd_erase);
	}
	putc(0x0D); 
	return;
}
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* |                                   | ID  | host  | programer  | Note |  */ 
/* | Write lock bits                   | 'l' |    dd |      | 13d |      |  */ 
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
//多功能函数4======锁位=======
void Write_lock_bits(void)
{
	unsigned char data;
    data = getc(); 
	if(G_device == AT89C51 || G_device == AT89C52)
	{
		//SetVPP12V();
		VPP(1);
		PORTA|= _BV(PA6);   //RST=H
		OutCONBit(PSEN,0);  //PSEN=L
		if(data & _BV(0))
		{
			OutP2Bit(P26,1);
			OutP3Bit(P36,1);
			OutP3Bit(P37,1);
			OutP2Bit(P27,1);
			SetVPP12V();
			OutCONBit(PROG,0);
			delay(1);  
			OutCONBit(PROG,1);
			SetVPP5V();
		}
		if(data & _BV(1))
		{
			OutP2Bit(P26,1);
			OutP3Bit(P36,0);
			OutP3Bit(P37,0);
			OutP2Bit(P27,1);
			SetVPP12V();
			OutCONBit(PROG,0);
			delay(1);  
			OutCONBit(PROG,1);
			SetVPP5V();
		}
		if(data & _BV(2))
		{
			OutP2Bit(P26,1);
			OutP3Bit(P36,1);
			OutP3Bit(P37,0);
			OutP2Bit(P27,0);
			SetVPP12V();
			OutCONBit(PROG,0);
			delay(1);  
			OutCONBit(PROG,1);
			SetVPP5V();
		}
	}
	else
	if(G_device == AT89S51 || G_device == AT89S52)
	{
		SPIWrite(0xAC); 
		SPIWrite(0xe0 | (data & 0x03)); 
		SPIWrite(0x00); 
		SPIWrite(0x00); 
		wait_1ms(g_deviceSupported[G_index].twd_fuse); 
	}
	else
	{
		SPIWrite(0xAC); 
		SPIWrite(0xe0); 
		SPIWrite(0x00); 
		SPIWrite(data |0xc0); 
		wait_1ms(g_deviceSupported[G_index].twd_fuse); 
	}
	putc(0x0D); 
	return;
}
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* |                                   | ID  | host  | programer  | Note |  */ 
/* | Write fuse bits                   | 'f' |    dd |      | 13d |  11  |  */ 
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
void Write_fuse_bits(void)
{
    //G_pgmode == TRUE;
	
	unsigned char data;
    data = getc(); 
    SPIWrite(0xAC); 
    SPIWrite(0xA0); 
    SPIWrite(0x00); 
    SPIWrite(data); //atmega16
    wait_1ms(g_deviceSupported[G_index].twd_fuse);   
    putc(0x0D); 
	return;
}
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
/* |                                   | ID  | host  | programer  | Note |  */ 
/* | Read fuse bits                    | 'F' |       |   dd |     |  11  |  */ 
/* +-----------------------------------+-----+-------+------+-----+------+  */ 
void Read_fuse_bits(void)
{
    //G_pgmode == TRUE;

⌨️ 快捷键说明

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