主函数中是一个蓝 {MOD}LED灯常亮,当定时器溢出后,跳到中断函数另一个红 {MOD}LED灯亮。想实现两个LED灯交替闪亮。结果开始蓝灯亮,接着红灯亮,但以后就红灯一直亮了。。。
程序中有个SysTick做延时用,可以忽略
程序如下:
主函数
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_conf.h"
static volatile uint32_t TimingDelay;
void RCC_Configuration(void) //时钟配置函数
{
ErrorStatus HSEStartUpStatus;
//使能外部晶振
RCC_HSEConfig(RCC_HSE_ON);
//等待外部晶振稳定
HSEStartUpStatus = RCC_WaitForHSEStartUp();
//如果外部晶振启动成功,则进行下一步操作
if(HSEStartUpStatus==SUCCESS)
{
//设置HCLK(AHB时钟)=SYSCLK 将系统时钟进行分频后,作为AHB总线时钟
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//PCLK1(APB1) = HCLK/2 将HCLK时钟2分频后给低速外部总线
RCC_PCLK1Config(RCC_HCLK_Div2);
//PCLK2(APB2) = HCLK HCLK时钟配置给高速外部总线
RCC_PCLK2Config(RCC_HCLK_Div1);
//外部高速时钟HSE 4倍频
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_4);
//启动PLL
RCC_PLLCmd(ENABLE);
//等待PLL稳定
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//系统时钟SYSCLK来自PLL输出
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//切换时钟后等待系统时钟稳定
while(RCC_GetSYSCLKSource()!=0x08);
}
//下面这些都是外设总线上所挂的外部设备时钟的配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
}
void systick_Configuration(void)
{
// SystemInit();
if (SysTick_Config(SystemCoreClock / 1000))
{
while (1);
}
}
void Delay(u32 nTime)
{
TimingDelay=nTime;
while(TimingDelay != 0);
}
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
void GPIO_Configuration(void) //GPIO配置函数
{
GPIO_DeInit(GPIOA);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_DeInit(GPIOB);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void TIM2_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* 启动 TIM2 的时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
/* 使能 TIM2 中断 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* TIM2 配置 */
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period = 1999;
TIM_TimeBaseStructure.TIM_Prescaler = 15999;//分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM2, ENABLE);
//TIM_PrescalerConfig(TIM2,1600, TIM_PSCReloadMode_Update );
/* 使能中断 */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* 使能 TIM2 */
TIM_Cmd(TIM2, ENABLE);
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
systick_Configuration();
TIM2_Config();
while(1)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
}
中断函数部分代码:
void Delay();
void TIM2_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM2 , TIM_IT_Update);
//TIM_ClearFlag(TIM2, TIM_IT_Update);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
Delay(500);
}
此帖出自
小平头技术问答
一周热门 更多>