一、引腳定義及通信協(xié)議
 SO:串行數(shù)據(jù)輸出腳,在一個讀操作的過程中,數(shù)據(jù)從SO腳移位輸出。在時鐘的下降沿時數(shù)據(jù)改變。
SI: 串行數(shù)據(jù)輸入腳,所有的操作碼、字節(jié)地址和數(shù)據(jù)從SI腳寫入,在時鐘的上升沿時數(shù)據(jù)被鎖定。
SCK:串行時鐘,控制總線上數(shù)據(jù)輸入和輸出的時序。
/CS :芯片使能信號,當(dāng)其為高電平時,芯片不被選擇,SO腳為高阻態(tài),除非一個內(nèi)部的寫操作正在進(jìn)行,否則芯片處于待機模式;當(dāng)引腳為低電平時,芯片處于活動模式,在上電后,在任何操作之前需要CS引腳的一個從高電平到低電平的跳變。
/WP:當(dāng)WP引腳為低時,芯片禁止寫入,但是其他的功能正常。當(dāng)WP引腳為高電平時,所有的功能都正常。當(dāng)CS為低時,WP變?yōu)榈涂梢灾袛鄬π酒膶懖僮。但是如果?nèi)部的寫周期已經(jīng)被初始化后,WP變?yōu)榈筒粫䦟懖僮髟斐捎绊憽?BR> 二、硬件連接

三、程序設(shè)計
狀態(tài)寄存器:
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
X |
X |
WD1 |
WD0 |
BL1 |
BL0 |
WEL |
WIP |
WIP: 寫操作標(biāo)志位, 為1表示內(nèi)部有一個寫操作正在進(jìn)行,為0則表示空閑,該位為只讀。 WEL: 寫操作允許標(biāo)志位,為1表示允許寫操作,為0表示禁止寫,該位為只讀。 BL0,BL1: 內(nèi)部保護(hù)區(qū)間的地址選擇。被保護(hù)的區(qū)間不能進(jìn)行看門狗的定時編程。 WD0,WD1:可設(shè)定看門狗溢出的時間。有四種可選擇:1.4s,600ms,200ms,無效。
操作碼: WREN 0x06 設(shè)置寫允許位 WRDI 0x04 復(fù)位寫允許位 RDSR 0x05 讀狀態(tài)寄存器 WRSR 0x01 寫狀態(tài)寄存器 READ 0x03/0x0b 讀操作時內(nèi)部EEPROM頁地址 WRITE 0x02/0x0a 寫操作時內(nèi)部EEPROM頁地址
程序代碼: #i nclude <reg51.h> sbit CS= P2^7; sbit SO= P2^6; sbit SCK= P2^5; sbit SI= P2^4;
#define WREN 0x06 // #define WRDI 0x04 // #define RDSR 0x05 // #define WRSR 0x01 //
#define READ0 0x03 // #define READ1 0x0b // #define WRITE0 0x02 // #define WRITE1 0x0a // #define uchar unsigned char
uchar ReadByte() //read a byte from device { bit bData; uchar ucLoop; uchar ucData; for(ucLoop=0;ucLoop<8;ucLoop++) { SCK=1; SCK=0; bData=SO; ucData<<=1; if(bData) { ucData =0x01; } } return ucData; }
void WriteByte(uchar ucData)//write a byte to device { uchar ucLoop; for(ucLoop=0;ucLoop<8;ucLoop++) { if((ucData&0x80)==0) //the MSB send first {SI=0;} else {SI=1;} SCK=0; SCK=1; ucData<<=1; } }
uchar ReadReg() //read register { uchar ucData; CS=0; WriteByte(RDSR); ucData=ReadByte(); CS=1; return ucData; }
uchar WriteReg(uchar ucData) //write register { uchar ucTemp; ucTemp=ReadReg(); if((ucTemp&0x01)==1) //the device is busy return 0; CS=0; WriteByte(WREN);//when write the WREN, the cs must have a high level CS=1; CS=0; WriteByte(WRSR); WriteByte(ucData); CS=1; return 1; }
void WriteEpm(uchar cData,uchar cAddress,bit bRegion) /* 寫入一個字節(jié),cData為寫入的數(shù),cAddress為寫入地址,bRegion為頁 */ { while((ReadReg()&0x01)==1); //the device is busy CS=0; WriteByte(WREN); //when write the wren , the cs must have a high level CS=1; CS=0; if(bRegion==0) { WriteByte(WRITE0);} //write the page addr else {WriteByte(WRITE1);} WriteByte(cAddress); WriteByte(cData); SCK=0; // CS=1; }
uchar ReadEpm(uchar cAddress,bit bRegion) /* 讀入一個字節(jié),cAddress為讀入地址,bRegion為頁 */ { uchar cData; while((ReadReg()&0x01)==1);//the device is busy CS=0; if(bRegion==0) {WriteByte(READ0); } else {WriteByte(READ1);} WriteByte(cAddress); cData=ReadByte(); CS=1; return cData; }
main() { WriteReg(0x00);//set the watchdog time as 1.4s
CS=1; CS=0; //reset the watchdog
}
|