Marek Trojan
/
_8_KL46_M_simple_write
program for I2C master device
Fork of I2C_HelloWorld_Mbed by
main.cpp@9:36fc715eb54c, 2015-12-17 (annotated)
- Committer:
- marcus255
- Date:
- Thu Dec 17 23:04:29 2015 +0000
- Revision:
- 9:36fc715eb54c
- Parent:
- 8:3e6945d5d9a1
- Child:
- 10:39dd1d3508c3
I2C device selected by pushbuttons, sequential memory read, writting random data sequentially (works, but some troubles with addressing occurs)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcus255 | 4:1ec553356dcc | 1 | // I2C Master |
marcus255 | 4:1ec553356dcc | 2 | #include "main.h" |
marcus255 | 9:36fc715eb54c | 3 | |
marcus255 | 9:36fc715eb54c | 4 | int main() |
marcus255 | 9:36fc715eb54c | 5 | { |
marcus255 | 4:1ec553356dcc | 6 | init(); |
marcus255 | 8:3e6945d5d9a1 | 7 | wait_ms(1000); |
marcus255 | 4:1ec553356dcc | 8 | while (1) {} |
marcus255 | 4:1ec553356dcc | 9 | } |
mbed_official | 0:f76c26307f9a | 10 | |
marcus255 | 9:36fc715eb54c | 11 | void write_and_read() |
marcus255 | 9:36fc715eb54c | 12 | { |
marcus255 | 9:36fc715eb54c | 13 | if (routine == MEMORY_ROUTINE) { |
marcus255 | 9:36fc715eb54c | 14 | write_data(); |
marcus255 | 9:36fc715eb54c | 15 | wait_ms(20); |
marcus255 | 9:36fc715eb54c | 16 | read_data(&memory_cell_addr); |
marcus255 | 9:36fc715eb54c | 17 | memory_cell_addr+=0x10; |
marcus255 | 9:36fc715eb54c | 18 | } else if (routine == SENSOR_ROUTINE) { |
marcus255 | 9:36fc715eb54c | 19 | rtc_sensor(); |
marcus255 | 9:36fc715eb54c | 20 | } |
marcus255 | 9:36fc715eb54c | 21 | } |
marcus255 | 8:3e6945d5d9a1 | 22 | |
marcus255 | 8:3e6945d5d9a1 | 23 | void rtc_sensor() |
marcus255 | 8:3e6945d5d9a1 | 24 | { |
marcus255 | 8:3e6945d5d9a1 | 25 | char temperature[2]; |
marcus255 | 9:36fc715eb54c | 26 | if(get_temperature_raw(temperature)) { |
marcus255 | 9:36fc715eb54c | 27 | float result = proceed_temperature(temperature); |
marcus255 | 9:36fc715eb54c | 28 | pc.printf("> Temperature: %1.2f\n\r",result); |
marcus255 | 9:36fc715eb54c | 29 | wait_ms(5); |
marcus255 | 9:36fc715eb54c | 30 | } else { |
marcus255 | 9:36fc715eb54c | 31 | pc.printf("> I2C Error\n\r"); |
marcus255 | 9:36fc715eb54c | 32 | } |
marcus255 | 8:3e6945d5d9a1 | 33 | } |
marcus255 | 8:3e6945d5d9a1 | 34 | |
marcus255 | 9:36fc715eb54c | 35 | int get_temperature_raw(char* temperature) |
marcus255 | 8:3e6945d5d9a1 | 36 | { |
marcus255 | 8:3e6945d5d9a1 | 37 | char reg_addr = TEMPERATURE_REGISTER; |
marcus255 | 9:36fc715eb54c | 38 | if(i2c.write(RTC_ADDR, ®_addr, 1)) { |
marcus255 | 9:36fc715eb54c | 39 | //pc.printf("Writing: Error\n\r"); |
marcus255 | 9:36fc715eb54c | 40 | return 0; |
marcus255 | 9:36fc715eb54c | 41 | } |
marcus255 | 8:3e6945d5d9a1 | 42 | wait_ms(20); |
marcus255 | 9:36fc715eb54c | 43 | if(i2c.read(RTC_ADDR, temperature, 2)) { |
marcus255 | 9:36fc715eb54c | 44 | //pc.printf("Reading: Error\n\r"); |
marcus255 | 9:36fc715eb54c | 45 | return 0; |
marcus255 | 9:36fc715eb54c | 46 | } |
marcus255 | 9:36fc715eb54c | 47 | return 1; |
marcus255 | 8:3e6945d5d9a1 | 48 | } |
marcus255 | 8:3e6945d5d9a1 | 49 | |
marcus255 | 8:3e6945d5d9a1 | 50 | float proceed_temperature(char * temperature) |
marcus255 | 8:3e6945d5d9a1 | 51 | { |
marcus255 | 9:36fc715eb54c | 52 | char MSB = temperature[0]; // MSB is a signed int8 and carries temperature in accuracy of 1 Celsius degree |
marcus255 | 8:3e6945d5d9a1 | 53 | char LSB = temperature[1]; // LSB carries fraction part of temperature. Possible values are 0, 0.25, 0.5, 0.75 |
marcus255 | 8:3e6945d5d9a1 | 54 | float high = (float)MSB; |
marcus255 | 8:3e6945d5d9a1 | 55 | float low = ((float)((uint8_t)LSB >> 6) * 0.25f); |
marcus255 | 8:3e6945d5d9a1 | 56 | return high + low; |
marcus255 | 8:3e6945d5d9a1 | 57 | } |
marcus255 | 8:3e6945d5d9a1 | 58 | |
marcus255 | 5:8dc6d7d81250 | 59 | |
marcus255 | 9:36fc715eb54c | 60 | void write_data() |
marcus255 | 9:36fc715eb54c | 61 | { |
marcus255 | 9:36fc715eb54c | 62 | for (uint8_t i = 0; i < DATA_SIZE; i++) { |
marcus255 | 9:36fc715eb54c | 63 | data[i+1] = (char) (rnd() % 90 + 33); |
marcus255 | 4:1ec553356dcc | 64 | } |
marcus255 | 9:36fc715eb54c | 65 | data[0] = memory_cell_addr; |
marcus255 | 9:36fc715eb54c | 66 | if (i2c.write(SLAVE_ADDR, data, DATA_SIZE + 1)) { |
marcus255 | 9:36fc715eb54c | 67 | pc.printf("> Writing: Error\n\r"); |
marcus255 | 9:36fc715eb54c | 68 | } else { |
marcus255 | 9:36fc715eb54c | 69 | pc.printf("> Writing: OK\n\r"); |
marcus255 | 9:36fc715eb54c | 70 | } |
marcus255 | 9:36fc715eb54c | 71 | |
marcus255 | 5:8dc6d7d81250 | 72 | } |
marcus255 | 5:8dc6d7d81250 | 73 | |
marcus255 | 9:36fc715eb54c | 74 | void read_data(char* cell_addr) |
marcus255 | 9:36fc715eb54c | 75 | { |
marcus255 | 9:36fc715eb54c | 76 | int success = 1; |
marcus255 | 9:36fc715eb54c | 77 | char fromSlave[DATA_SIZE + 1]; |
marcus255 | 9:36fc715eb54c | 78 | fromSlave[DATA_SIZE] = '\0'; |
marcus255 | 9:36fc715eb54c | 79 | if(i2c.write(SLAVE_ADDR, cell_addr, 1)) { |
marcus255 | 9:36fc715eb54c | 80 | success = 0; |
marcus255 | 7:b07706b16279 | 81 | } |
marcus255 | 9:36fc715eb54c | 82 | wait_ms(20); |
marcus255 | 9:36fc715eb54c | 83 | if (success) { |
marcus255 | 9:36fc715eb54c | 84 | if(i2c.read(SLAVE_ADDR, fromSlave, DATA_SIZE)) { |
marcus255 | 9:36fc715eb54c | 85 | //pc.printf("Reading: Error\n\r"); |
marcus255 | 9:36fc715eb54c | 86 | success = 0; |
marcus255 | 5:8dc6d7d81250 | 87 | } |
marcus255 | 9:36fc715eb54c | 88 | } |
marcus255 | 9:36fc715eb54c | 89 | if (success) { |
marcus255 | 9:36fc715eb54c | 90 | pc.printf("> Reading: OK, data at %#x is: %s\n\n\r", *cell_addr, fromSlave); |
marcus255 | 9:36fc715eb54c | 91 | } else { |
marcus255 | 9:36fc715eb54c | 92 | pc.printf("> Reading: Error\n\n\r"); |
marcus255 | 9:36fc715eb54c | 93 | } |
marcus255 | 5:8dc6d7d81250 | 94 | } |
marcus255 | 9:36fc715eb54c | 95 | |
marcus255 | 9:36fc715eb54c | 96 | void init(void) |
marcus255 | 9:36fc715eb54c | 97 | { |
marcus255 | 9:36fc715eb54c | 98 | i2c.frequency(50000); |
marcus255 | 9:36fc715eb54c | 99 | i2c_ticker.attach(&write_and_read, 2.0f); |
marcus255 | 9:36fc715eb54c | 100 | left_but.rise(&memory_routine); |
marcus255 | 9:36fc715eb54c | 101 | right_but.rise(&sensor_routine); |
marcus255 | 9:36fc715eb54c | 102 | pc.baud(921600); |
marcus255 | 9:36fc715eb54c | 103 | routine = MEMORY_ROUTINE; |
marcus255 | 9:36fc715eb54c | 104 | memory_cell_addr = 0x00; |
marcus255 | 9:36fc715eb54c | 105 | m_z=12434,m_w=33254; |
marcus255 | 9:36fc715eb54c | 106 | } |
mbed_official | 0:f76c26307f9a | 107 | |
marcus255 | 9:36fc715eb54c | 108 | void memory_routine(void) |
marcus255 | 9:36fc715eb54c | 109 | { |
marcus255 | 9:36fc715eb54c | 110 | routine = MEMORY_ROUTINE; |
marcus255 | 9:36fc715eb54c | 111 | } |
marcus255 | 9:36fc715eb54c | 112 | void sensor_routine(void) |
marcus255 | 9:36fc715eb54c | 113 | { |
marcus255 | 9:36fc715eb54c | 114 | routine = SENSOR_ROUTINE; |
marcus255 | 9:36fc715eb54c | 115 | } |
marcus255 | 4:1ec553356dcc | 116 | |
marcus255 | 9:36fc715eb54c | 117 | |
marcus255 | 9:36fc715eb54c | 118 | |
marcus255 | 9:36fc715eb54c | 119 | unsigned int rnd() |
marcus255 | 9:36fc715eb54c | 120 | { |
marcus255 | 9:36fc715eb54c | 121 | m_z = 36969 * (m_z & 65535) + (m_z >>16); |
marcus255 | 9:36fc715eb54c | 122 | m_w = 18000 * (m_w & 65535) + (m_w >>16); |
marcus255 | 9:36fc715eb54c | 123 | return ((m_z <<16) + m_w); |
marcus255 | 9:36fc715eb54c | 124 | } |