51單片機(jī)硬盤控制電路以及源程序
/*****************************************************
*寫寄存器值
********************************************************/
void WriteReg(byte Addr,byte Data)
{
RegAddr=Addr;
Write=0;
DataL=Data;
Write=1;
}
/*******************************************************
讀數(shù)據(jù)儲(chǔ)存器中數(shù)據(jù)
********************************************************/
void ReadData(void)
{
DataH=0xff;
DataL=0xff;
RegAddr=_Data;
Read=0;
Data_bufferL=DataL;
Data_bufferH=DataH;
Read=1;
}
/*******************************************************
寫數(shù)據(jù)寄存器中數(shù)據(jù)
********************************************************/
void WriteData(void)
{
RegAddr=_Data;
Write=0;
DataL=Data_bufferL;
DataH=Data_bufferH;
Write=1;
}
/**********************************************************
初始化硬盤 *
***********************************************************/
void Init(void)
{ do{
WriteReg(_DeviceAndHead,0xa0);
ReadReg(_Status_Command);
}while(!DRDY|BSY);
WriteReg(_DeviceAndHead,_MaxHead);
WriteReg(_SecCount,_MaxSector);
WriteReg(_Status_Command,0x91);
WaitBSY();
WriteReg(_Status_Command,0x10);
WaitBSY();
}
/**********************************************************
讀硬盤參數(shù)
***********************************************************/
void DriverID(void)
{
unsigned int i=512;
//send_string("Starting read driver ID");
WaitBSY();
//send_string("Now can read driver ID ");
WriteReg(_Status_Command,0xec);
//send_string("Waiting.. ");
do{ReadReg(_Status_Command);}while(BSY|!DRQ);
//send_string("Now Sending ");
while(i){
ReadData();
send_char(Data_bufferH);
send_char(Data_bufferL);
i-=2;
}
}
/*********************************************************
硬盤尋址
**********************************************************/
WriteCHS(byte head,uint cylinder,byte sector,byte read_count)
{
WaitBSY();
WriteReg(_DeviceAndHead,0xa0|head);
WriteReg(_CylinderH,(char)(cylinder>>8));
WriteReg(_CylinderL,(char)(cylinder0x00ff));
WriteReg(_SecNum,sector);
WriteReg(_SecCount,read_count);
}
/**********************************************************
*用途:將硬盤的返回?cái)?shù)據(jù)讀入BUFFER數(shù)組
***********************************************************/
void SendData()
{ uint i;
i=512*15;
do{ReadReg(_Status_Command);}while(BSY|!DRQ);
if(ERR){
send_string("x0dx0a Errorx0dx0a");
}
while(i){ReadData();send_char(Data_bufferL);send_char(Data_bufferH);i-=2;}
}
// 激活硬盤(轉(zhuǎn)動(dòng))
void SpinUP()
{
WaitBSY();
WriteReg(_Status_Command,0xE1);
}
// 讓硬盤休眠(停轉(zhuǎn))/
void SpinDown()
{
WaitBSY();
WriteReg(_Status_Command,0xE0);
}
void main(void)
{
//Initialize
SCON=0x50; //串口初始化
TMOD=0x20; //波特率為57600bps
TCON=0x40;
PCON=0x80;
TH1=0xFf;
TL1=0xFf;
TR1=1;
send_string("IDE Control Demo. Power By DDDLZHUx0dx0a");//send welcome word
Rst=0; //IDE 復(fù)位
delay(50);
Rst=1;
delay(255);
send_string("Reset Driver OK...x0dx0a");
Init(); //初始化硬盤
send_string("Initialize Driver OK,Now Read IDx0dx0a");
send_string("HardDisk ID is ....x0dx0a");
DriverID(); //讀硬盤id
send_string("Now Read The First Sector On this HardDiskx0dx0ax0dx0a");
delay(244);
delay(244);
delay(244);
delay(244);
WriteCHS(0,0,1,16); //寫地址
WaitBSY();
WriteReg(_Status_Command,0x20); //發(fā)送讀命令
SendData();
send_string("x0dx0ax0dx0a Read OK,Now Shut Down The HardDisk..x0dx0a");
SpinDown(); //硬盤停轉(zhuǎn)
while(1);
}
//**************************************串口子程序
void send_char(unsigned char word)
{
TI=0;
SBUF=word;
while(TI==0);
TI=0;
}
void send_string(unsigned char *word)
{
TI=0;
while(*word!=0)
{
SBUF=*word;
while(TI==0);
TI=0;
word++;
}
}
unsigned char get_char(void)
{
RI=0;
REN=1;
while(RI==0);
return(SBUF);
RI=0;
REN=0;
}
51單片機(jī)相關(guān)文章:51單片機(jī)教程
評(píng)論