写蓝桥杯嵌入式第六、七、八届决赛程序的一些收获

索引

  • point1:rcc时钟配置
  • point2:led一体化配置
  • point3:KEY按键配置
  • point4:ADC配置
  • point5:互补PWM输出配置
  • point6:IIC 读写eeprom
  • point7:用eeprom实现数据存储及再次开机利用
  • point8:LCD换页显示及变量赋入字符串
  • point9: PWM INPUT配置
  • point10:串口USART配置
  • point11: RTC实时时钟
  • point12: ADC多通道采集
  • point13:同一个定时器输出不同频率方波

point1:rcc时钟配置

把初始的rcc时钟开启放在一个函数中

void RCC_initCTY()
{
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
}

point2:led一体化配置

先初始化对应GPIOC 8~15 引脚, 及锁存器的引脚GPIOD 2

void LED_initCTY()
{
  	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
    GPIO_Write(GPIOC,0Xffff);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_Init(GPIOD, &GPIO_InitStructure);
	GPIO_SetBits(GPIOD,GPIO_Pin_2);
	GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}

led控制程序,当lednum为9时为控制所有的灯

void LED_control(u8 lednum,u16 stat)
{
    if(lednum==9) 
	  LED_sta=stat<<8;
	else{
	   if(stat==1)
	   LED_sta |= (1<<(lednum-1+8));
	   else 
	   LED_sta &=  ~(1<<(lednum-1+8));
	}
    GPIO_Write(GPIOC,LED_sta);
	GPIO_SetBits(GPIOD,GPIO_Pin_2);
	GPIO_ResetBits(GPIOD,GPIO_Pin_2); 
}

point3:KEY按键配置

在文件开头宏定义

#define BT1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
#define BT2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
#define BT3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
#define BT4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2) 先配置对应引脚PA0 PA8 PB1 PB2

void KEY_initCTY()
{
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
}

扫描函数

void KEY_scanCTY()
{
	if(BT1==0)		//BT44444444444
	{
	   Delay_Ms(5);
	   {
		 while(BT1==0);
	   }	
	}

   //  ....中间省略

	if(BT4==0)		//BT44444444444
	{
	   Delay_Ms(5);
	   {
		 while(BT4==0);
	   }	
	}
}

point4:ADC配置

用的是官方的历程ADC>RegSimul_DualMode修改来初始化

void ADC_initCTY()
{
    ADC_InitTypeDef ADC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);    //#######################
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent ;
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfChannel = 1;
    ADC_Init(ADC1, &ADC_InitStructure);
    /* ADC1 regular channels configuration */ 
    ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_13Cycles5);

	ADC_Cmd(ADC1, ENABLE);  
	ADC_ResetCalibration(ADC1);
	while(ADC_GetResetCalibrationStatus(ADC1));
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1));
}

ADC读取函数,这个要自己写(从xxx adc.h中找)

void ADC_readCTY()
{
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);    //#######################

	Delay_Ms(5);

	ADCvalue= ADC_GetConversionValue(ADC1)*3.3/0X0FFF;     //#######################

}

利用systick,每200ms采集一次,在main函数 while中体现如下

	   if(GetadcF==1)
	   {
	      GetadcF=0;
		  ADC_readCTY();	  
	   }

point5:互补PWM输出配置

先初始化配置

void PWM_hubuIint()
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_AFIO,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_14;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	TIM_TimeBaseStructure.TIM_Period = 1000-1;
    TIM_TimeBaseStructure.TIM_Prescaler =72-1;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;  //#######################
    TIM_OCInitStructure.TIM_Pulse = 500;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;     //#######################

    TIM_OC2Init(TIM1, &TIM_OCInitStructure);

	TIM_Cmd(TIM1, ENABLE);
    TIM_CCPreloadControl(TIM1,ENABLE);   //#######################
	TIM_CtrlPWMOutputs(TIM1,ENABLE);     //########################################

}

结合寄存器动态调节周期和频率

  TIMx->CCR2 = TIM_OCInitStruct->TIM_Pulse;	    调占空比
  TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ;	   调周期

point6:IIC 读写eeprom

也可以读写拓展板的三轴传感器,地址为0x38 0x39 不过需要先初始化器件 iic_write(0x20,0x47) 读0x29 0x2B 0x2D 对应x y z 轴加速度

eeprom地址为0xa0 0xa1
自己写读写函数

不要忘了在main函数开头调用i2c_init();

void IICwriteCTY(u8 add,u8 dat)
{
	I2CStart();	
	I2CSendByte(0xa0);    
	I2CWaitAck();

	I2CSendByte(add);
	I2CWaitAck();
	I2CSendByte(dat);
	I2CWaitAck();
	I2CStop();
	Delay_Ms(10);
  }

u8 IICreadCTY(u8 add)
{
	u8 tem=0;
	I2CStart();	
	I2CSendByte(0xa0);
	I2CWaitAck();
	
	I2CSendByte(add);
	I2CWaitAck();
	
	I2CStart();	
	I2CSendByte(0xa1);
	I2CWaitAck();
	tem=I2CReceiveByte();
	I2CStop();
	Delay_Ms(10);
	return tem;	
}

point7:用eeprom实现数据存储及再次开机利用

在需要存储变量值的地方以如下方式存储并植入密码

	if(BT2==0)		//BT2222222222222
	{
	   Delay_Ms(5);
	   if(BT2==0)
	   {
	     if(interF==1)
	        interF=2;
	     else {
		   interF=1;
	       TIM1->ARR = 1000/outFreq -1;
		   TIM1->CCR2 = pwmA9*10/outFreq;	     //*1000/100=10
		   IICwriteCTY(0x08,outFreq);
		   IICwriteCTY(0x10,12);          //#######################
		  }

		 BTV=2;

		 while(BT2==0);
	   }	
	}

main函数开始时这样写,意为若已在0x10中写入密码12,则读取0x08值给变量。否则变量值为最开始的初始值

	if(IICreadCTY(0x10)==12) {           //#######################
		Delay_Ms(10);
	 	outFreq=IICreadCTY(0x08);
	 	TIM1->ARR = 1000/outFreq -1;
	 	}

point8:LCD换页显示及变量赋入字符串

可以直接放在按键程序里clear lcd

void LCD_showarc()
{
    if(showsta==2){          //#######################
	   LCD_Clear(Blue);
	   showsta=1;
	   }
	LCD_DisplayStringLine(Line2 ,(unsigned char *)"        argv       ");

	sprintf(LCD_string,"collect V:  %.2f   ",ADCvalue);
	LCD_DisplayStringLine(Line4 ,LCD_string);
}

point9: PWM INPUT配置

可检测输入pwm的频率和占空比,初始化用TIM>PWM_Input中代码,并配置中断

void pwminput_init()
{
	TIM_ICInitTypeDef  TIM_ICInitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  /* TIM3 channel 2 pin (PA.07) configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);



   TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);

  /* Select the TIM3 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);

  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM3, ENABLE);

  /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
}

中断嵌套配置

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

中断处理, 采集处理

u32    DutyCycle = 0;
u32    Frequency = 0;
void TIM3_IRQHandler(void)
{
  u16  IC2Value;
  IC2Value = TIM_GetCapture2(TIM3);	
  if (IC2Value != 0)
  {
    DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value;      //#######################
    Frequency = SystemCoreClock / IC2Value;                    //#######################
  }
  else
  {
    DutyCycle = 0;
    Frequency = 0;
  }	
  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);               //#######################
}

point10:串口USART配置

初始化配置,用USART>Interrupt

void usart_initCTY()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	
	USART_InitStructure.USART_BaudRate = 9600;
	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_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	
	/* Configure USARTy */
	USART_Init(USART2, &USART_InitStructure);
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	USART_Cmd(USART2, ENABLE);
}

同样需要中断配置NVIC

  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

中断处理接受数据

extern u8 UST;
extern u8 USRf;
void USART2_IRQHandler(void)
{
  if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  {
  	USART_ClearITPendingBit(USART2, USART_IT_RXNE);            //#######################
    UST = USART_ReceiveData(USART2);                         //#######################
	USRf=1;
  }
}

发送数据函数 (自己写)

void usart_sendStr()
{
	u8 index=0;

	index=0;
	do{
	  USART_SendData( USART2, UsrsendBuf[index++]);             //#######################
	  while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==0);      //#######################
	  }while(UsrsendBuf[index]!='\0');                         //#######################
}

函数运行调用(在上位机显示换行需发 “\r\n”)

		if(USRf==1)	
		{
		  USRf=0;
		  if(UST=='C')
		  {
		   sprintf(UsrsendBuf,"\r\nUplimt temperature:   %dC ",UlimT);       //#######################
		   usart_sendStr();
		   sprintf(UsrsendBuf,"\r\nUplimt humidity:  %d%% ",UlimS);
		   usart_sendStr();
		   tempnum=RTC_GetCounter();
		   sprintf(UsrsendBuf,"\r\nTIME: %d-%d-%d",tempnum/3600,tempnum%3600/60,tempnum%60);
		   usart_sendStr();
		   }
		  else if(UST=='T')
		  {
		  	
		  }
		}

point11: RTC实时时钟

初始化配置,复制RTC>Calendar程序,注意修改LSE为LSI,及开始的使能函数

void RTC_Configuration(void)
{
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);   

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Reset Backup Domain */
  BKP_DeInit();

  /* Enable LSE */
  RCC_LSICmd(ENABLE);             //#######################  原为RCC_LSEConfig(RCC_LSE_ON);        
  /* Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  {}

  /* Select LSE as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

  /* Enable RTC Clock */
  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC registers synchronization */
  RTC_WaitForSynchro();

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Enable the RTC Second */
  RTC_ITConfig(RTC_IT_SEC, ENABLE);

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Set RTC prescaler: set RTC period to 1sec */   
  /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ 
  RTC_SetPrescaler(39999);      //#######################  原为32767
 
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  RTC_SetCounter(12*3600+50*60);        //#######################   自己写 初始化时间
  RTC_WaitForLastTask();

  Time_Display(RTC_GetCounter());		 //#######################
}

中断嵌套NVIC配置

  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

时间转化及显示 复制

void Time_Display(uint32_t TimeVar)
{
  uint32_t THH = 0, TMM = 0, TSS = 0;
  
  /* Reset RTC Counter when Time is 23:59:59 */
  if (RTC_GetCounter() == 0x0001517F)
  {
     RTC_SetCounter(0x0);
     /* Wait until last write operation on RTC registers has finished */
     RTC_WaitForLastTask();
  }
  
  /* Compute  hours */
  THH = TimeVar / 3600;
  /* Compute minutes */
  TMM = (TimeVar % 3600) / 60;
  /* Compute seconds */
  TSS = (TimeVar % 3600) % 60;

  sprintf(lcd_tem,"now time: %.2d-%.2d-%.2d ",THH, TMM, TSS);  
  LCD_DisplayStringLine(Line6 ,lcd_tem);
}

中断处理函数 (秒中断)

extern u8 TimeDisplay;
void RTC_IRQHandler(void)
{
  if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  {

    RTC_ClearITPendingBit(RTC_IT_SEC);

    TimeDisplay = 1;
    RTC_WaitForLastTask();
    
  }
}

调用

	if (TimeDisplay == 1)
    {
      /* Display current time */
      Time_Display(RTC_GetCounter());       //#######################
      TimeDisplay = 0; 
    }

point12: ADC多通道采集

正常初始化

void ADC_initC()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	ADC_InitTypeDef ADC_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 ;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent ;
	ADC_InitStructure.ADC_ScanConvMode = DISABLE;
	ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStructure.ADC_NbrOfChannel = 2;      //#######################            
	ADC_Init(ADC1, &ADC_InitStructure);
//    ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_13Cycles5);    
//    ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_13Cycles5);

	ADC_Cmd(ADC1, ENABLE);
	ADC_ResetCalibration(ADC1);
	while(ADC_GetResetCalibrationStatus(ADC1));
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1));
}

一次读取两个通道的采集值

void ADC_read()
{
	ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_13Cycles5);//#######################
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	//while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )){};
	Delay_Ms(5);
	ADCA4=ADC_GetConversionValue(ADC1)*3.3/0xfff;            //#######################
	
	ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_13Cycles5);  //#######################
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	//while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )){};
	Delay_Ms(5);
	ADCA5=(float)ADC_GetConversionValue(ADC1)*3.3/0xfff;
}

point13:同一个定时器输出不同频率方波

使用的是TIM>TIM9_OCToggle 初始化及中断处理

void pwmout_init()
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  u16 PrescalerValue = 0;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);	

 PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535; 
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* Output Compare Toggle Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 4096;         //#######################  自己暂时写一个   
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);

  /* Output Compare Toggle Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 8192;             //#######################  自己暂时写一个   

  TIM_OC2Init(TIM3, &TIM_OCInitStructure);

  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);

   TIM_Cmd(TIM3, ENABLE);

  /* TIM IT enable */
  TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2, ENABLE);
}

NVIC配置

    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

中断服务函数

void TIM3_IRQHandler(void)
{
  /* TIM3_CH1 toggling with frequency = 183.1 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
    captureT = TIM_GetCapture1(TIM3);
    TIM_SetCompare1(TIM3, captureT + 12000000/FA1);   //CCR1_Val );      //######################    
  }

  /* TIM3_CH2 toggling with frequency = 366.2 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
    captureT = TIM_GetCapture2(TIM3);
    TIM_SetCompare2(TIM3, captureT + 12000000/FA2);                 //######################
  }

}

你可能感兴趣的:(蓝桥杯嵌入式)