1

我正在尝试生成占空比为 50% 的 2MHz PWM。我的问题是我无法清除中断标志。这是我的代码:

#include "includes.h"

TIM_TimeBaseInitTypeDef TIM1_InitStruncture;

TIM_TimeBaseInitTypeDef TIM3_InitStruncture;
TIM_OCInitTypeDef       TIM3_OCInitStructure;

SPI_InitTypeDef         SPI_InitStructure;

void Timer3_IRQHandler(void)
{
  if(TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
  {
    TIM_ClearFlag(TIM3, TIM_IT_CC3);
    //dummy code
    ++StatusReg;
  }
}

void CLK_init()
{
  //activez HSI
  RCC_HSICmd(ENABLE);
  //astepst sa se activeze HSI
  while( RCC_GetFlagStatus( RCC_FLAG_HSIRDY) == RESET );
  //setez HSI ca sursa de clock
  RCC_SYSCLKConfig( RCC_SYSCLKSource_HSI );

  //activez HSE
  RCC_HSEConfig( RCC_HSE_ON );
  //astept sa se termine secventa de activare
  while( RCC_GetFlagStatus( RCC_FLAG_HSERDY) == RESET );

  //setez HSE (8MHz) ca input py PLL
  //setez factotul de multiplicare 9
  RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );

  //activez PLL-ul
  RCC_PLLCmd(ENABLE);

  //astept sa se termine secventa de activare
  while( RCC_GetFlagStatus( RCC_FLAG_PLLRDY) == RESET );

  #ifdef EMB_FLASH
  // 5. Init Embedded Flash
  // Zero wait state, if 0 < HCLK 24 MHz
  // One wait state, if 24 MHz < HCLK 56 MHz
  // Two wait states, if 56 MHz < HCLK 72 MHz
  // Flash wait state
  FLASH_SetLatency(FLASH_Latency_2);
  // Half cycle access
  FLASH_HalfCycleAccessCmd(FLASH_HalfCycleAccess_Disable);
  // Prefetch buffer
  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  #endif // EMB_FLASH 

  //setez iesirea de la PLL ca sursa de CLK
  RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );  
}

void Port_C_Enable()
{ 
  //GPIO_InitTypeDef    GPIOC_InitStructure; 

  //resetez portul C (just in case)
  RCC->APB2RSTR |= RCC_APB2RSTR_IOPCRST;
  RCC->APB2RSTR &= ~RCC_APB2RSTR_IOPCRST;

  //activez CLK-ul pentru portul C
  RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
  /*
  GPIOC_InitStructure.GPIO_Pin      = GPIO_Pin_5;
  GPIOC_InitStructure.GPIO_Speed    = GPIO_Speed_50MHz;
  GPIOC_InitStructure.GPIO_Mode     = GPIO_Mode_Out_PP;

  GPIO_Init(GPIOC, &GPIOC_InitStructure);
  */
}

void Timer3_Init()
{
  NVIC_InitTypeDef    NVIC_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  //reset Timer3 (just in case)  
  //RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST;
  //RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST;

  //give clock to Timer-ul 3
  RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;

  //frequency 2Mhz
  TIM3_InitStruncture.TIM_Period              = 36;
  TIM3_InitStruncture.TIM_Prescaler           = 0;
  TIM3_InitStruncture.TIM_ClockDivision       = 0;//TIM_CKD_DIV1;
  TIM3_InitStruncture.TIM_CounterMode         = TIM_CounterMode_CenterAligned3;

  TIM_TimeBaseInit(TIM3, &TIM3_InitStruncture);

  TIM_ITConfig(TIM3, TIM_IT_CC3, ENABLE);

  TIM_Cmd(TIM3, ENABLE);

  //dutycicle 50%
  TIM3_OCInitStructure.TIM_OCMode         = TIM_OCMode_PWM2;
  TIM3_OCInitStructure.TIM_OCPolarity     = TIM_OCPolarity_High;
  TIM3_OCInitStructure.TIM_OutputState    = TIM_OutputState_Enable;
  TIM3_OCInitStructure.TIM_Pulse          = 18;

  TIM_OC3Init(TIM3, &TIM3_OCInitStructure);
  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM3, ENABLE);
  TIM_Cmd(TIM3, ENABLE);
}

void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;*/

  //GPIO_Init(GPIOC, &GPIO_InitStructure);

  GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);     

  /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void NVIC_init(void)
{
     // NVIC init
  #ifndef  EMB_FLASH
    /* Set the Vector Table base location at 0x20000000 */
    NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  #else  /* VECT_TAB_FLASH  */
    /* Set the Vector Table base location at 0x08000000 */
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  #endif
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
}

void SPI_init()
{  
  SPI_InitStructure.SPI_Direction           = SPI_Direction_1Line_Tx;
  SPI_InitStructure.SPI_Mode                = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize            = SPI_DataSize_16b;
  SPI_InitStructure.SPI_CPOL                = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA                = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS                 = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler   = SPI_BaudRatePrescaler_16;
  SPI_InitStructure.SPI_FirstBit            = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial       = 0;

  SPI_Init(SPI2, &SPI_InitStructure);
}

void main(void)
{  
  #ifdef DEBUG
   debug();
#endif

//  NVIC_SETPRIMASK();

  CLK_init();

  NVIC_init();

  Port_C_Enable();
  GPIO_Configuration();

  //Timer1_Init();
  Timer3_Init();

  TIM_Cmd(TIM1, ENABLE);

  unsigned int j=0;
  while(1)
  {
    //dummy code
    ++j;
    if(j == 0xff)
    {
      j=0;
    }
  }
}

谁能告诉我为什么 CCR3(捕获/比较寄存器 3 标志)保持高电平?

谢谢。

4

2 回答 2

1

注意不要混合以下内容:

  • TIM_ClearFlag 应与 TIM_FLAG_CC1 一起使用
  • TIM_ClearITPendingBit 应与 TIM_IT_CC1 一起使用

据我所理解。否则,您可能会使用一些掩码来避免设置标记。

于 2013-12-18T10:04:09.080 回答
0

感谢 aamxip 的回答。

我发现了问题。看来配置是正确的,但我没有足够的时间来执行 ISR(中断服务程序)。每 36 个 CLK 产生一个新的中断,我需要大约 30 条或更多指令才能进入 ISR。经过更多研究后,我发现我并不真正需要那个频率,我采用了一种不同的方法,bit-banging。

于 2014-01-06T09:37:57.313 回答