The whole source code can be downloaded from our download center.
/*******************************************************************************
Function Name : main
Description : Main program.
Input : None
Output : None
Return : None
*****************************************************************************/
void main(void)
{
ENTR_CRT_SECTION();
/ Setup STM32 system (clock, PLL and Flash configuration) /
SystemInit();
EXT_CRT_SECTION();
// Choose your Slot (SPI1, SPI2)
void Bus_Type = SPI1;
/ controlled loop /
while (Dummy_var!=11) // To control the loop, e.g. (Dummy_var!=7)
{
if (Dummy_var10) Dummy_var=0; // Infinite loop
if(configured_trueFALSE)
{
configured_true = TRUE;
SPIx_GPIOs_Init(Bus_Type);
SPIx_Interface_Init(Bus_Type);
#ifdef EMBEDDED_SRAM
Embed_SRAM_Init();
#endif
Ext_Interrupt_Init();
gp22_send_1byte(Bus_Type, Power_On_Reset);
Dly100us((void)5); // 500 us wait for GP22
// Setting of the Configuration Registers
// CR0: DIV_CLKHS = 2, START_CLKHS = 1, CALIBRATE = 0, MESSB2 = 0, NEG_STOP =
NEGSTART = 0, …
Register_0 = 0x00240000; // NO_CAL_AUTO = 0
Register_0_NO_CAL = 0x00241000; // NO_CAL_AUTO = 1
// CR1: HITIN2 = 1, HITIN1 = 1, …
Register_1 = 0x19490000; // EN_FAST_INIT = 0
Register_1_FAST = 0x19C90000; // EN_FAST_INIT = 1
// CR2: EN_INT, RFEDGE1 = RFEDGE2 = 0, …
// (NOTE: EN_INT = b111, it doesn’t work with using EN_FAST_INT)
Register_2 = 0xA0000000; // EN_INT = Timeout(8) _ End HITs(4) _ ALU interrupt(2)
// CR3: …
Register_3 = 0x00000000;
// CR4: ...
Register_4 = 0x20000000;
// CR5: CON_FIRE = b000, EN_STARTNOISE = 1, ...
Register_5 = 0x10000000;
// CR6: QUAD_RES = 0, ...
Register_6 = 0x00000000;
// Writing to the configuration registers (CR)
gp22_wr_config_reg(Bus_Type, 0x80, Register_0_NO_CAL);
gp22_wr_config_reg(Bus_Type, 0x81, Register_1_FAST);
gp22_wr_config_reg(Bus_Type, 0x82, Register_2);
gp22_wr_config_reg(Bus_Type, 0x83, Register_3);
gp22_wr_config_reg(Bus_Type, 0x84, Register_4);
gp22_wr_config_reg(Bus_Type, 0x85, Register_5);
gp22_wr_config_reg(Bus_Type, 0x86, Register_6);
}
// .........................................................................
// ...START_CAL_RESONATOR..Calibrate High Speed Clock.......................
// ...START_CAL_TDC.....Update the CAL2- and CAL1-Value.....................
// ...................Laser Rangefinder Measurement CYCLE............LOOP...
// .........................Caluculate Result Values........................
N_Measure_Cycles = 10000;
diff_Cal2_Cal1_old = diff_Cal2_Cal1_new;
if((Dummy_var==0) | (Dummy_var==10))
{
//--------------------------------------------------------------------------
// Start Calibrate High Speed Clock Cycle (-->SLOW)
// Important Note: NO_CAL_AUTO and EN_FAST_INIT need to be cleared!
gp22_wr_config_reg(Bus_Type, 0x80, Register_0); // NO_CAL_AUTO = 0
gp22_wr_config_reg(Bus_Type, 0x81, Register_1); // EN_FAST_INIT = 0
gp22_send_1byte(Bus_Type, Init);
gp22_send_1byte(Bus_Type, Start_Cal_Resonator);
// Wait for INT Slot_x
if (Bus_Type==SPI1) while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_4)==1);
if (Bus_Type==SPI2) while (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_11)==1);
//Calculate Correction factor
//The time interval to be measured is set by ANZ_PER_CALRES
//which defines the number of periods of the 32.768 kHz clock:
//2 periods = 61.03515625 µs
CLKHS_freq_corr_fact = 61.03515625/
gp22_read_n_bytes_int(Bus_Type, 2, 0xB0, 0x00) * CLKHS_freq; // read
only two bytes
printf("\n Correction factor for clock = %1.4f\n", CLKHS_freq_corr_fact);
CLKHS_freq_cal = CLKHS_freq * CLKHS_freq_corr_fact; // Calibrated Clock frequency
//--------------------------------------------------------------------------
// Start Separate Calibration Measurement Cycle
// Important Note: EN_INT = End HITs
gp22_wr_config_reg(Bus_Type, 0x82, 0x40000000); // End HITs
gp22_send_1byte(Bus_Type, Init);
gp22_send_1byte(Bus_Type, Start_Cal_TDC); // update calibration data
// Note:
// The calibration data are not addressed directly after the calibration
// measurement but after the next regular measurement;
// Wait for INT Slot_x
if (Bus_Type==SPI1) while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_4)==1);
if (Bus_Type==SPI2) while (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_11)==1);
// Important Note: After Separate Calibration Measurement Cycle
// EN_INT = ALU interrupt
gp22_wr_config_reg(Bus_Type, 0x82, Register_2); // Timeout + ALU interrupt
//--------------------------------------------------------------------------
// 1st ToF Measurement plus calibration data readout
// Note: (NO_CAL_AUTO = 0 / EN_FAST_INIT = 0) --> SLOW
gp22_send_1byte(Bus_Type, Init);
//Trigger pulse laser
// SetPortHigh;
GPIO_SetBits(GPIOD, GPIO_Pin_8); // Output HIGH
// SetPortLow;
GPIO_ResetBits(GPIOD, GPIO_Pin_8); // Output LOW
// Wait for INT Slot_x
if (Bus_Type==SPI1) while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_4)==1);
if (Bus_Type==SPI2) while (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_11)==1);
// First regular measurement (to readout calibration data)
RAW_Result_int = gp22_read_n_bytes_int(Bus_Type,2,0xB0,0x00); // read
only two bytes
#ifdef EMBEDDED_SRAM
Write_Emb_SRAM_uint32_t(RAW_Result_int); // writes the first value into SRAM
#endif
// printf("\n 1. Measured RAW Value = %u \n",RAW_Result_int); // RAW value
// Check Status Register, next free result register
// printf("Stat_Reg = 0x%04X \n",gp22_read_status_bytes(Bus_Type));
// readout the new calibration data from result register adr 0x01
gp22_wr_config_reg(Bus_Type, 0x81, 0x67490000);
diff_Cal2_Cal1_new = gp22_read_n_bytes_int(Bus_Type,2,0xB0,0x01); // read
only two bytes
#ifdef EMBEDDED_SRAM
Write_Emb_SRAM_uint32_t(diff_Cal2_Cal1_new);
#endif
}
//--------------------------------------------------------------------------
// Calculate the real time after the hole first cycle loop
while (diff_Cal2_Cal1_old != 0)
{
avg_diff_Cal2_Cal1 = (diff_Cal2_Cal1_new+diff_Cal2_Cal1_old) / 2;
// printf("\n OLD Cal2-Cal1 RAW Value = %.0f \n",diff_Cal2_Cal1_old);
// printf("\n NEW Cal2-Cal1 RAW Value = %.0f \n",diff_Cal2_Cal1_new);
average_RAW_Result /= N_Measure_Cycles;
// Used Formulas:
// ---------------------------------------------------
// T_ref
// Time_Value = ----------- * measured_RAW_Value
// Cal2-Cal1
// ---------------------------------------------------
// velocity_of_light
// Distance_Value = ------------------- * Time_Value
// 2
// ---------------------------------------------------
// For this Source Code would be a Reference Clock used with 1 MHz
Time_Result = (average_RAW_Result/avg_diff_Cal2_Cal1) * 1000;//time [ns]
Distance_Result = Time_Result / 6.671281904; //distance [m]
printf("\n Time Measure Result (ToF) = %.3f ns\n",Time_Result);
printf(" corresponds to %.3f m of Distance\n",Distance_Result);
printf(" to reflected point after %u Measurements\n",N_Measure_Cycles);
diff_Cal2_Cal1_old = 0;
}
//--------------------------------------------------------------------------
// if more than one measure cycle (-->FAST)
average_RAW_Result = RAW_Result_int; // set first value of average_RAW_Result
gp22_wr_config_reg(Bus_Type, 0x80, Register_0_NO_CAL); // NO_CAL_AUTO = 1
gp22_wr_config_reg(Bus_Type, 0x81, Register_1_FAST); // EN_FAST_INIT = 1
gp22_send_1byte(Bus_Type, Init);
//--------------------------------------------------------------------------
// n'th ToF Measurement
for (int i=2; i<=N_Measure_Cycles;i++)
{
//Trigger pulse laser
// SetPortHigh;
GPIO_SetBits(GPIOD, GPIO_Pin_8); // Output HIGH
// SetPortLow;
GPIO_ResetBits(GPIOD, GPIO_Pin_8); // Output LOW
// Wait for INT Slot_x
if (Bus_Type==SPI1) while (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_4)==1);
if (Bus_Type==SPI2) while (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_11)==1);
RAW_Result_int = gp22_read_n_bytes_int(Bus_Type,2,0xB0,0x00); // read
only two bytes
// printf(" %u. Measure RAW Value = %.0f \n",i,RAW_Result_int); // RAW value
average_RAW_Result += RAW_Result_int;
#ifdef EMBEDDED_SRAM
Write_Emb_SRAM_uint32_t(RAW_Result_int); // writes the next values into SRAM
#endif
}
printf("\nNEW CYCLE...\n");
Dummy_var++; // To Control the loop
#ifdef EMBEDDED_SRAM
// clear internal SRAM of µC
sram_mem_offset = 0x0;
#endif
} // End while Dummy_var
} //End main
最新发布