Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
STM32 定时器同步 TIM3做主模式 TIM1和TIM2做从
发布于 2013-07-28 14:05:20 浏览:9419
订阅该版
#include "stm32f10x.h" TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* Private function prototypes -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); /* Private functions ---------------------------------------------------------*/ /** * @brief Main program * @param None * @retval : None */ int main(void) { /* System Clocks Configuration */ RCC_Configuration(); /* GPIO Configuration */ GPIO_Configuration(); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 255; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 9; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 4; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* Master Configuration in PWM1 Mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 64; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); /* Select the Master Slave Mode */ TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); /* Master Mode selection */ TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); /* Slaves Configuration: PWM1 Mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 3; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC1Init(TIM1, &TIM_OCInitStructure); /* Slave Mode selection: TIM2 */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated); TIM_SelectInputTrigger(TIM2, TIM_TS_ITR1); /* Slave Mode selection: TIM1 */ TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated); TIM_SelectInputTrigger(TIM1, TIM_TS_ITR1); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM2, ENABLE); TIM_Cmd(TIM1, ENABLE); TIM_CtrlPWMOutputs(TIM1, ENABLE); while (1) {} } /** * @brief Configures the different system clocks. * @param None * @retval : None */ void RCC_Configuration(void) { /* Setup the microcontroller system. Initialize the Embedded Flash Interface, initialize the PLL and update the SystemFrequency variable. */ SystemInit(); /* TIM1,TIM2, TIM3 and TIM4 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); /* GPIOA and GPIOB clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); } /** * @brief Configure the GPIOD Pins. * @param None * @retval : None */ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA Configuration: PA0(TIM2 CH1) and PA6(TIM3 CH1) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_6 | GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* GPIOB Configuration: PB6(TIM4 CH1) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_Init(GPIOB, &GPIO_InitStructure); } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval : None */ void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d
", file, line) */ while (1) {} } #endif /** * @} */ /** * @} */ /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/ 上面是程序代码,通过软件仿真发现TIM1和TIM2无法被TIM3触发启动,请大家帮忙看看,谢谢了!
查看更多
3
个回答
默认排序
按发布时间排序
tyw996
2013-07-29
这家伙很懒,什么也没写!
大略看了下,好像从定时器配置有问题,以下是我调好的从定时器配置,楼主可参考下:定时器5用作PWM输出(主定时器),Time3再作为Time5的从计数器,Time2再作为Time3的从计数器,实现无需软件干预的32位定时器记录PWM输出脉冲。 ``` /*定时器5用作PWM输出,当APB1=36MHz意味Timer2-6为72MHz*/ void Motor_TIM5_config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* Time base configuration[初始化时间基单元] */ TIM_TimeBaseStructure.TIM_Period =224;//周期值,fCK_PSC/(PSC[15:0]+1)。 TIM_TimeBaseStructure.TIM_Prescaler = 0;//时钟的预分频数=0x0000 TIM_TimeBaseStructure.TIM_ClockDivision = 0;//TIM_ClockDivision 配置时钟分割 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数 TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); /* Prescaler configuration [配置TIM的预分频数]*/ TIM_PrescalerConfig(TIM5, 72, TIM_PSCReloadMode_Immediate);//TIM5,72分频,预分频数在更新事件时装入 /* Output Compare Timing Mode configuration: Channel3 [配置输出比较模式:通道3]*/ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM输出脉冲宽度模式 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出允许 TIM_OCInitStructure.TIM_Pulse =20;//CCR1_Val;//脉冲CCR1_Val TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//极性Low TIM_OC4Init(TIM5, &TIM_OCInitStructure); /* Selects the regular update source for TIM5*/ TIM_UpdateRequestConfig(TIM5, TIM_UpdateSource_Global);//只有计数器溢出时才产生更新事件 /* Enables the TIM5 Preload on ARR Register */ TIM_ARRPreloadConfig(TIM5, ENABLE); /* ----------------------TIM3 Configuration as Master for the TIM5 -----------*/ /* Use the TIM5 Update event as TIM5 Trigger Output(TRGO) */ TIM_SelectOutputTrigger(TIM5, TIM_TRGOSource_Update);//送出TIM5更新事件UEV做为触发输出(TIM5_CR2寄存器的MMS=’010’) /* Enable the TIM5 Master Slave Mode */ TIM_SelectMasterSlaveMode(TIM5, TIM_MasterSlaveMode_Enable);//配置定时器5为主模式 /* TIM5 enable counter */ //TIM_Cmd(TIM5, ENABLE);//软件控制打开 } //定时器级连模式,Time2,Time3组成32位定时器,Time2为高16位,Time3为低16位,用于对Time5的PWM输出计数 //Time3作为Time5的从计数器 void Motor_TIM3_config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* Select the TIM3 Input Trigger: TIM5 TRGO used as Input Trigger for TIM3*/ TIM_SelectInputTrigger(TIM3, TIM_TS_ITR2);//定时器3选择定时器5获得输入触发(TIM3_SMCR寄存器的TS=010) /* Use the External Clock as TIM3 Slave Mode */ TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_External1);//配置定时器3使用外部时钟模式(TIM3_SMCR寄存器的SMS=111) /* Enable the TIM3 Master Slave Mode */ TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);//配置定时器3为主模式 TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);//送出TIM3更新事件UEV做为触发输出(TIM5_CR2寄存器的MMS=’010’) /*---------------------------- TIM3 Configuration ----------------------------*/ /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535;//fCK_PSC/(PSC[15:0]+1)。 TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* Enables the TIM3 Preload on ARR Register */ TIM_ARRPreloadConfig(TIM3, ENABLE); TIM_Cmd(TIM3, ENABLE); } //Time2作为Time3的从计数器 void Motor_TIM2_config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* Select the TIM3 Input Trigger: TIM3 TRGO used as Input Trigger for TIM2*/ TIM_SelectInputTrigger(TIM2, TIM_TS_ITR2);//定时器2选择定时器3获得输入触发(TIM2_SMCR寄存器的TS=010) /* Use the External Clock as TIM2 Slave Mode */ TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_External1);//配置定时器2使用外部时钟模式(TIM2_SMCR寄存器的SMS=111) /*---------------------------- TIM2 Configuration ----------------------------*/ /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 65535;//fCK_PSC/(PSC[15:0]+1)。 TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Enables the TIM3 Preload on ARR Register */ TIM_ARRPreloadConfig(TIM2, ENABLE); TIM_Cmd(TIM2, ENABLE); } ```
dingliangzhu
2013-11-05
这家伙很懒,什么也没写!
谢谢楼上的解答! 有段时间没上论坛了
撰写答案
登录
注册新账号
关注者
0
被浏览
9.4k
关于作者
dingliangzhu
这家伙很懒,什么也没写!
提问
2
回答
3
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
【NXP-MCXA153】 定时器驱动移植
2
GD32F450 看门狗驱动适配
3
【NXP-MCXA153】看门狗驱动移植
4
RT-Thread Studio V2.2.9 Release Note
5
CherryUSB的bootuf2配置
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
PWM
freemodbus
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
编译报错
Debug
rt_mq_消息队列_msg_queue
SFUD
keil_MDK
msh
ulog
C++_cpp
MicroPython
本月问答贡献
踩姑娘的小蘑菇
7
个答案
2
次被采纳
a1012112796
18
个答案
1
次被采纳
Ryan_CW
5
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
本月文章贡献
YZRD
3
篇文章
6
次点赞
catcatbing
3
篇文章
6
次点赞
lizimu
2
篇文章
9
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部