Is it possible to connect directly the SCL and SDA lines of two stm32f411re? One will be the master and one will be the slave? Here is the code for the master. Is it correct? How to set up the slave? the master will send a data and the slave will receive it. Is acknowledeg bit necessary? feel free to modify the code below for any changes needed. for the master: #include <stdint.h> #include <stm32f4xx.h> #include "i2c.h" void I2C_Config (void){ RCC->APB1ENR |= (1<<21); RCC->AHB1ENR |= (1<<1); GPIOB -> MODER |= (2<<16) | (2<<18); GPIOB -> OTYPER |= (1<<8) | (1<<9); GPIOB -> OSPEEDR |= (3<<16) | (3<<18); GPIOB -> PUPDR |= (1<<16) | (1<<18); GPIOB -> AFR[1] |= (4<<0) | (4<<4); I2C1 ->CR1 |= (1<<15); I2C1 ->CR1 &=~ (1<<15); I2C1 ->CR2 |= (45<<0); I2C1 ->CCR = 225<<0; I2C1->TRISE = 46; I2C1->CR1 |= (1<<1); } void I2C_Start (void) { I2C1 -> CR1 |= (1<<8); // GENERATE START while (!(I2C1->SR1 & (1<<0))); //WAIT FOR SB BIT TO SET } void I2C_Write (uint8_t data){ while (!(I2C1->SR1 & (1<<7))); // wait for TXE bit to set I2C1->DR = data; while (!(I2C1->SR1 & (1<<2))); //wait for BTF bit to set } void I2C_Address (uint8_t Address) { I2C1->DR = Address; //send the address while (!(I2c1->SR1 & (1<<1))); uint8_t temp = I2C1 ->SR1 | I2C1->SR2; //read SR1 and SR2 to clear the ADDR bit } void I2C_stop(void) { I2C1->CR1 |= (1<<9); } int main(void) { // Initialize I2C I2C_Config(); // Start I2C communication I2C_Start(); // Address the slave (replace SLAVE_ADDRESS with the actual address of the slave) I2C_Address(SLAVE_ADDRESS); //edit later address of another stm32 // Write data to the slave uint8_t data_to_send = 0x01; // Example data to send I2C_Write(data_to_send); // Stop I2C communication I2C_Stop(); while(1); // Loop indefinitely return 0; }
Is it possible to connect directly the SCL and SDA lines of two stm32f411re? One will be the master and one will be the slave? Here is the code for the master. Is it correct? How to set up the slave? the master will send a data and the slave will receive it. Is acknowledeg bit necessary? feel free to modify the code below for any changes needed.
for the master:
#include <stdint.h>
#include <stm32f4xx.h>
#include "i2c.h"
void I2C_Config (void){
RCC->APB1ENR |= (1<<21);
RCC->AHB1ENR |= (1<<1);
GPIOB -> MODER |= (2<<16) | (2<<18);
GPIOB -> OTYPER |= (1<<8) | (1<<9);
GPIOB -> OSPEEDR |= (3<<16) | (3<<18);
GPIOB -> PUPDR |= (1<<16) | (1<<18);
GPIOB -> AFR[1] |= (4<<0) | (4<<4);
I2C1 ->CR1 |= (1<<15);
I2C1 ->CR1 &=~ (1<<15);
I2C1 ->CR2 |= (45<<0);
I2C1 ->CCR = 225<<0;
I2C1->TRISE = 46;
I2C1->CR1 |= (1<<1);
}
void I2C_Start (void)
{
I2C1 -> CR1 |= (1<<8); // GENERATE START
while (!(I2C1->SR1 & (1<<0))); //WAIT FOR SB BIT TO SET
}
void I2C_Write (uint8_t data){
while (!(I2C1->SR1 & (1<<7))); // wait for TXE bit to set
I2C1->DR = data;
while (!(I2C1->SR1 & (1<<2))); //wait for BTF bit to set
}
void I2C_Address (uint8_t Address)
{
I2C1->DR = Address; //send the address
while (!(I2c1->SR1 & (1<<1)));
uint8_t temp = I2C1 ->SR1 | I2C1->SR2; //read SR1 and SR2 to clear the ADDR bit
}
void I2C_stop(void)
{
I2C1->CR1 |= (1<<9);
}
int main(void)
{
// Initialize I2C
I2C_Config();
// Start I2C communication
I2C_Start();
// Address the slave (replace SLAVE_ADDRESS with the actual address of the slave)
I2C_Address(SLAVE_ADDRESS); //edit later address of another stm32
// Write data to the slave
uint8_t data_to_send = 0x01; // Example data to send
I2C_Write(data_to_send);
// Stop I2C communication
I2C_Stop();
while(1); // Loop indefinitely
return 0;
}
Step by step
Solved in 2 steps