STM32F10X定时器

时间:2015-02-10 21:46:54

标签: arm interrupt stm32 cortex-m3

我的stm32f10控制器的定时器中断问题。 当我启用定时器的计数器时,状态寄存器设置为 0x1F,因为设置了以下标志:UIF,CC1IF,CC2IF,CC3IF,CC4IF

这就是我所做的:

#include <Compiler.h>
#include <Debug.h>
#include <STM32F10xxx.h>

#include <Reset.h>
#include "Example.h"
#include <HW_TIM.h>
#include <HW_NVIC.h>
#include <HW_RCC.h>
#include <RegBit.h>

unsigned volatile int sr =0;

static void SetBASEPRI(WORD const basepriVal) {
  register WORD basepriReg __asm("basepri");

    basepriReg = basepriVal;
}

static void SetCONTROL(WORD const controlVal) {
  register WORD controlReg __asm("control");

    controlReg = controlVal;
}

static void MainInit(void) {
 STM32F10xxx_Config();
 SetBASEPRI(0U);

 PeripheryEnable(RCC_TIM2);

 SET_RBINDEX_REG_BIT(TIM, 2, DIER, UIE);

 SET_RBINDEX_REG_BIT(TIM, 2, DIER, CC1IE);
 SET_RBINDEX_REG_BIT(TIM, 2, DIER, CC2IE);
 SET_RBINDEX_REG_BIT(TIM, 2, DIER, CC3IE);
 SET_RBINDEX_REG_BIT(TIM, 2, DIER, CC4IE);

 InterruptEnable(NVIC_TIM2);

 SetCONTROL(1U);
}

void timInit(void){

 TIM2.PSC = 0x0024;
 TIM2.ARR = 0xFFFF;

 TIM2.CCR[0] = 0x00FF; 
 TIM2.CCR[1] = 0x0A00; 
 TIM2.CCR[2] = 0x0F00; 
 TIM2.CCR[3] = 0xF000;

 SET_RBINDEX_REG_BIT(TIM, 2, CR1, CEN);

}

__declspec(noreturn) int main() {

  MainInit();

  timInit();

  while(1);
}

void IRQ_TIM2(void){
 //Delet IFs in SR
 //trying to clear SR by reading the register
 sr = TIM2.SR;
 sr= GET_RBINDEX_REG_BIT(TIM,2,SR,UIF);

 //trying to clear SR by resetting the single bits
 RES_RBINDEX_REG_BIT(TIM, 2, SR, CC1IF);
 RES_RBINDEX_REG_BIT(TIM, 2, SR, CC2IF);
 RES_RBINDEX_REG_BIT(TIM, 2, SR, CC3IF);
 RES_RBINDEX_REG_BIT(TIM, 2, SR, CC4IF);
 RES_RBINDEX_REG_BIT(TIM, 2, SR, UIF);

 //trying to clear SR bei assigning 0 to SR
 TIM2.SR=0;

}

void IRQ_SysTick(void){
}

void IRQ_HrdFault(void) {   for ( ;; ) DEBUG_BREAK(1); }
void IRQ_UsgFault(void) {   for ( ;; ) DEBUG_BREAK(1); }
void IRQ_BusFault(void) {   for ( ;; ) DEBUG_BREAK(1); }
void IRQ_MemFault(void) {   for ( ;; ) DEBUG_BREAK(1); }
void IRQ_NMI(void)      {   for ( ;; ) DEBUG_BREAK(1); }

正如我所说的,问题是当设置Counter-Enable-Bit(CEN)时所有标志都被置位。我无法在子程序中删除它们。

1 个答案:

答案 0 :(得分:0)

这是一个迟到的答案,但我今天遇到了类似的问题,我认为这可以帮助其他人。首先我怀疑问题是设置或未设置标志UIF,CC1IF,CC2IF,CC3IF,CC4IF。如果您查看参考手册在页面403上写的那些标志是由硬件设置的,那么当您重置它们时,硬件(即使在您正在调试时也在运行)将自动设置它们,您甚至不会注意到结果。如果你想看到用调试器清除标志,我建议禁用定时器,然后执行一些操作。另外,建议的样式是检查IRQ中的中断源:

//comment the next line before release
#define DEBUG    
void IRQ_TIM2(void){
 //disable the timer for debug purpose
 #ifdef DEBUG
    TIM2->CR1 &= ~TIM_CR1_CEN;
 #endif
 //trying to clear SR by reading the register
 if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //clearing UIF
 } 
 if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) {
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //clearing CCIF
 } 
 //etc...
 //enable the timer
 #ifdef DEBUG
   TIM2->CR1 |= TIM_CR1_CEN;
 #endif
}