stm32__标准库__前后台系统

#include "stm32f10x.h"                  // Device header
#include "user_gpio_init.h"
#include "user_time.h"
#include"user_pwm.h"
//前后台变量
#define fore_background_flag1_delayms 200
static int fore_background_count=1000000;
static unsigned char fore_background_flag1=0;
//前后台变量end

void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update))
{
    
		//前台程序计时
	  if(fore_background_count<=0)
		  fore_background_count=10000;
    else
	    fore_background_count--;
//前台程序计时end

//前台程序1标志位转换
		if(fore_background_count%fore_background_flag1_delayms==0)
			fore_background_flag1=1;
		else 
			fore_background_flag1=0;
//前台程序1标志位转换end                                                                

}

TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除中断标志位
}
//前后台函数







int main(void)
{
  unsigned char i=0;
	user_timer_init();
	user_gpio_init();

	
while(1)
 {

	 
//前台程序1
  if(fore_background_flag1==1)
  {

     user_pwm_pulse_change(50);
	
	fore_background_flag1=0;
  }
//前台程序1end
 

 
 }
}

#include "stm32f10x.h"                  // Device header
void user_timer_init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_InternalClockConfig (TIM2);
	
	TIM_TimeBaseInitTypeDef TIM_Timebaseinitstructure;
	TIM_Timebaseinitstructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_Timebaseinitstructure.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_Timebaseinitstructure.TIM_Period=10-1;
	TIM_Timebaseinitstructure.TIM_Prescaler=7200-1;
	TIM_Timebaseinitstructure.TIM_RepetitionCounter=0;
	TIM_TimeBaseInit(TIM2,&TIM_Timebaseinitstructure);
	
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_initstructure;
	NVIC_initstructure.NVIC_IRQChannel=TIM2_IRQn;
	NVIC_initstructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_initstructure.NVIC_IRQChannelPreemptionPriority=2;
	NVIC_initstructure.NVIC_IRQChannelSubPriority=1;
	
	NVIC_Init(&NVIC_initstructure);
	
	TIM_Cmd(TIM2,ENABLE);
}
//前后台函数中断,1ms一次

#ifndef __USER_TIME_H
#define __USER_TIME_H

void user_timer_init(void);

#endif
计数器计数频率: CK_CNT = CK_PSC / (PSC + 1)

你可能感兴趣的:(stm32,嵌入式硬件,单片机)