EEPROM_24C32_Analog_Input save story

Dependencies:   mbed

Committer:
DL3LD
Date:
Sat May 07 17:24:10 2016 +0000
Revision:
0:5af843e949f3
EEPROM 24C32 Analog Input Save Story

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DL3LD 0:5af843e949f3 1 #include "mbed.h"
DL3LD 0:5af843e949f3 2 #define DEV_ADDR 0xAE
DL3LD 0:5af843e949f3 3
DL3LD 0:5af843e949f3 4 I2C i2c(PB_11, PB_10);
DL3LD 0:5af843e949f3 5 Serial pc(PA_2, PA_3);
DL3LD 0:5af843e949f3 6 AnalogIn FWD(PA_0);
DL3LD 0:5af843e949f3 7 AnalogIn REF(PA_1);
DL3LD 0:5af843e949f3 8
DL3LD 0:5af843e949f3 9 void AT24C32_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
DL3LD 0:5af843e949f3 10 {
DL3LD 0:5af843e949f3 11 char buf[length+2];
DL3LD 0:5af843e949f3 12 buf[0] = (uint8_t)(addr>>8);
DL3LD 0:5af843e949f3 13 buf[1] = (uint8_t)(addr);
DL3LD 0:5af843e949f3 14 memcpy(&buf[2], pbuf, length);
DL3LD 0:5af843e949f3 15 i2c.write(DEV_ADDR, buf, length+2, false);
DL3LD 0:5af843e949f3 16 }
DL3LD 0:5af843e949f3 17
DL3LD 0:5af843e949f3 18 void AT24C32_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
DL3LD 0:5af843e949f3 19 {
DL3LD 0:5af843e949f3 20 char buf[2];
DL3LD 0:5af843e949f3 21 buf[0] = (uint8_t)(addr>>8);
DL3LD 0:5af843e949f3 22 buf[1] = (uint8_t)(addr);
DL3LD 0:5af843e949f3 23 i2c.write(DEV_ADDR, buf, 2, false);
DL3LD 0:5af843e949f3 24
DL3LD 0:5af843e949f3 25 i2c.read(DEV_ADDR, (char *)pbuf, length, false);
DL3LD 0:5af843e949f3 26 }
DL3LD 0:5af843e949f3 27
DL3LD 0:5af843e949f3 28 float measure_REF;
DL3LD 0:5af843e949f3 29
DL3LD 0:5af843e949f3 30 uint8_t w_buf[1];
DL3LD 0:5af843e949f3 31 uint8_t r_buf[1];
DL3LD 0:5af843e949f3 32 uint8_t byte =0;
DL3LD 0:5af843e949f3 33 int addr=0x00;
DL3LD 0:5af843e949f3 34 char Data=0;
DL3LD 0:5af843e949f3 35 int main(void)
DL3LD 0:5af843e949f3 36 {
DL3LD 0:5af843e949f3 37 uint8_t index=0;
DL3LD 0:5af843e949f3 38
DL3LD 0:5af843e949f3 39
DL3LD 0:5af843e949f3 40 pc.baud(9600);
DL3LD 0:5af843e949f3 41 pc.printf("IIC Demo Start \r\n");
DL3LD 0:5af843e949f3 42
DL3LD 0:5af843e949f3 43 while(1)
DL3LD 0:5af843e949f3 44 {
DL3LD 0:5af843e949f3 45 //WRITE**************************************
DL3LD 0:5af843e949f3 46
DL3LD 0:5af843e949f3 47 uint8_t byte = REF.read_u16() >> 8;
DL3LD 0:5af843e949f3 48 pc.printf("EEPROM Write Byte= %d\n",byte);
DL3LD 0:5af843e949f3 49 AT24C32_WriteBytes(addr,&byte,1);
DL3LD 0:5af843e949f3 50 //*******************************************
DL3LD 0:5af843e949f3 51
DL3LD 0:5af843e949f3 52 //READ***************************************
DL3LD 0:5af843e949f3 53 wait(1);
DL3LD 0:5af843e949f3 54 AT24C32_ReadBytes(addr, r_buf,1);
DL3LD 0:5af843e949f3 55 for(index=0; index<1; index++)
DL3LD 0:5af843e949f3 56 Data=(r_buf[index]);
DL3LD 0:5af843e949f3 57 pc.printf("EEPROM Read Byte=%d\n",Data);
DL3LD 0:5af843e949f3 58
DL3LD 0:5af843e949f3 59 }
DL3LD 0:5af843e949f3 60 }