蓝桥杯国赛复习总结

蓝桥杯国赛复习总结

  • 常用PA1引脚功能表
    GPIO 功能
    PA1 TIM2_CH2 / SER(74LS595串行数据输入引脚) / 频率可调脉冲
    PA2 TIM2_CH3 / USART2_TX / RCK(74LS595串行存储时钟输入引脚) / 频率可调脉冲
    PA3 USART2_RX / SCK(74LS595串行移位时钟输入引脚) / 光敏电阻读入引脚
    PA4 ADC1_IN4 / SCL(三轴传感器时钟引脚)
    PA5 ADC1_IN5 / SDA(三轴传感器数据引脚) / 4*2按键矩阵
    PA6 TIM3_CH1 / 占空比可调脉冲 / 温度传感器
    PA7 TIM3_CH2 / 占空比可调脉冲 / 温湿度传感器

LED

  • led.c

LED STM32
LED0 ~ 7 PC8 ~1 5
LE PD2

  • 快速驱动

Project->STM32F10x_StdPeriph_Examples->GPIO->IOToggle"文件夹下,打开"main.c"

void LED_Init(void)

{
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 	GPIO_Init(GPIOD, &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);


}

void LED_Con(uint16_t LED,uint16_t Status)
{
	   if(Status==0)
	   {	  
	   		  GPIO_SetBits(GPIOC,LED);
	   		  GPIO_SetBits(GPIOD,GPIO_Pin_2);
			  GPIO_ResetBits(GPIOD,GPIO_Pin_2);
	   }
	   else
	   {
			  GPIO_ResetBits(GPIOC,LED);
			  GPIO_SetBits(GPIOD,GPIO_Pin_2);
			  GPIO_ResetBits(GPIOD,GPIO_Pin_2);
	   }


}
  • led.h
#include "stm32f10x.h"


#define LED0 GPIO_Pin_8
#define LED1 GPIO_Pin_9
#define LED2 GPIO_Pin_10
#define LED3 GPIO_Pin_11
#define LED4 GPIO_Pin_12
#define LED5 GPIO_Pin_13
#define LED6 GPIO_Pin_14
#define LED7 GPIO_Pin_15
#define LEDALL GPIO_Pin_All



void LED_Init(void);
void LED_Con(uint16_t LED,uint16_t Status);

KEY

  • 引脚
  • KEY STM32
    KEY1 PA0
    KEY2 PA8
    KEY3 PB1
    KEY4 PB2
  • key.c
void KEY_Init(void)
{
	 GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
 	GPIO_Init(GPIOB, &GPIO_InitStructure);

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

}

void KEY_Scan()
{
	static u16 key1_sum=0,key2_sum=0,key3_sum=0,key4_sum=0;

	if(KEY1==0)
	{
		  key1_sum++;
		  if(key1_sum==1)
		  {	
		  }
		   if(key1_sum==40)//长按触发
		  {	
			
			key1_sum==30;//为了显示效果不断长按触发
		  }
	}else
		{
				  key1_sum=0;
		}

	if(KEY2==0)
	{
		  key2_sum++;
		  if(key2_sum==1)
		  {			  
		  }
	}else
	     {
		  key2_sum=0;
     	}
}

  • key.h
#include "stm32f10x.h"


#define KEY1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
#define KEY2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
#define KEY3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
#define KEY4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)

void KEY_Init(void);
void KEY_Scan(void);

  • 滴答
void SysTick_Handler(void)
{
		static u8 key_sum = 0;
		TimingDelay--;
		if(++key_sum == 50)  // 50ms
		{
				key_sum = 0;
				KEY_Flag = 1;
		}
}

  • main
	KEY_Init();
	while(1)
	{
			if(KEY_Flag) //按键读取
			{
					KEY_Flag = 0;
					KEY_Read();
			}
	}
  • 快速驱动
  • 这个没啥快速的,记住是上拉脚

LCD

	STM3210B_LCD_Init();//初始化
	LCD_Clear(Blue);//清屏
	LCD_SetBackColor(Blue);//设置背景色,高亮用。在写字之前改色,之后在改回来
	LCD_SetTextColor(White);//设置字体色
		    u8 string[20];//全局变量

			LCD_SetBackColor(Blue);
			sprintf((char*)string,"      measure       ");//写字
			LCD_DisplayStringLine(Line1,string);
			sprintf((char*)string,"AO1: %.2f             ",AO1);
			LCD_DisplayStringLine(Line5,string);

USART驱动

在这里插入图片描述

  • usart.c
void USART2_Init(u32 bound)
{
		GPIO_InitTypeDef GPIO_InitStructure;
		USART_InitTypeDef USART_InitStructure;
		NVIC_InitTypeDef NVIC_InitStructure;
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA口时钟
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//使能串口2时钟
	
		NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // 开启串口2中断
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级为最高
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;   //相应优先级为最高
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  //使能中断
		NVIC_Init(&NVIC_InitStructure);
	
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);//PA2为发送引脚,因此要配置成复用输出模式
	
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);//PA3为接收引脚,因此要配置成浮空输入模式
	
		USART_InitStructure.USART_BaudRate = bound; //波特率
		USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据
		USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止位
		USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
		USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  //使能发送和接收
		USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
		USART_Init(USART2,&USART_InitStructure);        //不使能硬件流控制
		
		USART_Cmd(USART2,ENABLE);   //使能串口
		USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);   //使能接收中断
		
}

void USART2_SendString(u8 *str)
{
		u8 index = 0;
		do
		{
				USART_SendData(USART2,str[index]); //发送数据
				while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == 0);  // 是否发送完成
				index++;
		}while(str[index] != 0);
}


  • usart.h
void USART2_Init(u32 bound);
void USART2_SendString(u8 *str);
  • 中断
void USART2_IRQHandler(void)
{	
		u8 temp;
		if(USART_GetITStatus(USART2,USART_IT_RXNE) == SET)
		{
				USART_ClearITPendingBit(USART2,USART_IT_RXNE);
				temp =  USART_ReceiveData(USART2);
				if(temp == '\n')
				{
						RXCUNT = 0;
						RXOVER = 1;
						USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
				}else
				{
						RXBUF[RXCUNT] = temp;
						RXCUNT++;
				}
		}
}
  • main
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	USART2_Init(9600);
	while(1)
	{
			if(RXOVER)
			{
					LCD_ClearLine(Line3);
					LCD_DisplayStringLine(Line4, RXBUF);
					USART2_SendString(RXBUF);
					for(i=0;i<20;i++)
					{
							RXBUF[i] = 0;
					}
					USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
					RXOVER = 0;
			}
	}
  • 快速驱动

Project->STM32F10x_StdPeriph_Examples->USART->Interrupt"文件夹下,打开"main.c":

蓝桥杯国赛复习总结_第1张图片
蓝桥杯国赛复习总结_第2张图片

蓝桥杯国赛复习总结_第3张图片

E2PROM

直接把比赛提供的"i2c.c"和"i2c.h"复制粘贴到我们工程目录里的HARDWARE文件夹里,并把i2c.c添加到工程里

  • i2c_add.c
void _24C02_write(u8 address ,u8 dat)
{
	 I2CStart();
	 
	 I2CSendByte(0xa0);
	 I2CWaitAck();
	 I2CSendByte(address);
	 
	 I2CWaitAck();
	 I2CSendByte(dat);
	 I2CWaitAck();
	 
	 I2CStop();
}

u8 	  _24C02_read(u8 address )
{
	u8 temp;
	 I2CStart();
	 
	 I2CSendByte(0xa0);
	 I2CWaitAck();
	 I2CSendByte(address);
	 I2CWaitAck();

	 I2CStart();
	 I2CSendByte(0xa1);
	 I2CWaitAck();
	 temp= I2CReceiveByte();
	 I2CWaitAck();

	 I2CStop();
	 return temp;
}
  • i2c_add.h
#include "stm32f10x.h"
#include "i2c.h"
void _24C02_write(u8 address ,u8 dat);
u8 	  _24C02_read(u8 address );
  • main
	i2c_init();
	va1=_24C02_read(0xa1);
	_24C02_write(0xa1,va1);

RTC

rtc 一定要添加那两个库文件

  • rtc.c
void My_Rtc_Init(u8 HH,u8 MM,u8 SS)
{
	 NVIC_InitTypeDef NVIC_InitStructure;
	 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
	 PWR_BackupAccessCmd(ENABLE);
	 BKP_DeInit();

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

	 RCC_LSICmd(ENABLE);
	 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
	 RCC_RTCCLKCmd(ENABLE);

	 RTC_WaitForSynchro();
	 RTC_WaitForLastTask();

	 RTC_ITConfig(RTC_IT_SEC, ENABLE);
	 RTC_WaitForLastTask();

	 RTC_SetPrescaler(40000-1);
	 RTC_WaitForLastTask();
	 RTC_SetCounter(3600 * HH + 60 * MM + SS);
	 RTC_WaitForLastTask();

}	
  • rtc.h
void My_Rtc_Init(u8 HH,u8 MM,u8 SS) ;
  • 中断
extern u8 RTC_Flag ;
void RTC_IRQHandler(void)
{
   u32 Times;

  if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  {
   	    RTC_ClearITPendingBit(RTC_FLAG_SEC);
		Times = RTC_GetCounter();
				RTC_WaitForLastTask();
				RTC_Flag = 1;
				if(Times == (24 * 3600))
				{
						RTC_SetCounter(0);
						RTC_WaitForLastTask();
				}
  }
}

  • main
	My_Rtc_Init(0,0,0);
	while(1)
	{
	if(RTC_Flag)
			{
					RTC_Flag = 0;
					TimeVal = RTC_GetCounter();
					hour = TimeVal / 3600;
					min = TimeVal % 3600 / 60;
					sec = TimeVal % 3600 % 60;
			}
			sprintf((char*)string,"TIME:%d:%d:%d             ",hour,min,sec);
			LCD_DisplayStringLine(Line7,string);
    }
  • 快速驱动
    “Project->STM32F10x_StdPeriph_Examples->RTC->LSI_Calib"文件夹下,打开"main.c”:蓝桥杯国赛复习总结_第4张图片
    直接复制粘贴RTC_Configuration()部分,去掉最后两句代码,加上我们的计数值设定即可。

在相同目录下,打开"stm32f10x_it.c"还可以看到RTC中断函数相关代码,修改修改直接使用即可:

蓝桥杯国赛复习总结_第5张图片

ADC

基础板上一个ADC,外接板两个ADC
蓝桥杯国赛复习总结_第6张图片

  • adc.c
void ADC1_Init(void)
 {
 	GPIO_InitTypeDef GPIO_InitStructure;
	ADC_InitTypeDef ADC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, 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 = 1;
	ADC_Init(ADC1, &ADC_InitStructure);
	
	ADC_Cmd(ADC1, ENABLE);
	
	ADC_ResetCalibration(ADC1);
	  
	while(ADC_GetResetCalibrationStatus(ADC1));
	
	ADC_StartCalibration(ADC1);
	
	while(ADC_GetCalibrationStatus(ADC1));

 }

 u16 Get_Adc(u8 channel)
 {
	u16 temp;
	ADC_RegularChannelConfig(ADC1,channel,1,ADC_SampleTime_239Cycles5);	
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==0);
	temp= ADC_GetConversionValue(ADC1);
	ADC_SoftwareStartConvCmd(ADC1,DISABLE);
	return temp;
 }
  • adc.h
#include "stm32f10x.h"


 void ADC1_Init(void);

 u16 Get_Adc(u8 channel);


  • main
ADC1_Init();
while(1)
{
   			AO1=Get_Adc(4)*3.3/4096;
		    AO2=Get_Adc(5)*3.3/4096;
}
  • 快速编写
  • 蓝桥杯国赛复习总结_第7张图片

蓝桥杯国赛复习总结_第8张图片

 u16 Get_Adc(u8 channel)
 {
	u16 temp;
	ADC_RegularChannelConfig(ADC1,channel,1,ADC_SampleTime_239Cycles5);	//442
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);  //438
	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==0);//460					
	temp= ADC_GetConversionValue(ADC1); //444
	ADC_SoftwareStartConvCmd(ADC1,DISABLE);//438
	return temp;
 }

基础PWM输出

void TIM3_PWM_Init(u16 pwm_fre,u8 ch1_duty,u8 ch2_duty)
{
		u16 arr;
		GPIO_InitTypeDef GPIO_InitStructure;
		TIM_OCInitTypeDef TIM_OCInitStructure;
		TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
		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); 
	
		arr = 1000000 / pwm_fre;  
		TIM_TimeBaseInitStructure.TIM_Period = arr - 1; //计数值
		TIM_TimeBaseInitStructure.TIM_Prescaler = 71; //分频系数 72分频
		TIM_TimeBaseInitStructure.TIM_CounterMode = 0x0; //向上计数
		TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0; //不分频
		TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
	
		
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
		TIM_OCInitStructure.TIM_Pulse = (arr - 1) * ch1_duty / 100;
		TIM_OC1Init( TIM3, &TIM_OCInitStructure);
		
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
		TIM_OCInitStructure.TIM_Pulse = (arr - 1) * ch2_duty / 100;
		TIM_OC2Init( TIM3, &TIM_OCInitStructure);
		
		TIM_CtrlPWMOutputs(TIM3, ENABLE);
		TIM_Cmd( TIM3,ENABLE);
}

“Project->STM32F10x_StdPeriph_Examples->TIM->PWM_Output”

蓝桥杯国赛复习总结_第9张图片

高级定时器PWM互补输出

void TIM1_PWM_Init(u16 fre,u8 duty,u8 status,u8 enable)
{
		u16 frequency;
		GPIO_InitTypeDef GPIO_InitStructure;
		TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
		TIM_OCInitTypeDef TIM_OCInitStructure;
		if(status)
		{
				RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE);
				RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, 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_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
				GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
				GPIO_Init(GPIOB, &GPIO_InitStructure);
		}
		frequency = 1000000 / fre;
		TIM_TimeBaseInitStructure.TIM_Period = frequency - 1;
		TIM_TimeBaseInitStructure.TIM_Prescaler = 71;
		TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;
		TIM_TimeBaseInitStructure.TIM_CounterMode = 0x0;
		TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
		
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
		if(enable)
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
				TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
		}else
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
				TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
		}
		TIM_OCInitStructure.TIM_Pulse = (frequency - 1) * duty / 100;
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
		TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
		TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
		TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
		TIM_OC2Init(TIM1,&TIM_OCInitStructure);
		
		TIM_CtrlPWMOutputs(TIM1, ENABLE);
		
		TIM_Cmd(TIM1, ENABLE);
}

PWM比较输出

蓝桥杯国赛复习总结_第10张图片

蓝桥杯国赛复习总结_第11张图片

  • pwm.c
extern u16 CH1_Val;
extern u16 CH2_Val;
extern u16 CH1_Duty;
extern u16 CH2_Duty;

void TIM3_PWM_OUT(u16 ch1_val,u16 ch1_duty,u16 ch2_val,u16 ch2_duty,u8 enable1,u8 enable2,u8 enable)
{
	
	   	GPIO_InitTypeDef GPIO_InitStructure;
		NVIC_InitTypeDef NVIC_InitStructure;
		TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
		TIM_OCInitTypeDef  TIM_OCInitStructure;

		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , 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);

		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);

		TIM_TimeBaseStructure.TIM_Period = 65535;
  		TIM_TimeBaseStructure.TIM_Prescaler = 71;
  		TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  		TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

		CH1_Val = 1000000/ ch1_val;
		CH2_Val = 1000000/ ch2_val;
		CH1_Duty = 	CH1_Val* ch1_duty/100;
		CH2_Duty = 	CH2_Val* ch2_duty/100;
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  		TIM_OCInitStructure.TIM_Pulse = CH1_Val;
  		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
		if(enable1)
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
		}else
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
		}
  		TIM_OC1Init(TIM3, &TIM_OCInitStructure);

		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  		TIM_OCInitStructure.TIM_Pulse = CH2_Val;
  		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
		if(enable2)
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
		}else
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
		}
  		TIM_OC2Init(TIM3, &TIM_OCInitStructure);

//		TIM_SetCounter(TIM3, 0x0);
//		TIM_SetCompare1(TIM3,0x0);
//		TIM_SetCompare2(TIM3,0x0);

	   if(enable)
	   {
	   	  	TIM_Cmd(TIM3, ENABLE);
 			TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 , ENABLE);
	   }
	   else
	   {
	   		TIM_Cmd(TIM3, DISABLE);
 			TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 , DISABLE);
	
	   }

}


  • pwm.h
#include "stm32f10x.h"

void TIM3_PWM_OUT(u16 ch1_val,u16 ch1_duty,u16 ch2_val,u16 ch2_duty,u8 enable1,u8 enable2,u8 enable);
  • 中断
u8 TIM3_CH1_Flag;
u8 TIM3_CH2_Flag;
extern u16 CH1_Val;
extern u16 CH2_Val;
extern u16 CH1_Duty;
extern u16 CH2_Duty;

void TIM3_IRQHandler(void)
{

	u16   capture;
	 if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
  {

	    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
	    capture = TIM_GetCapture1(TIM3);

		if(TIM3_CH1_Flag)  TIM_SetCompare1(TIM3, capture +CH1_Duty  );
	    else   TIM_SetCompare1(TIM3, capture + CH1_Val-CH1_Duty  );

		 TIM3_CH1_Flag ^=1;
  }

 

	  if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
	  {
	    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
	    capture = TIM_GetCapture2(TIM3);
	
		if(TIM3_CH2_Flag)  TIM_SetCompare2(TIM3, capture + CH2_Duty );
	    else   TIM_SetCompare2(TIM3, capture + CH2_Val-CH2_Duty   );
		 TIM3_CH2_Flag ^=1;

    
  }
}
  • main
TIM3_PWM_OUT(1000,20,4000,40,1,1,1);
  • 快速驱动
    Project->STM32F10x_StdPeriph_Examples->TIM->OCToggle"文件夹下,打开"main.c":

蓝桥杯国赛复习总结_第12张图片
再打开同目录下的"stm32f10x_it.h":
蓝桥杯国赛复习总结_第13张图片

PWM比较异相输出

D = (Vo/3.3)*360°,

void TIM3_Output_Init(u16 fre,u16 deg,u8 status,u8 enable)
{
		u16 phase;
		GPIO_InitTypeDef GPIO_InitStructure;
		NVIC_InitTypeDef NVIC_InitStructure;
		TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
		TIM_OCInitTypeDef TIM_OCInitStructure;
		if(status)
		{
				RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
				RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 ,ENABLE);
			
				GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
				GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
				GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
				GPIO_Init(GPIOA, &GPIO_InitStructure);
			
				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);
			
				TIM_TimeBaseInitStructure.TIM_Period = 0xffff;
				TIM_TimeBaseInitStructure.TIM_Prescaler = 71;
				TIM_TimeBaseInitStructure.TIM_CounterMode = 0x0;
				TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;
				TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
		}
		CH_Val = 1000000 / fre;
		phase = CH_Val * deg / 360;
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
		if(enable)
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
		}else
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
		}
		TIM_OCInitStructure.TIM_Pulse = CH_Val;
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
		TIM_OC1Init(TIM3, &TIM_OCInitStructure);
		
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
		if(enable)
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
		}else
		{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
		}
		TIM_OCInitStructure.TIM_Pulse = CH_Val;
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
		TIM_OC2Init(TIM3, &TIM_OCInitStructure);
		
		TIM_SetCounter(TIM3, 0X0);
		TIM_SetCompare1(TIM3, 0X0);
		TIM_SetCompare2(TIM3, phase);
		
		TIM_Cmd(TIM3,ENABLE);
		TIM_ITConfig( TIM3, TIM_IT_CC1 | TIM_IT_CC2,ENABLE);
		
}
void TIM3_IRQHandler(void)
{
		u16 capture;
		if(TIM_GetITStatus(TIM3, TIM_IT_CC1) == 1)
		{
				TIM_ClearITPendingBit( TIM3, TIM_IT_CC1);
				capture = TIM_GetCapture1(TIM3);
				TIM_SetCompare1(TIM3, capture + CH_Val * 0.5);
		}
		
		if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == 1)
		{
				TIM_ClearITPendingBit( TIM3, TIM_IT_CC2);
				capture = TIM_GetCapture2(TIM3);
				TIM_SetCompare2(TIM3, capture + CH_Val * 0.5);
		}
}

CAPTURE

  • capture.c
void TIM2_PWM_IN()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	TIM_ICInitTypeDef  TIM_ICInitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1|GPIO_Pin_2;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
 	GPIO_Init(GPIOA, &GPIO_InitStructure);


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


	TIM_TimeBaseStructure.TIM_Period = 65535;
  	TIM_TimeBaseStructure.TIM_Prescaler = 71;
  	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);


	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_ICInit(TIM2, &TIM_ICInitStructure);

	TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
  	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_ICInit(TIM2, &TIM_ICInitStructure);

    TIM_Cmd(TIM2, ENABLE);
  	TIM_ITConfig(TIM2, TIM_IT_CC2|TIM_IT_CC3, ENABLE);

}

  • capture.h
#include "stm32f10x.h"

void TIM2_PWM_IN(void);
  • 中断
extern u8 TIM2_CH2_CAPTURE_MODE ;
extern u32 TIM2_CH2_CAPTURE_H ;
extern u32 TIM2_CH2_CAPTURE_HL;
extern u8 TIM2_CH3_CAPTURE_MODE ;
extern u32 TIM2_CH3_CAPTURE_H ;
extern u32 TIM2_CH3_CAPTURE_HL ;


void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET) 
  {
    	TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
        if(capture_flag)
    	 {
		 	switch(TIM2_CH2_CAPTURE_MODE)
			{
				case 0: TIM2_CH2_CAPTURE_H=0;
						TIM2_CH2_CAPTURE_HL=0;
						TIM2_CH2_CAPTURE_MODE=1;
		 				TIM_OC2PolarityConfig(TIM2,TIM_OCPolarity_Low);
		 				TIM_SetCounter(TIM2,0x0);
						break;
				case 1:	TIM2_CH2_CAPTURE_H=	TIM_GetCounter(TIM2);
				 		TIM_OC2PolarityConfig(TIM2,TIM_OCPolarity_High);
						TIM2_CH2_CAPTURE_MODE=2;
						break;
				case 2:	TIM2_CH2_CAPTURE_HL=TIM_GetCounter(TIM2);
						TIM_OC2PolarityConfig(TIM2,TIM_OCPolarity_High);
						TIM2_CH2_CAPTURE_MODE=3;
						break;
			 }
		 }else
		 {
		 	   TIM2_CH2_CAPTURE_MODE=0;
			   TIM_OC2PolarityConfig(TIM2,TIM_OCPolarity_High);
		 }


	}

	 if(TIM_GetITStatus(TIM2, TIM_IT_CC3) == SET) 
  	{
    	TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
        if(!capture_flag)
    	 {
		 	switch(TIM2_CH3_CAPTURE_MODE)
			{
				case 0: TIM2_CH3_CAPTURE_H=0;
						TIM2_CH3_CAPTURE_HL=0;
						TIM2_CH3_CAPTURE_MODE=1;
		 				TIM_OC3PolarityConfig(TIM2,TIM_OCPolarity_Low);
		 				TIM_SetCounter(TIM2,0x0);
						break;
				case 1:	TIM2_CH3_CAPTURE_H=	TIM_GetCounter(TIM2);
				 		TIM_OC3PolarityConfig(TIM2,TIM_OCPolarity_High);
						TIM2_CH3_CAPTURE_MODE=2;
						break;
				case 2:	TIM2_CH3_CAPTURE_HL=TIM_GetCounter(TIM2);
						TIM_OC3PolarityConfig(TIM2,TIM_OCPolarity_High);
						TIM2_CH3_CAPTURE_MODE=3;
						break;
			 }
		 }else
		 {
		 	   TIM2_CH3_CAPTURE_MODE=0;
			   TIM_OC3PolarityConfig(TIM2,TIM_OCPolarity_High);
		 }
		
  }	

	   
}
  • main
u16 TIM2_CH2_Fre;//全局变量
u16 TIM2_CH3_Fre;
u16 TIM2_CH2_Duty;
u16 TIM2_CH3_Duty;
u8 TIM2_CH2_CAPTURE_MODE = 0;//全局变量
u32 TIM2_CH2_CAPTURE_H = 0;
u32 TIM2_CH2_CAPTURE_HL = 0;
u8 TIM2_CH3_CAPTURE_MODE = 0;
u32 TIM2_CH3_CAPTURE_H = 0;
u32 TIM2_CH3_CAPTURE_HL = 0;


while(1)
{
	if(TIM2_CH2_CAPTURE_MODE==3)
		{
			TIM2_CH2_Fre=1000000/TIM2_CH2_CAPTURE_HL/1000;
			TIM2_CH2_Duty=TIM2_CH2_CAPTURE_H*100/TIM2_CH2_CAPTURE_HL;
			TIM2_CH2_CAPTURE_MODE = 0;	
		}
		if(TIM2_CH3_CAPTURE_MODE==3)
		{
			TIM2_CH3_Fre=1000000/TIM2_CH3_CAPTURE_HL/1000;
			TIM2_CH3_Duty=TIM2_CH3_CAPTURE_H*100/TIM2_CH3_CAPTURE_HL;
			TIM2_CH3_CAPTURE_MODE = 0;	
		}
}		
		
  • 快速驱动

Project->STM32F10x_StdPeriph_Examples->TIM->InputCapture"文件夹下,"main.c"和"stm32f10x_it.c"分别有着输入捕获模式初始化和中断服务程序相关代码:
蓝桥杯国赛复习总结_第14张图片

蓝桥杯国赛复习总结_第15张图片

ds18b20

蓝桥杯国赛复习总结_第16张图片

#include "ds18b20.h"
int main()
{
    u32 z;
    u8 str[20];
    ...
    ds18b20_init_x();
    while(1)
    {
        z=(ds18b20_read()&0x7ff);
     	LCD_ClearLine(Line0);
     	sprintf(str," t= %2.2f",(float)z/16.);
     	LCD_DisplayStringLine(Line0, str);
    }
}

DHT11

蓝桥杯国赛复习总结_第17张图片

#include "dht11.h"
int main()
{
    u32 z;
    u8 str[20];
    ...
    dht11_init();
    while(1)
    {
        z=dht11_read();
     	LCD_ClearLine(Line1);
     	sprintf(str1," shidu= %2d %%",z>>24);
     	LCD_DisplayStringLine(Line1, str1);
     	LCD_ClearLine(Line2);
     	sprintf(str1," wendu= %3d ",(z>>8)&0xff);
     	LCD_DisplayStringLine(Line2, str1);
     	delay_ms(2000);
    }
}

总结

到现在为止参加的两年蓝桥杯中从大一迷茫稀里糊涂的省二,到大二可以进入国赛。
感谢自己在大一时候早早的开始学习32,希望自己能在国赛取得一个不让自己后悔的成绩

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