当前位置:   article > 正文

GD32 开发笔记_gd32e23 gpio输入初始化

gd32e23 gpio输入初始化

0x01 GPIO时钟使能的坑

使用GD32的GPIO引脚来控制 74HC595 ,发现引脚一直无法控制,始终输出3.3v,初始化环节应该是出了问题。用通俗的话来说,就是点灯点不亮

排查了MCU、光耦隔离芯片、被强行上拉等问题,最后发现是GD的GPIO时钟使能问题。

以下是错误使用方法:

    rcu_periph_clock_enable(RCU_SER | RCU_SCK);
  • 1

修改为以下方法即可:

    rcu_periph_clock_enable(RCU_SER);
    rcu_periph_clock_enable(RCU_SCK);
  • 1
  • 2

日后想必也会经常需要验证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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

0x03 串口初始化的坑

串口初始化常常使用到的是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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/789246
推荐阅读
相关标签
  

闽ICP备14008679号