Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:9b7603b90063
- Parent:
- 0:6cf9362fc1a1
- Child:
- 2:4dd2f26c5648
--- a/main.cpp Wed Aug 26 06:22:26 2020 +0000 +++ b/main.cpp Mon Aug 31 15:09:36 2020 +0000 @@ -1,4 +1,5 @@ #include "mbed.h" +#include <I2CSlave.h> #include "Robot.h" #define L_dir D2 @@ -8,18 +9,51 @@ #define R_step D6 #define R_enb D7 +#define CMD_NULL 0 +#define CMD_LM_MOVE 1 +#define CMD_RM_MOVE 2 +#define CMD_MOTOR_STOP 3 +#define CMD_ROBOT_MOVE 4 +#define CMD_ROBOT_STOP 5 + +uint8_t device_addr = 0x0A; +char data[52] = {device_addr, 1, CMD_NULL,}; + +I2CSlave slave(I2C_SDA, I2C_SCL); +AnalogIn adc_temp(ADC_TEMP); + Robot a(L_dir, L_step, L_enb, R_dir, R_step, R_enb); -int main(){ - a.enable(both); - a.move_cm(go, 10); +//void writeByte(uint8_t address, uint8_t regAddress, uint8_t data); +//char readByte(uint8_t address, uint8_t regAddress); +int main() +{ + slave.frequency(400000); + printf("slave started\n"); + slave.address(device_addr); + uint8_t status = 0; while(1){ - //a.move_speed(go, 30); - while(a.isRun()){ - //a.run_speed(both); - a.run(both); + status = slave.receive(); + if(status) printf("status: %d\n", status); + if(status == I2CSlave::WriteAddressed){ + slave.read(data, 52); + printf("Read from master: %s\n", data); } - a.stop(both); } } +/*void writeByte(uint8_t address, uint8_t regAddress, uint8_t data){ + char data_write[2]; + data_write[0] = regAddress; + data_write[1] = data; + i2c.write(address, data_write, 2, 0); +} + +char readByte(uint8_t address, uint8_t regAddress){ + char data[1];// 'data' will store the register data + char data_write[1]; // for assigning register address + data_write[0] = regAddress; + i2c.write(address, data_write, 1, 1);// no stop + i2c.read(address, data, 1, 0); + return data[0]; +}*/ \ No newline at end of file