eeprom讀寫實驗,系統上電后從eeprom讀一個位元組資料送數碼管顯示,值的范圍00--99,該值可以通過鍵盤修改,修改后通過按鍵保存到eeprom,下次系統上電讀到的就是修改后的值。
*連續寫:2k最多連續寫7個位元組 4k、8k、16k最多連續寫15個位元組連續讀:沒有位元組數限制,如果超出存盤器范圍,則自動從頭再開始*/#include <intrins.h>//#include <reg51.h>#include <stc\stc15.h>#include <absacc.h>#define uchar unsigned char#define uint unsigned intuchar writenumber[10]={1,2,3,4,5,6,7,8,9,10}; uchar readnumber[10]={0};uchar controlbyte; /*-------IIC控制位元組---------*/sbit SCL=P3^5;sbit SDA=P3^4;sbit WRITE_LED=P1^5;sbit READ_LED=P1^6;sbit ERR_LED=P1^7;void RW_24cxx( unsigned char *DataBuff, unsigned char ByteQuantity, unsigned char Address, unsigned char ControlByte);void Delay1us(uchar T){ while(T) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); --T; }} void Delay1ms(uchar T){ while(T) { Delay1us(1000); --T; }} /*----------IIC操作------------*/void IIC_Start(void){ SDA=1; _nop_(); SCL=1; _nop_(); SDA=0; _nop_(); SCL=0; _nop_();}void IIC_Stop(void){ SDA=0; _nop_(); SCL=1; _nop_(); SDA=1; _nop_(); SCL=0; _nop_();} void IIC_Wait_ACK(void){ unsigned char err_time=255; SCL=0; _nop_(); SDA=1; _nop_(); SCL=1; while(SDA) err_time--; _nop_(); _nop_(); SCL=0; _nop_();}void IIC_ACK(void){ SDA=0; _nop_(); SCL=1; _nop_(); _nop_(); SCL=0; _nop_(); SDA=1; _nop_();}void IIC_NoAck(void){ SDA=1; _nop_(); SCL=1; _nop_(); _nop_(); SCL=0; _nop_();} void IIC_WriteByte(unsigned char sendbyte){ unsigned char j; for(j=0;j<8;j++) { SCL=0; _nop_(); SDA=((0x80&sendbyte)==0x80); /*--------只有SCL=0時,SDA上的資料改變才是有效的。-------*/ _nop_(); SCL=1; _nop_(); sendbyte<<=1; SCL=0; }} unsigned char IIC_ReadByte(void){ unsigned char i,ReceiveByte=0; SCL=0; for(i=1;i<=8;i++) { SCL=1; _nop_(); if(SDA==1) { ReceiveByte=ReceiveByte|(0x01<<(8-i)); } SCL=0; _nop_(); } return ReceiveByte; }void RW_24cxx( unsigned char *DataBuff, unsigned char ByteQuantity, unsigned char Address, unsigned char ControlByte){ unsigned char i; i=ByteQuantity; if(!(ControlByte&0x01)) /*--------若為寫命令-------*/ { IIC_Start(); IIC_WriteByte(ControlByte&0xfe); IIC_Wait_ACK();/*proteus中的24c02必須用雙位元組地址,電路板上的24c02,地址單位元組雙位元組都可以*/// IIC_WriteByte((unsigned char)(Address>>8)); //實驗箱器件若為24c32及以上,地址是雙位元組// IIC_Wait_ACK(); IIC_WriteByte((unsigned char)(Address)); IIC_Wait_ACK(); while(i--) { IIC_WriteByte(*DataBuff++); IIC_Wait_ACK(); } IIC_Stop(); } else /*--------若為讀命令-------*/ { IIC_Start(); IIC_WriteByte(ControlByte&0xfe); IIC_Wait_ACK();// IIC_WriteByte((unsigned char)(Address>>8)); //器件若為24c32及以上,地址是雙位元組// IIC_Wait_ACK(); IIC_WriteByte((unsigned char)(Address)); IIC_Wait_ACK(); IIC_Start(); IIC_WriteByte(ControlByte); IIC_Wait_ACK(); while(--ByteQuantity) { *DataBuff++=IIC_ReadByte(); IIC_ACK(); Delay1ms(100); } *DataBuff=IIC_ReadByte(); IIC_NoAck(); IIC_Stop(); }} main( ){ uchar i,success=1; WRITE_LED = 1; READ_LED = 1; ERR_LED = 1; controlbyte=0xa0; RW_24cxx( writenumber, 7, 0, controlbyte); /*-------- 寫資料測驗--------*/ Delay1ms(100); WRITE_LED = 0; controlbyte=0xa1; RW_24cxx( readnumber, 7, 0, controlbyte); /*-------- 讀資料測驗--------*/ Delay1ms(100); READ_LED = 0; for(i=0;i<7;i++) { if(readnumber[i]!=writenumber[i]) success=0; } while(1) { if(success==0) { ERR_LED = ~ERR_LED; Delay1ms(100); } else ERR_LED = 0; }}
uj5u.com熱心網友回復:
?????你這貼的啥???uj5u.com熱心網友回復:
下面是老師寫的一個eeprom的程式uj5u.com熱心網友回復:
老師寫的eeprom的程式
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/283696.html
標籤:單片機/工控
上一篇:資料幀幀尾
