美文网首页
GD32VF103 驱动舵机

GD32VF103 驱动舵机

作者: T_K_233 | 来源:发表于2020-10-01 02:42 被阅读0次
    #include "gd32vf103.h"
    #include <stdio.h>
    
    void init_timer(void) {   
      
      rcu_periph_clock_enable(RCU_GPIOA);
      rcu_periph_clock_enable(RCU_TIMER0);
    
    
      gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
    
      timer_deinit(TIMER0); 
      timer_parameter_struct timer_initpara;
      timer_initpara.prescaler = 107;
      timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
      timer_initpara.counterdirection = TIMER_COUNTER_UP;
      timer_initpara.period = 0;
      timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
      timer_initpara.repetitioncounter = 0;
      timer_init(TIMER0, &timer_initpara);
      
      timer_oc_parameter_struct timer_ocinitpara;
      timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
      timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
      timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
      timer_ocinitpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
      timer_ocinitpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
      timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
      timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocinitpara);
    
      timer_autoreload_value_config(TIMER0, 20000);
      // timer_counter_value_config(TIMER0, 0);
    
      timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
      timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
      
      timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, 1500);
    
      timer_primary_output_config(TIMER0, ENABLE);
    
      timer_enable(TIMER0);
    }
    
    
    void USART0_init(void) {    
      /* enable GPIO clock */
      rcu_periph_clock_enable(RCU_GPIOA);
      /* enable USART clock */
      rcu_periph_clock_enable(RCU_USART0);
    
      /* connect port to USARTx_Tx */
      gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
      /* connect port to USARTx_Rx */
      gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
    
      /* USART configure */
      usart_deinit(USART0);
      usart_baudrate_set(USART0, 115200U);
      usart_word_length_set(USART0, USART_WL_8BIT);
      usart_stop_bit_set(USART0, USART_STB_1BIT);
      usart_parity_config(USART0, USART_PM_NONE);
      usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
      usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
      usart_receive_config(USART0, USART_RECEIVE_ENABLE);
      usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
      usart_enable(USART0);
    
      usart_interrupt_enable(USART0, USART_INT_RBNE);
    }
    
    // retarget the C library printf function to USART0
    int _put_char(int ch) {
         usart_data_transmit(USART0, (uint8_t) ch );
         while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET){
         }
         return ch;
    }
    
    /**
     * delay a time in milliseconds.
     * 
     * @param t time to wait, in milliseconds
     */
    void delay(uint32_t t) {
      // Don't start measuruing until we see an mtime tick
      uint64_t start_mtime = get_timer_value();
      uint64_t current_mtime = get_timer_value();
      while (current_mtime == start_mtime) {
        current_mtime = get_timer_value();
      }
      uint64_t target_mtime = current_mtime + (get_timer_freq() / 1000 * t);
      while(get_timer_value() < target_mtime) {}
    }
    
    
    /**
     * main function
     */
    int main(void) {
      gpio_deinit(GPIOA);
      gpio_afio_deinit();
    
      USART0_init();
      
     /*配置系统时钟*/  
      rcu_config();  
      /*配置GPIO口 */  
      gpio_config();  
      /*配置TIMER*/  
      /*配置ADC*/  
      adc_config(); 
    
      init_timer();
      
    
      adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
    
      while(1) {
        uint16_t adc_value = adc_regular_data_read(ADC0);  // 0 - 4096   0V - 3.3V
    
        uint32_t output = adc_value;
    
        timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, output);
    
        printf("ADC0: %d\t output: %d\n", adc_value, output);
        delay(100);
      }
    }
      
    void rcu_config(void) {
      /* enable GPIOA clock */
      rcu_periph_clock_enable(RCU_GPIOA);
      /* enable ADC0 clock */
      rcu_periph_clock_enable(RCU_ADC0);
      /* enable DMA0 clock */
      //rcu_periph_clock_enable(RCU_DMA0);
      /* enable timer1 clock */
      //rcu_periph_clock_enable(RCU_TIMER1);
      /* config ADC clock */
      rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV8);
    }  
      
    void gpio_config(void) {  
      /* config the GPIO as analog mode */  
      gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0);  
    }  
    /*
    void dma_config(void) {  
        /* ADC_DMA_channel configuration *
        dma_parameter_struct dma_data_parameter;  
      
        /* ADC DMA_channel configuration *
        dma_deinit(DMA0, DMA_CH0);  
      
        /* initialize DMA data mode *
        dma_data_parameter.periph_addr  = (uint32_t)(&ADC_RDATA(ADC0));  
        dma_data_parameter.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;  
        dma_data_parameter.memory_inc   = DMA_MEMORY_INCREASE_DISABLE;  
        dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;  
        dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;  
        dma_data_parameter.direction    = DMA_PERIPHERAL_TO_MEMORY;  
        dma_data_parameter.number       = 1;  
        dma_data_parameter.priority     = DMA_PRIORITY_HIGH;  
        dma_init(DMA0, DMA_CH0, &dma_data_parameter);  
      
        dma_circulation_enable(DMA0, DMA_CH0);  
      
        /* enable DMA channel *
        dma_channel_enable(DMA0, DMA_CH0);  
    }  
      
    void timer_config(void)  
    {  
        timer_oc_parameter_struct timer_ocintpara;  
        timer_parameter_struct timer_initpara;  
      
        /* TIMER1 configuration *
        timer_struct_para_init(&timer_initpara);  
        timer_initpara.prescaler               = 5399;  
        timer_initpara.alignedmode             = TIMER_COUNTER_EDGE;  
        timer_initpara.counterdirection        = TIMER_COUNTER_UP;  
        timer_initpara.period                  = 9999;  
        timer_initpara.clockdivision           = TIMER_CKDIV_DIV1;  
        timer_initpara.repetitioncounter       = 0;  
        timer_init(TIMER1,&timer_initpara);  
      
        /* CH1 configuration in PWM mode1 *
        timer_channel_output_struct_para_init(&timer_ocintpara);  
        timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_HIGH;  
        timer_ocintpara.outputstate = TIMER_CCX_ENABLE;  
        timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);  
      
        timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 3999);  
        timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM1);  
        timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);  
    }  
    */
      
    void adc_config(void) {  
      /* reset ADC */
      adc_deinit(ADC0);
    
      /* ADC mode config */
      adc_mode_config(ADC_MODE_FREE);
      /* ADC continous function enable */
      adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
    
    
      /* ADC data alignment config */
      adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
    
      /* ADC channel length config */
      adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
    
      /* ADC regular channel config */
      adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_1POINT5);
    
      /* ADC trigger config */
      adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE);
    
      /* ADC external trigger enable */
      adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
    
      /* enable ADC interface */
      adc_enable(ADC0);
      /* ADC calibration and reset calibration */
      adc_calibration_enable(ADC0);
      delay(1);
    } 
    

    相关文章

      网友评论

          本文标题:GD32VF103 驱动舵机

          本文链接:https://www.haomeiwen.com/subject/hfcmuktx.html