📄 eeprom.c
字号:
// Written by Ed Waugh 2004, www.edwaugh.co.uk// for the original source, and hundreds more examples of PIC C code, see:// http://www.microchipc.com/sourcecode/#eeprom#use I2C(Master,sda=PIN_C4,scl=PIN_C3,FAST)void i2c_eeprom_write(unsigned char addrh,unsigned char addrl,unsigned char data);unsigned char i2c_eeprom_read(unsigned char addrh,unsigned char addrl);void i2c_eeprom_write_str(unsigned char addrh,unsigned char addrl,char* data);/************************** Eeprom Write **************************************//**** Writes a single byte to the eeprom supports 2^16 addresses ****/void i2c_eeprom_write(unsigned char addrh,unsigned char addrl,unsigned char data){ i2c_start(); i2c_write(0xA0); // send chip addr and set data to write mode i2c_write(addrh); // high addr bits i2c_write(addrl); // low addr bits i2c_write(data); // write data i2c_stop(); delay_ms(5);}/************************** Eeprom Read **************************************//**** Reads a single byte from the eeprom supports 2^16 addresses ****/unsigned char i2c_eeprom_read(unsigned char addrh,unsigned char addrl){ unsigned char temp; i2c_start(); i2c_write(0xA0); // send address data in write mode i2c_write(addrh); // high addr bits i2c_write(addrl); // low addr bits i2c_start(); i2c_write(0xA1); // change to read mode temp = i2c_read(0); // reads are not acknowledged (0) i2c_stop(); // if no stop() can get continuous readreturn temp;}/************************** Eeprom write string **************************************//**** Writes a null terminated string of any length to the eeprom ****/void i2c_eeprom_write_str(unsigned char addrh,unsigned char addrl,char* data){ while(*data) { i2c_start(); i2c_write(0xA0); // send chip addr and set data to write mode i2c_write(addrh); // high addr bits i2c_write(addrl); // low addr bits i2c_write(*data++); // write data i2c_stop(); addrl++; if(addrl==0x00) addrh++; delay_ms(5); }}/************************** Eeprom read contiguous start **************************************//**** starts a contiguous read from the eeprom, use read_contig() after this ****/int eeprom_read_contig_start(long addr){ int data; int upper_addr; int lower_addr; upper_addr=(int)(addr >> 8); /*Shift upper address bits to form an int holding just the upper portion of the address*/ lower_addr=(int)(addr & 0x00FF); /*Mask off lower address bits and place in int*/ i2c_start(); /*Start communication */ i2c_write(0xa0); /*Xmit control code, chip addr, and write bit */ i2c_write(upper_addr); /*X Upper mem addr bits*/ i2c_write(lower_addr); /*Lower addr bits*/ i2c_start(); /*Xmit start again to signify read portion*/ i2c_write(0xa1); /*Xmit control code, chip addr, and read bit */ data=i2c_read(); /*Read data*/ return(data);}/************************** Eeprom read contiguous **************************************//**** Reads the next byte of a contiguous read, you must have read the 1st byte with contig_start ****/int eeprom_read_contig(void){ int data; data=i2c_read(); /*Read data*/ return(data);}/************************** Eeprom end read contiguous **************************************//**** ends contiguous read ****/int eeprom_read_contig_stop(void){ int data; data=i2c_read(0); /*Read data - no ack*/ i2c_stop(); /*Stop communication*/ return(data);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -