![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
for learning
Diff: SPI_EEP_ENC/SPI_EEP_ENC.cpp
- Revision:
- 170:42c938a40313
- Parent:
- 16:903b5a4433b4
- Child:
- 253:0246366e0dcb
diff -r 645207e160ca -r 42c938a40313 SPI_EEP_ENC/SPI_EEP_ENC.cpp --- a/SPI_EEP_ENC/SPI_EEP_ENC.cpp Mon Nov 16 02:23:23 2020 +0000 +++ b/SPI_EEP_ENC/SPI_EEP_ENC.cpp Wed Nov 18 12:03:39 2020 +0000 @@ -3,48 +3,51 @@ #include "SPI_EEP_ENC.h" // EEPROM -//void spi_eeprom_ready(void){ -// int temp1, temp2; -// do{ -// eeprom_cs=0; -// eeprom.write(0x06); //write enable -// eeprom_cs=1; -// -// eeprom_cs=0; -// temp1 = eeprom.write(0x05); -// temp2 = eeprom.write(0x00); -// eeprom_cs=1; -// temp2=(temp2&(0x03))!= 0x02; -// } while(temp2); // before writing or reading -// } -// -// void spi_eeprom_write(unsigned short add, unsigned int data){ -// eeprom_cs=0; -// eeprom.write(0x02); -// eeprom.write(0xff&(add>>8)); -// eeprom.write(0xff&add); -// eeprom.write(0xff&data); -// eeprom.write(0xff&(data>>8)); -// eeprom.write(0xff&(data>>16)); -// eeprom.write(0xff&(data>>24)); -// eeprom_cs=1; -//} -// -//unsigned int spi_eeprom_read(unsigned short add){ -// eeprom_cs=0; -// eeprom.write(0x03); -// eeprom.write(0xff&(add>>8)); -// eeprom.write(0xff&add); -// -// int a1 = eeprom.write(0x00); -// int a2 = eeprom.write(0x00); -// int a3 = eeprom.write(0x00); -// int a4 = eeprom.write(0x00); -// eeprom_cs=1; -// //unsigned int final = (a4<<24)+(a3<<16) + (a2<<8) + a1; -// unsigned int final = (int32_t) (a1 | a2 << 8 | a3 << 16 | a4 << 24); -// return final; -// } +void spi_eeprom_ready(void){ + int temp1, temp2; + do{ + eeprom_cs=0; + eeprom.write(0x06); //write enable + eeprom_cs=1; + + eeprom_cs=0; + temp1 = eeprom.write(0x05); + temp2 = eeprom.write(0x00); + eeprom_cs=1; + temp2=(temp2&(0x03))!= 0x02; + } while(temp2); // before writing or reading + } + + void spi_eeprom_write(unsigned short add, unsigned int data){ + spi_eeprom_ready(); + add=add*4; + eeprom_cs=0; + eeprom.write(0x02); + eeprom.write(0xff&(add>>8)); + eeprom.write(0xff&add); + eeprom.write(0xff&data); + eeprom.write(0xff&(data>>8)); + eeprom.write(0xff&(data>>16)); + eeprom.write(0xff&(data>>24)); + eeprom_cs=1; +} + +unsigned int spi_eeprom_read(unsigned short add){ + add=add*4; + eeprom_cs=0; + eeprom.write(0x03); + eeprom.write(0xff&(add>>8)); + eeprom.write(0xff&add); + + int a1 = eeprom.write(0x00); + int a2 = eeprom.write(0x00); + int a3 = eeprom.write(0x00); + int a4 = eeprom.write(0x00); + eeprom_cs=1; + //unsigned int final = (a4<<24)+(a3<<16) + (a2<<8) + a1; + unsigned int final = (int32_t) (a1 | a2 << 8 | a3 << 16 | a4 << 24); + return final; + } // ENCODER