初级会员

- 积分
- 125
- 金钱
- 125
- 注册时间
- 2018-6-22
- 在线时间
- 21 小时
|
1金钱
# include <stm32f10x.h>
# include <sys.h>
# include <delay.h>
# define LED0 PBout(5)
//# define LED1 PEout(5)
// 初始化TIM
//void delay (void);
static int i=4999,j=0;
void TIM(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3 ,ENABLE);
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=arr;
TIM_TimeBaseInitStruct.TIM_Prescaler=psc;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
TIM_ITConfig (TIM3,TIM_IT_Update, ENABLE);//使能更新中断
TIM_Cmd (TIM3,ENABLE);//使能TIM3
//中断优先级初始化
NVIC_InitStruct.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd= ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=3;
NVIC_Init (&NVIC_InitStruct);
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);
}
void TIM3_IRQHandler (void)//中断服务函数
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
if (j==0)
{
i+=100;
if (i>9900)
j=1;
}
if (j==1)
{
i-=100;
if (i<4999)
j=0;
}
TIM_TimeBaseInitStruct.TIM_Period=i;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
if(TIM_GetITStatus (TIM3,TIM_IT_Update)==SET)
{
TIM_ClearITPendingBit (TIM3,TIM_IT_Update);
LED0=!LED0;
}
}
//GPIO口初始化
void GPIO_intInit (void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB ,ENABLE);
GPIO_InitStruct.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin= GPIO_Pin_5;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init ( GPIOB,& GPIO_InitStruct);
GPIO_SetBits (GPIOB, GPIO_Pin_5);
}
int main (void)
{
//delay();
TIM (i,7199);
GPIO_intInit ();
while (1);
}
这个程序不能实现小灯闪烁速度越来越快吗?为什么灯一直亮啊
|
|