问题描述
我试图在STM32F466RE板上的两个Adafruit TOF传感器VL6180X之间建立I²C通信。我正在使用cubeMX和VS代码,并且还可以使用ST website的x-nucleo-6180xa1上的VL6180X API进行帮助,因此我想对其进行设置,以便可以分别测试两个传感器。到目前为止,我仅成功测量了一个传感器,但是当我尝试将两个传感器都连接到SHDN到STM32F466RE核上的GPIO时,却遇到了问题。我正在尝试管理一些主从操作,但是我是新手,没人知道怎么做吗?
我正在寻找的是示例代码,因此我可以看到它是如何实现的。这不是生产运行,而是供家庭使用。要在这两个TOF传感器上进行管理,我一直找不到要研究的代码。
那是我的主要代码:
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void WaitMilliSec(int ms);
VL6180xDev_t theVL6180xDev;
struct MyVL6180Dev_t BoardDevs[2] = {
[0]= { .DevID = 0 },[1]= { .DevID = 1 }
};
VL6180xDev_t theVL6180xDev = &BoardDevs[0];
/**
* VL6180x CubeMX F401 multiple device i2c implementation
*/
#define i2c_bus (&hi2c1)
#define def_i2c_time_out 100
int VL6180x_I2CWrite(VL6180xDev_t dev,uint8_t *buff,uint8_t len) {
int status;
status = HAL_I2C_Master_Transmit(i2c_bus,dev->I2cAddr,buff,len,def_i2c_time_out);
if (status) {
HAL_I2C_MspInit(&hi2c1);
}
return status? -1 : 0;
}
int VL6180x_I2CRead(VL6180xDev_t dev,uint8_t len) {
int status;
status = HAL_I2C_Master_Receive(i2c_bus,def_i2c_time_out);
if (status) {
HAL_I2C_MspInit(&hi2c1);
}
return status? -1 : 0;
}
void WaitMilliSec(int ms) {
HAL_Delay(ms); /* it's milli sec cos we do set systick to 1KHz */
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
#define MAX_DEV 1
VL6180x_RangeData_t Range[MAX_DEV];
int status;
int i;
int n_dev=1;
int PresentDevMask;
int nPresentDevs;
int PresentDevIds[MAX_DEV];
int nReady;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals,Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
/* detect presence and initialize devices i2c address */
/*set device i2c address for dev[i] = 0x52+(i+1)*2 */
PresentDevMask = 0;
nPresentDevs = 0;
//strcpy(DisplayStr,"TLBR");
for (i = 0; i <= n_dev; i++){
int FinalI2cAddr;
uint8_t id;
/* unreset device that wake up at default i2c addres 0x52 */
WaitMilliSec(2); /* at least 400usec before to acces device */
BoardDevs[i].I2cAddr = 0x52;
/* to detect device presence try to read it's dev id */
status = VL6180x_RdByte(&BoardDevs[i],IDENTIFICATION_MODEL_ID,&id);
if (status) {
/* these device is not present skip init and clear it's letter on string */
BoardDevs[i].Present = 0;
//DisplayStr[i]=' ';
continue;
}
/* device present only */
BoardDevs[i].Present = 1;
PresentDevMask |= 1 << i;
PresentDevIds[nPresentDevs]=i;
nPresentDevs++;
status = VL6180x_InitData(&BoardDevs[i]);
FinalI2cAddr = 0x52 + ((i+1) * 2);
if (FinalI2cAddr != 0x52) {
status = VL6180x_SetI2CAddress(&BoardDevs[i],FinalI2cAddr);
if( status ){
//HandleError("VL6180x_SetI2CAddress fail");
}
BoardDevs[i].I2cAddr = FinalI2cAddr;
}
WaitMilliSec(1);
status = VL6180x_RdByte(&BoardDevs[i],&id);
WaitMilliSec(1);
status= VL6180x_Prepare(&BoardDevs[i]);
if( status<0 ){
//HandleError("VL6180x_Prepare fail");
}
/* Disable Dmax computation */
VL6180x_DMaxSetState(&BoardDevs[i],0);
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
VL6180xDev_t dev;
/*
VL6180x_RangePollMeasurement(dev,&Range[0]);
HAL_Delay(20);
*/
// kick off measure on all device
for( i=0; i<nPresentDevs; i++){
dev = BoardDevs + PresentDevIds[i];
//TODO: GPIO enamble of pa11
status = VL6180x_RangeStartSingleShot(dev);
if( status<0 ){
//HandleError("VL6180x_RangeStartSingleShot fail");
}
dev->Ready=0;
}
// wait for all present device to have a measure
nReady=0;
do{
//DISP_ExecLoopBody();
for( i=0; i<nPresentDevs; i++){
dev = BoardDevs + PresentDevIds[i];
if( !dev->Ready ){
status = VL6180x_RangeGetMeasurementIfReady(dev,&Range[i]);
if( status == 0 ){
if(Range[i].errorStatus == DataNotReady)
continue;
// New measurement ready
dev->Ready=1;
nReady++;
} else {
//HandleError("VL6180x_RangeStartSingleShot fail");
}
}
}
}
while( nReady<nPresentDevs);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
...
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)