Home > other >  IIC
IIC

Time:05-11

Eeprom read-write experiment, after the system is powered on block from eeprom read a byte data to send digital tube display, values range 00-99, this value can be modified by keyboard, a modified block through the save button to eeprom, read the next system on electric is the value of the modified,
* continuous write: 2 k up to write seven consecutive bytes 4 k, 8 k, 16 k most continuous write 15 successive bytes read: there is no limit to the number of bytes, if beyond memory, automatically from the beginning to start */# include & lt; Intrins. H>//# include & lt; Reg51. H> #include #include # define uchar unsigned char# define uint unsigned intuchar writenumber [10]=,2,3,4,5,6,7,8,9,10 {1}; Uchar readnumber [10]={0}; Uchar controlbyte;/* -- -- -- -- -- -- -- IIC control bytes -- -- -- -- -- -- -- -- - */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); {while void Delay1us (uchar T) (T) {_nop_ (); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); - T; {}} void Delay1ms (uchar T) while (T) {Delay1us (1000); - T; }}/* -- -- -- -- -- -- -- -- -- -- IIC operation -- -- -- -- -- -- -- -- -- -- -- - */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=((0 x80 & amp; Sendbyte)==0 x80);/* -- -- -- -- -- -- -- -- only SCL=0, change the data on the SDA is effective, -- -- -- -- -- -- -- */_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 | (0 x01 & lt; <(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& 0 x01))/* -- -- -- -- -- -- -- -- if write command -- -- -- -- -- -- -- */{IIC_Start (); IIC_WriteByte (ControlByte& 0 xfe); IIC_Wait_ACK ();/* 24 our fleet of proteus must use double byte address, circuit boards 24 our fleet, address single-byte double byte can be *///IIC_WriteByte ((unsigned char) (Address> 8));//experiment box device for 24 c32 and above, the address is double byte//IIC_Wait_ACK (); IIC_WriteByte ((unsigned char) (Address)); IIC_Wait_ACK (); While (I -) {IIC_WriteByte DataBuff++ (*); IIC_Wait_ACK (); } IIC_Stop (); } the else/* -- -- -- -- -- -- -- -- if read command -- -- -- -- -- -- -- */{IIC_Start (); IIC_WriteByte (ControlByte& 0 xfe); IIC_Wait_ACK ();//IIC_WriteByte ((unsigned char) (Address> 8));//device for 24 c32 and above, the address is double byte//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 (); }} the main () {uchar I, success=1; WRITE_LED=1; READ_LED=1; ERR_LED=1; Controlbyte=0 xa0; RW_24cxx (writenumber, seven, zero, controlbyte);/* -- -- -- -- -- -- -- -- write data test -- -- -- -- -- -- -- - */Delay1ms (100); WRITE_LED=0; Controlbyte=0 xa1; RW_24cxx (readnumber, seven, zero, controlbyte);/* -- -- -- -- -- -- -- -- read data test -- -- -- -- -- -- -- - */Delay1ms (100); READ_LED=0; for(i=0; i<7. I++) {if (readnumber [I]! Success=0=writenumber [I]); {if} the while (1) (success==0) {ERR_LED=~ ERR_LED; Delay1ms (100); } the else ERR_LED=0; }}

CodePudding user response:

????? You this post????

CodePudding user response:

The following was written by the teacher block an eeprom program

CodePudding user response:

reference 1st floor seedundersnow response:
????? You this post????

The teacher write block of the eeprom program