0x01 GPIO时钟使能的坑
使用GD32的GPIO引脚来控制 74HC595 ,发现引脚一直无法控制,始终输出3.3v,初始化环节应该是出了问题。用通俗的话来说,就是点灯点不亮
排查了MCU、光耦隔离芯片、被强行上拉等问题,最后发现是GD的GPIO时钟使能问题。
以下是错误使用方法:
rcu_periph_clock_enable(RCU_SER | RCU_SCK);
修改为以下方法即可:
rcu_periph_clock_enable(RCU_SER);rcu_periph_clock_enable(RCU_SCK);
日后想必也会经常需要验证GD32,留个点灯函数在这里
rcu_periph_clock_enable(RCU_GPIOC);
gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_13);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);gpio_bit_set(GPIOC,GPIO_PIN_13);
gpio_bit_reset(GPIOC,GPIO_PIN_13);
0x02 微秒级延时函数
systick.c
#include "gd32f1x0.h"
#include "systick.h"volatile static uint32_t delay;#include "systick.h"volatile static float count_1us = 0;
volatile static float count_1ms = 0;void delay_decrement(void)
{if (0U != delay){delay--;}
}void systick_config(void)
{/* systick clock source is from HCLK/8 内部时钟的108M/8分频 */systick_clksource_set(SYSTICK_CLKSOURCE_HCLK_DIV8);count_1us = (float)SystemCoreClock/12000000;//延时1us需要的时钟数量 108M/8M = 13.5 count_1ms = (float)count_1us * 1000;//延时1ms需要的时钟数量 108M/8M *1000= 13500
}void delay_1us(uint32_t count)
{uint32_t ctl;SysTick->LOAD = (uint32_t)(count * count_1us);SysTick->VAL = 0x0000U;SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;do{ctl = SysTick->CTRL;}while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;SysTick->VAL = 0x0000U;
}void delay_1ms(uint32_t count)
{uint32_t ctl;SysTick->LOAD = (uint32_t)(count * count_1ms);SysTick->VAL = 0x0000U;SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;do{ctl = SysTick->CTRL;}while((ctl&SysTick_CTRL_ENABLE_Msk)&&!(ctl & SysTick_CTRL_COUNTFLAG_Msk));SysTick->VAL = 0x0000U;
}
systick.h
#ifndef SYSTICK_H
#define SYSTICK_H#include <stdint.h>void systick_config(void);
void delay_1ms(uint32_t count);
void delay_1us(uint32_t count);
void delay_decrement(void);#endif