查看: 4062|回复: 3

开箱上电,把板上资源测一遍(串口+LED+按键+PWM等)

[复制链接]
  • TA的每日心情
    无聊
    2016-10-8 20:34
  • 签到天数: 10 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2017-5-9 13:11:19 | 显示全部楼层 |阅读模式
    分享到:
    感谢EEBOARD、感谢GD等,
    板子到手,
    板子照片.jpg
    先把板上资源测一遍,熟悉板子,熟悉芯片,为后续的项目做好准备;
    1、串口;
    2、LED;
    3、按键中断;
    4、定时PWM输出;
    详细过程如下:
    1、串口;
    串口连接到PD8,PD9
    串口IO.png
    串口转USB.png
    串口2.png
    参考GD给的例程,自己写一个这块板子的串口初始化,并重定向printf函数
    1. /*!
    2.    \brief      usb_to_uart_init
    3.    \param[in]  none
    4.    \param[out] none
    5.    \retval     none
    6.    \attention  USART2,tx:PD8,rx:PD9,baud:115200
    7. */
    8. void usb_to_uart_init(void)
    9. {
    10.    /* enable GPIO clock */   
    11.    rcu_periph_clock_enable(RCU_GPIOD);
    12.    /* enable USART clock */
    13.    rcu_periph_clock_enable(RCU_USART2);
    14.    /* connect port to USARTx_Tx */
    15.    gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_8);
    16.    /* connect port to USARTx_Rx */
    17.    gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_9);
    18.    /* configure USART Tx as alternate function push-pull */
    19.    gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_8);
    20.    gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
    21.    /* configure USART Rx as alternate function push-pull */
    22.    gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_9);
    23.    gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
    24.    
    25.    /* USART configure */
    26.    usart_deinit(USART2);
    27.    usart_baudrate_set(USART2, 115200U);
    28.    usart_receive_config(USART2, USART_RECEIVE_ENABLE);
    29.    usart_transmit_config(USART2, USART_TRANSMIT_ENABLE);
    30.    usart_word_length_set(USART2, USART_WL_8BIT);
    31.    usart_stop_bit_set(USART2, USART_STB_1BIT);
    32.    usart_parity_config(USART2, USART_PM_NONE);
    33.    usart_enable(USART2);

    34. }

    35. /* retarget the C library printf function to the USART */
    36. int fputc(int ch, FILE *f)
    37. {
    38.     usart_data_transmit(USART2, (uint8_t)ch);
    39.     while(RESET == usart_flag_get(USART2, USART_FLAG_TBE));
    40.     return ch;
    41. }
    复制代码
    2、LED;
    板上有LED5、6、4,分别连接到PD7、PB3、PB4:
    LED电路.png
    参考GD的例程,写一个使用LED的程序
    1. void bsp_led_on(uint8_t lednum)
    2. {
    3.    switch(lednum)
    4.    {
    5.       case LED4:gpio_bit_set(GPIOB,GPIO_PIN_4);break;
    6.       case LED6:gpio_bit_set(GPIOB,GPIO_PIN_3);break;
    7.       case LED5:gpio_bit_set(GPIOD,GPIO_PIN_7);break;
    8.       default:break;
    9.    }
    10. }

    11. /*!
    12.    \brief      bsp_led_off
    13.    \param[in]  lednum: specify the Led to be turned off
    14.                   LED4
    15.                   LED6
    16.                   LED5
    17.    \param[out] none
    18.    \retval     none
    19.    \attention  turn off selected led
    20. */
    21. void bsp_led_off(uint8_t lednum)
    22. {
    23.    switch(lednum)
    24.    {
    25.       case LED4:gpio_bit_reset(GPIOB,GPIO_PIN_4);break;
    26.       case LED6:gpio_bit_reset(GPIOB,GPIO_PIN_3);break;
    27.       case LED5:gpio_bit_reset(GPIOD,GPIO_PIN_7);break;
    28.       default:break;
    29.    }
    30. }
    复制代码
    3、按键与中断;
    板上有4个按键,分别连到RST、PA0、PE0、PE1,RST那个是芯片复位,另外三个连到IO口可以用作按键输入;
    按键电路.png
    参考GD的例程,写一个使用按键的程序,这里两个按键都连到了外部中断0,为了偷懒就只使用后面两个按键:
    1. /*!
    2.    \brief      bsp_key_init
    3.    \param[in]  none
    4.    \param[out] none
    5.    \retval     none
    6.    \attention  LED4:PB4,LED5:PB3,LED6:PD7
    7. */
    8. void bsp_key_init(void)
    9. {
    10.     {
    11.         /* KEY3 is conect to PE0 */
    12.         /* enable the key clock */
    13.         rcu_periph_clock_enable(RCU_GPIOE);
    14.         /* configure button pin as input */
    15.         gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE,GPIO_PIN_0);
    16.         
    17.         /* enable the syscfg clock */
    18.         rcu_periph_clock_enable(RCU_SYSCFG);
    19.         /* enable and set key EXTI interrupt priority */
    20.         nvic_irq_enable(EXTI0_IRQn, 2U, 0U);
    21.         /* connect key EXTI line to key GPIO pin */
    22.         syscfg_exti_line_config(EXTI_SOURCE_GPIOE, EXTI_SOURCE_PIN0);
    23.         /* configure key EXTI line */
    24.         exti_init(EXTI_0, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    25.         exti_interrupt_flag_clear(EXTI_0);
    26.     }
    27.    
    28.     {
    29.         /* KEY4 is conect to PE1 */
    30.         /* enable the key clock */
    31.         rcu_periph_clock_enable(RCU_GPIOE);
    32.         /* configure button pin as input */
    33.         gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE,GPIO_PIN_1);
    34.         
    35.         /* enable the syscfg clock */
    36.         rcu_periph_clock_enable(RCU_SYSCFG);
    37.         /* enable and set key EXTI interrupt priority */
    38.         nvic_irq_enable(EXTI1_IRQn, 2U, 1U);
    39.         /* connect key EXTI line to key GPIO pin */
    40.         syscfg_exti_line_config(EXTI_SOURCE_GPIOE, EXTI_SOURCE_PIN1);
    41.         /* configure key EXTI line */
    42.         exti_init(EXTI_1, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    43.         exti_interrupt_flag_clear(EXTI_1);
    44.     }
    45. }


    46. /*!
    47.    \brief      EXTI0_IRQHandler
    48.    \param[in]  none
    49.    \param[out] none
    50.    \retval     none
    51.    \attention  this function handles exti0 exception
    52. */
    53. void EXTI0_IRQHandler(void)
    54. {
    55.     bsp_led_on(LED5);
    56.     bsp_led_off(LED4);
    57.     delay_ms(5);
    58.     exti_interrupt_flag_clear(EXTI_0);
    59. }

    60. /*!
    61.    \brief      EXTI1_IRQHandler
    62.    \param[in]  none
    63.    \param[out] none
    64.    \retval     none
    65.    \attention  this function handles exti1 exception
    66. */
    67. void EXTI1_IRQHandler(void)
    68. {
    69.     bsp_led_off(LED5);
    70.     bsp_led_on(LED4);
    71.     delay_ms(5);
    72.     exti_interrupt_flag_clear(EXTI_1);
    73. }
    复制代码
    4、定时PWM输出;
    看了下3个LED灯连接的IO口,其中PB3是TIMER1的CH1,那么可以用TIMER1的PWM输出来控制LED的亮度;
    PB3-TIMER1.png
    参考GD的TIMER1的例程,写一个使用PWM输出的程序
    1. #include "bsp_timer.h"

    2. /*!
    3.    \brief      bsp_timer_1_init
    4.    \param[in]  none
    5.    \param[out] none
    6.    \retval     none
    7.    \attention  LED4:PB4,LED5:PB3,LED6:PD7
    8. */
    9. void bsp_timer_1_init(void)
    10. {
    11.     /* -----------------------------------------------------------------------
    12.     TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
    13.     TIMER1CLK = SystemCoreClock / 120 = 1MHz

    14.     TIMER1 channel1 duty cycle = (4000/ 16000)* 100  = 25%
    15.     TIMER1 channel2 duty cycle = (8000/ 16000)* 100  = 50%
    16.     TIMER1 channel3 duty cycle = (12000/ 16000)* 100 = 75%
    17.     ----------------------------------------------------------------------- */
    18.     timer_oc_parameter_struct timer_ocintpara;
    19.     timer_parameter_struct timer_initpara;
    20.    
    21.     /* enable GPIO clock */  
    22.     rcu_periph_clock_enable(RCU_GPIOB);
    23.    
    24.     /*Configure PB3 PB10 PB11(TIMER1 CH1 CH2 CH3) as alternate function*/
    25.     /*Configure PB3 */
    26.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
    27.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);
    28.     /*Configure PB10 */
    29.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
    30.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
    31.     /*Configure PB11 */
    32.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
    33.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);

    34.     /* connect port to GPIO_AF_1: TIMER0, TIMER1 */
    35.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_3);
    36.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
    37.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);

    38.     /* enable TIMER clock */
    39.     rcu_periph_clock_enable(RCU_TIMER1);
    40.     /* TIMER clock prescaler select */
    41.     rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);   // APB2时钟的4倍,=SYS_CLK

    42.     timer_deinit(TIMER1);

    43.     /* TIMER1 configuration */
    44.     timer_initpara.prescaler         = 119;                 /*!< prescaler value *//*!< 预分频值 */
    45.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;  /*!< aligned mode *//*!< 对齐模式 */
    46.     timer_initpara.counterdirection  = TIMER_COUNTER_UP;    /*!< counter direction *//*!< 计数方向 */
    47.     timer_initpara.period            = 15999;               /*!< period value *//*!< 计数值 */
    48.     timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;    /*!< clock division value *//*!< 时钟分频值 */
    49.     timer_initpara.repetitioncounter = 0;                   /*!< the counter repetition value *//*!< 重复计数器的值,高级定时器才有 */
    50.     timer_init(TIMER1,&timer_initpara);

    51.     /* CH1,CH2 and CH3 configuration in PWM OC mode1 */
    52.     timer_ocintpara.outputstate     = TIMER_CCX_ENABLE;             /*!< channel output state *//*!< 通道输出状态 */
    53.     timer_ocintpara.ocpolarity      = TIMER_OC_POLARITY_HIGH;       /*!< channel output polarity *//*!< 通道输出极性 */
    54.     timer_ocintpara.ocidlestate     = TIMER_OC_IDLE_STATE_LOW;      /*!< idle state of channel output *//*!< 通道输出的空闲状态 */  
    55.     /*!< configure TIMER channel output function */
    56.     timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);    /*!< configure TIMER channel output function */
    57.     timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
    58.     timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);

    59.     /* CH1 configuration in PWM mode1,duty cycle 25% */
    60.     /* configure TIMER channel output pulse value *//* 脉冲计数值 */
    61.     timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,3999);
    62.     /* configure TIMER channel output compare mode *//* 输出比较模式 */
    63.     timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
    64.     /* configure TIMER channel output shadow function *//* 输出比较影子寄存器使能 */
    65.     timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    66.     /* CH2 configuration in PWM mode1,duty cycle 50% */
    67.     timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,7999);
    68.     timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM0);
    69.     timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);

    70.     /* CH3 configuration in PWM mode1,duty cycle 75% */
    71.     timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,11999);
    72.     timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM0);
    73.     timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);

    74.     /* auto-reload preload enable */
    75.     timer_auto_reload_shadow_enable(TIMER1);
    76.     /* TIMER enable */
    77.     timer_enable(TIMER1);
    78.    
    79. }
    复制代码
    调试:
    写一个测试的main函数:
    1. /*!
    2.     \brief      main function
    3.     \param[in]  none
    4.     \param[out] none
    5.     \retval     none
    6. */
    7. int main(void)
    8. {

    9.     nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
    10.     usb_to_uart_init();
    11.     delay_init();
    12.     bsp_led_init();
    13.     bsp_key_init();
    14.     bsp_timer_1_init();
    15.    
    16.    while (1)
    17.    {
    18.         static uint8_t i = 0;
    19.         static uint32_t pwm_cnt = 0;
    20.         printf("--%3d--\r\n",i++);
    21.         delay_ms(111);
    22.         if(pwm_cnt < 8000)
    23.             pwm_cnt = pwm_cnt + 200;
    24.         else
    25.             pwm_cnt = 0;
    26.         timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,pwm_cnt);
    27.         printf("PWM_CNT:%3d--\r\n",pwm_cnt);
    28.    }
    29. }
    复制代码
    下载程序并按复位键,观察运行情况;
    结果:
    串口可用;
    LED可用;
    KEY可用;
    PWM可用;
    串口助手.png
    总结:
    板子没有什么问题;
    已基本熟悉板子和芯片;
    可以做后续的设计了;
    回复

    使用道具 举报

    该用户从未签到

    发表于 2017-5-11 16:42:14 | 显示全部楼层
    请教楼主,
    gpio_output_options_set
    这种风格的固件库,示例代码哪里有?
    现在固件是从keil网站的器件中得来的。但都没有示例,只能靠猜!

    另外,这固件库的下标全从0开始,而数据手册全是从1开始,好反人类呀。
    但看更新日期比较新,也放到keil网站,应该后面会继续维护,所以想用。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2016-10-8 20:34
  • 签到天数: 10 天

    连续签到: 1 天

    [LV.3]偶尔看看II

     楼主| 发表于 2017-5-13 01:17:45 | 显示全部楼层
    aozima 发表于 2017-5-11 16:42
    请教楼主,
    gpio_output_options_set
    这种风格的固件库,示例代码哪里有?

    在固件库里面,固件库在论坛里有下载,固件库解压后:GD32F4xx_Firmware_Library_V1.2\Examples里面有例程,GD32F4xx_Firmware_Library_V1.2\Utilities也有一些评估板的例程,Examples里面的GPIO的例程不完整,需要涉及到Utilities里面的文件。需要使用某个功能的时候,可以参考着Examples里面的例子,不过Examples里面的注释可能有一些瑕疵,第一次用某个功能的时候,最好对着用户手册看一下寄存器的功能理解下,而且有一些功能的配置步骤在用户手册里也会有说明。
    固件库.png LED.png 输出比较模式.png
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2017-5-13 12:43:35 | 显示全部楼层
    杨肉师傅 发表于 2017-5-13 01:17
    在固件库里面,固件库在论坛里有下载,固件库解压后:GD32F4xx_Firmware_Library_V1.2\Examples里面有例 ...

    谢谢,我也发现了。
    因为我用的是130,但130是没有示例的。
    前天在网站上看到有F4的固件库,下载回来一试,风格一致,所以对130也有很大的参考价值。
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-29 20:57 , Processed in 0.163010 second(s), 22 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.