[mw_shl_code=c,true]#include "stm32f10x.h"
#include "TIMBASE.h"
#include "LED.h"
#include "USART_CONFIG.h"
volatile u32 time = 0;
int main()
{
LED_PB5_Config();
USARTx_config();
TIM2_Config();
TIM2_NVIC_Config();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
while(1)
{
if(time == 1000)
{
LED_TOGGLE(GPIOB,GPIO_Pin_5);
time = 0;
}
}
}
void TIM2_IRQHandler(void)
{
if ( TIM_GetITStatus( TIM2, TIM_IT_Update) != RESET )
{
time++;
TIM_ClearITPendingBit(TIM2 , TIM_FLAG_Update);
}
}
[/mw_shl_code]
上面是主函数的程序;
[mw_shl_code=c,true]void USARTx_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*USARTx???±??????*/
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART1, USART_FLAG_TC);
}
[/mw_shl_code]
上面是串口配置的程序;
[mw_shl_code=c,true]#include "TIMBASE.h"
void TIM2_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
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);
}
void TIM2_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,DISABLE);
}[/mw_shl_code]
上面是关于定时器设置的程序; |