m0_51371841 2022-03-05 19:24 采纳率: 100%
浏览 83
已结题

定时器0(1ms)无法进入中断

我使用的片子是gd32f103c8t6,例程中有的定时器2(1ms)中断例程测试后是可以运行输出的,但我把所有参数换成定时器0时无法输出。以下时我改写的例程。看到您的博文感觉您对嵌入式十分了解,这个问题困扰我几天了,希望您在闲暇之余能有时间帮我解答疑惑,感谢您了。

#include "gd32e23x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"

void delay(int time)
{
while(time--);

return;

}

void gpio_config(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_4);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);

}

void nvic_config(void)
{
nvic_irq_enable(TIMER0_Channel_IRQn, 0);
timer_interrupt_enable(TIMER0, TIMER_INT_UP);
}
void timer_config(void)
{
/* -----------------------------------------------------------------------
TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
TIMER1CLK = SystemCoreClock / 72 = 1MHz
TIMER1 channel 2 duty cycle = 50%
----------------------------------------------------------------------- */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;

rcu_periph_clock_enable(RCU_TIMER0);

timer_deinit(TIMER0);

/* TIMER1 configuration */
timer_initpara.prescaler         = 71;
timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection  = TIMER_COUNTER_UP;
timer_initpara.period            = 999;
timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER0,&timer_initpara);


/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER0);
/* auto-reload preload enable */
timer_enable(TIMER0);

}
int main(void)
{ rcu_ahb_clock_config(RCU_AHB_CKSYS_DIV1);
systick_config();
gpio_config();
nvic_config();
timer_config();

while(1)
{

}

}
之后时中断源文件的编写/**********************************************************************************************************************
/*!
\file gd32e23x_it.c
\brief interrupt service routines

\version 2019-02-19, V1.0.0, firmware for GD32E23x

*/

/*
Copyright (c) 2019, GigaDevice Semiconductor Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 

are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this 
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, 
   this list of conditions and the following disclaimer in the documentation 
   and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors 
   may be used to endorse or promote products derived from this software without 
   specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 

AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "gd32e23x_it.h"
#include "main.h"
#include "systick.h"

/*!
\brief this function handles NMI exception
\param[in] none
\param[out] none
\retval none
*/
void NMI_Handler(void)
{
}

/*!
\brief this function handles HardFault exception
\param[in] none
\param[out] none
\retval none
/
void HardFault_Handler(void)
{
/
if Hard Fault exception occurs, go to infinite loop */
while (1){
}
}

/*!
\brief this function handles SVC exception
\param[in] none
\param[out] none
\retval none
*/
void SVC_Handler(void)
{
}

/*!
\brief this function handles PendSV exception
\param[in] none
\param[out] none
\retval none
*/
void PendSV_Handler(void)
{
}

/*!
\brief this function handles SysTick exception
\param[in] none
\param[out] none
\retval none
*/
void SysTick_Handler(void)
{
delay_decrement();
}

uint32_t Timcounter;
uint8_t LedFlag;
void TIMER0_Channel_IRQHandler(void)
{
if(timer_flag_get(TIMER0,TIMER_FLAG_UP)!=RESET)
{
timer_flag_clear(TIMER0,TIMER_FLAG_UP);
Timcounter++;
if(Timcounter>=1)
{
Timcounter=0;
LedFlag^=0x01;

  }
if(LedFlag)
 {
    gpio_bit_set(GPIOB,GPIO_PIN_4);
 }else
 {
    gpio_bit_reset(GPIOB,GPIO_PIN_4);
}

}
}

  • 写回答

1条回答 默认 最新

  • Justice_Gao 2022-03-06 17:09
    关注

    首先,确定开发板上的8Mhz晶振X3和匹配电容有没有焊接,我自己焊接了外部晶振,然后设置外部晶振作为系统时钟源

    img

    配置T0中断定时100ms,

    img

    T0定时中断函数如下,

    img

    按照这个配置看一下 这是我自己的代码。实际测试有效

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 已采纳回答 3月7日
  • 创建了问题 3月5日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改