




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、#include /* harddisk control demo * power by dddlzhu* 編譯環(huán)境 : keil c51 v7.07 支持器件 : at89c51 * copyright (c) 2004*/#define byte unsigned char#define uint unsigned int/*/線路連接定義。如電路有變直接修改就可以了*/#define datah p1 #define datal p0#define regaddr p2#define read p3_4#define write p3_3#define rst p3_2/*/線路連接定義
2、。如電路有變直接修改就可以了*/寄存器地址定義#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/*/*這里為初始化硬盤的重要參數(shù),每個(gè)硬盤的參數(shù)都不盡相同。若不正確則讀不了盤中的數(shù)據(jù)。計(jì)算方法如下:先看清楚你的 硬盤表面標(biāo)簽中的數(shù)據(jù),
3、里面有三個(gè)重要參數(shù):1。柱面數(shù)(cylinder)2。磁頭數(shù)(head)3。磁道數(shù)(sector)其中 _maxhead=0xa+head _maxsector=sector例如我的130m硬盤(很老吧,哈哈),磁頭數(shù)為15,十六進(jìn)制為0xf,所以_maxhead=0xaf,磁道數(shù)為17,所以_maxsector=0x11*/#define _maxhead 0xaf#define _maxsector 0x11/*byte bdata status=0x00;sbit err=status0;sbit idx=status1;sbit corr=status2;sbit drq=status
4、3;sbit dsc=status4;sbit df=status5;sbit drdy=status6;sbit bsy=status7;/* d7 d6 d5 d4 d3 d2 d1 d0 *bsy drdy dwf dsc drq corr idx err *bsy:驅(qū)動(dòng)器忙; *drdy:驅(qū)動(dòng)器準(zhǔn)備好; *dwf:驅(qū)動(dòng)器寫失?。?*dsc:尋道結(jié)束; *drq:請(qǐng)求服務(wù),驅(qū)動(dòng)器希望通過數(shù)據(jù)寄存器與cpu交換一字節(jié)數(shù)據(jù);*corr:當(dāng)可以糾正的讀錯(cuò)誤發(fā)生時(shí),該位置1,數(shù)據(jù)傳輸將繼續(xù)進(jìn)行 *idx:收到綜引信號(hào); *err:命令執(zhí)行出錯(cuò)。 */byte data_bufferh=0x0;b
5、yte data_bufferl=0x0;/*串口子程序void send_string(unsigned char *word);void send_char(unsigned char word);unsigned char get_char(void);/*:延遲函數(shù) */ void delay(byte ms) byte i,j;for(i=0;ims;i+) for(j=0;j=254) return(0xff);while(bsy);return(1);/*寫寄存器值*/void writereg(byte addr,byte data)regaddr=addr;write=0;d
6、atal=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) dowritereg(_deviceandhead,0xa0); re
7、adreg(_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 idn);waitbsy();/send_stri
8、ng(now can read driver id n);writereg(_status_command,0xec);/send_string(waiting. );doreadreg(_status_command);while(bsy|!drq);/send_string(now sending n);while(i)readdata();send_char(data_bufferh);send_char(data_bufferl);i-=2;/*硬盤尋址*/writechs(byte head,uint cylinder,byte sector,byte read_count)wait
9、bsy();writereg(_deviceandhead,0xa0|head);writereg(_cylinderh,(char)(cylinder8); writereg(_cylinderl,(char)(cylinder&0x00ff);writereg(_secnum,sector); writereg(_seccount,read_count);/*用途:將硬盤的返回?cái)?shù)據(jù)讀入buffer數(shù)組*/void senddata() uint i;i=512*15;doreadreg(_status_command);while(bsy|!drq);if(err)send_string(
10、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; /波特率為57600bpstcon=0x
11、40;pcon=0x80;th1=0xff;tl1=0xff;tr1=1;send_string(ide control demo. power by dddlzhux0dx0a);/send welcome wordrst=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();
12、/讀硬盤idsend_string(nnnow 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;sbu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 廠里工序分包協(xié)議合同范本
- 債券抵押合同范本
- 一年級(jí)班主任的個(gè)人年底總結(jié)
- 一年級(jí)下學(xué)期體育教學(xué)計(jì)劃
- 廠家招商加盟合同范例
- 《黃帝內(nèi)經(jīng)》讀書心得體會(huì)
- 廠家飯盒供貨合同范本
- 卡丁車購銷合同范本
- 冷庫空調(diào)保養(yǎng)合同范例
- 農(nóng)村建房木工合同范本
- 酒店公共區(qū)域電梯安全使用培訓(xùn)
- 急性冠脈綜合征ACS課件
- 三角函數(shù)的誘導(dǎo)公式(一)完整版
- 零信任安全模型研究
- 中小學(xué)幼兒園安全風(fēng)險(xiǎn)防控工作規(guī)范
- 正確認(rèn)識(shí)民族與宗教的關(guān)系堅(jiān)持教育與宗教相分離
- 畜禽廢棄物資源化利用講稿課件
- 土地糾紛調(diào)解簡單協(xié)議書
- 服裝倉庫管理制度及流程
- 架子工安全教育培訓(xùn)試題(附答案)
- 《高血壓5項(xiàng)化驗(yàn)》課件
評(píng)論
0/150
提交評(píng)論