Distributed & Cognitive Systems Group / Mbed 2 deprecated Master_I2C

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 I2C i2c(I2C_SDA, I2C_SCL);
00004 
00005 int main()
00006 {
00007     int address = 0x52;
00008     char data[2];
00009     char switch_word;
00010     switch_word=0xa0;
00011     char recd_val;
00012     
00013 i2c.frequency(400);
00014     while(1) {
00015         switch_word=switch_word|0x01;
00016         i2c.start();
00017 //force a start condition
00018         i2c.write(address);
00019 //send the address
00020         i2c.write(switch_word);
00021 //send one byte of data, ie switch_word
00022         i2c.stop();
00023         wait(0.002);
00024 //receive a single byte of data, in correct I2C package
00025         i2c.start();
00026         i2c.write(address|0x01);
00027 //send address, with R/W bit set to Read
00028         recd_val=i2c.read(address);
00029 //Read and save the received byte
00030         i2c.stop();
00031 
00032         printf("ACK: %d\n\r",recd_val);
00033        
00034     }
00035 }