新手入门
- 积分
- 15
- 金钱
- 15
- 注册时间
- 2019-12-19
- 在线时间
- 2 小时
|
1金钱
本帖最后由 iversonkamp 于 2020-10-19 21:22 编辑
根据board.h的提示使能了TIM3和TIM15,但是两个定时器都不能正常工作。
我原本是想让TIM15定时器每2秒打印一次,但是运行结果如上图,只运行一次就停止了,TIM3也是一样。main.c代码如下。
- #include <rtthread.h>
- #include <rtdevice.h>
- #define DBG_TAG "main"
- #define DBG_LVL DBG_LOG
- #include <rtdbg.h>
- static rt_device_t serial;
- #define HWTIMER_DEV_NAME "timer15"
- static int cnt;
- static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
- {
- rt_kprintf("this is hwtimer %dtimes timeout callback fucntion!\n",cnt);
- cnt++;
- return 0;
- }
- void serial_open(char* uart_name)
- {
- serial = rt_device_find(uart_name);
- if (!serial)
- {
- rt_kprintf("find %s failed!\n", uart_name);
- }
- struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
- config.baud_rate = BAUD_RATE_115200; //修改波特率为 115200
- config.data_bits = DATA_BITS_8; //数据位 8
- config.stop_bits = STOP_BITS_1; //停止位 1
- config.bufsz = 128; //修改缓冲区 buff size 为 128
- config.parity = PARITY_NONE; //无奇偶校验位
- rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
- rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
- }
- /*
- *
- */
- int main(void)
- {
- rt_err_t ret;
- rt_hwtimerval_t timeout_s;
- rt_device_t hw_dev;
- rt_hwtimer_mode_t mode;
- hw_dev = rt_device_find(HWTIMER_DEV_NAME);
- if (hw_dev == RT_NULL)
- {
- rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
- return RT_ERROR;
- }
- rt_device_set_rx_indicate(hw_dev, timeout_cb);
- mode = HWTIMER_MODE_PERIOD;
- ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
- if (ret != RT_EOK)
- {
- rt_kprintf("set mode failed! ret is :%d\n", ret);
- return ret;
- }
- timeout_s.sec = 0;
- timeout_s.usec = 2;
- ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
- if (ret != RT_EOK)
- {
- rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
- return ret;
- }
- rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s));
- rt_thread_mdelay(10000);
- rt_kprintf("end\n");
- return RT_EOK;
- }
复制代码 board.h里面定时器也打开了,hal_conf.h定时器也使能了。
- #define BSP_USING_TIM
- #ifdef BSP_USING_TIM
- #define BSP_USING_TIM3
- #define BSP_USING_TIM15
- /*#define BSP_USING_TIM16*/
- /*#define BSP_USING_TIM17*/
- #endif
复制代码
- /*#define HAL_SRAM_MODULE_ENABLED */
- /*#define HAL_SWPMI_MODULE_ENABLED */
- #define HAL_TIM_MODULE_ENABLED
- /*#define HAL_TSC_MODULE_ENABLED */
- #define HAL_UART_MODULE_ENABLED
- /*#define HAL_USART_MODULE_ENABLED */
复制代码
board.c也复制了相关Msp函数:
- void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
- {
- if(htim_base->Instance==TIM3)
- {
- /* USER CODE BEGIN TIM3_MspInit 0 */
- /* USER CODE END TIM3_MspInit 0 */
- /* Peripheral clock enable */
- __HAL_RCC_TIM3_CLK_ENABLE();
- /* TIM3 interrupt Init */
- HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(TIM3_IRQn);
- /* USER CODE BEGIN TIM3_MspInit 1 */
- /* USER CODE END TIM3_MspInit 1 */
- }
- else if(htim_base->Instance==TIM15)
- {
- /* USER CODE BEGIN TIM15_MspInit 0 */
- /* USER CODE END TIM15_MspInit 0 */
- /* Peripheral clock enable */
- __HAL_RCC_TIM15_CLK_ENABLE();
- /* TIM15 interrupt Init */
- HAL_NVIC_SetPriority(TIM1_BRK_TIM15_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(TIM1_BRK_TIM15_IRQn);
- /* USER CODE BEGIN TIM15_MspInit 1 */
- /* USER CODE END TIM15_MspInit 1 */
- }
- }
复制代码 tim_config.h我是这么定义的:
- #ifdef BSP_USING_TIM3
- #ifndef TIM3_CONFIG
- #define TIM3_CONFIG \
- { \
- .tim_handle.Instance = TIM3, \
- .tim_irqn = TIM3_IRQn, \
- .name = "timer3", \
- }
- #endif /* TIM3_CONFIG */
- #endif /* BSP_USING_TIM3 */
- #ifdef BSP_USING_TIM15
- #ifndef TIM15_CONFIG
- #define TIM15_CONFIG \
- { \
- .tim_handle.Instance = TIM15, \
- .tim_irqn = TIM1_BRK_TIM15_IRQn, \
- .name = "timer15", \
- }
- #endif /* TIM15_CONFIG */
- #endif /* BSP_USING_TIM15 */
复制代码
但是不知道为什么会出那样的问题,请问该如何解决?用keil中的例程可以正常运行,而且相似的代码也可以在f103中运行,只是f103不用更改tim_config.h文件。
|
-
代码运行结果
|