![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
EEPROMTEST
Fork of Test_nucleo_MCUUU by
main.cpp@17:41160d6c66ae, 2018-05-23 (annotated)
- Committer:
- MockyBirdTwo
- Date:
- Wed May 23 09:35:34 2018 +0000
- Revision:
- 17:41160d6c66ae
- Parent:
- 16:c998931f8395
Opgeschoond
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xorjoep | 0:952479e15354 | 1 | #include "mbed.h" // Include files and define parameters. |
xorjoep | 0:952479e15354 | 2 | #include "Adafruit_ADS1015.h" |
MockyBirdTwo | 7:65325c237ac5 | 3 | #include <vector> |
MockyBirdTwo | 9:cc559f905411 | 4 | #include <string> |
MockyBirdTwo | 16:c998931f8395 | 5 | #define EEPROM_adr (0xA0)//8Bit address for I2C to EEPROM |
MockyBirdTwo | 9:cc559f905411 | 6 | using namespace std; |
xorjoep | 0:952479e15354 | 7 | |
MockyBirdTwo | 9:cc559f905411 | 8 | I2C i2c(PB_9, PB_8); // I2C for sensorplate. |
xorjoep | 0:952479e15354 | 9 | Serial usb_serial(SERIAL_TX, SERIAL_RX); // tx, rx |
xorjoep | 0:952479e15354 | 10 | |
xorjoep | 0:952479e15354 | 11 | int i2c__frequency = 100000; // I2C Frequency. |
xorjoep | 0:952479e15354 | 12 | int baud_rate = 115200; // Baud rate. |
xorjoep | 4:fd10d8609295 | 13 | Timer timer; |
xorjoep | 4:fd10d8609295 | 14 | |
xorjoep | 0:952479e15354 | 15 | DigitalOut myled(LED1); |
MockyBirdTwo | 3:efb3a5b4bc9e | 16 | |
MockyBirdTwo | 12:c0a99de7c6d3 | 17 | //EEPROM |
MockyBirdTwo | 12:c0a99de7c6d3 | 18 | // Device Code Chip Enable RW |
MockyBirdTwo | 12:c0a99de7c6d3 | 19 | //Bit b7 b6 b5 b4 b3 b2 b1 b0 |
MockyBirdTwo | 12:c0a99de7c6d3 | 20 | //Device Select 1 0 1 0 E2 E1 E0 RW |
MockyBirdTwo | 12:c0a99de7c6d3 | 21 | // E0,E1 and E2 are to identify device |
MockyBirdTwo | 9:cc559f905411 | 22 | //Writing EEPROM through I2C |
MockyBirdTwo | 15:44d291c21362 | 23 | void EEPROM_ready(){ |
MockyBirdTwo | 14:be6538b80545 | 24 | int ack; |
MockyBirdTwo | 15:44d291c21362 | 25 | uint8_t waddr[2]; |
MockyBirdTwo | 15:44d291c21362 | 26 | waddr[0] = 0; |
MockyBirdTwo | 15:44d291c21362 | 27 | // Wait end of write |
MockyBirdTwo | 15:44d291c21362 | 28 | do { |
MockyBirdTwo | 15:44d291c21362 | 29 | ack = i2c.write(EEPROM_adr,(char *)waddr,0); |
MockyBirdTwo | 15:44d291c21362 | 30 | } while(ack != 0); |
MockyBirdTwo | 9:cc559f905411 | 31 | } |
MockyBirdTwo | 8:2875bb7a4e67 | 32 | |
MockyBirdTwo | 15:44d291c21362 | 33 | void EEPROM_write(uint32_t Wreg, int8_t EEPROM_dat){ |
MockyBirdTwo | 15:44d291c21362 | 34 | int ack; |
MockyBirdTwo | 15:44d291c21362 | 35 | uint8_t cmd[3]; |
MockyBirdTwo | 15:44d291c21362 | 36 | cmd[0] = (uint8_t) (Wreg >> 8); //Address MSB |
MockyBirdTwo | 15:44d291c21362 | 37 | cmd[1] = (uint8_t) Wreg; //Address LSB |
MockyBirdTwo | 15:44d291c21362 | 38 | cmd[2] = (uint8_t) EEPROM_dat; //Data |
MockyBirdTwo | 15:44d291c21362 | 39 | |
MockyBirdTwo | 15:44d291c21362 | 40 | ack=i2c.write(EEPROM_adr,(char*)cmd,sizeof(cmd)); |
MockyBirdTwo | 15:44d291c21362 | 41 | if(ack != 0) { |
MockyBirdTwo | 15:44d291c21362 | 42 | usb_serial.printf("ERRORwrite\r\n"); |
MockyBirdTwo | 15:44d291c21362 | 43 | } |
MockyBirdTwo | 15:44d291c21362 | 44 | // Wait end of write |
MockyBirdTwo | 15:44d291c21362 | 45 | EEPROM_ready(); |
MockyBirdTwo | 15:44d291c21362 | 46 | } |
MockyBirdTwo | 15:44d291c21362 | 47 | |
MockyBirdTwo | 15:44d291c21362 | 48 | void EEPROM_read(uint32_t Wreg, int8_t& EEPROM_dat){ |
MockyBirdTwo | 15:44d291c21362 | 49 | int ack; |
MockyBirdTwo | 15:44d291c21362 | 50 | uint8_t cmd[2]; |
MockyBirdTwo | 15:44d291c21362 | 51 | |
MockyBirdTwo | 16:c998931f8395 | 52 | cmd[0] = (uint8_t) (Wreg >> 8); //Address MSB |
MockyBirdTwo | 16:c998931f8395 | 53 | cmd[1] = (uint8_t) Wreg; //Address LSB |
MockyBirdTwo | 16:c998931f8395 | 54 | |
MockyBirdTwo | 15:44d291c21362 | 55 | ack = i2c.write(EEPROM_adr,(char*)cmd,2,true); |
MockyBirdTwo | 15:44d291c21362 | 56 | if(ack != 0) { |
MockyBirdTwo | 15:44d291c21362 | 57 | usb_serial.printf("ERRORfakewrite\r\n"); |
MockyBirdTwo | 15:44d291c21362 | 58 | } |
MockyBirdTwo | 15:44d291c21362 | 59 | ack=i2c.read(EEPROM_adr,(char*)&EEPROM_dat,sizeof(EEPROM_dat)); |
MockyBirdTwo | 15:44d291c21362 | 60 | if(ack != 0) { |
MockyBirdTwo | 15:44d291c21362 | 61 | usb_serial.printf("ERRORread\r\n"); |
MockyBirdTwo | 15:44d291c21362 | 62 | } |
MockyBirdTwo | 15:44d291c21362 | 63 | } |
MockyBirdTwo | 14:be6538b80545 | 64 | |
xorjoep | 0:952479e15354 | 65 | int main(){ |
MockyBirdTwo | 14:be6538b80545 | 66 | |
MockyBirdTwo | 9:cc559f905411 | 67 | i2c.frequency(i2c__frequency); // Set frequency for i2c connection to sensorplate (variable is declared in config part). |
MockyBirdTwo | 15:44d291c21362 | 68 | usb_serial.baud(baud_rate); // Set serial USB connection baud rate (variable is declared in config part). |
MockyBirdTwo | 14:be6538b80545 | 69 | |
MockyBirdTwo | 15:44d291c21362 | 70 | uint32_t Wreg= 10; |
MockyBirdTwo | 15:44d291c21362 | 71 | int8_t EEPROM_dat=56; |
MockyBirdTwo | 15:44d291c21362 | 72 | int8_t result=0; |
MockyBirdTwo | 15:44d291c21362 | 73 | |
MockyBirdTwo | 15:44d291c21362 | 74 | usb_serial.printf("Startooo\r\n"); |
MockyBirdTwo | 15:44d291c21362 | 75 | EEPROM_write(Wreg,EEPROM_dat); |
MockyBirdTwo | 15:44d291c21362 | 76 | EEPROM_read(Wreg,result); |
MockyBirdTwo | 15:44d291c21362 | 77 | usb_serial.printf("EEPROMdata = %d\r\n", result); |
xorjoep | 0:952479e15354 | 78 | } |