由于刚接触STM,在使用定时器2中断点亮LED时出现定时器好像不起作用,而且打开定时器2的时钟,会使另外一个LED灯不能正常进行点亮与熄灭状态的切换,请问有没有遇到过类似问题的朋友,程序附载如下:
#include "stm32f10x.h"
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_IniStructure;
SPI_InitTypeDef SPI_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
ErrorStatus HSEStartUpStatus;
// vu8 i;
#define LED1 PAout(3)// PA5
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n)
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define GPIOA_ODR_Addr (GPIOA_BASE+12)
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
// __IO uint16_t CCR1_Val = 40961;
// __IO uint16_t CCR2_Val = 27309;
// __IO uint16_t CCR3_Val = 13654;
// __IO uint16_t CCR4_Val = 6826;
// uint16_t PrescalerValue = 0;
void delay(uint32_t ncount)
{
for(;ncount>>=0;ncount--);
}
void RCC_Configuration(void)
{
RCC_DeInit(); //3?ê??ˉRCC??′??÷
RCC_HSEConfig(RCC_HSE_ON);//?a??HSE??μ′?÷
HSEStartUpStatus=RCC_WaitForHSEStartUp();//μè′yHSE???ˉ
if(HSEStartUpStatus==SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//?¤′??o3????êDí
FLASH_SetLatency(FLASH_Latency_2);//′ú??2???óê±?ü?ú
RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHBê±?óμèóúSYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //APB2ê±?óμèóúHCLK
RCC_PCLK1Config(RCC_HCLK_Div1); //APB1ê±?óμèóúHCLK/2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//PLLê±?óê?è?μèóúHSEê±?óμ?9±??μ
RCC_PLLCmd(ENABLE);//?êDíPLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//μè′yPLLê±?ó×?±??íD÷
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//????SYSCLOCKê±?ó?aPLLê±?ó
while(RCC_GetSYSCLKSource()!=0x08);//μè′yPLL×÷?a?μí3ê±?ó
}
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifndef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x00);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x00);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ó??è???£ê?£??è??ó??è??2??£?′óó??è??2??
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;//?????¨ê±?÷2?D??
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//ê1?ü?D??
NVIC_Init(&NVIC_InitStructure);
}
void TIM_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//′ò?a?¨ê±?÷2μ?ê±?ó
TIM_DeInit(TIM2);//?′??TIM2?¨ê±?÷
TIM_TimeBaseStructure.TIM_Period=2000-1;//éè???ú??ò????üD?ê??t×°è????ˉμ?×??ˉ??×°????′??÷?ü?úμ??μ
TIM_TimeBaseStructure.TIM_Prescaler=36000-1;//éè??ó?à′×÷?aTIMxê±?ó?μ?ê3yêyμ??¤·??μ?μ
TIM_TimeBaseStructure.TIM_ClockDivision= TIM_CKD_DIV1; //éè??ê±?ó·???:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//??êy?÷·??ò?òé???êy
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);//??3yTIMò?3??D??±ê??
TIM_ARRPreloadConfig(TIM2,ENABLE);//ê1?üTIM2?úARRé?μ??¤×°????′??÷
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//ò?3??D???êDí
TIM_Cmd(TIM2,ENABLE);//?êDíTIM2??êy
}
void GPIO_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
int main(void)
{
SystemInit();
RCC_Configuration();
NVIC_Configuration();
TIM_Configuration();
//RCC_MCOConfig(RCC_MCO_PLLCLK_Div2);//2aê?ê±?óμ?ê?3?????
GPIO_Configuration();
GPIO_SetBits(GPIOB,GPIO_Pin_2);//
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_3);//
//GPIO_ResetBits(GPIOA,GPIO_Pin_5);//
//LED1=!LED1;//
// if(i==20)
// GPIO_ResetBits(GPIOB,GPIO_Pin_2);
delay(0x7FFFFF);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
delay(0x7FFFFF);
}
}
void TIM2_IRQHandler(void)
{
static vu8 i;
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
// i++;
i=!i;
if(i)
GPIO_ResetBits(GPIOB,GPIO_Pin_2);
else
GPIO_SetBits(GPIOB,GPIO_Pin_2);
}
}
|