I2C Tester
main.cpp@0:bc53a5d93a32, 2015-08-06 (annotated)
- Committer:
- jdiogo10
- Date:
- Thu Aug 06 17:05:41 2015 +0000
- Revision:
- 0:bc53a5d93a32
ESRG - JV
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jdiogo10 | 0:bc53a5d93a32 | 1 | #include "mbed.h" |
jdiogo10 | 0:bc53a5d93a32 | 2 | |
jdiogo10 | 0:bc53a5d93a32 | 3 | #define SHT21_REG_DATA (0x00) // Data Register |
jdiogo10 | 0:bc53a5d93a32 | 4 | #define SHT21_REG_CONF (0x01) // Configuration Register |
jdiogo10 | 0:bc53a5d93a32 | 5 | #define SHT21_ADDR (0xAA) // SHT21 address |
jdiogo10 | 0:bc53a5d93a32 | 6 | |
jdiogo10 | 0:bc53a5d93a32 | 7 | I2C i2c1(D14, D15); // This is I2C1 SDA, SCL |
jdiogo10 | 0:bc53a5d93a32 | 8 | //PwmOut attiny(D3); // This is PWM2/2 |
jdiogo10 | 0:bc53a5d93a32 | 9 | DigitalOut attiny(D3); // This is PWM2/2 |
jdiogo10 | 0:bc53a5d93a32 | 10 | Serial usart1(D10, D2); // This is USART1 TX, RX |
jdiogo10 | 0:bc53a5d93a32 | 11 | InterruptIn button(USER_BUTTON); // This is USERBUTTON (PC_13) |
jdiogo10 | 0:bc53a5d93a32 | 12 | |
jdiogo10 | 0:bc53a5d93a32 | 13 | Ticker flipper; |
jdiogo10 | 0:bc53a5d93a32 | 14 | DigitalOut led(LED1); |
jdiogo10 | 0:bc53a5d93a32 | 15 | |
jdiogo10 | 0:bc53a5d93a32 | 16 | volatile bool button_pressed = false; |
jdiogo10 | 0:bc53a5d93a32 | 17 | |
jdiogo10 | 0:bc53a5d93a32 | 18 | //void usart1_RxIrq() |
jdiogo10 | 0:bc53a5d93a32 | 19 | //{ |
jdiogo10 | 0:bc53a5d93a32 | 20 | // if(usart1.readable()) |
jdiogo10 | 0:bc53a5d93a32 | 21 | // { |
jdiogo10 | 0:bc53a5d93a32 | 22 | // } |
jdiogo10 | 0:bc53a5d93a32 | 23 | //} |
jdiogo10 | 0:bc53a5d93a32 | 24 | |
jdiogo10 | 0:bc53a5d93a32 | 25 | void pwmon() |
jdiogo10 | 0:bc53a5d93a32 | 26 | { |
jdiogo10 | 0:bc53a5d93a32 | 27 | button_pressed = true; |
jdiogo10 | 0:bc53a5d93a32 | 28 | } |
jdiogo10 | 0:bc53a5d93a32 | 29 | |
jdiogo10 | 0:bc53a5d93a32 | 30 | void pwmoff() |
jdiogo10 | 0:bc53a5d93a32 | 31 | { |
jdiogo10 | 0:bc53a5d93a32 | 32 | button_pressed = false; |
jdiogo10 | 0:bc53a5d93a32 | 33 | } |
jdiogo10 | 0:bc53a5d93a32 | 34 | |
jdiogo10 | 0:bc53a5d93a32 | 35 | void flip() |
jdiogo10 | 0:bc53a5d93a32 | 36 | { |
jdiogo10 | 0:bc53a5d93a32 | 37 | led = !led; |
jdiogo10 | 0:bc53a5d93a32 | 38 | attiny = !attiny; |
jdiogo10 | 0:bc53a5d93a32 | 39 | } |
jdiogo10 | 0:bc53a5d93a32 | 40 | |
jdiogo10 | 0:bc53a5d93a32 | 41 | int main() |
jdiogo10 | 0:bc53a5d93a32 | 42 | { |
jdiogo10 | 0:bc53a5d93a32 | 43 | bool set_pwm = false; |
jdiogo10 | 0:bc53a5d93a32 | 44 | int data = 0; |
jdiogo10 | 0:bc53a5d93a32 | 45 | char data_write[2]; |
jdiogo10 | 0:bc53a5d93a32 | 46 | char data_read[1]; |
jdiogo10 | 0:bc53a5d93a32 | 47 | |
jdiogo10 | 0:bc53a5d93a32 | 48 | // Configure the Temperature sensor device SHT21: |
jdiogo10 | 0:bc53a5d93a32 | 49 | // data_write[0] = SHT21_REG_CONF; |
jdiogo10 | 0:bc53a5d93a32 | 50 | // data_write[1] = 0x00; |
jdiogo10 | 0:bc53a5d93a32 | 51 | // i2c1.write(SHT21_ADDR, data_write, 2, 0); |
jdiogo10 | 0:bc53a5d93a32 | 52 | |
jdiogo10 | 0:bc53a5d93a32 | 53 | // Configure serial port |
jdiogo10 | 0:bc53a5d93a32 | 54 | usart1.baud(115200); // Baud rate |
jdiogo10 | 0:bc53a5d93a32 | 55 | //usart1.attach(&usart1_RxIrq, Serial::RxIrq); // RX interrupt handler |
jdiogo10 | 0:bc53a5d93a32 | 56 | |
jdiogo10 | 0:bc53a5d93a32 | 57 | // Configure button interrupt |
jdiogo10 | 0:bc53a5d93a32 | 58 | button.fall(&pwmon); |
jdiogo10 | 0:bc53a5d93a32 | 59 | button.rise(&pwmoff); |
jdiogo10 | 0:bc53a5d93a32 | 60 | |
jdiogo10 | 0:bc53a5d93a32 | 61 | while(1) |
jdiogo10 | 0:bc53a5d93a32 | 62 | { |
jdiogo10 | 0:bc53a5d93a32 | 63 | if(button_pressed) |
jdiogo10 | 0:bc53a5d93a32 | 64 | { |
jdiogo10 | 0:bc53a5d93a32 | 65 | data = 0; |
jdiogo10 | 0:bc53a5d93a32 | 66 | |
jdiogo10 | 0:bc53a5d93a32 | 67 | if(!set_pwm) |
jdiogo10 | 0:bc53a5d93a32 | 68 | { |
jdiogo10 | 0:bc53a5d93a32 | 69 | // Configure pwm |
jdiogo10 | 0:bc53a5d93a32 | 70 | // attiny.period_ms(50); // choose the period of the pwm |
jdiogo10 | 0:bc53a5d93a32 | 71 | // attiny.write(0.50f); // 50% duty cycle |
jdiogo10 | 0:bc53a5d93a32 | 72 | |
jdiogo10 | 0:bc53a5d93a32 | 73 | set_pwm = true; |
jdiogo10 | 0:bc53a5d93a32 | 74 | led = 1; |
jdiogo10 | 0:bc53a5d93a32 | 75 | attiny = 1; |
jdiogo10 | 0:bc53a5d93a32 | 76 | |
jdiogo10 | 0:bc53a5d93a32 | 77 | flipper.attach(&flip, 3.0); // the address of the function to be attached (flip) and the interval (3 seconds) |
jdiogo10 | 0:bc53a5d93a32 | 78 | } |
jdiogo10 | 0:bc53a5d93a32 | 79 | } |
jdiogo10 | 0:bc53a5d93a32 | 80 | else |
jdiogo10 | 0:bc53a5d93a32 | 81 | { |
jdiogo10 | 0:bc53a5d93a32 | 82 | // attiny.write(0); // Turn off the pwm |
jdiogo10 | 0:bc53a5d93a32 | 83 | flipper.detach(); |
jdiogo10 | 0:bc53a5d93a32 | 84 | |
jdiogo10 | 0:bc53a5d93a32 | 85 | set_pwm = false; |
jdiogo10 | 0:bc53a5d93a32 | 86 | attiny = 0; |
jdiogo10 | 0:bc53a5d93a32 | 87 | led = 0; |
jdiogo10 | 0:bc53a5d93a32 | 88 | |
jdiogo10 | 0:bc53a5d93a32 | 89 | // Write and Read from the slave |
jdiogo10 | 0:bc53a5d93a32 | 90 | data_write[0] = SHT21_REG_DATA; |
jdiogo10 | 0:bc53a5d93a32 | 91 | data_write[1] = data; |
jdiogo10 | 0:bc53a5d93a32 | 92 | i2c1.write(SHT21_ADDR, data_write, 2, false); |
jdiogo10 | 0:bc53a5d93a32 | 93 | i2c1.read(SHT21_ADDR, data_read, 1, false); |
jdiogo10 | 0:bc53a5d93a32 | 94 | |
jdiogo10 | 0:bc53a5d93a32 | 95 | usart1.printf("Value = %d\n", data_read); |
jdiogo10 | 0:bc53a5d93a32 | 96 | wait(2.0); |
jdiogo10 | 0:bc53a5d93a32 | 97 | |
jdiogo10 | 0:bc53a5d93a32 | 98 | data++; |
jdiogo10 | 0:bc53a5d93a32 | 99 | } |
jdiogo10 | 0:bc53a5d93a32 | 100 | } |
jdiogo10 | 0:bc53a5d93a32 | 101 | } |