![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
program for I2C master device
Fork of I2C_HelloWorld_Mbed by
main.cpp@11:eff597e2366e, 2016-01-08 (annotated)
- Committer:
- marcus255
- Date:
- Fri Jan 08 13:47:36 2016 +0000
- Revision:
- 11:eff597e2366e
- Parent:
- 10:39dd1d3508c3
Latest version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcus255 | 10:39dd1d3508c3 | 1 | /* |
marcus255 | 10:39dd1d3508c3 | 2 | * Project: I2C to UART converter |
marcus255 | 11:eff597e2366e | 3 | * File: main.cpp |
marcus255 | 10:39dd1d3508c3 | 4 | * Utility: simulating I2C Master devices |
marcus255 | 11:eff597e2366e | 5 | * Author: Marek Trojan |
marcus255 | 10:39dd1d3508c3 | 6 | */ |
marcus255 | 10:39dd1d3508c3 | 7 | |
marcus255 | 4:1ec553356dcc | 8 | #include "main.h" |
marcus255 | 9:36fc715eb54c | 9 | |
marcus255 | 9:36fc715eb54c | 10 | int main() |
marcus255 | 9:36fc715eb54c | 11 | { |
marcus255 | 4:1ec553356dcc | 12 | init(); |
marcus255 | 8:3e6945d5d9a1 | 13 | wait_ms(1000); |
marcus255 | 4:1ec553356dcc | 14 | while (1) {} |
marcus255 | 4:1ec553356dcc | 15 | } |
mbed_official | 0:f76c26307f9a | 16 | |
marcus255 | 9:36fc715eb54c | 17 | void write_and_read() |
marcus255 | 9:36fc715eb54c | 18 | { |
marcus255 | 11:eff597e2366e | 19 | //led = !led; |
marcus255 | 9:36fc715eb54c | 20 | if (routine == MEMORY_ROUTINE) { |
marcus255 | 9:36fc715eb54c | 21 | read_data(&memory_cell_addr); |
marcus255 | 11:eff597e2366e | 22 | wait_ms(20); |
marcus255 | 11:eff597e2366e | 23 | write_data(); |
marcus255 | 9:36fc715eb54c | 24 | memory_cell_addr+=0x10; |
marcus255 | 10:39dd1d3508c3 | 25 | } else if (routine == TIME_ROUTINE) { |
marcus255 | 10:39dd1d3508c3 | 26 | rtc_time(); |
marcus255 | 9:36fc715eb54c | 27 | } else if (routine == SENSOR_ROUTINE) { |
marcus255 | 9:36fc715eb54c | 28 | rtc_sensor(); |
marcus255 | 9:36fc715eb54c | 29 | } |
marcus255 | 9:36fc715eb54c | 30 | } |
marcus255 | 8:3e6945d5d9a1 | 31 | |
marcus255 | 8:3e6945d5d9a1 | 32 | void rtc_sensor() |
marcus255 | 8:3e6945d5d9a1 | 33 | { |
marcus255 | 8:3e6945d5d9a1 | 34 | char temperature[2]; |
marcus255 | 9:36fc715eb54c | 35 | if(get_temperature_raw(temperature)) { |
marcus255 | 9:36fc715eb54c | 36 | float result = proceed_temperature(temperature); |
marcus255 | 9:36fc715eb54c | 37 | pc.printf("> Temperature: %1.2f\n\r",result); |
marcus255 | 10:39dd1d3508c3 | 38 | wait_ms(1); |
marcus255 | 10:39dd1d3508c3 | 39 | } else { |
marcus255 | 10:39dd1d3508c3 | 40 | pc.printf("> I2C Error\n\r"); |
marcus255 | 10:39dd1d3508c3 | 41 | } |
marcus255 | 10:39dd1d3508c3 | 42 | } |
marcus255 | 10:39dd1d3508c3 | 43 | |
marcus255 | 10:39dd1d3508c3 | 44 | void rtc_time() |
marcus255 | 10:39dd1d3508c3 | 45 | { |
marcus255 | 10:39dd1d3508c3 | 46 | char time[3]; |
marcus255 | 10:39dd1d3508c3 | 47 | if(get_time_raw(time)) { |
marcus255 | 10:39dd1d3508c3 | 48 | uint8_t t[6]; |
marcus255 | 10:39dd1d3508c3 | 49 | proceed_time(time, t); |
marcus255 | 10:39dd1d3508c3 | 50 | pc.printf("> Time: %x%x:%x%x:%x%x\n\r", t[0], t[1], t[2], t[3], t[4], t[5]); |
marcus255 | 10:39dd1d3508c3 | 51 | wait_ms(1); |
marcus255 | 9:36fc715eb54c | 52 | } else { |
marcus255 | 9:36fc715eb54c | 53 | pc.printf("> I2C Error\n\r"); |
marcus255 | 9:36fc715eb54c | 54 | } |
marcus255 | 8:3e6945d5d9a1 | 55 | } |
marcus255 | 8:3e6945d5d9a1 | 56 | |
marcus255 | 9:36fc715eb54c | 57 | int get_temperature_raw(char* temperature) |
marcus255 | 8:3e6945d5d9a1 | 58 | { |
marcus255 | 8:3e6945d5d9a1 | 59 | char reg_addr = TEMPERATURE_REGISTER; |
marcus255 | 9:36fc715eb54c | 60 | if(i2c.write(RTC_ADDR, ®_addr, 1)) { |
marcus255 | 9:36fc715eb54c | 61 | //pc.printf("Writing: Error\n\r"); |
marcus255 | 9:36fc715eb54c | 62 | return 0; |
marcus255 | 9:36fc715eb54c | 63 | } |
marcus255 | 8:3e6945d5d9a1 | 64 | wait_ms(20); |
marcus255 | 9:36fc715eb54c | 65 | if(i2c.read(RTC_ADDR, temperature, 2)) { |
marcus255 | 9:36fc715eb54c | 66 | //pc.printf("Reading: Error\n\r"); |
marcus255 | 9:36fc715eb54c | 67 | return 0; |
marcus255 | 9:36fc715eb54c | 68 | } |
marcus255 | 9:36fc715eb54c | 69 | return 1; |
marcus255 | 8:3e6945d5d9a1 | 70 | } |
marcus255 | 8:3e6945d5d9a1 | 71 | |
marcus255 | 10:39dd1d3508c3 | 72 | int get_time_raw(char* time) |
marcus255 | 10:39dd1d3508c3 | 73 | { |
marcus255 | 10:39dd1d3508c3 | 74 | char reg_addr = TIME_REGISTER; |
marcus255 | 10:39dd1d3508c3 | 75 | if(i2c.write(RTC_ADDR, ®_addr, 1)) { |
marcus255 | 10:39dd1d3508c3 | 76 | return 0; |
marcus255 | 10:39dd1d3508c3 | 77 | } |
marcus255 | 10:39dd1d3508c3 | 78 | wait_ms(20); |
marcus255 | 10:39dd1d3508c3 | 79 | if(i2c.read(RTC_ADDR, time, 3)) { |
marcus255 | 10:39dd1d3508c3 | 80 | return 0; |
marcus255 | 10:39dd1d3508c3 | 81 | } |
marcus255 | 10:39dd1d3508c3 | 82 | return 1; |
marcus255 | 10:39dd1d3508c3 | 83 | } |
marcus255 | 10:39dd1d3508c3 | 84 | |
marcus255 | 8:3e6945d5d9a1 | 85 | float proceed_temperature(char * temperature) |
marcus255 | 8:3e6945d5d9a1 | 86 | { |
marcus255 | 9:36fc715eb54c | 87 | char MSB = temperature[0]; // MSB is a signed int8 and carries temperature in accuracy of 1 Celsius degree |
marcus255 | 8:3e6945d5d9a1 | 88 | char LSB = temperature[1]; // LSB carries fraction part of temperature. Possible values are 0, 0.25, 0.5, 0.75 |
marcus255 | 8:3e6945d5d9a1 | 89 | float high = (float)MSB; |
marcus255 | 8:3e6945d5d9a1 | 90 | float low = ((float)((uint8_t)LSB >> 6) * 0.25f); |
marcus255 | 8:3e6945d5d9a1 | 91 | return high + low; |
marcus255 | 8:3e6945d5d9a1 | 92 | } |
marcus255 | 8:3e6945d5d9a1 | 93 | |
marcus255 | 10:39dd1d3508c3 | 94 | void proceed_time(char * time, uint8_t * tiny_time) |
marcus255 | 10:39dd1d3508c3 | 95 | { |
marcus255 | 10:39dd1d3508c3 | 96 | char secs = time[0]; |
marcus255 | 10:39dd1d3508c3 | 97 | char mins = time[1]; |
marcus255 | 10:39dd1d3508c3 | 98 | char hrs = time[2]; |
marcus255 | 10:39dd1d3508c3 | 99 | |
marcus255 | 10:39dd1d3508c3 | 100 | *(tiny_time + 0) = ((hrs >> 4) & 0x01); |
marcus255 | 10:39dd1d3508c3 | 101 | *(tiny_time + 1) = (hrs & 0x0F); |
marcus255 | 10:39dd1d3508c3 | 102 | *(tiny_time + 2) = ((mins >> 4) & 0x0F); |
marcus255 | 10:39dd1d3508c3 | 103 | *(tiny_time + 3) = (mins & 0x0F); |
marcus255 | 10:39dd1d3508c3 | 104 | *(tiny_time + 4) = ((secs >> 4) & 0x0F); |
marcus255 | 10:39dd1d3508c3 | 105 | *(tiny_time + 5) = (secs & 0x0F); |
marcus255 | 10:39dd1d3508c3 | 106 | } |
marcus255 | 10:39dd1d3508c3 | 107 | |
marcus255 | 5:8dc6d7d81250 | 108 | |
marcus255 | 9:36fc715eb54c | 109 | void write_data() |
marcus255 | 9:36fc715eb54c | 110 | { |
marcus255 | 11:eff597e2366e | 111 | srand(time(NULL)); |
marcus255 | 9:36fc715eb54c | 112 | for (uint8_t i = 0; i < DATA_SIZE; i++) { |
marcus255 | 9:36fc715eb54c | 113 | data[i+1] = (char) (rnd() % 90 + 33); |
marcus255 | 4:1ec553356dcc | 114 | } |
marcus255 | 9:36fc715eb54c | 115 | data[0] = memory_cell_addr; |
marcus255 | 10:39dd1d3508c3 | 116 | if (i2c.write(EEPROM_ADDRESS, data, DATA_SIZE + 1)) { |
marcus255 | 9:36fc715eb54c | 117 | pc.printf("> Writing: Error\n\r"); |
marcus255 | 9:36fc715eb54c | 118 | } else { |
marcus255 | 9:36fc715eb54c | 119 | pc.printf("> Writing: OK\n\r"); |
marcus255 | 9:36fc715eb54c | 120 | } |
marcus255 | 9:36fc715eb54c | 121 | |
marcus255 | 5:8dc6d7d81250 | 122 | } |
marcus255 | 5:8dc6d7d81250 | 123 | |
marcus255 | 9:36fc715eb54c | 124 | void read_data(char* cell_addr) |
marcus255 | 9:36fc715eb54c | 125 | { |
marcus255 | 9:36fc715eb54c | 126 | int success = 1; |
marcus255 | 9:36fc715eb54c | 127 | char fromSlave[DATA_SIZE + 1]; |
marcus255 | 9:36fc715eb54c | 128 | fromSlave[DATA_SIZE] = '\0'; |
marcus255 | 10:39dd1d3508c3 | 129 | if(i2c.write(EEPROM_ADDRESS, cell_addr, 1)) { |
marcus255 | 9:36fc715eb54c | 130 | success = 0; |
marcus255 | 7:b07706b16279 | 131 | } |
marcus255 | 9:36fc715eb54c | 132 | wait_ms(20); |
marcus255 | 9:36fc715eb54c | 133 | if (success) { |
marcus255 | 10:39dd1d3508c3 | 134 | if(i2c.read(EEPROM_ADDRESS, fromSlave, DATA_SIZE)) { |
marcus255 | 9:36fc715eb54c | 135 | //pc.printf("Reading: Error\n\r"); |
marcus255 | 9:36fc715eb54c | 136 | success = 0; |
marcus255 | 5:8dc6d7d81250 | 137 | } |
marcus255 | 9:36fc715eb54c | 138 | } |
marcus255 | 9:36fc715eb54c | 139 | if (success) { |
marcus255 | 10:39dd1d3508c3 | 140 | pc.printf("> Reading: OK, data at 0x%x is: %s\n\r", *cell_addr, fromSlave); |
marcus255 | 9:36fc715eb54c | 141 | } else { |
marcus255 | 10:39dd1d3508c3 | 142 | pc.printf("> Reading: Error\n\r"); |
marcus255 | 9:36fc715eb54c | 143 | } |
marcus255 | 5:8dc6d7d81250 | 144 | } |
marcus255 | 9:36fc715eb54c | 145 | |
marcus255 | 9:36fc715eb54c | 146 | void init(void) |
marcus255 | 9:36fc715eb54c | 147 | { |
marcus255 | 11:eff597e2366e | 148 | i2c.frequency(I2C_FREQ_STANDARD / 2); |
marcus255 | 11:eff597e2366e | 149 | i2c_ticker.attach(&write_and_read, 1.0f); |
marcus255 | 11:eff597e2366e | 150 | pc.baud(PC_BAUD); |
marcus255 | 11:eff597e2366e | 151 | pc.attach(&select_routine); |
marcus255 | 9:36fc715eb54c | 152 | routine = MEMORY_ROUTINE; |
marcus255 | 9:36fc715eb54c | 153 | memory_cell_addr = 0x00; |
marcus255 | 9:36fc715eb54c | 154 | m_z=12434,m_w=33254; |
marcus255 | 9:36fc715eb54c | 155 | } |
mbed_official | 0:f76c26307f9a | 156 | |
marcus255 | 11:eff597e2366e | 157 | void select_routine() |
marcus255 | 9:36fc715eb54c | 158 | { |
marcus255 | 11:eff597e2366e | 159 | char cmd = pc.getc(); |
marcus255 | 11:eff597e2366e | 160 | switch (cmd) { |
marcus255 | 11:eff597e2366e | 161 | case 'm': |
marcus255 | 11:eff597e2366e | 162 | routine = MEMORY_ROUTINE; |
marcus255 | 11:eff597e2366e | 163 | break; |
marcus255 | 11:eff597e2366e | 164 | case 't': |
marcus255 | 10:39dd1d3508c3 | 165 | routine = SENSOR_ROUTINE; |
marcus255 | 11:eff597e2366e | 166 | break; |
marcus255 | 11:eff597e2366e | 167 | default: |
marcus255 | 11:eff597e2366e | 168 | routine = TIME_ROUTINE; |
marcus255 | 11:eff597e2366e | 169 | } |
marcus255 | 9:36fc715eb54c | 170 | } |
marcus255 | 4:1ec553356dcc | 171 | |
marcus255 | 9:36fc715eb54c | 172 | unsigned int rnd() |
marcus255 | 9:36fc715eb54c | 173 | { |
marcus255 | 9:36fc715eb54c | 174 | m_z = 36969 * (m_z & 65535) + (m_z >>16); |
marcus255 | 9:36fc715eb54c | 175 | m_w = 18000 * (m_w & 65535) + (m_w >>16); |
marcus255 | 9:36fc715eb54c | 176 | return ((m_z <<16) + m_w); |
marcus255 | 10:39dd1d3508c3 | 177 | } |