欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GD32F4xx调试笔记

程序员文章站 2024-02-22 12:24:34
...

GD32F4xx调试笔记

RCU

  1. 设置输入晶振频率 默认25MHz,需要根据硬件设备进行调整,这里设置为8MHz。
    /* define value of high speed crystal oscillator (HXTAL) in Hz */
    #if !defined  (HXTAL_VALUE)
    #define HXTAL_VALUE    ((uint32_t)8000000)
    #endif /* high speed crystal oscillator value */
    
  2. 设置内部时钟频率,默认选择__SYSTEM_CLOCK_200M_PLL_25M_HXTAL,这里设置为系统主时钟为168MHz,输入晶振8MHz。
    /* select a system clock by uncommenting the following line */
    //#define __SYSTEM_CLOCK_IRC16M                   (uint32_t)(__IRC16M)
    //#define __SYSTEM_CLOCK_HXTAL                    (uint32_t)(__HXTAL)
    //#define __SYSTEM_CLOCK_120M_PLL_IRC16M          (uint32_t)(120000000)
    //#define __SYSTEM_CLOCK_120M_PLL_8M_HXTAL          (uint32_t)(120000000)
    //#define __SYSTEM_CLOCK_120M_PLL_25M_HXTAL       (uint32_t)(120000000)
    //#define __SYSTEM_CLOCK_168M_PLL_IRC16M          (uint32_t)(168000000)
    #define __SYSTEM_CLOCK_168M_PLL_8M_HXTAL        (uint32_t)(168000000)
    //#define __SYSTEM_CLOCK_168M_PLL_25M_HXTAL       (uint32_t)(168000000)
    // #define __SYSTEM_CLOCK_200M_PLL_IRC16M          (uint32_t)(200000000)
    //#define __SYSTEM_CLOCK_200M_PLL_8M_HXTAL        (uint32_t)(200000000)
    //#define __SYSTEM_CLOCK_200M_PLL_25M_HXTAL         (uint32_t)(200000000)
    

Systick

  1. 初始化函数
    ErrStatus Systick_Config(void)
    {
        /* setup systick timer for 1000Hz interrupts */
        if (SysTick_Config(SystemCoreClock / 1000U) == 1)
        {
            return ERROR;
        }
        /* configure the systick handler priority */
        NVIC_SetPriority(SysTick_IRQn, 0x00U);
    
        return SUCCESS;
    }
    
  2. 中断函数
    /*!
        \brief      this function handles SysTick exception
        \param[in]  none
        \param[out] none
        \retval     none
    */
    void SysTick_Handler(void)
    {
    
    }
    

GPIO

  1. 配置输出模式
    void GPIO_Output_Config(void)
    {
        /* enable the GPIO clock */
        rcu_periph_clock_enable(RCU_GPIOD);
        /* configure GPIO port */
        gpio_mode_set(GPIOD,GPIO_MODE_OUTPUT,GPIO_PUPD_PULLUP,GPIO_PIN_0);
        gpio_output_options_set(GPIOD,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
    }
    
  2. 配置输入模式
    /*!
        \brief      Configure Keyboard port
        \param[in]  none
        \param[out] none
        \retval     none
    */void Key_GPIO_Config(void)
    {
        KEY_CLOCK_ENABLE();
        
        gpio_mode_set(KEY_GPIOx,GPIO_MODE_INPUT,GPIO_PUPD_PULLUP,KEY_PIN);
    }
    

USART

  1. 初始化函数

    
    /*!
        \brief      USART0 configure
        \param[in] usart_periph: USARTx(x=0,1,2,5)/UARTx(x=3,4,6,7)
        \param[in] baudval: baud rate value
        \param[in] paritycfg: configure USART parity
                   only one parameter can be selected which is shown as below:
          \arg       USART_PM_NONE: no parity
          \arg       USART_PM_EVEN: even parity 
          \arg       USART_PM_ODD:  odd parity
        \param[in] wlen: USART word length configure
                   only one parameter can be selected which is shown as below:
          \arg       USART_WL_8BIT: 8 bits
          \arg       USART_WL_9BIT: 9 bits
        \param[in] stblen: USART stop bit configure
                   only one parameter can be selected which is shown as below:
          \arg       USART_STB_1BIT:   1 bit
          \arg       USART_STB_0_5BIT: 0.5 bit(not available for UARTx(x=3,4,6,7))
          \arg       USART_STB_2BIT:   2 bits
          \arg       USART_STB_1_5BIT: 1.5 bits(not available for UARTx(x=3,4,6,7))
        \param[out] none
        \retval     none
    */
    void Uart0_Config(uint32_t baudval,uint32_t paritycfg,uint32_t wlen,uint32_t stblen)
    {
    	/* enable USART GPIO clock */
    	rcu_periph_clock_enable(RCU_GPIOA);
    	/* enable USART clock */
    	rcu_periph_clock_enable(RCU_USART0);
    
        /* configure USART0 Tx as alternate function push-pull */
        gpio_af_set(UART0_GPIO_PORT, GPIO_AF_7, UART0_TX_PIN | UART0_RX_PIN);
        gpio_mode_set(UART0_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, UART0_TX_PIN | UART0_RX_PIN);
        gpio_output_options_set(UART0_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, UART0_TX_PIN | UART0_RX_PIN);
    
        usart_deinit(USART0);
        /* USART configure */
        usart_baudrate_set(USART0, baudval);
        usart_parity_config(USART0,paritycfg);
        usart_word_length_set(USART0,wlen);
        usart_stop_bit_set(USART0,stblen);
        
        UART0_NVIC_IRQ();
        usart_interrupt_enable(USART0, USART_INT_RBNE);
        
        usart_receive_config(USART0, USART_RECEIVE_ENABLE);
        usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
        usart_enable(USART0);
    }
    
  2. 中断函数

    	void USART0_IRQHandler(void)
    	{
    	    if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET)
    	    {
    	        if (cntRxd < sizeof(bufRxd))
    	        {
    	            bufRxd[cntRxd++] =(uint8_t)usart_data_receive(USART0);
    	        }
    	        usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
    	    }
    	}
    
  3. 发送函数

    void Uart0_SendString(uint8_t *dat, uint16_t len)
    {
        while(len--)
        {
            usart_data_transmit(USART0,*dat++);
            while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
        }
    }
    

Basic Timer

  1. 初始化函数

    /*!
        \brief      Configure basic timer related setting parameters.
        \param[in]  arr: period value
        \param[in]  psc: prescaler value
        \param[out] none
        \retval     none
    */
    void Basic_Timer_Config(uint16_t arr, uint16_t psc)
    {
        timer_parameter_struct timer_initpara;
        
        timer_initpara.period = arr;
        timer_initpara.prescaler = psc;
        timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
        timer_initpara.counterdirection  = TIMER_COUNTER_UP;
        timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
        timer_initpara.repetitioncounter = 0;
    
        /* TIMx clock enable */    
        BASIC_TIMER_CLOCK_ENABLE();
        /* TIMx interrupt Init */
        BASIC_TIMER_NVIC_IRQ();
    
        timer_deinit(BASIC_TIMER_X);
        timer_init(BASIC_TIMER_X, &timer_initpara);
    
        timer_update_event_enable(BASIC_TIMER_X);
        timer_interrupt_enable(BASIC_TIMER_X, TIMER_INT_UP);
        timer_flag_clear(BASIC_TIMER_X, TIMER_FLAG_UP);
        timer_update_source_config(BASIC_TIMER_X, TIMER_UPDATE_SRC_GLOBAL);
        
        timer_enable(BASIC_TIMER_X);
    }
    
  2. 中断函数

    void Basic_TIMERx_IRQHandler( void )
    {
        if( timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP ) != RESET )
        {
        
            timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
        }
    }
    

PWM

  1. 初始化函数

    /*!
        \brief      Configure PWM related setting parameters.
        \param[in]  arr: period value
        \param[in]  psc: prescaler value
        \param[out] none
        \retval     none
    */
    void PWM_Timer_Config(uint16_t arr, uint16_t psc)
    {
        timer_parameter_struct timer_initpara;
        timer_oc_parameter_struct timer_ocintpara;
        
        /* TIM clock enable */
        PWM_CLOCK_ENABLE();
        PWM_GPIO_CLOCK_ENABLE();
        
        gpio_mode_set(PWM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, PWM_PIN0|PWM_PIN1); 
        gpio_output_options_set(PWM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, PWM_PIN0|PWM_PIN1);
        gpio_af_set(PWM_GPIO_PORT, GPIO_AF_2, PWM_PIN0|PWM_PIN1);
    
        timer_deinit(PWM_TIMER_X);
        
        timer_initpara.period = arr;
        timer_initpara.prescaler = psc;
        timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
        timer_initpara.counterdirection  = TIMER_COUNTER_UP;
        timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
        timer_initpara.repetitioncounter = 0;
        timer_init(PWM_TIMER_X, &timer_initpara);
        
        /* CH2 configuration in PWM mode 0 */
        timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_HIGH;
        timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
        timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
        timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
        timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
        timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
        
        timer_channel_output_config(PWM_TIMER_X,PWM_CH0,&timer_ocintpara);
        timer_channel_output_config(PWM_TIMER_X,PWM_CH1,&timer_ocintpara);
    
        /* configuration in PWM mode 0,duty cycle 0% */
        timer_channel_output_pulse_value_config(PWM_TIMER_X,PWM_CH0,0);
        timer_channel_output_mode_config(PWM_TIMER_X,PWM_CH0,TIMER_OC_MODE_PWM0);
        timer_channel_output_shadow_config(PWM_TIMER_X,PWM_CH0,TIMER_OC_SHADOW_DISABLE);
        
        timer_channel_output_pulse_value_config(PWM_TIMER_X,PWM_CH1,0);
        timer_channel_output_mode_config(PWM_TIMER_X,PWM_CH1,TIMER_OC_MODE_PWM0);
        timer_channel_output_shadow_config(PWM_TIMER_X,PWM_CH1,TIMER_OC_SHADOW_DISABLE);
    
        /* auto-reload preload enable */
        timer_auto_reload_shadow_enable(PWM_TIMER_X);
        /* TIMER enable */
        timer_enable(PWM_TIMER_X);
    }
    
  2. 占空比设置参数

    /*!
        \brief      According to the selected PWM channel, set the required output duty cycle.
        \param[in]  ch: Select output channel(0~1) 
        \param[in]  duty: Setting the duty cycle(0~168) 
        \param[out] none
        \retval     none
    */
    void PWM_Set_duty(uint8_t ch, uint16_t duty)
    {
        if ( ch == 0)
            timer_channel_output_pulse_value_config(PWM_TIMER_X,PWM_CH0,duty);
        else if (ch == 1)
            timer_channel_output_pulse_value_config(PWM_TIMER_X,PWM_CH1,duty);
    }
    

DAC

  1. DAC配置函数
    /*!
        \brief      configure the DAC
        \param[in]  none
        \param[out] none
        \retval     none
    */
    void DAC_Config(void)
    {
        dac_deinit();
        
        rcu_periph_clock_enable(RCU_GPIOA);
        rcu_periph_clock_enable(RCU_DAC);
        
        /* once enabled the DAC, the corresponding GPIO pin is connected to the DAC converter automatically */
        gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_4|GPIO_PIN_5);
    
        /* configure the DAC0 */
        dac_trigger_disable(DAC0);
        dac_wave_mode_config(DAC0, DAC_WAVE_DISABLE);
        dac_output_buffer_enable(DAC0);
        
        dac_trigger_disable(DAC1);
        dac_wave_mode_config(DAC1, DAC_WAVE_DISABLE);
        dac_output_buffer_enable(DAC1);
        
        /* enable DAC0 and set data */
        dac_enable(DAC0);
        dac_enable(DAC1);
    }
    
相关标签: GD32F4xx