generated from hulk/gd32e23x_template
new function
This commit is contained in:
parent
138fc45e27
commit
890287a706
@ -13,12 +13,25 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define LED_PORT GPIOA
|
/*Register Rddr*/
|
||||||
#define LED_PIN GPIO_PIN_7
|
/***************************************************************************/
|
||||||
#define LED_RCU RCU_GPIOA
|
#define CONVERTION_RESULT_REG_START 0X00
|
||||||
#define LED_TIMER_RCU RCU_TIMER16
|
#define SET_CONVERSION_TIME_REG_START 0X08
|
||||||
#define LED_TIMER TIMER16
|
#define SET_CONVERSION_OFFSET_REG_START 0X0C
|
||||||
#define LED_IRQ TIMER16_IRQn
|
#define SET_LC_STABILIZE_REG_START 0X10
|
||||||
|
#define SET_FREQ_REG_START 0X14
|
||||||
|
|
||||||
|
#define SENSOR_STATUS_REG 0X18
|
||||||
|
#define ERROR_CONFIG_REG 0X19
|
||||||
|
#define SENSOR_CONFIG_REG 0X1A
|
||||||
|
#define MUL_CONFIG_REG 0X1B
|
||||||
|
#define SENSOR_RESET_REG 0X1C
|
||||||
|
#define SET_DRIVER_CURRENT_REG 0X1E
|
||||||
|
|
||||||
|
#define READ_MANUFACTURER_ID 0X7E
|
||||||
|
#define READ_DEVICE_ID 0X7F
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
#define I2C_TIME_OUT (uint16_t)(10000)
|
#define I2C_TIME_OUT (uint16_t)(10000)
|
||||||
#define LDC_I2C I2C0
|
#define LDC_I2C I2C0
|
||||||
@ -55,7 +68,10 @@ typedef enum {
|
|||||||
#define DRIVE_CURRENT0_ADDR 0x1E
|
#define DRIVE_CURRENT0_ADDR 0x1E
|
||||||
#define CONFIG_ADDR 0x1A
|
#define CONFIG_ADDR 0x1A
|
||||||
|
|
||||||
void led_config(void);
|
|
||||||
|
|
||||||
|
void LDC1612_read_sensor_infomation(void);
|
||||||
|
|
||||||
void LDC1612_I2CConfig(void);
|
void LDC1612_I2CConfig(void);
|
||||||
void LDC1612_Init(void);
|
void LDC1612_Init(void);
|
||||||
int LDC1612_read_reg(uint8_t device_address, uint8_t reg_address, uint8_t *data, uint16_t length);
|
int LDC1612_read_reg(uint8_t device_address, uint8_t reg_address, uint8_t *data, uint16_t length);
|
||||||
@ -65,6 +81,8 @@ uint8_t eeprom_byte_write_timeout(uint8_t device_address, uint8_t *data);
|
|||||||
|
|
||||||
void I2C_Scan(void);
|
void I2C_Scan(void);
|
||||||
|
|
||||||
|
uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value);
|
||||||
|
|
||||||
int LDC_getID(void);
|
int LDC_getID(void);
|
||||||
|
|
||||||
#endif //LDC1612_H
|
#endif //LDC1612_H
|
||||||
|
@ -35,4 +35,13 @@ OF SUCH DAMAGE.
|
|||||||
#ifndef MAIN_H
|
#ifndef MAIN_H
|
||||||
#define MAIN_H
|
#define MAIN_H
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
void led_config(void);
|
||||||
|
|
||||||
#endif /* MAIN_H */
|
#endif /* MAIN_H */
|
||||||
|
107
src/LDC1612.c
107
src/LDC1612.c
@ -4,36 +4,27 @@
|
|||||||
|
|
||||||
#include "LDC1612.h"
|
#include "LDC1612.h"
|
||||||
|
|
||||||
void led_config(void)
|
|
||||||
|
void LDC1612_read_sensor_infomation(void)
|
||||||
{
|
{
|
||||||
rcu_periph_clock_enable(LED_RCU);
|
uint16_t device_id;
|
||||||
|
uint8_t result;
|
||||||
|
|
||||||
gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN);
|
// 读取设备ID
|
||||||
gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN);
|
result = IIC_read_16bit(READ_DEVICE_ID, &device_id);
|
||||||
gpio_bit_write(LED_PORT, LED_PIN, SET);
|
|
||||||
|
|
||||||
rcu_periph_clock_enable(LED_TIMER_RCU);
|
if (result == 0) {
|
||||||
timer_deinit(LED_TIMER);
|
printf("Device ID: 0x%04X\n", device_id);
|
||||||
|
} else {
|
||||||
|
printf("Failed to read Device ID\n");
|
||||||
|
}
|
||||||
|
|
||||||
timer_parameter_struct timer_initpara;
|
|
||||||
timer_struct_para_init(&timer_initpara);
|
|
||||||
timer_initpara.prescaler = 799;
|
|
||||||
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, 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief This function configure the I2C peripheral & GPIO
|
* @brief This function configure the I2C peripheral & GPIO
|
||||||
* @param[in] none
|
* @param[in] none
|
||||||
@ -60,6 +51,7 @@ void LDC1612_I2CConfig(void) {
|
|||||||
i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE);
|
i2c_ack_config(LDC_I2C, I2C_ACK_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 向I2C设备寄存器写入数据
|
* @brief 向I2C设备寄存器写入数据
|
||||||
* @param device_address: 设备地址
|
* @param device_address: 设备地址
|
||||||
@ -455,6 +447,77 @@ void I2C_Scan(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 读取16位I2C数据,带有超时机制
|
||||||
|
* @param reg: 要读取的寄存器地址
|
||||||
|
* @param value: 存储读取数据的指针
|
||||||
|
* @retval 0: 成功,1: 超时
|
||||||
|
*/
|
||||||
|
uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value) {
|
||||||
|
uint32_t timeout = 0;
|
||||||
|
|
||||||
|
// 发送启动信号
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
timeout = 0;
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) {
|
||||||
|
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送从设备地址和写信号
|
||||||
|
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER);
|
||||||
|
timeout = 0;
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
||||||
|
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
|
||||||
|
}
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
|
||||||
|
// 发送寄存器地址
|
||||||
|
i2c_data_transmit(I2C0, reg);
|
||||||
|
timeout = 0;
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE)) {
|
||||||
|
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重新启动信号
|
||||||
|
i2c_start_on_bus(I2C0);
|
||||||
|
timeout = 0;
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) {
|
||||||
|
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送从设备地址和读信号
|
||||||
|
i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER);
|
||||||
|
timeout = 0;
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) {
|
||||||
|
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
|
||||||
|
}
|
||||||
|
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||||
|
|
||||||
|
// 读取数据
|
||||||
|
timeout = 0;
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) {
|
||||||
|
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
|
||||||
|
}
|
||||||
|
*value = i2c_data_receive(I2C0) << 8; // 读取高8位
|
||||||
|
|
||||||
|
timeout = 0;
|
||||||
|
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) {
|
||||||
|
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
|
||||||
|
}
|
||||||
|
*value |= i2c_data_receive(I2C0); // 读取低8位
|
||||||
|
|
||||||
|
// 发送停止信号
|
||||||
|
i2c_stop_on_bus(I2C0);
|
||||||
|
timeout = 0;
|
||||||
|
while (I2C_CTL0(I2C0) & I2C_CTL0_STOP) {
|
||||||
|
if ((timeout++) > I2C_TIME_OUT) return 1; // 超时
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0; // 成功
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int LDC_getID(void) {
|
int LDC_getID(void) {
|
||||||
uint8_t data[2] = {0};
|
uint8_t data[2] = {0};
|
||||||
uint16_t timeout = 0;
|
uint16_t timeout = 0;
|
||||||
|
41
src/main.c
41
src/main.c
@ -9,12 +9,37 @@
|
|||||||
#include "gd32e23x.h"
|
#include "gd32e23x.h"
|
||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
#include "gd32e23x_libopt.h"
|
#include "gd32e23x_libopt.h"
|
||||||
|
|
||||||
#include "peripheral.h"
|
|
||||||
|
|
||||||
#include "RS485.h"
|
#include "RS485.h"
|
||||||
#include "LDC1612.h"
|
#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 = 799;
|
||||||
|
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, 2);
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief main function
|
\brief main function
|
||||||
\param[in] none
|
\param[in] none
|
||||||
@ -28,7 +53,7 @@ int main(void)
|
|||||||
RS485_config();
|
RS485_config();
|
||||||
led_config();
|
led_config();
|
||||||
|
|
||||||
delay_ms(5000);
|
// delay_ms(5000);
|
||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
printf("XLSW-3DP-LDC1612! V0.0.1\r\n");
|
printf("XLSW-3DP-LDC1612! V0.0.1\r\n");
|
||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
@ -36,13 +61,11 @@ int main(void)
|
|||||||
LDC1612_I2CConfig();
|
LDC1612_I2CConfig();
|
||||||
// LDC1612_Init();
|
// LDC1612_Init();
|
||||||
// I2C_Scan();
|
// I2C_Scan();
|
||||||
int ret = 0;
|
LDC1612_read_sensor_infomation();
|
||||||
ret = LDC_getID();
|
|
||||||
printf("ret:%d\r\n",ret);
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t data[2] = {0};
|
|
||||||
uint32_t value = 0;
|
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
// LDC1612_read_reg(LDC1612_ADDR, 0x7f, data, 2);
|
// LDC1612_read_reg(LDC1612_ADDR, 0x7f, data, 2);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user