初级会员

- 积分
- 125
- 金钱
- 125
- 注册时间
- 2018-6-22
- 在线时间
- 21 小时
|
# include <stm32f10x.h>
# include <sys.h>
# include <delay.h>
# define LED0 PBout(5)
# define LED1 PEout(5)
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;
static int i=4999,j=0;
if (j==0)
{
i-=1000;
if (i<1999)
j=1;
}
if (j==1)
{
i+=1000;
if (i>4999)
j=0;
}
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Prescaler=7199;
TIM_TimeBaseInitStruct.TIM_Period=i;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
TIM_Cmd (TIM3,ENABLE);
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);
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOE ,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_InitStruct.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin= GPIO_Pin_5;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init ( GPIOE,& GPIO_InitStruct);
GPIO_SetBits (GPIOB, GPIO_Pin_5);
GPIO_SetBits (GPIOE, GPIO_Pin_5);
}
int main (void)
{
TIM (4999,7199);
GPIO_intInit ();
delay_init ();
while (1)
{
LED1=!LED1;
delay_ms (500);
}
}
这个程序为什么不能实现LED0越闪越快啊?
|
|