I2C Tester
main.cpp
- Committer:
- jdiogo10
- Date:
- 2015-08-06
- Revision:
- 0:bc53a5d93a32
File content as of revision 0:bc53a5d93a32:
#include "mbed.h" #define SHT21_REG_DATA (0x00) // Data Register #define SHT21_REG_CONF (0x01) // Configuration Register #define SHT21_ADDR (0xAA) // SHT21 address I2C i2c1(D14, D15); // This is I2C1 SDA, SCL //PwmOut attiny(D3); // This is PWM2/2 DigitalOut attiny(D3); // This is PWM2/2 Serial usart1(D10, D2); // This is USART1 TX, RX InterruptIn button(USER_BUTTON); // This is USERBUTTON (PC_13) Ticker flipper; DigitalOut led(LED1); volatile bool button_pressed = false; //void usart1_RxIrq() //{ // if(usart1.readable()) // { // } //} void pwmon() { button_pressed = true; } void pwmoff() { button_pressed = false; } void flip() { led = !led; attiny = !attiny; } int main() { bool set_pwm = false; int data = 0; char data_write[2]; char data_read[1]; // Configure the Temperature sensor device SHT21: // data_write[0] = SHT21_REG_CONF; // data_write[1] = 0x00; // i2c1.write(SHT21_ADDR, data_write, 2, 0); // Configure serial port usart1.baud(115200); // Baud rate //usart1.attach(&usart1_RxIrq, Serial::RxIrq); // RX interrupt handler // Configure button interrupt button.fall(&pwmon); button.rise(&pwmoff); while(1) { if(button_pressed) { data = 0; if(!set_pwm) { // Configure pwm // attiny.period_ms(50); // choose the period of the pwm // attiny.write(0.50f); // 50% duty cycle set_pwm = true; led = 1; attiny = 1; flipper.attach(&flip, 3.0); // the address of the function to be attached (flip) and the interval (3 seconds) } } else { // attiny.write(0); // Turn off the pwm flipper.detach(); set_pwm = false; attiny = 0; led = 0; // Write and Read from the slave data_write[0] = SHT21_REG_DATA; data_write[1] = data; i2c1.write(SHT21_ADDR, data_write, 2, false); i2c1.read(SHT21_ADDR, data_read, 1, false); usart1.printf("Value = %d\n", data_read); wait(2.0); data++; } } }