Prueba de comunicación I2C

Dependencies:   BLE_API mbed nRF51822

Fork of I2C_Demo by RedBearLab

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    0xB0
00038 
00039 //Serial pc(USBTX, USBRX);
00040 TwoWire Wire = TwoWire(NRF_TWI0);
00041 
00042 int WriteBytes(uint8_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     return (Wire.endTransmission());
00049 }
00050 
00051 int ReadBytes(uint8_t addr, uint8_t *pbuf, uint16_t length){
00052     int error=0;
00053     Wire.beginTransmission(DEV_ADDR);
00054     //Wire.write( (uint8_t)addr>>8 );
00055     Wire.write( (uint8_t)addr );    
00056     error = Wire.endTransmission();
00057        
00058     Wire.requestFrom(DEV_ADDR+1, length);
00059     while( Wire.available() > 0 )
00060     {
00061         *pbuf = Wire.read();
00062         pbuf++;
00063     }
00064     return(error);
00065 }
00066 
00067 
00068 //static uint8_t wt_data[10] = {'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'};
00069 //static uint8_t rd_data[10];
00070 
00071 //static uint16_t index;
00072 
00073 DigitalOut  led (p17);
00074 DigitalIn   button(p13,PullUp);
00075 
00076 int estado;
00077 uint8_t address, data[3];
00078 
00079 
00080 int main(void)
00081 {
00082     int estado=0;
00083     led=1;
00084     data[0]=0;
00085     data[1]=0;
00086     data[2]=0;
00087     //pc.baud(9600);
00088     //wait(5);
00089     //Wire.begin();
00090     Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);
00091     //pc.printf("IIC Demo Start \r\n");
00092     wait(0.1);
00093     while(1)
00094     {
00095         if (button==0) {
00096         estado = WriteBytes(0, data/*wt_data*/, 3);
00097         if (estado==0) {
00098             led=0;
00099         }
00100         else {
00101            led=1;
00102         }
00103             wait (0.5f);
00104         }
00105         //ReadBytes(0, rd_data, 10);     
00106         //pc.printf("Read data from AT24C512 \r\n");
00107         //for(index=0; index<10; index++)
00108         //{
00109         //    pc.putc(rd_data[index]);
00110         //    rd_data[index] = 0x00;
00111         //}
00112         //pc.printf("\r\n");
00113         //wait(1);
00114     }
00115 }
00116 
00117 
00118 
00119 
00120 
00121 
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135