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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003 Copyright (c) 2012-2014 RedBearLab
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 
00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
00009 subject to the following conditions:
00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00011 
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 
00018 */
00019 
00020 #include "mbed.h"
00021 #include "wire.h"
00022 
00023 #define BLE_Nano
00024 //#define nRF_51822
00025 
00026 
00027 #ifdef nRF_51822
00028 #define SCL         28
00029 #define SDA         29
00030 #endif
00031 
00032 #ifdef BLE_Nano
00033 #define SCL         7  
00034 #define SDA         6
00035 #endif
00036 
00037 #define DEV_ADDR    0xA0
00038 
00039 Serial pc(USBTX, USBRX);
00040 TwoWire Wire = TwoWire(NRF_TWI0);
00041 
00042 void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
00043 {
00044     Wire.beginTransmission(DEV_ADDR);
00045     Wire.write( (uint8_t)addr>>8 );
00046     Wire.write( (uint8_t)addr );
00047     Wire.write(pbuf, length);
00048     Wire.endTransmission();
00049 }
00050 
00051 void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length)
00052 {
00053     Wire.beginTransmission(DEV_ADDR);
00054     Wire.write( (uint8_t)addr>>8 );
00055     Wire.write( (uint8_t)addr );    
00056     Wire.endTransmission();
00057        
00058     Wire.requestFrom(DEV_ADDR+1, length);
00059     while( Wire.available() > 0 )
00060     {
00061         *pbuf = Wire.read();
00062         pbuf++;
00063     }
00064 }
00065 
00066 
00067 static uint8_t wt_data[10] = {'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'};
00068 static uint8_t rd_data[10];
00069 
00070 static uint16_t index;
00071 
00072 int main(void)
00073 {
00074     pc.baud(9600);
00075     wait(5);
00076     //Wire.begin();
00077     Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
00078     pc.printf("IIC Demo Start \r\n");
00079     
00080     AT24C512_WriteBytes(0, wt_data, 10);
00081     wait(0.1);
00082     
00083     while(1)
00084     {
00085         AT24C512_ReadBytes(0, rd_data, 10);     
00086         pc.printf("Read data from AT24C512 \r\n");
00087         for(index=0; index<10; index++)
00088         {
00089             pc.putc(rd_data[index]);
00090             rd_data[index] = 0x00;
00091         }
00092         pc.printf("\r\n");
00093         wait(1);
00094     }
00095 }
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115