赞
踩
使用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);
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
串口初始化常常使用到的是PA9.PA10,但这两个引脚有些时候并不太方便去飞线,相较于GD32F130C8T6,串口1的PA2、PA3引脚就在MCU边角上,方便调试
#define EVAL_COM USART1
#define EVAL_COM_CLK RCU_USART1
#define EVAL_COM_TX_PIN GPIO_PIN_2
#define EVAL_COM_RX_PIN GPIO_PIN_3
#define EVAL_COM_GPIO_PORT GPIOA
#define EVAL_COM_GPIO_CLK RCU_GPIOA
#define EVAL_COM_AF GPIO_AF_1
void gd_eval_com_init(uint32_t COM)
{
/* enable GPIO clock */
uint32_t COM_ID = 0U;
if(EVAL_COM0==COM){
COM_ID = 0U;
}
rcu_periph_clock_enable( EVAL_COM_GPIO_CLK);
rcu_periph_clock_enable(EVAL_COM_CLK);
/* connect port to USARTx_Tx */
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF,EVAL_COM_TX_PIN);
/* connect port to USARTx_Rx */
gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF,EVAL_COM_RX_PIN);
/* configure USART Tx as alternate function push-pull */
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP,EVAL_COM_TX_PIN);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ,EVAL_COM_TX_PIN);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP,EVAL_COM_RX_PIN);
gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ,EVAL_COM_RX_PIN);
/* USART configure */
usart_deinit(COM);
usart_baudrate_set(COM,9600U);
usart_transmit_config(COM, USART_TRANSMIT_ENABLE);
usart_receive_config(COM, USART_RECEIVE_ENABLE);
usart_enable(COM);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。