2013-07-25 07:06 PM
I have been trying to make a slave I2C device communicate with my STM32F0 board, with no success. Can anyone give me an example code to config, init and send and receive data through the I2C bus.
2014-05-28 05:40 AM
Hi did you solve your problem if yes please send me the sample code for i2c stm32f030
2014-05-28 06:07 AM
Hi
The standard peripheral library has I2C examples : http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/PF2578842014-05-28 06:16 AM
now im trying in this way.. but something its wrong.. on a terminal im getting only ''error2''.. any ideas? ;)
#include ''stm32f0xx.h''
#include ''stm32f0xx_rcc.h''
#include ''stm32f0xx_gpio.h''
#include ''stm32f0xx_usart.h''
#include ''stm32f0xx_i2c.h''
#include <
stdio.h
>
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
#define MPU6050_RA_WHO_AM_I 0x75
#define Slave_address 0x68
#define mpu6050_FLAG_TIMEOUT ((uint32_t)0x1000)
#define mpu6050_LONG_TIMEOUT ((uint32_t)(500 * mpu6050_FLAG_TIMEOUT))
void USART_send( unsigned char data){
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, data);
}
void USART_print(char* StringPtr){
while(*StringPtr != 0x00){
USART_send(*StringPtr);
StringPtr++;}
}
void frej(uint8_t value)
{
int maska=0x01;
int i;
for(i=7;i>=0;i--){
int l = (((int)value)>>i)&maska;
char t = (char)(l+48);
USART_send(t);
}
USART_send(' ');
}
uint8_t ReadReg(uint8_t RegName)
{
uint8_t mpu6050_BufferRX[2] ={0,0};
uint8_t tmp = 0;
uint32_t DataNum = 0;
int mpu6050_Timeout = 0;
/* Test on BUSY Flag */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error1 '');
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2C1, Slave_address, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error2 '');
}
/* Send Register address */
I2C_SendData(I2C1, (uint8_t)RegName);
/* Wait until TC flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_TC) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error3 '');
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2C1, Slave_address, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
/* Reset local variable */
DataNum = 0;
/* Wait until all data are received */
while (DataNum != 1)
{
/* Wait until RXNE flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_RXNE) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error4 '');
}
/* Read data from RXDR */
mpu6050_BufferRX[DataNum]= I2C_ReceiveData(I2C1);
/* Update number of received data */
DataNum++;
}
/* Wait until STOPF flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error5 '');
}
/* Clear STOPF flag */
I2C_ClearFlag(I2C1, I2C_ICR_STOPCF);
// !< Store LM75_I2C received data
//tmp = (uint16_t)(LM75_BufferRX[0] << 8);
//tmp |= LM75_BufferRX[0];
tmp = mpu6050_BufferRX[0];
// return a Reg value
return (uint8_t)tmp;
}
void LED_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void USART_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
//Configure pins: Rx and Tx --------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void I2C_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* Configure the I2C clock source. The clock is derived from the HSI */
RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);
//Configure pins: SCL and SDA ------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00; // MPU6050 7-bit adress = 0x68, 8-bit adress = 0xD0;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
//I2C_InitStructure.I2C_Timing = 0xA0120227;
I2C_InitStructure.I2C_Timing = 0x20310A0D;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
int main(void)
{
LED_init();
USART_init();
I2C_init();
int i=0;
while(1)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
ReadReg(MPU6050_RA_WHO_AM_I);
for(i=0;i<10000000;i++);
/*
int i=0;
if(USART_ReceiveData(USART1)=='x'){
GPIO_SetBits(GPIOC, GPIO_Pin_8);
}
for(i=0;i<1000000;i++);
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
for(i=0;i<1000000;i++);
*/
}
}
2014-05-29 03:11 AM
(SlaveAddr & 0x7f) << 1
2014-08-01 03:27 AM
Hi das,
Can you please share your working I2C code. It would be helpful for me to change that code to work on RTC. Currently, I am not able to read data from the RTC :( Thnaks in advancePrashanth2014-11-28 09:45 PM
2015-04-12 01:05 AM
hii,
if you were able to run your i2c of stm32f030 can you please do send me the code as i am trying to run the i2c and it is showing same and wrong data on each changed data also2016-03-09 12:49 AM
2016-03-09 04:39 AM
Those would be part of the STM32F0 Standard Peripheral Library, why wouldn't you just download that?