![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Prueba de comunicación I2C
Dependencies: BLE_API mbed nRF51822
Fork of I2C_Demo by
Diff: main.cpp
- Revision:
- 3:982bc9dad64e
- Parent:
- 0:34c256ab7309
diff -r 0298ffee8eba -r 982bc9dad64e main.cpp --- a/main.cpp Thu Jan 07 02:55:12 2016 +0000 +++ b/main.cpp Thu Jun 30 07:31:52 2016 +0000 @@ -20,8 +20,8 @@ #include "mbed.h" #include "wire.h" -#define BLE_Nano -//#define nRF_51822 +//#define BLE_Nano +#define nRF_51822 #ifdef nRF_51822 @@ -34,26 +34,26 @@ #define SDA 6 #endif -#define DEV_ADDR 0xA0 +#define DEV_ADDR 0xB0 -Serial pc(USBTX, USBRX); +//Serial pc(USBTX, USBRX); TwoWire Wire = TwoWire(NRF_TWI0); -void AT24C512_WriteBytes(uint16_t addr, uint8_t *pbuf, uint16_t length) -{ +int WriteBytes(uint8_t addr, uint8_t *pbuf, uint16_t length){ + Wire.beginTransmission(DEV_ADDR); - Wire.write( (uint8_t)addr>>8 ); + //Wire.write( (uint8_t)addr>>8 ); Wire.write( (uint8_t)addr ); Wire.write(pbuf, length); - Wire.endTransmission(); + return (Wire.endTransmission()); } -void AT24C512_ReadBytes(uint16_t addr, uint8_t *pbuf, uint16_t length) -{ +int ReadBytes(uint8_t addr, uint8_t *pbuf, uint16_t length){ + int error=0; Wire.beginTransmission(DEV_ADDR); - Wire.write( (uint8_t)addr>>8 ); + //Wire.write( (uint8_t)addr>>8 ); Wire.write( (uint8_t)addr ); - Wire.endTransmission(); + error = Wire.endTransmission(); Wire.requestFrom(DEV_ADDR+1, length); while( Wire.available() > 0 ) @@ -61,36 +61,56 @@ *pbuf = Wire.read(); pbuf++; } + return(error); } -static uint8_t wt_data[10] = {'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'}; -static uint8_t rd_data[10]; +//static uint8_t wt_data[10] = {'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'}; +//static uint8_t rd_data[10]; + +//static uint16_t index; -static uint16_t index; +DigitalOut led (p17); +DigitalIn button(p13,PullUp); + +int estado; +uint8_t address, data[3]; + int main(void) { - pc.baud(9600); - wait(5); + int estado=0; + led=1; + data[0]=0; + data[1]=0; + data[2]=0; + //pc.baud(9600); + //wait(5); //Wire.begin(); Wire.begin(SCL, SDA, TWI_FREQUENCY_100K); - pc.printf("IIC Demo Start \r\n"); - - AT24C512_WriteBytes(0, wt_data, 10); + //pc.printf("IIC Demo Start \r\n"); wait(0.1); - while(1) { - AT24C512_ReadBytes(0, rd_data, 10); - pc.printf("Read data from AT24C512 \r\n"); - for(index=0; index<10; index++) - { - pc.putc(rd_data[index]); - rd_data[index] = 0x00; + if (button==0) { + estado = WriteBytes(0, data/*wt_data*/, 3); + if (estado==0) { + led=0; + } + else { + led=1; + } + wait (0.5f); } - pc.printf("\r\n"); - wait(1); + //ReadBytes(0, rd_data, 10); + //pc.printf("Read data from AT24C512 \r\n"); + //for(index=0; index<10; index++) + //{ + // pc.putc(rd_data[index]); + // rd_data[index] = 0x00; + //} + //pc.printf("\r\n"); + //wait(1); } }