TA的每日心情 | 无聊 2016-10-8 20:34 |
---|
签到天数: 10 天 连续签到: 1 天 [LV.3]偶尔看看II
|
感谢EEBOARD、感谢GD等,
板子到手,
先把板上资源测一遍,熟悉板子,熟悉芯片,为后续的项目做好准备;
1、串口;
2、LED;
3、按键中断;
4、定时PWM输出;
详细过程如下:
1、串口;
串口连接到PD8,PD9
参考GD给的例程,自己写一个这块板子的串口初始化,并重定向printf函数- /*!
- \brief usb_to_uart_init
- \param[in] none
- \param[out] none
- \retval none
- \attention USART2,tx:PD8,rx:PD9,baud:115200
- */
- void usb_to_uart_init(void)
- {
- /* enable GPIO clock */
- rcu_periph_clock_enable(RCU_GPIOD);
- /* enable USART clock */
- rcu_periph_clock_enable(RCU_USART2);
- /* connect port to USARTx_Tx */
- gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_8);
- /* connect port to USARTx_Rx */
- gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_9);
- /* configure USART Tx as alternate function push-pull */
- gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_8);
- gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
- /* configure USART Rx as alternate function push-pull */
- gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_9);
- gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
-
- /* USART configure */
- usart_deinit(USART2);
- usart_baudrate_set(USART2, 115200U);
- usart_receive_config(USART2, USART_RECEIVE_ENABLE);
- usart_transmit_config(USART2, USART_TRANSMIT_ENABLE);
- usart_word_length_set(USART2, USART_WL_8BIT);
- usart_stop_bit_set(USART2, USART_STB_1BIT);
- usart_parity_config(USART2, USART_PM_NONE);
- usart_enable(USART2);
-
- }
- /* retarget the C library printf function to the USART */
- int fputc(int ch, FILE *f)
- {
- usart_data_transmit(USART2, (uint8_t)ch);
- while(RESET == usart_flag_get(USART2, USART_FLAG_TBE));
- return ch;
- }
复制代码 2、LED;
板上有LED5、6、4,分别连接到PD7、PB3、PB4:
参考GD的例程,写一个使用LED的程序- void bsp_led_on(uint8_t lednum)
- {
- switch(lednum)
- {
- case LED4:gpio_bit_set(GPIOB,GPIO_PIN_4);break;
- case LED6:gpio_bit_set(GPIOB,GPIO_PIN_3);break;
- case LED5:gpio_bit_set(GPIOD,GPIO_PIN_7);break;
- default:break;
- }
- }
- /*!
- \brief bsp_led_off
- \param[in] lednum: specify the Led to be turned off
- LED4
- LED6
- LED5
- \param[out] none
- \retval none
- \attention turn off selected led
- */
- void bsp_led_off(uint8_t lednum)
- {
- switch(lednum)
- {
- case LED4:gpio_bit_reset(GPIOB,GPIO_PIN_4);break;
- case LED6:gpio_bit_reset(GPIOB,GPIO_PIN_3);break;
- case LED5:gpio_bit_reset(GPIOD,GPIO_PIN_7);break;
- default:break;
- }
- }
复制代码 3、按键与中断;
板上有4个按键,分别连到RST、PA0、PE0、PE1,RST那个是芯片复位,另外三个连到IO口可以用作按键输入;
参考GD的例程,写一个使用按键的程序,这里两个按键都连到了外部中断0,为了偷懒就只使用后面两个按键:- /*!
- \brief bsp_key_init
- \param[in] none
- \param[out] none
- \retval none
- \attention LED4:PB4,LED5:PB3,LED6:PD7
- */
- void bsp_key_init(void)
- {
- {
- /* KEY3 is conect to PE0 */
- /* enable the key clock */
- rcu_periph_clock_enable(RCU_GPIOE);
- /* configure button pin as input */
- gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE,GPIO_PIN_0);
-
- /* enable the syscfg clock */
- rcu_periph_clock_enable(RCU_SYSCFG);
- /* enable and set key EXTI interrupt priority */
- nvic_irq_enable(EXTI0_IRQn, 2U, 0U);
- /* connect key EXTI line to key GPIO pin */
- syscfg_exti_line_config(EXTI_SOURCE_GPIOE, EXTI_SOURCE_PIN0);
- /* configure key EXTI line */
- exti_init(EXTI_0, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
- exti_interrupt_flag_clear(EXTI_0);
- }
-
- {
- /* KEY4 is conect to PE1 */
- /* enable the key clock */
- rcu_periph_clock_enable(RCU_GPIOE);
- /* configure button pin as input */
- gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_NONE,GPIO_PIN_1);
-
- /* enable the syscfg clock */
- rcu_periph_clock_enable(RCU_SYSCFG);
- /* enable and set key EXTI interrupt priority */
- nvic_irq_enable(EXTI1_IRQn, 2U, 1U);
- /* connect key EXTI line to key GPIO pin */
- syscfg_exti_line_config(EXTI_SOURCE_GPIOE, EXTI_SOURCE_PIN1);
- /* configure key EXTI line */
- exti_init(EXTI_1, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
- exti_interrupt_flag_clear(EXTI_1);
- }
- }
- /*!
- \brief EXTI0_IRQHandler
- \param[in] none
- \param[out] none
- \retval none
- \attention this function handles exti0 exception
- */
- void EXTI0_IRQHandler(void)
- {
- bsp_led_on(LED5);
- bsp_led_off(LED4);
- delay_ms(5);
- exti_interrupt_flag_clear(EXTI_0);
- }
- /*!
- \brief EXTI1_IRQHandler
- \param[in] none
- \param[out] none
- \retval none
- \attention this function handles exti1 exception
- */
- void EXTI1_IRQHandler(void)
- {
- bsp_led_off(LED5);
- bsp_led_on(LED4);
- delay_ms(5);
- exti_interrupt_flag_clear(EXTI_1);
- }
复制代码 4、定时PWM输出;
看了下3个LED灯连接的IO口,其中PB3是TIMER1的CH1,那么可以用TIMER1的PWM输出来控制LED的亮度;
参考GD的TIMER1的例程,写一个使用PWM输出的程序- #include "bsp_timer.h"
- /*!
- \brief bsp_timer_1_init
- \param[in] none
- \param[out] none
- \retval none
- \attention LED4:PB4,LED5:PB3,LED6:PD7
- */
- void bsp_timer_1_init(void)
- {
- /* -----------------------------------------------------------------------
- TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
- TIMER1CLK = SystemCoreClock / 120 = 1MHz
- TIMER1 channel1 duty cycle = (4000/ 16000)* 100 = 25%
- TIMER1 channel2 duty cycle = (8000/ 16000)* 100 = 50%
- TIMER1 channel3 duty cycle = (12000/ 16000)* 100 = 75%
- ----------------------------------------------------------------------- */
- timer_oc_parameter_struct timer_ocintpara;
- timer_parameter_struct timer_initpara;
-
- /* enable GPIO clock */
- rcu_periph_clock_enable(RCU_GPIOB);
-
- /*Configure PB3 PB10 PB11(TIMER1 CH1 CH2 CH3) as alternate function*/
- /*Configure PB3 */
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);
- /*Configure PB10 */
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
- /*Configure PB11 */
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
- /* connect port to GPIO_AF_1: TIMER0, TIMER1 */
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_3);
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
- /* enable TIMER clock */
- rcu_periph_clock_enable(RCU_TIMER1);
- /* TIMER clock prescaler select */
- rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4); // APB2时钟的4倍,=SYS_CLK
- timer_deinit(TIMER1);
- /* TIMER1 configuration */
- timer_initpara.prescaler = 119; /*!< prescaler value *//*!< 预分频值 */
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE; /*!< aligned mode *//*!< 对齐模式 */
- timer_initpara.counterdirection = TIMER_COUNTER_UP; /*!< counter direction *//*!< 计数方向 */
- timer_initpara.period = 15999; /*!< period value *//*!< 计数值 */
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1; /*!< clock division value *//*!< 时钟分频值 */
- timer_initpara.repetitioncounter = 0; /*!< the counter repetition value *//*!< 重复计数器的值,高级定时器才有 */
- timer_init(TIMER1,&timer_initpara);
- /* CH1,CH2 and CH3 configuration in PWM OC mode1 */
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE; /*!< channel output state *//*!< 通道输出状态 */
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH; /*!< channel output polarity *//*!< 通道输出极性 */
- timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW; /*!< idle state of channel output *//*!< 通道输出的空闲状态 */
- /*!< configure TIMER channel output function */
- timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara); /*!< configure TIMER channel output function */
- timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
- timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);
- /* CH1 configuration in PWM mode1,duty cycle 25% */
- /* configure TIMER channel output pulse value *//* 脉冲计数值 */
- timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,3999);
- /* configure TIMER channel output compare mode *//* 输出比较模式 */
- timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
- /* configure TIMER channel output shadow function *//* 输出比较影子寄存器使能 */
- timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
- /* CH2 configuration in PWM mode1,duty cycle 50% */
- timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,7999);
- timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
- /* CH3 configuration in PWM mode1,duty cycle 75% */
- timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,11999);
- timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM0);
- timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER1);
- /* TIMER enable */
- timer_enable(TIMER1);
-
- }
复制代码 调试:
写一个测试的main函数:- /*!
- \brief main function
- \param[in] none
- \param[out] none
- \retval none
- */
- int main(void)
- {
- nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
- usb_to_uart_init();
- delay_init();
- bsp_led_init();
- bsp_key_init();
- bsp_timer_1_init();
-
- while (1)
- {
- static uint8_t i = 0;
- static uint32_t pwm_cnt = 0;
- printf("--%3d--\r\n",i++);
- delay_ms(111);
- if(pwm_cnt < 8000)
- pwm_cnt = pwm_cnt + 200;
- else
- pwm_cnt = 0;
- timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,pwm_cnt);
- printf("PWM_CNT:%3d--\r\n",pwm_cnt);
- }
- }
复制代码 下载程序并按复位键,观察运行情况;
结果:
串口可用;
LED可用;
KEY可用;
PWM可用;
总结:
板子没有什么问题;
已基本熟悉板子和芯片;
可以做后续的设计了;
|
|