通信协议
SPI通信速率:在3.3V供电电压下,最大为20Mhz
SPI通信格式:时钟相位=1,时钟极性==0;
cubeMX的配置如下图所示:
数据的写入与读取
对于EEPROM的写入与数据的写入与读取一般遵循:控制指令码+寄存器地址+数据
文章来源:https://www.toymoban.com/news/detail-575542.html
TDC-GP22与stm32之间的连接
CJ-MCU公司为tdc-gp22做了封装,在某宝中可以买到这个板子。
根据官方所提供的原理图,模式一脉冲激光测距的引脚对应图如下表所示:文章来源地址https://www.toymoban.com/news/detail-575542.html
stm32 | tdc-gp22 |
---|---|
3.3V | vcc |
GND | GND |
3.3V | dis |
gpiox | intn |
gpiox | ssn |
sck | sck |
mosi | si |
miso | so |
gpiox | rtn |
gpiox | sta |
gpiox | sp1 |
stm32HAL库程序编写
void gp22_wr_config_reg(uint8_t opcode_address, uint32_t config_reg_data) //配置寄存器
{
uint8_t temp[5];
temp[0] = opcode_address;
temp[1] = (uint8_t) ((config_reg_data)>>24);
temp[2] = (uint8_t) ((config_reg_data)>>16);
temp[3] = (uint8_t) ((config_reg_data)>>8);
temp[4] = (uint8_t) (config_reg_data);
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi2,temp,5,0xffff);
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_SET);
HAL_Delay(1);
}
void gp22_send_1byte (uint8_t opcode) //发送一个字节操作码
{
uint8_t temp = opcode;
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi2,&temp,1,0xffff);
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_SET);
HAL_Delay(1);
}
void gp22_read_n_bytes(uint8_t opcode_address,uint8_t n_bytes,uint8_t* result) //读取n个字节的数据到result数组中并打印
{
uint8_t temp[5];
temp[0] = opcode_address;
switch(n_bytes)
{
case 1:
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi2, temp, result, 1, 0xffff);
HAL_UART_Transmit(&huart1,result,1,0xffff);
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_SET);
HAL_Delay(1);
case 2:
temp[1] = 0x00;
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi2, temp, result, 2, 0xffff);
HAL_UART_Transmit(&huart1,result,2,0xffff);
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_SET);
HAL_Delay(1);
case 3:
temp[1] = 0x00;
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi2, temp, result, 3, 0xffff);
HAL_UART_Transmit(&huart1,result,3,0xffff);
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_SET);
HAL_Delay(1);
case 4:
temp[1] = 0x00;
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi2, temp, result, 4, 0xffff);
HAL_UART_Transmit(&huart1,result,4,0xffff);
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_SET);
HAL_Delay(1);
case 5:
temp[1] = 0x00;
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi2, temp, result, 5, 0xffff);
HAL_UART_Transmit(&huart1,result,5,0xffff);
HAL_GPIO_WritePin(GPIOC,nss_tdc_Pin,GPIO_PIN_SET);
HAL_Delay(1);
}
}
void gp22_config(void)
{
//硬件复位
HAL_GPIO_WritePin(GPIOC, tdc_reset_Pin, GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOC, tdc_reset_Pin, GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOC, tdc_reset_Pin, GPIO_PIN_SET);
//上电软件复位
gp22_send_1byte (0x50);
HAL_Delay(1);
gp22_wr_config_reg(0x80,0x00242000);
gp22_wr_config_reg(0x81,0x01410000);
gp22_wr_config_reg(0x82,0xe0000000);
gp22_wr_config_reg(0x83,0x00000000);
gp22_wr_config_reg(0x84,0x20000000);
gp22_wr_config_reg(0x85,0x08000000);
}
int main()
{
gp22_config();
while(1)
{
gp22_send_1byte (0x70); //初始化测量
//这里是开始信号与停止信号触发
while(HAL_GPIO_ReadPin(GPIOA, tdc_intn_Pin) == 1); //INTN引脚复位表示计算完成
gp22_read_n_bytes(0xb0,5,result);//这里的参数5代表发送一个字节的数据读取4个字节的数据(理解这句代码很重要)
}
}
到了这里,关于stm32(HAL库)控制TDC-GP22激光测距的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!