蓝桥杯嵌入式第十届省赛程序

最后都变了- 提交于 2020-03-11 14:00:34

第十届赛题程序部分较为简单,客观题相对较难。主要会用adc+led即可ok,本科组甚至没有用到EEPROM(本代码加入此功能将led灯信息,上下阈值存入EEPROM,reset后数据不丢失)

先看目录

初始化函数

头文件:

初始化程序

#include "stm32f10x.h"
#include "lcd.h"
#include "init.h"
#include "i2c.h"

void GPIO_Int(void)
{
 	GPIO_InitTypeDef GPIO_InitStructure;
	ADC_InitTypeDef  ADC_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                         RCC_APB2Periph_GPIOC| RCC_APB2Periph_GPIOD, ENABLE);
 //led

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

    /* Configure PD.2 as output push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

 //key
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
    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);

	GPIO_SetBits(GPIOC,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_SetBits(GPIOD,GPIO_Pin_2);
	GPIO_ResetBits(GPIOD,GPIO_Pin_2);

	//adc
	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 = ENABLE;
  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 channel14 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_13Cycles5);

  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));

}

void LCD_Init(void)
{
   	STM3210B_LCD_Init();
	LCD_Clear(Blue);
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	
	SysTick_Config(SystemCoreClock/1000);


	LCD_DisplayStringLine(Line0,"                    ");	
	LCD_DisplayStringLine(Line1,"      Main          ");	
	LCD_DisplayStringLine(Line2,"                    ");
	LCD_DisplayStringLine(Line3,"   Volt  : 3.22 V    ");
	LCD_DisplayStringLine(Line4,"                    ");
	LCD_DisplayStringLine(Line5,"   Status: None     ");
	LCD_DisplayStringLine(Line6,"                    ");
	LCD_DisplayStringLine(Line7,"                    ");	
	LCD_DisplayStringLine(Line8,"                    ");
	LCD_DisplayStringLine(Line9,"                    ");	

}

void I2C_Write(unsigned char add,unsigned char dat)
{
   	I2CStart();
	I2CSendByte(0xa0);
	I2CSendAck();

	I2CSendByte(add);
	I2CSendAck();

	I2CSendByte(dat);
	I2CSendAck();
	I2CStop();

}

unsigned char I2C_Read(unsigned char add)
{
	unsigned char temp;

 	I2CStart();
	I2CSendByte(0xa0);
	I2CSendAck();

	I2CSendByte(add);
	I2CSendAck();

	I2CStart();
	I2CSendByte(0xa1);
	I2CSendAck();
	temp=I2CReceiveByte();
	I2CSendAck();
	I2CStop();

	return temp;




}





主函数:

#include "stm32f10x.h"
#include "lcd.h"
#include "init.h"
#include "i2c.h"
#include "stdio.h"

u32 TimingDelay = 0;

void Delay_Ms(u32 nTime);
void Key_Scan(void);
unsigned long ulTick_ms;
u8 string[20];
void ADC_Read(void);
u8 shezhi=0,qie=0;
float Max,Min;  //注意这里的都是10倍,因为eeprom只能存储整数
unsigned char ucled=1;
int Upper,Lower;   
u8 State=0;
float adc_val=0;
u16 miao=0; //数据类型应该大于10位这里是u16;注意不能是u8,因为u8最大表示2^8=256,加不到1000
u8 miao_flag=0;



//Main Body
int main(void)
{
	LCD_Init();
	GPIO_Int();

	i2c_init();
	Delay_Ms(5);
	if(I2C_Read(0x10)!=4)
	{
	Delay_Ms(5);
	I2C_Write(0x10,4);

	Delay_Ms(5);
	I2C_Write(0x00,24);
	Delay_Ms(5);
	I2C_Write(0x01,12);
	Delay_Ms(5);
	I2C_Write(0x02,0);
	Delay_Ms(5);
	I2C_Write(0x03,1);
	
	}
	Delay_Ms(5);
	Max=I2C_Read(0x00);
	Delay_Ms(5);
	Min=I2C_Read(0x01);
	Delay_Ms(5);
	Upper=I2C_Read(0x02);
	Delay_Ms(5);
	Lower=I2C_Read(0x03);

	
	while(1)
	{
	 Key_Scan();
	 if(shezhi==0)
	 {
	 	 if(miao_flag==1)//这里设置一秒采集一次,题目没要求,完全是习惯
		 {
		  miao_flag=0;
	      ADC_Read();
		  }
		  
	
	 }
	 
	  	
	}
}

void Key_Scan(void)
{
	if(RB1==0)
	{
	Delay_Ms(5);
	if(RB1==0)
	{
	  if(shezhi==0)
	  {
	  shezhi=1;
	  	LCD_DisplayStringLine(Line1,"      Setting       ");
		sprintf((char*)string," Max Volt: %.1f V    ",Max/10.0);	
		LCD_DisplayStringLine(Line2,string);
		sprintf((char*)string," Min Volt: %.1f V    ",Min/10.0);//注意这里是10倍关系,下同
		LCD_DisplayStringLine(Line3,string);
		sprintf((char*)string,"   Upper : LD%d      ",Upper+1);
		LCD_DisplayStringLine(Line4,string);
		sprintf((char*)string,"   Lower : LD%d      ",Lower+1);
		LCD_DisplayStringLine(Line5,string);
	  }

	  else if(shezhi==1)
	  {
	  	shezhi=0;
	  	Delay_Ms(5);
		I2C_Write(0x00,Max);
		Delay_Ms(5);
		I2C_Write(0x01,Min);
		Delay_Ms(5);
		I2C_Write(0x02,Upper);
		Delay_Ms(5);
		I2C_Write(0x03,Lower);

	 LCD_DisplayStringLine(Line1,"      Main          ");
	 LCD_DisplayStringLine(Line2,"                    ");
	 LCD_DisplayStringLine(Line4,"                    ");
	 LCD_DisplayStringLine(Line5,"   Status: None     ");

	  }
	}while(!RB1);
	}

	else if(RB2==0)
	{
	Delay_Ms(5);
	if(RB2==0)
	{
	  if(shezhi==1)
	  {
		  if(qie==0||qie==4)
		  {
		  qie=1;

		  LCD_SetTextColor(Red);
		  sprintf((char*)string," Max Volt: %.1f V    ",Max/10.0);	
		  LCD_DisplayStringLine(Line2,string);

		  LCD_SetTextColor(White);
		  sprintf((char*)string," Min Volt: %.1f V    ",Min/10.0);
		  LCD_DisplayStringLine(Line3,string);
		  sprintf((char*)string,"   Upper : LD%d      ",Upper+1);
		  LCD_DisplayStringLine(Line4,string);
	 	  sprintf((char*)string,"   Lower : LD%d      ",Lower+1);
		  LCD_DisplayStringLine(Line5,string);

	
		  }
		  else if(qie==1)
		  {
		  qie=2;

		  LCD_SetTextColor(Red);
  		  sprintf((char*)string," Min Volt: %.1f V    ",Min/10.0);
		  LCD_DisplayStringLine(Line3,string);
		  		  
		  LCD_SetTextColor(White);
		  sprintf((char*)string," Max Volt: %.1f V    ",Max/10.0);	
		  LCD_DisplayStringLine(Line2,string);
		  sprintf((char*)string,"   Upper : LD%d      ",Upper+1);
		  LCD_DisplayStringLine(Line4,string);
	 	  sprintf((char*)string,"   Lower : LD%d      ",Lower+1);
		  LCD_DisplayStringLine(Line5,string);

	
		  }

		  else if(qie==2)
		  {
		  qie=3;

		  LCD_SetTextColor(Red);
          sprintf((char*)string,"   Upper : LD%d      ",Upper+1);
		  LCD_DisplayStringLine(Line4,string);

		  LCD_SetTextColor(White);
		  sprintf((char*)string," Min Volt: %.1f V    ",Min/10.0);
		  LCD_DisplayStringLine(Line3,string);
		  sprintf((char*)string," Max Volt: %.1f V    ",Max/10.0);	
		  LCD_DisplayStringLine(Line2,string);
	 	  sprintf((char*)string,"   Lower : LD%d      ",Lower+1);
		  LCD_DisplayStringLine(Line5,string);

	
		  }

		  else if(qie==3)
		  {
		  qie=4;

		  LCD_SetTextColor(Red);
		  sprintf((char*)string,"   Lower : LD%d      ",Lower+1);
		  LCD_DisplayStringLine(Line5,string); 

		  LCD_SetTextColor(White);
		  sprintf((char*)string," Min Volt: %.1f V    ",Min/10.0);
		  LCD_DisplayStringLine(Line3,string);
		  sprintf((char*)string,"   Upper : LD%d      ",Upper+1);
		  LCD_DisplayStringLine(Line4,string);
		  sprintf((char*)string," Max Volt: %.1f V    ",Max/10.0);	
		  LCD_DisplayStringLine(Line2,string);
		
		  }

	  }
	}while(!RB2);
	}

	else if(RB3==0)
	{
	Delay_Ms(5);
	if(RB3==0)
	{
	  if(shezhi==1)
	  {
		  if(qie==1)
		  {
		  Max+=3;
		  if(Max>33)
		  Max=33;

		  LCD_SetTextColor(Red);
		  sprintf((char*)string," Max Volt: %.1f V    ",Max/10.0);	
		  LCD_DisplayStringLine(Line2,string);
		  LCD_SetTextColor(White);

		  }

		  else 	if(qie==2)
		  {
		  Min+=3;
		  if(Min>=Max)
		  Min=Max;

		  LCD_SetTextColor(Red);
		  sprintf((char*)string," Min Volt: %.1f V    ",Min/10.0);	
		  LCD_DisplayStringLine(Line3,string);
		  LCD_SetTextColor(White);

		  }

		  else 	if(qie==3)
		  {
		  Upper+=1;
		  if(Upper==Lower)
		  Upper+=1;
		  if(Upper>7)
		  Upper=0;

	   	  LCD_SetTextColor(Red);
          sprintf((char*)string,"   Upper : LD%d      ",Upper+1);
		  LCD_DisplayStringLine(Line4,string);

		  LCD_SetTextColor(White);

		  }

		  else 	if(qie==4)
		  {
		  Lower+=1;
		  if(Upper==Lower)
		  Lower+=1;
		  if(Lower>7)
		  Lower=0;

	   	  LCD_SetTextColor(Red);
          sprintf((char*)string,"   Lower : LD%d      ",Lower+1);
		  LCD_DisplayStringLine(Line5,string);
		  LCD_SetTextColor(White);

		  }
	  }
	}while(!RB3);
	}

	else if(RB4==0)
	{
	Delay_Ms(5);
	if(RB4==0)
	{
	  	 if(shezhi==1)
	  {
		  if(qie==1)
		  {
		  Max-=3;
		  
		  if(Max<=Min)
		  Max=Min;

		  LCD_SetTextColor(Red);
		  sprintf((char*)string," Max Volt: %.1f V    ",Max/10.0);	
		  LCD_DisplayStringLine(Line2,string);
		  LCD_SetTextColor(White);

		  }

		  else 	if(qie==2)
		  {
		  Min-=3;
		  if(Min<0)
		  Min=0;
		  

		  LCD_SetTextColor(Red);
		  sprintf((char*)string," Min Volt: %.1f V    ",Min/10.0);	
		  LCD_DisplayStringLine(Line3,string);
		  LCD_SetTextColor(White);

		  }

		  else 	if(qie==3)
		  {
		  Upper-=1;
		  if(Upper==Lower)
		  Upper-=1;
		  if(Upper<0)
		  Upper=7;

	   	  LCD_SetTextColor(Red);
          sprintf((char*)string,"   Upper : LD%d      ",Upper+1);
		  LCD_DisplayStringLine(Line4,string);

		  LCD_SetTextColor(White);

		  }

		  else 	if(qie==4)
		  {
		  Lower-=1;
		  if(Upper==Lower)
		  Lower-=1;
		  if(Lower<0)
		  Lower=7;

	   	  LCD_SetTextColor(Red);
          sprintf((char*)string,"   Lower : LD%d      ",Lower+1);
		  LCD_DisplayStringLine(Line5,string);
		  LCD_SetTextColor(White);

		  }
	  }
	}while(!RB4);
	}

}

void ADC_Read(void)
{
   float temp;
	/* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  temp=ADC_GetConversionValue(ADC1)*3.3/0xfff;
  adc_val=temp;

  sprintf((char*)string,"   Volt  : %.2f V   ",temp);
  LCD_DisplayStringLine(Line3,string);

  if(adc_val<Min/10.0)    //注意这里是10倍关系
		  {
		  LCD_DisplayStringLine(Line5,"   Status: Lower    ");
		  State=0;
		  }
	
		  else if (adc_val<Max/10.0)
		  {
		  LCD_DisplayStringLine(Line5,"   Status: None     ");
		  State=1;
		  }
		  
		  else {
		   	LCD_DisplayStringLine(Line5,"   Status: Upper    ");
			State=2;
		  }

}

void Delay_Ms(u32 nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);	
}

void LED_Disp(unsigned char ucled)
{
   GPIO_Write(GPIOC,~ucled<<8);
   GPIO_SetBits(GPIOD,GPIO_Pin_2);
   GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}

void SysTick_Handler(void)
{

	TimingDelay--;
	ulTick_ms++;
	if(miao++==999)
 {
  miao=0;
  miao_flag=1;
 }


	 switch(State)
	 {
	 case 0:
	 if(ulTick_ms%200==0)
	 ucled^=1<<Lower;     //这里用到位操作
	 break;

	 case 1:
	 ucled=0;
	 break;

	 case 2:
	 if(ulTick_ms%200==0)
	 ucled^=1<<Upper;
	 }
	 LED_Disp(ucled);

}


代码应该不难看懂,也没有多少解释,如有问题,欢迎勘正,同时欢迎交流讨论。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!