cancel
Showing results for 
Search instead for 
Did you mean: 

Getting data from VL53L8CX using vl53l8cx_api

kgb
Associate II

Hello  everyone, 

I started learning programming on the STM32 ecosystem a few weeks ago, and I'm currently trying to get data from the VL53L8CX sensor using the NUCLEO-F401RE expansion boards and the vl53l8cx_api. I wrote a small program based on what I have seen in other projects, but my code doesn't work at all. While debugging, I discovered that the program jumps to the HardFault_Handler() function at this line in vl53l8cx_init().

 
	status |= VL53L8CX_WrByte(&(p_dev->platform), 0x7fff, 0x00);

Here is the essential part of my code maybe someone have any solution:

#include "vl53l8cx_api.h"
#include "main.h"
#include "stm32f4xx_hal.h"
#include <stdio.h>

// Private variables ---------------------------------------------------------
static VL53L8CX_Configuration tof_dev;  // Sensor-Konfiguration
static VL53L8CX_ResultsData results;  // Ergebnis der Messung
static int32_t status ; // Status für Fehlerbehandlung

static void MX_VL53L8CX_SimpleRanging_Process(void);
static void print_result(VL53L8CX_ResultsData *results) ;
static void MX_VL53L8CX_SimpleRanging_Init(void);
void ToF_EventDetected(void);


#define TIMING_BUDGET (30U)  // Timing-Budget (ms)
#define RANGING_FREQUENCY (10U)  // Messfrequenz (Hz)
#define POLLING_PERIOD (1)  // Polling-Periode (ms)

// Initialisierungs-Funktion für VL53L8CX
 void MX_TOF_Init(void)
{
    // Initialisiere die Peripherie und den Sensor
    MX_VL53L8CX_SimpleRanging_Init();
}

// Polling-Task für die Messungen
void MX_TOF_Process(void)
{
    MX_VL53L8CX_SimpleRanging_Process();
}

// Funktion zur Initialisierung des Sensors
void MX_VL53L8CX_SimpleRanging_Init(void)
{
    // Sensor-Reset (falls erforderlich)
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);  // Beispiel für Reset-Pin
    HAL_Delay(2);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);    // Reset-Pin auf High setzen
    HAL_Delay(2);


    // Initialisierung des Sensors
    printf("VL53L8CX Simple Ranging demo application\n");
    printf("Sensor initialization...\n");

    tof_dev.platform.address = VL53L8CX_DEFAULT_I2C_ADDRESS ;
    status += vl53l8cx_init(&tof_dev);
    status += vl53l8cx_set_ranging_mode(&tof_dev , VL53L8CX_RANGING_MODE_AUTONOMOUS );
    status += vl53l8cx_set_resolution(&tof_dev ,VL53L8CX_RESOLUTION_8X8 );
    // Sensor-Konfiguration
    vl53l8cx_set_integration_time_ms(&tof_dev, TIMING_BUDGET);
    vl53l8cx_set_ranging_frequency_hz(&tof_dev, RANGING_FREQUENCY);
    vl53l8cx_start_ranging(&tof_dev);  // Startet den kontinuierlichen Messmodus
}

// Funktion für den Messprozess
static void MX_VL53L8CX_SimpleRanging_Process(void)
{
	status = vl53l8cx_get_ranging_data(&tof_dev, &results);
    if (status != VL53L8CX_STATUS_ERROR)
    {
        print_result(&results);
    }
    HAL_Delay(POLLING_PERIOD);
}

// Ergebnis ausdrucken
static void print_result(VL53L8CX_ResultsData *results)
{
    char buffer[128];  // Buffer für die serielle Ausgabe

    for (int i = 0; i < 64; i++)  // Sensor liefert 64 Werte (8x8 Matrix)
    {
        uint16_t distance = results->distance_mm[i];  // Korrekte Strukturzugriffsnotation

        // Formatierte Ausgabe für jeden Wert
        snprintf(buffer, sizeof(buffer), "Pixel %d: Distance = %d mm\n", i, distance);
        HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
    }

    // Falls nur der erste Wert gesendet werden soll:
    snprintf(buffer, sizeof(buffer), "First Distance: %d mm\n", results->distance_mm[0]);
    HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
}

void ToF_EventDetected(void)
{
    // optional: printf("Event detected\n");
}



#ifdef __cplusplus
}
#endif

 

0 REPLIES 0