From 828721759db484d73042614db763e337594ac06a Mon Sep 17 00:00:00 2001 From: yelvlab Date: Tue, 3 Dec 2024 20:04:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90LED=E3=80=81RS485=E3=80=81IIC?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 ++ inc/LDC1612.h | 42 +++++++++++++++++++++++++++++++++ inc/RS485.h | 31 ++++++++++++++++++++++++ src/LDC1612.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++ src/RS485.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ src/gd32e23x_it.c | 24 +++++++++++++++++++ src/main.c | 11 ++++++--- src/peripheral.c | 2 +- 8 files changed, 227 insertions(+), 4 deletions(-) create mode 100644 inc/LDC1612.h create mode 100644 inc/RS485.h create mode 100644 src/LDC1612.c create mode 100644 src/RS485.c diff --git a/CMakeLists.txt b/CMakeLists.txt index add71d6..fe82fc0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,8 @@ set(TARGET_C_SRC ${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c ${CMAKE_SOURCE_DIR}/src/systick.c ${CMAKE_SOURCE_DIR}/src/peripheral.c + ${CMAKE_SOURCE_DIR}/src/LDC1612.c + ${CMAKE_SOURCE_DIR}/src/RS485.c ) add_executable(xlsw_3dp_LDC1612 ${TARGET_C_SRC}) diff --git a/inc/LDC1612.h b/inc/LDC1612.h new file mode 100644 index 0000000..84cde97 --- /dev/null +++ b/inc/LDC1612.h @@ -0,0 +1,42 @@ +// +// Created by dell on 24-12-3. +// + +#ifndef LDC1612_H +#define LDC1612_H + +#include "gd32e23x_it.h" +#include "gd32e23x.h" +#include "systick.h" +#include +#include +#include +#include + +#define LED_PORT GPIOA +#define LED_PIN GPIO_PIN_7 +#define LED_RCU RCU_GPIOA +#define LED_TIMER_RCU RCU_TIMER16 +#define LED_TIMER TIMER16 +#define LED_IRQ TIMER16_IRQn + +#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 SLAVE_ADDR (0x5A << 1) + + +void led_config(void); +void LDC1612_I2CConfig(void); + + +#endif //LDC1612_H diff --git a/inc/RS485.h b/inc/RS485.h new file mode 100644 index 0000000..9951949 --- /dev/null +++ b/inc/RS485.h @@ -0,0 +1,31 @@ +// +// Created by dell on 24-12-3. +// + +#ifndef RS485_H +#define RS485_H + +#include "gd32e23x_it.h" +#include "gd32e23x.h" +#include "systick.h" +#include +#include +#include +#include + +#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_1 + +#define RX_BUFFER_SIZE 64 + +void RS485_config(void); +void process_command(char *cmd); + +#endif //RS485_H \ No newline at end of file diff --git a/src/LDC1612.c b/src/LDC1612.c new file mode 100644 index 0000000..d936d8f --- /dev/null +++ b/src/LDC1612.c @@ -0,0 +1,60 @@ +// +// Created by dell on 24-12-3. +// + +#include "LDC1612.h" + + +void led_config(void) +{ + rcu_periph_clock_enable(LED_RCU); + + gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN); + gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN); + gpio_bit_write(LED_PORT, LED_PIN, SET); + + rcu_periph_clock_enable(LED_TIMER_RCU); + timer_deinit(LED_TIMER); + + timer_parameter_struct timer_initpara; + timer_struct_para_init(&timer_initpara); + timer_initpara.prescaler = 7199; + timer_initpara.alignedmode = TIMER_COUNTER_EDGE; + timer_initpara.counterdirection = TIMER_COUNTER_UP; + timer_initpara.period = 999; + timer_initpara.clockdivision = TIMER_CKDIV_DIV1; + timer_init(LED_TIMER, &timer_initpara); + + timer_auto_reload_shadow_enable(LED_TIMER); + timer_interrupt_enable(LED_TIMER, TIMER_INT_UP); + + timer_enable(LED_TIMER); + + nvic_irq_enable(LED_IRQ, 0); +} + +/** + * @brief This function configure the I2C peripheral & GPIO + * @param[in] none + * @param[out] none + * @retval None + */ +void LDC1612_I2CConfig(void) { + rcu_periph_clock_enable(RCU_IR_GPIO); + rcu_periph_clock_enable(RCU_I2C); + + gpio_af_set(I2C_SCL_PORT, I2C_GPIO_AF, I2C_SCL_PIN); + gpio_af_set(I2C_SDA_PORT, I2C_GPIO_AF, I2C_SDA_PIN); + + gpio_mode_set(I2C_SCL_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SCL_PIN); + gpio_output_options_set(I2C_SCL_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SCL_PIN); + + gpio_mode_set(I2C_SDA_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SDA_PIN); + gpio_output_options_set(I2C_SDA_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SDA_PIN); + + i2c_clock_config(IR_I2C, I2C_SPEED, I2C_DTCY_2); + + i2c_mode_addr_config(IR_I2C, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, SLAVE_ADDR); + i2c_enable(IR_I2C); + i2c_ack_config(IR_I2C, I2C_ACK_ENABLE); +} \ No newline at end of file diff --git a/src/RS485.c b/src/RS485.c new file mode 100644 index 0000000..a53d3a8 --- /dev/null +++ b/src/RS485.c @@ -0,0 +1,59 @@ +// +// Created by dell on 24-12-3. +// + +#include "RS485.h" + +#define MAX_CMD_SIZE 16 +#define BUFSIZE 8 + +extern uint16_t g_distance_uint16; +extern uint16_t g_temperature_uint16; + +void RS485_config(void) { + rcu_periph_clock_enable(RS485_GPIO_RCU); + rcu_periph_clock_enable(RS485_RCU); + + gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3); + + /* configure USART Tx&Rx as alternate function push-pull */ + gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN); + gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_TX_PIN | RS485_RX_PIN); + + /* configure RS485 EN Pin */ + gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN); + gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN); + gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, SET); + + /* USART configure */ + usart_deinit(RS485_PHY); + usart_baudrate_set(RS485_PHY, RS485_BAUDRATE); + usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE); + usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE); + + usart_enable(RS485_PHY); + + nvic_irq_enable(USART0_IRQn, 0); + usart_interrupt_enable(RS485_PHY, USART_INT_RBNE); +} + +void process_command(char *cmd) { + if (strncmp(cmd, "M1", 2) == 0) { + printf("M1 -=-=- OK!\r\n"); + printf("Distance: %d\r\n", g_distance_uint16); + } else if (strncmp(cmd, "M2", 2) == 0) { + printf("M2 -=-=- OK!\r\n"); + printf("Temperature: %d\r\n", g_temperature_uint16); + // } else if (strncmp(cmd, "M3", 2) == 0) { + // char *param_str = cmd + 2; // Skip "M3" + // int param = atoi(param_str + 1); // Skip "S" and convert to integer + // if (param >= 0 && param <= 100) { + // printf("M3 with parameter %d -=-=- OK!\r\n", param); + // } else { + // printf("Invalid parameter for M3 command!\r\n"); + // } + } else { + printf("Invalid Command!\r\n"); + } +} + diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index 0c77955..12b5a1e 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -35,6 +35,7 @@ OF SUCH DAMAGE. #include "gd32e23x_it.h" #include "main.h" #include "systick.h" +#include "LDC1612.h" /*! \brief this function handles NMI exception @@ -116,4 +117,27 @@ void TIMER13_IRQHandler(void) } led_status = !led_status; } +} + +/** + * @brief This function handles TIMER5 interrupt request. + * @param[in] none + * @param[out] none + * @retval None + */ +void TIMER16_IRQHandler(void) { + if (timer_interrupt_flag_get(LED_TIMER, TIMER_INT_FLAG_UP) == SET) + { + timer_interrupt_flag_clear(LED_TIMER, TIMER_INT_FLAG_UP); + static uint8_t led_status = 0; + if (led_status) + { + gpio_bit_write(LED_PORT, LED_PIN, RESET); + timer_autoreload_value_config(LED_TIMER, 19200); + } else { + gpio_bit_write(LED_PORT, LED_PIN, SET); + timer_autoreload_value_config(LED_TIMER, 800); + } + led_status = !led_status; + } } \ No newline at end of file diff --git a/src/main.c b/src/main.c index ec1f78d..015696c 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,9 @@ #include "peripheral.h" +#include "RS485.h" +#include "LDC1612.h" + /*! \brief main function \param[in] none @@ -22,11 +25,13 @@ int main(void) { /* configure systick */ systick_config(); - usart_config(); - led_blink_config(); + RS485_config(); + led_config(); delay_ms(5000); - printf("system start!\r\n"); + printf("\r\n"); + printf("XLSW-3DP-LDC1612! V0.0.1\r\n"); + printf("\r\n"); while(1){ printf("hello world!\r\n"); diff --git a/src/peripheral.c b/src/peripheral.c index 9e8d282..7b0b1af 100644 --- a/src/peripheral.c +++ b/src/peripheral.c @@ -40,7 +40,7 @@ void led_blink_config(void) { rcu_periph_clock_enable(RCU_GPIOB); - gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1); + gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1); gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_1); gpio_bit_write(GPIOB, GPIO_PIN_1, SET);