中级会员
 
- 积分
- 414
- 金钱
- 414
- 注册时间
- 2014-11-15
- 在线时间
- 75 小时
|
5金钱
#include "stm32f10x.h"
//开启时钟注意APB1还是APB2
void RCC_cfg(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
}
//配置计时器
void TIM_cfg()
{
TIM_TimeBaseInitTypeDef TIM_TimebaseInitStructure;
TIM_DeInit(TIM2);
TIM_InternalClockConfig(TIM2);//把内部时钟配置给TIM2;
TIM_TimebaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimebaseInitStructure.TIM_Period = 2000-1;
TIM_TimebaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up;
TIM_TimebaseInitStructure.TIM_Prescaler =36000-1;
TIM_TimebaseInitStructure.TIM_RepetitionCounter = 0;//重复计数溢出 X次才中断 0=1次
TIM_TimeBaseInit(TIM2,&TIM_TimebaseInitStructure);
}
//配置中断
void NVIC_cfg()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_1);//先占和从占分配
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//配置GPIO 输出为推挽式
void GPIO_Cfg()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_DeInit(GPIOA);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void TIM2_IRQHandler()
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
}
//主函数
int main()
{
SystemInit();
RCC_cfg();
TIM_cfg();
NVIC_cfg();
GPIO_Cfg();
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
//开启TIM2计时器
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2,ENABLE);
while(1);
}
GPIO_Cfg();这句话被执行后
IDR=0x00005EFF
ODR=0x0000A000 |
|