Free Electronic Circuits & 8085 projects

Electronic projects with circuit diagram and 8085 microprocessor projects.

Computer Hard Drive control circuit with program

Published on Oct 26 2010 // Micrcontroller circuits


Computer Hard Drive  control circuit with program

I believe many people want to use the microcontroller to control hard disk mass storage of data and development of some things (such as hard disk mp3), but the Internet
is little information on this aspect, and really gives the source code and circuits are extremely small, and even if given the original procedure, but I do not know
because it is a good understanding of errors or ata agreement itself has not, or for the protection of intellectual property, procedures, or large or less has a
mistake. This program is just a demo, not perfect, just to give you a reference procedures for the development of ata, but I guarantee you can use. Of course,
you can modify and extend about to become a perfect ata driver.

Characteristics and use

All microcontroller programs its own hardware-related parameters to define the beginning of the process, when transplanted to other types just changed a bit microcontroller pin definitions on the line. Note that when used to modify according to your hard drive about the corresponding parameters (there are comments in the program.)

Use steps and Performance

Connect the circuit (very simple), in the process burned into the chip. Turn hard disk power first, then open any of the serial debugger, set the baud rate 57600bps, then you can run the microcontroller. Effect with the next:

Include at89x51.h

/************************************************* *

* HardDisk Control Demo

* Power BY DDDLZHU

* Build environment: KEIL C51 V7.07 Support devices: AT89C51

************************************************** * /

# Define byte unsigned char

# Define uint unsigned int

/*************************************************

/ / Connections defined. If a change in the circuit can directly modify the

*************************************************/

# Define DataH P1

# Define DataL P0

# Define RegAddr P2

# Define Read P3_4

# Define Write P3_3

# Define Rst P3_2

/*************************************************

/ / Connections defined. If a change in the circuit can directly modify the

*************************************************/

/ / Register address definitions

# Define _Status_Control 0x16

# Define _Data 0x8

# Define _Err_Features 0x9

# Define _SecCount 0xa

# Define _SecNum 0xb

# Define _CylinderL 0xc

# Define _CylinderH 0xd

# Define _DeviceAndHead 0xe

# Define _Status_Command 0xf

//************************************************ **************************************/

/ * Initialize the hard disk here for important parameters, the parameters of each disk are different. If the correct disk is not reading data. Calculated as follows:

First look at the surface of your hard drive data in the label, there are three important parameters:

1. Number of cylinders (Cylinder)

2. Heads (Head)

3. Track number (Sector)

One _MaxHead = 0xA Head

_MaxSector = Sector

For example, 130M hard drive to my (very old, ha ha), number of heads is 15, hexadecimal is 0xf, so _MaxHead = 0xAF, track number is 17, so _MaxSector = 0x11

* /

# Define _MaxHead 0xAF

# Define _MaxSector 0x11

//************************************************ ************************************

byte bdata Status = 0x00;

sbit ERR = Status ^ 0;

sbit IDX = Status ^ 1;

sbit CORR = Status ^ 2;

sbit DRQ = Status ^ 3;

sbit DSC = Status ^ 4;

sbit DF = Status ^ 5;

sbit DRDY = Status ^ 6;

sbit BSY = Status ^ 7;

/************************************************* ***********

* D7 D6 D5 D4 D3 D2D1 D0 *

BSYDRDYDWFDSC DRQCORRIDX ERR *

BSY: Busy drive; *

DRDY: drive is ready; *

DWF: write failed drive; *

DSC: seek an end; *

DRQ: requesting service, the drive hopes to exchange data registers and the CPU one byte of data; *

CORR: When to correct the read error occurs, the position 1, the data transmission will continue to be *

IDX: Comprehensive guidelines received signal; *

ERR: command execution error. *

************************************************** ***********/

byte Data_bufferH = 0x0;

byte Data_bufferL = 0x0;

//*************** Serial subroutine

void send_string (unsigned char * word);

void send_char (unsigned char word);

unsigned char get_char (void);

/ / 8888888888888888888888

/************************************************* ******

: Delay function

************************************************** ******/

void delay (byte ms)

{Byte i, j;

for (i = 0; ims; i)

for (j = 0; j255; j);

}

/************************************************* ******

* Read register

************************************************** ******/

byte ReadReg (byte Addr)

{

RegAddr = Addr;

DataL = 0xff;

Read = 0;

Status = DataL;

Read = 1;

return Status;

}

/************************************************* ******

* Waiting for BSY signal

************************************************** ******/

byte WaitBSY (void)

{

byte timeOut = 0;

do {

ReadReg (_Status_Command);

timeOut;

/ / If (timeOut = 254) return (0xff);

} While (BSY);

return (1);

}

/************************************************* ****

* Write register values

************************************************** ******/

void WriteReg (byte Addr, byte Data)

{

RegAddr = Addr;

Write = 0;

DataL = Data;

Write = 1;

}

/************************************************* ******

Reading the data in the data storage

************************************************** ******/

void ReadData (void)

{

DataH = 0xff;

DataL = 0xff;

RegAddr = _Data;

Read = 0;

Data_bufferL = DataL;

Data_bufferH = DataH;

Read = 1;

}

/************************************************* ******

The data in write data register

************************************************** ******/

void WriteData (void)

{

RegAddr = _Data;

Write = 0;

DataL = Data_bufferL;

DataH = Data_bufferH;

Write = 1;

}

/************************************************* *********

* Initialize the hard disk

************************************************** *********/

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 ();

}

/************************************************* *********

Read drive parameters

************************************************** *********/

void DriverID (void)

{

unsigned int i = 512;

/ / Send_string (“Starting read driver ID \ n”);

WaitBSY ();

/ / Send_string (“Now can read driver ID \ n”);

WriteReg (_Status_Command, 0xec);

/ / Send_string (“Waiting ..”);

do {ReadReg (_Status_Command);} while (BSY |! DRQ);

/ / Send_string (“Now Sending \ n”);

while (i) {

ReadData ();

send_char (Data_bufferH);

send_char (Data_bufferL);

i-= 2;

}

}

/************************************************* ********

Hard Addressing

************************************************** ********/

WriteCHS (byte head, uint cylinder, byte sector, byte read_count)

{

WaitBSY ();

WriteReg (_DeviceAndHead, 0xa0 | head);

WriteReg (_CylinderH, (char) (cylinder8));

WriteReg (_CylinderL, (char) (cylinder0x00ff));

WriteReg (_SecNum, sector);

WriteReg (_SecCount, read_count);

}

/************************************************* *********

* Purpose: return the hard disk data read into an array * BUFFER

************************************************** *********/

void SendData ()

{Uint i;

i = 512 * 15;

do {ReadReg (_Status_Command);} while (BSY |! DRQ);

if (ERR) {

send_string (“\ x0d \ x0a Error \ x0d \ x0a”);

}

while (i) {ReadData (); send_char (Data_bufferL); send_char (Data_bufferH); i-= 2;}

}

/ / Activate the hard disk (rotation)

void SpinUP ()

{

WaitBSY ();

WriteReg (_Status_Command, 0xE1);

}

/ / Let the hard disk sleep (stopped) /

void SpinDown ()

{

WaitBSY ();

WriteReg (_Status_Command, 0xE0);

}

void main (void)

{

/ / Initialize

SCON = 0x50; / / initialize the serial port

TMOD = 0x20; / / baud rate is 57600bps

TCON = 0x40;

PCON = 0x80;

TH1 = 0xFf;

TL1 = 0xFf;

TR1 = 1;

send_string (“IDE Control Demo. Power By DDDLZHU \ x0d \ x0a “);// send welcome word

Rst = 0; / / IDE reset

delay (50);

Rst = 1;

delay (255);

send_string (“Reset Driver OK … \ x0d \ x0a”);

Init (); / / initialize the hard disk

send_string (“Initialize Driver OK, Now Read ID \ x0d \ x0a”);

send_string (“HardDisk ID is …. \ x0d \ x0a”);

DriverID ();// read disk id

send_string (“\ n \ nNow Read The First Sector On this HardDisk \ x0d \ x0a \ x0d \ x0a”);

delay (244);

delay (244);

delay (244);

delay (244);

WriteCHS (0,0,1,16); / / write address

WaitBSY ();

WriteReg (_Status_Command, 0x20); / / send read command

SendData ();

send_string (“\ x0d \ x0a \ x0d \ x0a Read OK, Now Shut Down The HardDisk .. \ x0d \ x0a”);

SpinDown ();// hard to stall

while (1);

}

//************************************** Serial subroutine

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;

}

/ / 88888888888888888888888888888888888888888