ywfw30 发表于 2024-11-4 15:42

stm32f103c8t6定时器1遇到的诡异问题

本帖最后由 ywfw30 于 2024-11-4 16:32 编辑

<p>stmf103c8t6定时器1遇到的诡异问题:</p>

<div>&nbsp;
<div>1.&nbsp; 想通过定时器1产生1s的中断用作计时,</div>

<div>具体参数配置 TIM1_Int_Init(<b style=" overflow-wrap:break-word">9999</b>,7199);</div>

<div>&nbsp;</div>

<div>
<div>void TIM1_Int_Init(u16 arr,u16 psc)</div>

<div>{</div>

<div>&nbsp; TIM_TimeBaseInitTypeDef&nbsp; TIM_TimeBaseStructure;</div>

<div>NVIC_InitTypeDef NVIC_InitStructure;</div>

<div>&nbsp;</div>

<div>RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //时钟使能</div>

<div>&nbsp;</div>

<div>TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值</div>

<div>TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值&nbsp;&nbsp;</div>

<div>TIM_TimeBaseStructure.TIM_ClockDivision =<b style=" overflow-wrap:break-word">&nbsp;0</b>; //设置时钟分割:TDTS = Tck_tim</div>

<div>TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;&nbsp; //TIM向上计数模式</div>

<div>TIM_TimeBaseInit(TIM1, &amp;TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位</div>

<div>&nbsp;</div>

<div>TIM_ITConfig(&nbsp; //使能或者失能指定的TIM中断</div>

<div>TIM1, //TIM2</div>

<div>TIM_IT_Update ,</div>

<div>ENABLE&nbsp; //使能</div>

<div>);</div>

<div>NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;&nbsp; //TIM3中断</div>

<div>NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;&nbsp; //先占优先级0级</div>

<div>NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;&nbsp; //从优先级3级</div>

<div>NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能</div>

<div>NVIC_Init(&amp;NVIC_InitStructure);&nbsp; //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器</div>

<div>&nbsp;</div>

<div>TIM_Cmd(TIM1, ENABLE);&nbsp; //使能TIMx外设</div>

<div>&nbsp;</div>

<div>}</div>
</div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div>2.遇到的诡异问题是和PA0、PA1管脚有关</div>

<div>如果程序中不用PA0、PA1管脚,那么定时器可以稳定的产生1s中断</div>

<div>可是一旦要用到PA0、PA1管脚,定时器的中断时间突然变成了3s??????????</div>

<div>&nbsp;</div>

<div>用到PA0和PA1时的代码如下,而且是仅仅初始化一下就会影响定时器1</div>

<div>
<div>&nbsp;GPIO_InitTypeDef GPIO_InitStructure;</div>

<div>&nbsp;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能PORTA时钟</div>

<div>&nbsp;</div>

<div>&nbsp;GPIO_InitStructure.GPIO_Pin&nbsp; = GPIO_Pin_0;//PA0</div>

<div>&nbsp;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PA0设置成输入,默认上拉 &nbsp;&nbsp;</div>

<div>&nbsp;GPIO_Init(GPIOA, &amp;GPIO_InitStructure);</div>

<div>&nbsp;</div>

<div>&nbsp;GPIO_InitStructure.GPIO_Pin&nbsp; = GPIO_Pin_1;//PA0</div>

<div>&nbsp;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PA0设置成输入,默认上拉 &nbsp;&nbsp;</div>

<div>&nbsp;GPIO_Init(GPIOA, &amp;GPIO_InitStructure);</div>
</div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div>3.想不通PA0、PA1怎么影响到定时器1呢,感觉有点无厘头??</div>
</div>

<div>难道买到假的芯片了?不可能这种假法吧??</div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div>
<div>&nbsp;</div>
</div>

tagetage 发表于 2024-11-4 22:11

<h1><a href="https://news.eeworld.com.cn/mcu/ic540747.html" target="_blank">STM32定时器配置,定时计数模式下总结</a></h1>

damiaa 发表于 2024-11-5 08:57

<p>芯片是真的,应该是代码的问题吧。可以看看时钟树和时钟相关的寄存器。看看哪里变了,为什么变了。</p>

ywfw30 发表于 2024-11-6 09:18

<p><span style="font-size:16px; text-align:start"><span style="color:#444444"><span style="font-family:Tahoma, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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">&nbsp;&nbsp;TIM_TimeBaseInitTypeDef&nbsp;&nbsp;</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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;NVIC_InitTypeDef&nbsp;</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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值&nbsp;&nbsp;</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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;&nbsp;&nbsp;//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, &quot;Microsoft Yahei&quot;, 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, &quot;Microsoft Yahei&quot;, 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">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;TIM_TimeBaseInit(TIM1, &amp;TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位</span></span></span></span></span></span></span></span></span></span></span></span></span></p>
页: [1]
查看完整版本: stm32f103c8t6定时器1遇到的诡异问题