program for I2C master device

Dependencies:   mbed

Fork of I2C_HelloWorld_Mbed by mbed official

Files at this revision

API Documentation at this revision

Comitter:
marcus255
Date:
Fri Jan 08 13:47:36 2016 +0000
Parent:
10:39dd1d3508c3
Commit message:
Latest version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Dec 25 21:35:57 2015 +0000
+++ b/main.cpp	Fri Jan 08 13:47:36 2016 +0000
@@ -1,8 +1,8 @@
 /*
 * Project: I2C to UART converter
-* File: main.cpp
+* File:    main.cpp
 * Utility: simulating I2C Master devices
-* Author: Marek Trojan
+* Author:  Marek Trojan
 */
 
 #include "main.h"
@@ -16,10 +16,11 @@
 
 void write_and_read()
 {
+    //led = !led;
     if (routine == MEMORY_ROUTINE) {
         read_data(&memory_cell_addr);
-        wait_ms(20); 
-        write_data();  
+        wait_ms(20);
+        write_data();
         memory_cell_addr+=0x10;
     } else if (routine == TIME_ROUTINE) {
         rtc_time();
@@ -107,7 +108,7 @@
 
 void write_data()
 {
-    srand(time(NULL)); 
+    srand(time(NULL));
     for (uint8_t i = 0; i < DATA_SIZE; i++) {
         data[i+1] = (char) (rnd() % 90 + 33);
     }
@@ -144,26 +145,28 @@
 
 void init(void)
 {
-    i2c.frequency(I2C_FREQUENCY_STANDARD / 2);
-    i2c_ticker.attach(&write_and_read, 2.0f);
-    left_but.rise(&memory_routine);
-    right_but.rise(&rtc_routine);
-    pc.baud(921600);
+    i2c.frequency(I2C_FREQ_STANDARD / 2);
+    i2c_ticker.attach(&write_and_read, 1.0f);
+    pc.baud(PC_BAUD);
+    pc.attach(&select_routine);
     routine = MEMORY_ROUTINE;
     memory_cell_addr = 0x00;
     m_z=12434,m_w=33254;
 }
 
-void memory_routine(void)
+void select_routine()
 {
-    routine = MEMORY_ROUTINE;
-}
-void rtc_routine(void)
-{
-    if (routine == SENSOR_ROUTINE)
-        routine = TIME_ROUTINE;
-    else
+    char cmd = pc.getc();
+    switch (cmd) {
+        case 'm':
+        routine = MEMORY_ROUTINE;
+        break;
+        case 't':
         routine = SENSOR_ROUTINE;
+        break;
+        default:
+        routine = TIME_ROUTINE; 
+    }    
 }
 
 unsigned int rnd()
--- a/main.h	Fri Dec 25 21:35:57 2015 +0000
+++ b/main.h	Fri Jan 08 13:47:36 2016 +0000
@@ -1,37 +1,36 @@
 /*
 * Project: I2C to UART converter
-* File: main.h
+* File:    main.h
 * Utility: simulating I2C Master devices
-* Author: Marek Trojan
+* Author:  Marek Trojan
 */
 
 #ifndef MAIN_H
 #define MAIN_H
 
 #include "mbed.h"
-#define EEPROM_ADDRESS       0xA0
-#define RTC_ADDR             0xD0
-#define TEMPERATURE_REGISTER 0x11
-#define TIME_REGISTER        0x00
-#define RIGHT_PUSH_BUTTON PTC3
-#define LEFT_PUSH_BUTTON PTC12
-#define DATA_SIZE 16
-#define SENSOR_ROUTINE 1
-#define MEMORY_ROUTINE 2
-#define TIME_ROUTINE   3
-#define I2C_FREQUENCY_STANDARD   100000 /* Hz */
 
-InterruptIn left_but(LEFT_PUSH_BUTTON);
-InterruptIn right_but(RIGHT_PUSH_BUTTON);
+#define EEPROM_ADDRESS        0xA0
+#define RTC_ADDR              0xD0
+#define TEMPERATURE_REGISTER  0x11
+#define TIME_REGISTER         0x00
+#define DATA_SIZE             16
+#define SENSOR_ROUTINE        1
+#define MEMORY_ROUTINE        2
+#define TIME_ROUTINE          3
+#define I2C_FREQ_STANDARD     100000 /* Hz */
+#define PC_BAUD               921600
+
 I2C i2c(PTE0, PTE1);
 Serial pc(USBTX, USBRX);
-DigitalOut led(LED_GREEN);
+//DigitalOut led(LED_GREEN);
 Ticker i2c_ticker;
 void write_data(void);
 void read_data(char*);
 void write_and_read(void);
 void memory_routine(void);
 void rtc_routine(void);
+void select_routine(void);
 
 void rtc_sensor();
 int get_temperature_raw(char*);