新手上路
- 积分
- 22
- 金钱
- 22
- 注册时间
- 2023-9-17
- 在线时间
- 3 小时
|
3金钱
定时器中断实验
只修改了led.c led.h部分,并删除了主函数中的原来的while(1)函数,其余是官方的,结果灯不闪烁,一直处于亮的状态
led.h
#ifndef __LED_H
#define __LED_H
#include"sys.h"
#define LED0 PAout(5)
#define LED1 PAout(6)
void LED_Init(void);
#endif
led.c
#include"led.h"
#include"sys.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_ResetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6);
}
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é2
delay_init(168); //³õʼ»¯ÑÓʱº¯Êý
LED_Init(); //³õʼ»¯LED¶Ë¿Ú
TIM3_Int_Init(5000-1,8400-1); //¶¨Ê±Æ÷ʱÖÓ84M£¬·ÖƵϵÊý8400£¬ËùÒÔ84M/8400=10KhzµÄ¼ÆÊýƵÂÊ£¬¼ÆÊý5000´ÎΪ500ms
}
timer.h
#ifndef _TIMER_H
#define _TIMER_H
#include "sys.h"
void TIM3_Int_Init(u16 arr,u16 psc);
#endif
timer.c
#include "timer.h"
#include "led.h"
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); ///ʹÄÜTIM3ʱÖÓ
TIM_TimeBaseInitStructure.TIM_Period = arr; //×Ô¶¯ÖØ×°ÔØÖµ
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //¶¨Ê±Æ÷·ÖƵ
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//³õʼ»¯TIM3
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //ÔÊÐí¶¨Ê±Æ÷3¸üÐÂÖжÏ
TIM_Cmd(TIM3,ENABLE); //ʹÄܶ¨Ê±Æ÷3
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //¶¨Ê±Æ÷3ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //ÇÀÕ¼ÓÅÏȼ¶1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//¶¨Ê±Æ÷3ÖжϷþÎñº¯Êý
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //Òç³öÖжÏ
{
LED1=!LED1;//DS1·­×ª
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //Çå³ýÖжϱê־λ
}
|
|