I2C example for BLE Nano and RedBearLab nRF51822. It demonstrates how to use I2C to communicate with AT24C512 EEPROM device.

Dependencies:   mbed BLE_API nRF51822

Committer:
RedBearLab
Date:
Thu Jan 07 02:55:12 2016 +0000
Revision:
2:0298ffee8eba
Parent:
0:34c256ab7309
Update libraries.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 0:34c256ab7309 1 /*
RedBearLab 0:34c256ab7309 2
RedBearLab 0:34c256ab7309 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 0:34c256ab7309 4
RedBearLab 0:34c256ab7309 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 0:34c256ab7309 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 0:34c256ab7309 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 0:34c256ab7309 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 0:34c256ab7309 9 subject to the following conditions:
RedBearLab 0:34c256ab7309 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 0:34c256ab7309 11
RedBearLab 0:34c256ab7309 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 0:34c256ab7309 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 0:34c256ab7309 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 0:34c256ab7309 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 0:34c256ab7309 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 0:34c256ab7309 17
RedBearLab 0:34c256ab7309 18 */
RedBearLab 0:34c256ab7309 19
RedBearLab 0:34c256ab7309 20 #include "mbed.h"
RedBearLab 0:34c256ab7309 21 #include "wire.h"
RedBearLab 0:34c256ab7309 22
RedBearLab 0:34c256ab7309 23 #define BLE_Nano
RedBearLab 0:34c256ab7309 24 //#define nRF_51822
RedBearLab 0:34c256ab7309 25
RedBearLab 0:34c256ab7309 26
RedBearLab 0:34c256ab7309 27 #ifdef nRF_51822
RedBearLab 0:34c256ab7309 28 #define SCL 28
RedBearLab 0:34c256ab7309 29 #define SDA 29
RedBearLab 0:34c256ab7309 30 #endif
RedBearLab 0:34c256ab7309 31
RedBearLab 0:34c256ab7309 32 #ifdef BLE_Nano
RedBearLab 0:34c256ab7309 33 #define SCL 7
RedBearLab 0:34c256ab7309 34 #define SDA 6
RedBearLab 0:34c256ab7309 35 #endif
RedBearLab 0:34c256ab7309 36
RedBearLab 0:34c256ab7309 37 #define DEV_ADDR 0xA0
RedBearLab 0:34c256ab7309 38
RedBearLab 0:34c256ab7309 39 Serial pc(USBTX, USBRX);
RedBearLab 0:34c256ab7309 40 TwoWire Wire = TwoWire(NRF_TWI0);
RedBearLab 0:34c256ab7309 41
RedBearLab 0:34c256ab7309 42 void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
RedBearLab 0:34c256ab7309 43 {
RedBearLab 0:34c256ab7309 44 Wire.beginTransmission(DEV_ADDR);
RedBearLab 0:34c256ab7309 45 Wire.write( (uint8_t)addr>>8 );
RedBearLab 0:34c256ab7309 46 Wire.write( (uint8_t)addr );
RedBearLab 0:34c256ab7309 47 Wire.write(pbuf, length);
RedBearLab 0:34c256ab7309 48 Wire.endTransmission();
RedBearLab 0:34c256ab7309 49 }
RedBearLab 0:34c256ab7309 50
RedBearLab 0:34c256ab7309 51 void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
RedBearLab 0:34c256ab7309 52 {
RedBearLab 0:34c256ab7309 53 Wire.beginTransmission(DEV_ADDR);
RedBearLab 0:34c256ab7309 54 Wire.write( (uint8_t)addr>>8 );
RedBearLab 0:34c256ab7309 55 Wire.write( (uint8_t)addr );
RedBearLab 0:34c256ab7309 56 Wire.endTransmission();
RedBearLab 0:34c256ab7309 57
RedBearLab 0:34c256ab7309 58 Wire.requestFrom(DEV_ADDR+1, length);
RedBearLab 0:34c256ab7309 59 while( Wire.available() > 0 )
RedBearLab 0:34c256ab7309 60 {
RedBearLab 0:34c256ab7309 61 *pbuf = Wire.read();
RedBearLab 0:34c256ab7309 62 pbuf++;
RedBearLab 0:34c256ab7309 63 }
RedBearLab 0:34c256ab7309 64 }
RedBearLab 0:34c256ab7309 65
RedBearLab 0:34c256ab7309 66
RedBearLab 0:34c256ab7309 67 static uint8_t wt_data[10] = {'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'};
RedBearLab 0:34c256ab7309 68 static uint8_t rd_data[10];
RedBearLab 0:34c256ab7309 69
RedBearLab 0:34c256ab7309 70 static uint16_t index;
RedBearLab 0:34c256ab7309 71
RedBearLab 0:34c256ab7309 72 int main(void)
RedBearLab 0:34c256ab7309 73 {
RedBearLab 0:34c256ab7309 74 pc.baud(9600);
RedBearLab 0:34c256ab7309 75 wait(5);
RedBearLab 0:34c256ab7309 76 //Wire.begin();
RedBearLab 0:34c256ab7309 77 Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
RedBearLab 0:34c256ab7309 78 pc.printf("IIC Demo Start \r\n");
RedBearLab 0:34c256ab7309 79
RedBearLab 0:34c256ab7309 80 AT24C512_WriteBytes(0, wt_data, 10);
RedBearLab 0:34c256ab7309 81 wait(0.1);
RedBearLab 0:34c256ab7309 82
RedBearLab 0:34c256ab7309 83 while(1)
RedBearLab 0:34c256ab7309 84 {
RedBearLab 0:34c256ab7309 85 AT24C512_ReadBytes(0, rd_data, 10);
RedBearLab 0:34c256ab7309 86 pc.printf("Read data from AT24C512 \r\n");
RedBearLab 0:34c256ab7309 87 for(index=0; index<10; index++)
RedBearLab 0:34c256ab7309 88 {
RedBearLab 0:34c256ab7309 89 pc.putc(rd_data[index]);
RedBearLab 0:34c256ab7309 90 rd_data[index] = 0x00;
RedBearLab 0:34c256ab7309 91 }
RedBearLab 0:34c256ab7309 92 pc.printf("\r\n");
RedBearLab 0:34c256ab7309 93 wait(1);
RedBearLab 0:34c256ab7309 94 }
RedBearLab 0:34c256ab7309 95 }
RedBearLab 0:34c256ab7309 96
RedBearLab 0:34c256ab7309 97
RedBearLab 0:34c256ab7309 98
RedBearLab 0:34c256ab7309 99
RedBearLab 0:34c256ab7309 100
RedBearLab 0:34c256ab7309 101
RedBearLab 0:34c256ab7309 102
RedBearLab 0:34c256ab7309 103
RedBearLab 0:34c256ab7309 104
RedBearLab 0:34c256ab7309 105
RedBearLab 0:34c256ab7309 106
RedBearLab 0:34c256ab7309 107
RedBearLab 0:34c256ab7309 108
RedBearLab 0:34c256ab7309 109
RedBearLab 0:34c256ab7309 110
RedBearLab 0:34c256ab7309 111
RedBearLab 0:34c256ab7309 112
RedBearLab 0:34c256ab7309 113
RedBearLab 0:34c256ab7309 114
RedBearLab 0:34c256ab7309 115