打印

为什么灯并没有一闪一灭,是没有中断,还是终端服务程序没写好?

[复制链接]
325|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
roucun|  楼主 | 2018-6-29 17:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
想写一个程序,每隔一秒,就进行中断,把LED灯打开,一秒之后,中断进来,熄灭LED灯,依次循环

main函数:
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "misc.h"

void RCC_Config(void)
{
        SystemInit();
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
}




void LED_GPIO_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;  //推挽输出
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOC,&GPIO_InitStructure);
}


void NVIC_Config(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
        
        NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStructure);        
}


void TIM3_Config()
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStruct;


        TIM_ClearITPendingBit(TIM3,TIM_IT_Update);


        TIM_TimeBaseStruct.TIM_Period=2000;
        TIM_TimeBaseStruct.TIM_Prescaler=35999;
        TIM_TimeBaseStruct.TIM_ClockDivision=0;
        TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;


        TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
        TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
        TIM_Cmd(TIM3,ENABLE);


}




int main(void)
{
        RCC_Config();
        LED_GPIO_Config();
        NVIC_Config();
        TIM3_Config();


        while(1) ;
         
}


中断服务程序:
void TIM3_IRQHandler(void)
{
        TIM_ClearITPendingBit(TIM3,TIM_IT_Update);

        if(GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_4)==Bit_RESET)
        {        
                 GPIO_SetBits(GPIOC,GPIO_Pin_4);            //熄灭
        }
               
        else
        {        
                GPIO_ResetBits(GPIOC,GPIO_Pin_4);   //发光
        }
}

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

421

主题

446

帖子

0

粉丝