我自己写了一个
tiM控制灯闪烁的小程序,是选用的
STM32l151c8t6的
芯片,PB10灯是由for循环控制灯的闪烁,PB11引脚连接的灯由TIM2定时器控制,定时为一秒,现不懂定时器该如何去翻转PB11的电平,请各位大神指教,一下是我写的代码:
/*main.c*/
int main(void)
{
int i;
GPIO_Configuration();
NVIC_Configuration();
TIM_Configuration();
GPIO_WriteBit(GPIOB,GPIO_Pin_10,Bit_RESET);
GPIO_WriteBit(GPIOB,GPIO_Pin_11,Bit_RESET);
while(1)
{
GPIO_WriteBit(GPIOB,GPIO_Pin_10,(BitAction)0x00);
for(i = 0;i < 1000000;i++);
GPIO_WriteBit(GPIOB,GPIO_Pin_10,(BitAction)0x01);
for(i = 0;i < 1000000;i++);
}
}
void GPIO_Configuration(void)//配置GPIOB端口
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
//PB10为灯1控制端,由for循环控制闪烁
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//PB11为灯2控制端,由TIM2控制控制闪烁
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
//TIM2的配置,在这只选用一个TimeBaseInit设置
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure;
// TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_DeInit(TIM2);
TIM_InternalClockConfig(TIM2);
TIM_TimeBaseInitStructure.TIM_Prescaler = 35999;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_Period = 1999;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM2, DISABLE);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
再加上TIM定时器后,main.c中的while(1)不能实现无限循环,而且PB11的灯一直为亮,无法实现闪烁,请各位大神指教指教啊。
跪求!!!
我也在为中断函数和PB11的反转功能纠结呢,能说一下这两方面的具体函数怎么写吗?没思路啊
一周热门 更多>