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

📄 main.c

📁 avr单片机硬盘控制 (含代码、原理图) avr单片机硬盘控制 (含代码、原理图)
💻 C
📖 第 1 页 / 共 2 页
字号:
{
	byte temp;
	RegAddrChangePhysics(byAddr);
	DDRA=0;
	PORTA=0xff;
	Clr_Bit(PORTC,Read);
	nop();
	nop();
	nop();
	nop();
	temp=ReversalData(RDataL);
//	temp=RDataL;
	Set_Bit(PORTC,Read);
	return temp;
}
//******************************************************************************
/********************************************************
*函数名称:void ReadData(void)							*
*函数用途:读硬盘数据寄存器内容							*
********************************************************/
void ReadData(void)
{
	PORTA=0xff;
	DDRA=0;
	PORTB=0xff;
	DDRB=0;
	RegAddrChangePhysics(_Data);
	Clr_Bit(PORTC,Read);
	nop();
	nop();
	nop();
	nop();
	HDDATAL=ReversalData(RDataL);
//	HDDATAL=RDataL;
	HDDATAH=RDataH;
	Set_Bit(PORTC,Read);
}
//******************************************************************************
/********************************************************
*函数名称:void WriteData(byte byDataL,byte byDataH)		*
*函数用途:写硬盘数据寄存器内容							*
********************************************************/
void WriteData(byte byDataL,byte byDataH)
{
	byDataL=ReversalData(byDataL);
	DDRA=0xff;
	DDRB=0xff;
	RegAddrChangePhysics(_Data);
	WDataL=byDataL;
	WDataH=byDataH;
	Clr_Bit(PORTC,Write);
	nop();
	nop();
	nop();
	nop();
	Set_Bit(PORTC,Write);
}
/************************************************************
*函数名称:void WriteReg(byte byAddr,byte byData)			*
*用途:写寄存器:寄存器地址,数据,无返回值					*
************************************************************/ 
void WriteReg(byte byAddr,byte byData)
{
	byData=ReversalData(byData);
	RegAddrChangePhysics(byAddr);
	DDRA=0xff;
	WDataL=byData;
	Clr_Bit(PORTC,Write);
	nop();
	nop();
	nop();
	nop();
	Set_Bit(PORTC,Write);
}
/************************************************************
*函数名称:byte WaitBSY(void)								*
*用途:等待BSY信号											*
************************************************************/ 
byte WaitBSY(void)
{
	//byte timeOut=0;
	do
	{
		HDstate=ReadReg(_Status_AND_Command);
 		//timeOut++;
		//if(timeOut>=254)
			//return(0xff);	//超时
	}while(BSY);
	return(1);
}
/************************************************************
*函数名称:byte WaitDRQ(void)								*
*用途:等待DRQ信号											*
************************************************************/ 
byte WaitDRQ(void)
{
	//byte timeOut=0;
	do
	{
		HDstate=ReadReg(_Status_AND_Command);
 		//timeOut++;
		//if(timeOut>=254)
			//return(0xff);	//超时
	}while(!DRQ);
	return(1);
}
/************************************************************
*函数名称:void RegAddrChangePhysics(byte byRegAddr)			*
*用途:把寄存器地址转化为物理接口							*
************************************************************/ 
void RegAddrChangePhysics(byte byRegAddr)
{
	Clr_Bit(PORTC,CS0);
	Set_Bit(PORTC,CS1);
	switch(byRegAddr)
	{
		case _Data:
				Clr_Bit(PORTD,DA0);
				Clr_Bit(PORTD,DA1);
				Clr_Bit(PORTD,DA2);
				break;
		case _Err_Features:
				Set_Bit(PORTD,DA0);
				Clr_Bit(PORTD,DA1);
				Clr_Bit(PORTD,DA2);
				break;
		case _SecCount:
				Clr_Bit(PORTD,DA0);
				Set_Bit(PORTD,DA1);
				Clr_Bit(PORTD,DA2);
				break;
		case _SecNum:
				Set_Bit(PORTD,DA0);
				Set_Bit(PORTD,DA1);
				Clr_Bit(PORTD,DA2);
				break;
		case _CylinderL:
				Clr_Bit(PORTD,DA0);
				Clr_Bit(PORTD,DA1);
				Set_Bit(PORTD,DA2);
				break;
		case _CylinderH:
				Set_Bit(PORTD,DA0);
				Clr_Bit(PORTD,DA1);
				Set_Bit(PORTD,DA2);
				break;
		case _DeviceAndHead:
				Clr_Bit(PORTD,DA0);
				Set_Bit(PORTD,DA1);
				Set_Bit(PORTD,DA2);
				break;
		case _Status_AND_Command:
				Set_Bit(PORTD,DA0);
				Set_Bit(PORTD,DA1);
				Set_Bit(PORTD,DA2);
				break;
	}
}
/************************************************************
*函数名称:void Read_HD_ID(void)								*
*用途:读硬盘ID												*
************************************************************/ 
void Read_HD_ID(void) 
{
	uint i,b;
	do
	{
		HDstate=ReadReg(_Status_AND_Command);
	}while(BSY);
	WriteReg(_Status_AND_Command,0xec);
	do
	{
		HDstate=ReadReg(_Status_AND_Command);
	}while((!DRQ)||BSY);
	for(i=0;i<256;i++)
	{
		WaitDRQ();
		ReadData();
		HDSectorDATA[b++]=HDDATAL;
		HDSectorDATA[b++]=HDDATAH;
	}
}
/************************************************************
*函数名称:void HDRunningActivation(void)					*
*用途:激活硬盘(转动)										*
************************************************************/ 
void HDRunningActivation(void)
{
	byte temp;
	temp=WaitBSY();
	//if(temp==1)
		WriteReg(_Status_AND_Command,0xE1);
}
/************************************************************
*函数名称:void HDStop(void)									*
*用途:让硬盘休眠(停转)										*
************************************************************/ 
void HDStop(void)
{
	byte temp;
	temp=WaitBSY();
	//if(temp==1)
		WriteReg(_Status_AND_Command,0xE0);
}
/************************************************************
*函数名称:void LBA(unsigned long lba)						*
*用途:LBA寻址												*
************************************************************/ 
void LBA(unsigned long lba)
{
	WriteReg(6,0xe0+(lba>>24));
	WriteReg(5,(lba>>16));
	WriteReg(4,(lba>>8));
	WriteReg(3,(lba));
	WriteReg(2,1);
}
/************************************************************
*函数名称:void WriteCHS(uint iCylinder,byte byHead,byte bySector,byte byReadCount)*
*用途:CHS寻址												*
************************************************************/ 
void WriteCHS(uint iCylinder,byte byHead,byte bySector,byte byReadCount)
{
	WaitBSY();
	WriteReg(_DeviceAndHead,0x40|byHead);
	//WriteReg(_DeviceAndHead,byHead);
	WriteReg(_CylinderH,(byte)(iCylinder>>8)); 
	WriteReg(_CylinderL,(byte)(iCylinder&0x00ff));
	WriteReg(_SecNum,bySector); 
	WriteReg(_SecCount,byReadCount);
}
/************************************************************
*函数名称:void ReadSectorData(ulong lSectorAdd)				*
*用途:读一个扇区的数据										*
************************************************************/
void ReadSectorData(ulong lSectorAdd)
{
	uint temp,iAddend=0;
	LBA(lSectorAdd);
	WriteReg(_Status_AND_Command,CMD_READ_SECTORS);//0x20
	for(temp=0;temp<256;temp++)
	{
		WaitDRQ();
		ReadData();
		HDSectorDATA[iAddend++]=HDDATAL;
		HDSectorDATA[iAddend++]=HDDATAH;
	}
}
/************************************************************
*函数名称:void WriteSectorData(ulong lSectorAdd)			*
*用途:写一个扇区的数据										*
************************************************************/
void WriteSectorData(ulong lSectorAdd)
{
	uint temp,iAddend=0;
	LBA(lSectorAdd);
	WriteReg(_Status_AND_Command,CMD_WRITE_SECTORS);//0x30
	for(temp=0;temp<256;temp++)
	{
		HDDATAL=HDSectorDATA[iAddend++];
		HDDATAH=HDSectorDATA[iAddend++];
		WaitDRQ();
		WriteData(HDDATAL,HDDATAH);
	}
}
/************************************************************
*函数名称:byte ReversalData(byte byReversal)				*
*用途:颠倒数据,0位换到7位,7位换到0位						*
************************************************************/ 
byte ReversalData(byte byReversal)
{
	byte temp;
	temp=byReversal<<7;
	temp=temp+((byReversal&0x2)<<5);
	temp=temp+((byReversal&0x4)<<3);
	temp=temp+((byReversal&0x8)<<1);
	temp=temp+(byReversal>>7);
	temp=temp+((byReversal&0x40)>>5);
	temp=temp+((byReversal&0x20)>>3);
	temp=temp+((byReversal&0x10)>>1);
	return temp;
}

⌨️ 快捷键说明

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