0% found this document useful (0 votes)
0 views6 pages

( - MCTR601 - ) 10 - I2C Code (Supplementary Notes)

This document provides a tutorial on Bare Metal I2C communication using the STM32F103C8T6 microcontroller, specifically focusing on reading accelerometer data from the MPU6050 sensor. It includes example code that demonstrates how to initialize I2C, read acceleration values in burst mode, and control an LED based on the x-acceleration reading. The tutorial is authored by Prof. Ayman A. El-Badawy from the Department of Mechatronics Engineering.

Uploaded by

laleri2060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

( - MCTR601 - ) 10 - I2C Code (Supplementary Notes)

This document provides a tutorial on Bare Metal I2C communication using the STM32F103C8T6 microcontroller, specifically focusing on reading accelerometer data from the MPU6050 sensor. It includes example code that demonstrates how to initialize I2C, read acceleration values in burst mode, and control an LED based on the x-acceleration reading. The tutorial is authored by Prof. Ayman A. El-Badawy from the Department of Mechatronics Engineering.

Uploaded by

laleri2060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Mechatronics Engineering

Bare Metal I2C Additional Tutorial

Prof. Ayman A. El-Badawy


Department of Mechatronics Engineering
Faculty of Engineering and Material Science
Tutorial Contents
• Bare Metal I2C Communication in STM32F103C8T6

Prof. Ayman A. El-Badawy


Department of Mechatronics Engineering
Faculty of Engineering and Material Science
The following is an example of reading mpu6050 x, y and z accelerations
in Burst Mode (Reading more than 1 Byte at a time) and depending on the
reading from the x-acceleration, an LED on PA0 is turned ON if reading is
positive and OFF if negative.

Prof. Ayman A. El-Badawy


Department of Mechatronics Engineering
Faculty of Engineering and Material Science
#include "stm32f103xb.h"
void I2C_PWR();
void I2C_read();
void Delay();

volatile uint8_t Address_Read;


int16_t data_x,data_y,data_z;
#define delay_time 10

void init() {
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN|RCC_APB2ENR_IOPAEN;
RCC->APB1ENR |= RCC_APB1ENR_I2C2EN;
GPIOB->CRH = 0x4444FF44; // AF open drain
GPIOA->CRL = 0x44444441; // LED PA0 output G.P. push-pull
I2C2->CR1 &= ~ I2C_CR1_PE;
I2C2->CR1 |= (1 << 15); // reset the I2C
I2C2->CR1 &= ~(1 << 15); // Normal operation
I2C2->CR2 |= 8; // I2C APB clock frequency
I2C2->CCR = 40; // I2C Standard Mode
I2C2->TRISE = 9;
I2C2->CR1 |= I2C_CR1_PE;
}

int main(){
init();
I2C_PWR();
while(1){
I2C_read();
if(data_x>0)
GPIOA->ODR|=1;
else
GPIOA->ODR&=~1;
}
}
Prof. Ayman A. El-Badawy
Department of Mechatronics Engineering
Faculty of Engineering and Material Science
void I2C_PWR(){

while(I2C2->SR2 & I2C_SR2_BUSY); // check if the I2C is busy

I2C2->CR1|=I2C_CR1_START; // send start bit


while(!(I2C2->CR1 & I2C_SR1_SB)); // check start bit
Delay(); // small delay inserted after the start bit for timing

I2C2->DR = 0x68 << 1 ; // send address


while (!(I2C2->SR1 & I2C_SR1_ADDR));// check for address, skip in debugging
Address_Read = I2C2->SR1 | I2C2->SR2;// read SR1 and SR2 to clear the ADDR bit

while(!(I2C2->SR1 & (I2C_SR1_TXE))); // I2C ready for next byte


I2C2->DR=0x6B; // send address of pwr register

while(!(I2C2->SR1 & (I2C_SR1_TXE))); // I2C ready for next byte


I2C2->DR=0; // send 0
while(!(I2C2->SR1 & (I2C_SR1_BTF))); // check last byte transmission ended

I2C2->CR1|=I2C_CR1_STOP; // Stop I2C


}
void I2C_read(){
while(I2C2->SR2 & I2C_SR2_BUSY);
I2C2->CR1|=I2C_CR1_START; // send start bit
while(!(I2C2->CR1 & I2C_SR1_SB)); // check start bit
Delay(); // small delay inserted after the start bit for timing

I2C2->DR = (0x68 << 1) ; // send address


while (!(I2C2->SR1 & I2C_SR1_ADDR));// check for address, skip in debugging
Address_Read = I2C2->SR1 | I2C2->SR2;// read SR1 and SR2 to clear the ADDR bit

Prof. Ayman A. El-Badawy


Department of Mechatronics Engineering
Faculty of Engineering and Material Science
while(!(I2C2->SR1 & (I2C_SR1_TXE))); // I2C ready for next byte
I2C2->DR=59; // send address of a_x register
while(!(I2C2->SR1 & (I2C_SR1_BTF))); // check last byte transmission ended

I2C2->CR1|=I2C_CR1_START|I2C_CR1_ACK; // double start, enable acknowledgment


while(!(I2C2->CR1 & I2C_SR1_SB)); // check start bit
Delay(); // small delay inserted after the start bit for timing

I2C2->DR = (0x68 << 1)|1 ; // send address


while (!(I2C2->SR1 & I2C_SR1_ADDR));// check for address, skip in debugging}
Address_Read = I2C2->SR1 | I2C2->SR2;// read SR1 and SR2 to clear the ADDR bit

while(!(I2C2->SR1 & I2C_SR1_RXNE));// check for data received, skip in debugging


data_x= I2C2->DR;

while(!(I2C2->SR1 & I2C_SR1_RXNE));// check for data received, skip in debugging


data_x= (data_x<<8)|I2C2->DR;

while(!(I2C2->SR1 & I2C_SR1_RXNE));// check for data received, skip in debugging


data_y= I2C2->DR;

while(!(I2C2->SR1 & I2C_SR1_RXNE));// check for data received, skip in debugging


data_y= (data_y<<8)|I2C2->DR;

while(!(I2C2->SR1 & I2C_SR1_RXNE));// check for data received, skip in debugging


data_z= I2C2->DR;

I2C2->CR1 |= (1 << 9); // Stop I2C before the end of the last byte
I2C2->CR1 &= ~I2C_CR1_ACK; // Stop ACK

while(!(I2C2->SR1 & I2C_SR1_RXNE));// check for data received, skip in debugging


data_z= (data_z<<8)|I2C2->DR;
}

void Delay(){
for(int i=0;i<=delay_time;i++);
}

Prof. Ayman A. El-Badawy


Department of Mechatronics Engineering
Faculty of Engineering and Material Science

You might also like