In this week, I have learned how to communicate between two MCUs. For me, I will mainly use two tyoes of communications in my project:

  1. I2C, used for reading accelerometer and gyrometer data from sensors
  2. Serial communication. Because I need to control three BLDCs, I will need nine PWM outputs. However, there are only six such channels in atmega328p. Therefore, I will need some kinds of communication between different MCUs to generate enough PWM signals.

I2C readout from MPU6050

Since I plan to read two MPU6050 sensors at the same time, I decide to modify Neil's code to make use of any pins as I2C pins. However, there are several problems with this idea. The first one is that it does not work, for some unknown reason. I know it's not problem with the sensor as I can easily get the data out with an Arduino. The second problem is that the example code for I2C will ocupy the processor all the time, and I will need to write a complex interrupt structure to deal with that.

After talking to one of the TA at Harvard, Xiaomeng, I decide to use the on-chip I2C.

Another challenge is that the two sensors I will use are identical, which means that they will have the same address on the I2C. Fortunately, the MPU6050 sensor offers a function that can change the device address by pulling AD0 pin high.

Here is the code for implementing the I2C:


Communication between MCUs

The reason I need communication between MCus is that there is not enough PWM outputs on a single MCU.

Same as the I2C, I first tried the example code with "put_string()", and I encountered the same problem that it will ocupy the procesor all the time as the interrupt will be hard to implement. Therefore, after talking to Xiaomeng, I decide to use on-chip USART to realize serial communication between different MCUS, as it's much easier to make use of the default interrupt coming with USART.

Here are the codes for serial communication between two different MCUs with interrupts: