基于IIC总线的EEPROM读写实验源程序+数码管显示写入内容+protues仿真
单片机源程序如下:
- //IIC bus EEPROM write and read experiment
- //2018.9.8 WXY
- #include<reg52.h>
- #define unit unsigned int
- #define uchar unsigned char
- #define M 10
- sbit scl=P0^0;
- sbit sda=P0^1;
- uchar code writebytes[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- uchar idata recivebytes[10];
- /**************function define***************/
- void DELAY(unsigned long int n)
- {
- while(n!=0)
- n--;
- }
- /*********************/
- void START()
- {
- scl=1;
- sda=1;
- DELAY(M);
- sda=0;
- DELAY(M);
- scl=0;
- DELAY(M);
- }
- /*********************/
- void STOP()
- {
- sda=0;
- DELAY(M);
- scl=1;
- DELAY(M);
- sda=1;
- DELAY(M);
- }
- /*********************/
- void ACK()
- {
- uchar i;
- scl=1;
- DELAY(M);
- while(sda==1&&i<100)
- i++;
- scl=0;
- DELAY(M);
- }
- /**********************/
- void NO_ACK()
- {
- sda=1;
- DELAY(M); ///////////////
- scl=1;
- DELAY(M);
- scl=0;
- DELAY(M);
- }
- /**********************/
- void WRITE_A_BYTE(uchar input)
- {
- uchar t;
- for(t=8;t!=0;t--)
- {
- sda=(bit)(input&0x80);
- DELAY(M);
- scl=1;
- DELAY(M);
- input=input<<1;
- scl=0;
- DELAY(M);
- }
- }
- /*****************************/
- void WRITE_N_BYTE(uchar wadd,uchar k)
- {
- uchar input;
- START();
- WRITE_A_BYTE(0xa0);
- ACK();
- WRITE_A_BYTE(wadd+k); //write
- ACK();
- input=writebytes[k];
- WRITE_A_BYTE(input);
- ACK();
- STOP();
- }
- /*****************************/
- uchar RECIVE_A_BYTE()
- {
- uchar t;
- uchar output=0;
- for(t=8;t!=0;t--)
- {
- scl=1;
- output=output<<1;
- output=output|(uchar)sda;
- DELAY(M);
- scl=0;
- DELAY(M);
- }
- return(output);
- }
- /**************************************/
- uchar RECIVE_N_BYTE(uchar radd,uchar k) //recive n byte
- {
- uchar d;
- START();
- WRITE_A_BYTE(0xa0); //Pseudo write operation
- ACK();
- WRITE_A_BYTE(radd+k);
- ACK();
- START();
- WRITE_A_BYTE(0xa1);
- ACK();
- d=RECIVE_A_BYTE();
- NO_ACK();
- STOP();
- return d;
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
IIC与EEPROM.rar
(69.69 KB, 下载次数: 168)
|