STM32F303(Discovery) I2C problem in MikroC IDE - Project Need HELP - Page 1
Forum section: electronics / general

author: mzdenkov
author_rank: Regular Contributor
timestamp: December 14, 2019, 09:01:34 pm
content: Hello i have problem with my code:I use I2C to communicate with accelerometer.In Code below you can see that i try do set registers in LSM303DLHC.My problem is that program doesn't work when both init functions are called one after another.When i test them separately init functions work as they should.Any Ideas?Code is here(in attachment files are .c and .h files):Code:#include <stdint.h>#include "LSM303DLHC_REG_Protype_Code.h"/*** @brief Read LSM303DLHC output register, and calculate the acceleration ACC=(1/SENSITIVITY)* (out_h*256+out_l)/16 (12 bit rappresentation)* @param pnData: pointer to float buffer where to store data* @retval None*/#define ABS(x) (x < 0) ? (-x) : x#define ENABLE 1#define DISABLE 0#define L3G_Sensitivity_250dps (float) 114.285f /*!< gyroscope sensitivity with 250 dps full scale [LSB/dps] */#define L3G_Sensitivity_500dps (float) 57.1429f /*!< gyroscope sensitivity with 500 dps full scale [LSB/dps] */#define L3G_Sensitivity_2000dps (float) 14.285f /*!< gyroscope sensitivity with 2000 dps full scale [LSB/dps] */#define PI (float) 3.14159265f#define LSM_Acc_Sensitivity_2g (float) 1.0f /*!< accelerometer sensitivity with 2 g full scale [LSB/mg] */#define LSM_Acc_Sensitivity_4g (float) 0.5f /*!< accelerometer sensitivity with 4 g full scale [LSB/mg] */#define LSM_Acc_Sensitivity_8g (float) 0.25f /*!< accelerometer sensitivity with 8 g full scale [LSB/mg] */#define LSM_Acc_Sensitivity_16g (float) 0.0834f /*!< accelerometer sensitivity with 12 g full scale [LSB/mg] */void I2C1_TimeoutCallback(char errorCode) {GPIOE_ODR.B15 = 1;GPIOE_ODR.B14 = 0;Delay_ms(500);GPIOE_ODR.B15 = 0;GPIOE_ODR.B14 = 1;Delay_ms(500);}void LSM303DLHC_LowLevel_Init(){GPIO_Alternate_Function_Enable(&_GPIO_MODULE_I2C1_PB67);I2C1_Init_Advanced(100000,&_GPIO_MODULE_I2C1_PB67);I2C1_SetTimeoutCallback(1000, I2C1_TimeoutCallback);Delay_10us;}// Init I2C1uint16_t LSM303DLHC_Read(unsigned char DeviceAddr,unsigned char RegAddr,unsigned char* pBuffer, unsigned long NumByteToRead){if(I2C1_Start() != 0){I2C1_Write(DeviceAddr, &RegAddr, 1, END_MODE_RESTART); //Start condition to write to LSM303DLHC_CTRL_REG4_A register of LSM303DLHCI2C1_Read(DeviceAddr, pBuffer, NumByteToRead, END_MODE_STOP); // Read from register @ RegAddr and save data to pBuffer}}uint16_t LSM303DLHC_Write(unsigned char DeviceAddr, unsigned char RegAddr, unsigned char* pBuffer){static unsigned char data_;data_ = RegAddr;data_ = *pBuffer;if(I2C1_Start() != 0){I2C1_Write(DeviceAddr,data_, 2, END_MODE_STOP); //Start condition to write to LSM303DLHC_CTRL_REG4_A register of LSM303DLHC}}void LSM303DLHC_MagInit(){uint8_t cra_regm = 0x00, crb_regm = 0x00, mr_regm = 0x00;/*Configure the low level interface ---------------------------------------*///LSM303DLHC_LowLevel_Init();/* Configure MEMS: temp and Data rate */cra_regm |= (uint8_t) (LSM303DLHC_TEMPSENSOR_ENABLE | LSM303DLHC_ODR_30_HZ);/* Configure MEMS: full Scale */crb_regm |= (uint8_t) (LSM303DLHC_FS_8_1_GA);/* Configure MEMS: working mode */mr_regm |= (uint8_t) (LSM303DLHC_CONTINUOS_CONVERSION);/* Write value to Mag MEMS CRA_REG regsister */LSM303DLHC_Write(MAG_I2C_ADDRESS, LSM303DLHC_CRA_REG_M, &cra_regm);/* Write value to Mag MEMS CRB_REG regsister */LSM303DLHC_Write(MAG_I2C_ADDRESS, LSM303DLHC_CRB_REG_M, &crb_regm);/* Write value to Mag MEMS MR_REG regsister */LSM303DLHC_Write(MAG_I2C_ADDRESS, LSM303DLHC_MR_REG_M, &mr_regm);}void LSM303DLHC_AccInit(){uint8_t ctrl1 = 0x00, ctrl4 = 0x00;/*Configure the low level interface ---------------------------------------*///LSM303DLHC_LowLevel_Init();/* Configure MEMS: data rate, power mode, full scale and axes */ctrl1 |= (uint8_t) (LSM303DLHC_NORMAL_MODE | LSM303DLHC_ODR_50_HZ | LSM303DLHC_AXES_ENABLE);ctrl4 |= (uint8_t) (LSM303DLHC_BlockUpdate_Continous |LSM303DLHC_FULLSCALE_2G | LSM303DLHC_BLE_LSB | LSM303DLHC_HR_ENABLE);/* Write value to ACC MEMS CTRL_REG1 regsister */LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG1_A, &ctrl1);///* Write value to ACC MEMS CTRL_REG4 regsister */LSM303DLHC_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A, &ctrl4);}void main(){LSM303DLHC_LowLevel_Init();LSM303DLHC_MagInit();Delay_100ms;LSM303DLHC_AccInit();GPIO_Config(&GPIOE_BASE,_GPIO_PINMASK_HIGH,_GPIO_CFG_DIGITAL_OUTPUT);while(1){GPIOE_ODR.B15 = 1;GPIOE_ODR.B14 = 1;Delay_ms(500);GPIOE_ODR.B15 = 0;GPIOE_ODR.B14 = 0;Delay_ms(500);}}In attachments are pictures from logic analyser.thank you, Best Regards