static void BSP_NVICConfiguration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //抢占式优先级别设置为无抢占优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn ; //指定中断源
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //指定响应优先级别1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道3
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn ; //指定中断源
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //指定响应优先级别1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn ; //指定中断源
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //指定响应优先级别1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure);
}
static void BSP_LedInit(void)
{
GPIO_InitTypeDef gpio_init;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
gpio_init.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 |GPIO_Pin_14 ;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &gpio_init);
gpio_init.GPIO_Pin = GPIO_Pin_15;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &gpio_init);
GPIO_SetBits(GPIOB, GPIO_Pin_14);
GPIO_ResetBits(GPIOB, GPIO_Pin_12|GPIO_Pin_13);
}
static void Tim1_Configuration(void)
{TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1); //重设为缺省值
RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, ENABLE);
/*TIM1时钟配置*/
TIM_TimeBaseStructure.TIM_Prescaler = 36000; //psc预分频(时钟分频)72M/36k=2K
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_Period = 1000; //arr 装载值 2k/1k=2hz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置了时钟分割 不懂得不管
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //初始化TIMx的时间基数单位
/* Channel 1 Configuration in PWM mode 通道一的PWM */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效
TIM_OCInitStructure.TIM_Pulse = 40; //占空时间
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //空闲状态下的非工作状态
TIM_OC3Init(TIM1,&TIM_OCInitStructure); //数初始化外设TIMx通道1
TIM_ClearFlag(TIM1, TIM_FLAG_Update); //清除溢出中断标志
TIM_ARRPreloadConfig(TIM1, DISABLE); //禁止ARR预装载缓冲器
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //TIM1 Interrupt enable
//TIM_Cmd(TIM1,ENABLE); //TIM1 Main Output Enable 使能TIM1外设的主输出
TIM_CtrlPWMOutputs(TIM1,ENABLE); //TIM1 PWM Output Enable 使能TIM1外设的主输出
}
/*******************************************************************************
* Function Name : Task_TIM1
* Description : TIM任务
* Input : None
* Output : None
* Return : None
*******************************************************************************/
static void App_Task_TIM1(void* p_arg)
{
(void) p_arg ;
INT8U err;
Sem_Task_TIM1=OSSemCreate(0);
while(1)
{
OSSemPend(Sem_Task_TIM1,0,&err);// waiting for SEMAPHORE
ClockNum=0;
TIM_Cmd(TIM1,DISABLE);
GPIO_ResetBits(GPIOB, GPIO_Pin_14);
OSTimeDlyHMSM(0,0,1,0);
GPIO_SetBits(GPIOB, GPIO_Pin_14);
OSTimeDlyHMSM(0,0,1,0);
}
}
void TIM1_UP_IRQHandler(void)
{
OSIntEnter();
ClockNum++;
if(ClockNum==1)
{
OSSemPost(Sem_Task_TIM1); // 发送信号量,这个函数并不会引起系统调度,所以中断服务函数一定要简洁。
}
TIM_ClearITPendingBit(TIM1,TIM_IT_Update); // 清除标志位
OSIntExit();
}