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日

悬赏问题

  • ¥15 为什么apriori的运行时间会比fp growth的运行时间短呢
  • ¥15 帮我解决一下膳食平衡的线性规划模型的数据实例
  • ¥40 万年历缺少农历,需要和阳历同时显示
  • ¥250 雷电模拟器内存穿透、寻基址和特征码的教学
  • ¥200 比特币ord程序wallet_constructor.rs文件支持一次性铸造1000个代币,并将它们分配到40个UTXO上(每个UTXO上分配25个代币),并设置找零地址
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"