|
- /*****單片機模擬I2C總線,并存取I2C串行EEPROM 24C02C****/
- #include <reg52.h>
- #include <string.h>
- #include "I2C_51.h"
- #include "uart.h"
- sbit sda=P2^1;//數(shù)據(jù)線
- sbit scl=P2^0; //時鐘線
- sbit dula=P2^6;
- sbit wela=P2^7;
- typedef unsigned char uchar;
- typedef unsigned int uint;
- uchar code table[]={0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71,
- };
- /********************I2C_51.h***********************************************************
- void I2C_Start(void);
- void I2C_Stop(void);
- bit Check_Acknowledge(void);
- void WriteI2CByte(char b)reentrant;
- char ReadI2CByte(void)reentrant
- ****************************************************************************************/
- /***************write one byte***********************************************************
- 功能:將ch寫到地址add處 *
- 輸入:地址add,待寫字節(jié)數(shù)據(jù)ch *
- 輸出:無 *
- ****************************************************************************************/
- void WriteByte(uchar add,uchar ch){
- I2C_Start(); //start
- WriteI2CByte(0xa0); while(!Check_Acknowledge());//control byte:1010+A2+A1+A0+1(write)/0(read)
- WriteI2CByte(add); while(!Check_Acknowledge());//address
- WriteI2CByte(ch); while(!Check_Acknowledge());//data
- I2C_Stop(); //stop
- }
- /***************Read one byte************************************************************
- 功能:從地址ch處讀取一字節(jié)數(shù)據(jù) *
- 輸入:地址add *
- 輸出:從地址add處讀得的一字節(jié)數(shù)據(jù) *
- ****************************************************************************************/
- char ReadByte(uchar add){
- char b;
- I2C_Start(); //start
- WriteI2CByte(0xa0); while(!Check_Acknowledge());//control byte,
- WriteI2CByte(add); while(!Check_Acknowledge());//address
- I2C_Start(); //start
- WriteI2CByte(0xa1); while(!Check_Acknowledge());//data
- b=ReadI2CByte(); //N0 Acknowledge
- I2C_Stop(); //stop
- return b;
- }
- /***************write string***************************************************************
- 功能:以add為起始地址連續(xù)寫入字符串p *
- 輸入:地址,待寫字符串 *
- 輸出:無 *
- 注意:字符串p的長度不需小于一頁數(shù)據(jù)(24c02c的一頁為16byte)。 *
- 數(shù)據(jù)字節(jié)臨時存儲在片內(nèi)頁緩沖器中。在主器件發(fā)送停止條件之后,這些數(shù)據(jù)將被寫入存儲器。*
- 每接收一個字,內(nèi)部地址計數(shù)器加一。如果在停止條件產(chǎn)生前,主器件有超出一頁的數(shù)據(jù)要發(fā)送,
- 地址計數(shù)器將會翻轉(zhuǎn),先前寫入的數(shù)據(jù)將被覆蓋。 *
- ******************************************************************************************/
- void WriteString(uchar add,uchar *p){ /*224C02C(2kbit),strlen(p)should<=16bit*/
- uchar i;
- I2C_Start();
- WriteI2CByte(0xa0); while(!Check_Acknowledge());
- WriteI2CByte(add); while(!Check_Acknowledge());
- for(i=strlen(p);i!=0;i--){
- WriteI2CByte(*p); while(!Check_Acknowledge());
- p++;
- }
- I2C_Stop();
- }
- /***************read string*************************************************************************
- 功能:以add為起始地址連續(xù)讀len長字節(jié)數(shù)據(jù),并通過*P返回 *
- 輸入:地址add,讀取長度len *
- 輸出:字符串*p *
- 說明:連續(xù)讀操作的起動過程和隨機讀操作相同,只是在 24XX發(fā)送完第一個數(shù)據(jù)字節(jié)后,主器件發(fā)出確認信號,*
- 而在隨機讀操作中發(fā)送的是停止條件。確認信號指示 24XX 器件發(fā)送下一個連續(xù)地址的數(shù)據(jù)字節(jié)(圖 8-4)。
- 在24器件向主器件發(fā)送完最后一個字節(jié)后,主器件不會 產(chǎn)生確認信號,而是產(chǎn)生停止條件。 *
- ***************************************************************************************************/
- void ReadString(uchar add,uchar *p,uchar len){
- I2C_Start(); //start
- WriteI2CByte(0xa0); while(!Check_Acknowledge()); //control byte,
- WriteI2CByte(add); while(!Check_Acknowledge()); //address
- I2C_Start(); //start
- WriteI2CByte(0xa1); while(!Check_Acknowledge()); //data
- while(len--){
- *p=ReadI2CByte();
- SDA=0; //MCU發(fā)確認信號,使器件發(fā)送下一個連續(xù)地址的數(shù)據(jù)字節(jié)
- SCL=1;
- DELAY(DELAY_TIME);
- SCL=0;
- DELAY(DELAY_TIME);
- p++;
- }//讀完最后一個字節(jié)不確認,停止結(jié)束
- I2C_Stop(); //stop
- }
- /*******************main fuction*************************/
- void delayms(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void display(uchar shu)
- {
- dula=1;
- P0=table[shu];
- dula=0;
- P0=0xff;
-
- wela=1;
- P0=0xfe;
- wela=0;
- delayms(5);
- }
- void main()
- { uchar a;
- uint b;
- UartInit();
- WriteByte(1,'3');
- a=ReadByte(1);
- b=a;
- putchar(a);
- putchar(b);
- display(b-48);
- while(1);
-
- for(;;);
- }
復(fù)制代碼
|
-
-
PROTEUS.zip
2016-8-15 15:31 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
118.1 KB, 下載次數(shù): 58, 下載積分: 黑幣 -5
|