stm32f103c8t6定时器1遇到的诡异问题
本帖最后由 ywfw30 于 2024-11-4 16:32 编辑<p>stmf103c8t6定时器1遇到的诡异问题:</p>
<div>
<div>1. 想通过定时器1产生1s的中断用作计时,</div>
<div>具体参数配置 TIM1_Int_Init(<b style=" overflow-wrap:break-word">9999</b>,7199);</div>
<div> </div>
<div>
<div>void TIM1_Int_Init(u16 arr,u16 psc)</div>
<div>{</div>
<div> TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;</div>
<div>NVIC_InitTypeDef NVIC_InitStructure;</div>
<div> </div>
<div>RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //时钟使能</div>
<div> </div>
<div>TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值</div>
<div>TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 </div>
<div>TIM_TimeBaseStructure.TIM_ClockDivision =<b style=" overflow-wrap:break-word"> 0</b>; //设置时钟分割:TDTS = Tck_tim</div>
<div>TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式</div>
<div>TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位</div>
<div> </div>
<div>TIM_ITConfig( //使能或者失能指定的TIM中断</div>
<div>TIM1, //TIM2</div>
<div>TIM_IT_Update ,</div>
<div>ENABLE //使能</div>
<div>);</div>
<div>NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM3中断</div>
<div>NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级</div>
<div>NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级</div>
<div>NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能</div>
<div>NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器</div>
<div> </div>
<div>TIM_Cmd(TIM1, ENABLE); //使能TIMx外设</div>
<div> </div>
<div>}</div>
</div>
<div> </div>
<div> </div>
<div>2.遇到的诡异问题是和PA0、PA1管脚有关</div>
<div>如果程序中不用PA0、PA1管脚,那么定时器可以稳定的产生1s中断</div>
<div>可是一旦要用到PA0、PA1管脚,定时器的中断时间突然变成了3s??????????</div>
<div> </div>
<div>用到PA0和PA1时的代码如下,而且是仅仅初始化一下就会影响定时器1</div>
<div>
<div> GPIO_InitTypeDef GPIO_InitStructure;</div>
<div> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能PORTA时钟</div>
<div> </div>
<div> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PA0</div>
<div> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PA0设置成输入,默认上拉 </div>
<div> GPIO_Init(GPIOA, &GPIO_InitStructure);</div>
<div> </div>
<div> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;//PA0</div>
<div> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PA0设置成输入,默认上拉 </div>
<div> GPIO_Init(GPIOA, &GPIO_InitStructure);</div>
</div>
<div> </div>
<div> </div>
<div>3.想不通PA0、PA1怎么影响到定时器1呢,感觉有点无厘头??</div>
</div>
<div>难道买到假的芯片了?不可能这种假法吧??</div>
<div> </div>
<div> </div>
<div> </div>
<div>
<div> </div>
</div>
<h1><a href="https://news.eeworld.com.cn/mcu/ic540747.html" target="_blank">STM32定时器配置,定时计数模式下总结</a></h1>
<p>芯片是真的,应该是代码的问题吧。可以看看时钟树和时钟相关的寄存器。看看哪里变了,为什么变了。</p>
<p><span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none">经高人指点,初始化0就好了。<img height="50" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/wanwan33.gif" width="58" /></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p><span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none">void TIM1_Int_Init(u16 arr,u16 psc)</span></span></span></span></span></span></span></span></span></span></span></span></span><br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none">{</span></span></span></span></span></span></span></span></span></span></span></span></span><br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none"> TIM_TimeBaseInitTypeDef </span></span></span></span></span></span></span></span></span></span></span></span></span><font color="Red"><font style="overflow-wrap:break-word; word-break:break-all; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial">TIM_TimeBaseStructure = {0};</font></font><br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none"> NVIC_InitTypeDef </span></span></span></span></span></span></span></span></span></span></span></span></span><font color="Red"><font style="overflow-wrap:break-word; word-break:break-all; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial">NVIC_InitStructure ={0};</font></font><br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none"> RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //时钟使能</span></span></span></span></span></span></span></span></span></span></span></span></span><br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none"> TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 </span></span></span></span></span></span></span></span></span></span></span></span></span><br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none"> TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 </span></span></span></span></span></span></span></span></span></span></span></span></span><br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none"> TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim</span></span></span></span></span></span></span></span></span></span></span></span></span><br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none"> TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式</span></span></span></span></span></span></span></span></span></span></span></span></span><br style="overflow-wrap:break-word; word-break:break-all; color:#444444; font-family:Tahoma, "Microsoft Yahei", Simsun; font-size:16px; font-style:normal; font-variant-ligatures:normal; font-weight:400; text-align:start; white-space:normal; background-color:#f2f9fd; text-decoration-thickness:initial; text-decoration-style:initial; text-decoration-color:initial" />
<span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, "Microsoft Yahei", Simsun"><span style="font-style:normal"><span style="font-variant-ligatures:normal"><span style="font-weight:400"><span style="white-space:normal"><span style="background-color:#f2f9fd"><span style="text-decoration-thickness:initial"><span style="text-decoration-style:initial"><span style="text-decoration-color:initial"><span style="display:inline !important"><span style="float:none"> TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位</span></span></span></span></span></span></span></span></span></span></span></span></span></p>
页:
[1]