重构代码结构(拆分为多文件),并添加了XLSW通信协议。
This commit is contained in:
16
inc/RS485.h
16
inc/RS485.h
@@ -1,16 +0,0 @@
|
||||
//
|
||||
// Created by dell on 24-11-29.
|
||||
//
|
||||
|
||||
#ifndef RS485_H
|
||||
#define RS485_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define RX_BUFFER_SIZE 64
|
||||
|
||||
void process_command(char *cmd);
|
||||
|
||||
#endif //RS485_H
|
61
inc/i2c.h
Normal file
61
inc/i2c.h
Normal file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// Created by dell on 24-12-25.
|
||||
//
|
||||
|
||||
#ifndef I2C_H
|
||||
#define I2C_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include "main.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define I2C_SPEED 20000
|
||||
#define RCU_GPIO_I2C RCU_GPIOF
|
||||
#define RCU_I2C RCU_I2C0
|
||||
#define I2C_PHY I2C0
|
||||
#define I2C_SCL_PORT GPIOF
|
||||
#define I2C_SCL_PIN GPIO_PIN_1
|
||||
#define I2C_SDA_PORT GPIOF
|
||||
#define I2C_SDA_PIN GPIO_PIN_0
|
||||
#define I2C_GPIO_AF GPIO_AF_1
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define I2C_TIME_OUT (uint16_t)(10000)
|
||||
#define I2C_OK 1
|
||||
#define I2C_FAIL 0
|
||||
#define I2C_END 1
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
typedef enum {
|
||||
I2C_START = 0,
|
||||
I2C_SEND_ADDRESS,
|
||||
I2C_CLEAR_ADDRESS_FLAG,
|
||||
I2C_TRANSMIT_DATA,
|
||||
I2C_STOP
|
||||
} i2c_process_enum;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void i2c_gpio_config(void);
|
||||
|
||||
void i2c_config(void);
|
||||
|
||||
void i2c_bus_reset(void);
|
||||
|
||||
void i2c_scan(void);
|
||||
|
||||
uint8_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]);
|
||||
|
||||
uint8_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data);
|
||||
|
||||
#endif //I2C_H
|
30
inc/led.h
Normal file
30
inc/led.h
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by dell on 24-12-25.
|
||||
//
|
||||
|
||||
#ifndef LED_H
|
||||
#define LED_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_9
|
||||
#define LED_RCU RCU_GPIOA
|
||||
#define LED_TIMER_RCU RCU_TIMER5
|
||||
#define LED_TIMER TIMER5
|
||||
#define LED_IRQ TIMER5_IRQn
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void led_config(void);
|
||||
|
||||
#endif //LED_H
|
@@ -6,27 +6,19 @@
|
||||
#define MLX90614_H
|
||||
|
||||
#include "gd32e23x.h"
|
||||
#include "i2c.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define I2C_SPEED 100000
|
||||
#define IR_I2C I2C0
|
||||
#define RCU_IR_GPIO RCU_GPIOF
|
||||
#define RCU_I2C RCU_I2C0
|
||||
#define I2C_SCL_PORT GPIOF
|
||||
#define I2C_SCL_PIN GPIO_PIN_1
|
||||
#define I2C_SDA_PORT GPIOF
|
||||
#define I2C_SDA_PIN GPIO_PIN_0
|
||||
#define I2C_GPIO_AF GPIO_AF_1
|
||||
/******************************************************************************/
|
||||
|
||||
#define I2C_TIME_OUT (uint16_t)(5000)
|
||||
#define MLX90614_ADDR (0x5A << 1)
|
||||
#define MLX90614_OBJ_TEMP_REG 0x07
|
||||
#define MLX90614_AMB_TEMP_REG 0x06
|
||||
|
||||
#define SLAVE_ADDR (0x5A << 1)
|
||||
#define REG_ADDR_OBJ_TEMP 0x07
|
||||
#define REG_ADDR_AMB_TEMP 0x06
|
||||
/******************************************************************************/
|
||||
|
||||
/* function declarations */
|
||||
/* this function configures I2C Peripheral & GPIO AF for I2C */
|
||||
void MLX90614_I2CConfig(void);
|
||||
/* this function reads object temperature */
|
||||
int MLX90614_GetObjectTemperature(void);
|
||||
uint16_t mlx90614_get_objrct_temperature(void);
|
||||
|
||||
#endif //MLX90614_H
|
||||
|
59
inc/rs485.h
Normal file
59
inc/rs485.h
Normal file
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// Created by dell on 24-11-29.
|
||||
//
|
||||
|
||||
#ifndef RS485_H
|
||||
#define RS485_H
|
||||
|
||||
#include "gd32e23x_it.h"
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define RS485_RCU RCU_USART0
|
||||
#define RS485_GPIO_RCU RCU_GPIOA
|
||||
#define RS485_GPIO_PORT GPIOA
|
||||
#define RS485_TX_PIN GPIO_PIN_2
|
||||
#define RS485_RX_PIN GPIO_PIN_3
|
||||
#define RS485_PHY USART0
|
||||
#define RS485_BAUDRATE 115200U
|
||||
#define RS485_EN_PIN GPIO_PIN_4
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define RX_BUFFER_SIZE 32
|
||||
|
||||
#define US_IR_PACKAGE_HEADER 0xD5
|
||||
#define US_IR_BOARD_TYPE 0x04
|
||||
#define US_IR_PACKAGE_LENGTH 0x02
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
typedef enum
|
||||
{
|
||||
VALIDATION_SUCCESS = 0,
|
||||
VALIDATION_CRC_ERROR = 1,
|
||||
VALIDATION_HEADER_ERROR = 2,
|
||||
VALIDATION_TYPE_ERROR = 4,
|
||||
VALIDATION_LENGTH_ERROR = 8
|
||||
} validation_result_t;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void rs485_config(void);
|
||||
void process_command(uint8_t* cmd, size_t length);
|
||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length);
|
||||
validation_result_t validate_package_crc(uint8_t* data, uint8_t data_length);
|
||||
validation_result_t validate_package_header(uint8_t* data);
|
||||
validation_result_t validate_package_type(uint8_t* data);
|
||||
validation_result_t validate_data_length(uint8_t* data);
|
||||
void ultrasonic_distance_value_report(void);
|
||||
void mlx90614_tempture_value_report(void);
|
||||
|
||||
#endif //RS485_H
|
@@ -6,6 +6,7 @@
|
||||
#define ULTRASONIC_DRIVER_H
|
||||
|
||||
#include "gd32e23x.h"
|
||||
#include "i2c.h"
|
||||
|
||||
#define POWER_SUPPLY_12V
|
||||
// #define POWER_SUPPLY_24V
|
||||
@@ -25,23 +26,6 @@
|
||||
#define ULTRASONIC_CYCLES 0x05U
|
||||
#define ULTRASONIC_TRAN_US 498 // (ms)
|
||||
|
||||
#define LED_PORT GPIOA
|
||||
#define LED_PIN GPIO_PIN_9
|
||||
#define LED_RCU RCU_GPIOA
|
||||
#define LED_TIMER_RCU RCU_TIMER5
|
||||
#define LED_TIMER TIMER5
|
||||
#define LED_IRQ TIMER5_IRQn
|
||||
|
||||
#define USART_RCU RCU_USART0
|
||||
#define USART_GPIO_RCU RCU_GPIOA
|
||||
#define USART_GPIO_PORT GPIOA
|
||||
#define USART_TX_PIN GPIO_PIN_2
|
||||
#define USART_RX_PIN GPIO_PIN_3
|
||||
#define USART0_PHY USART0
|
||||
#define USART_BAUDRATE 115200U
|
||||
|
||||
#define USART_EN_PIN GPIO_PIN_4
|
||||
|
||||
#define US_TRAN_GPIO_RCU RCU_GPIOB
|
||||
#define US_TRAN_GPIO_PORT GPIOB
|
||||
#define US_TRAN_PIN GPIO_PIN_1
|
||||
@@ -65,15 +49,12 @@
|
||||
#define US_ECHO_TIMER TIMER16
|
||||
#define US_ECHO_CH TIMER_CH_0
|
||||
|
||||
void led_config(void);
|
||||
void usart_config(void);
|
||||
void UltraSonic_GPIO_Config(void);
|
||||
void UltraSonic_Transmit_Config(void);
|
||||
void UltraSonic_PwmOut_Cycles(const uint8_t cycles);
|
||||
void UltraSonic_Transmit_Delay(const uint16_t micro_second);
|
||||
void UltraSonic_ReceExti_Config(void);
|
||||
void UltraSonic_EchoTimer_Config(void);
|
||||
void UltraSonic_Receive_Config(void);
|
||||
uint16_t UltraSonic_CalcDistance(uint32_t us_value);
|
||||
void ultrasonic_gpio_config(void);
|
||||
void ultrasonic_pwm_out_cycles(const uint8_t cycles);
|
||||
void ultrasonic_transmit_delay(const uint16_t micro_second);
|
||||
void ultrasonic_rece_exti_Config(void);
|
||||
void ultrasonic_echo_timer_config(void);
|
||||
void ultrasonic_receive_config(void);
|
||||
uint16_t ultrasonic_calc_distance(uint32_t us_value);
|
||||
|
||||
#endif //ULTRASONIC_DRIVER_H
|
||||
|
Reference in New Issue
Block a user