第七届蓝桥杯全国软件和信息技术专业人才大赛个人赛 (电子类)省赛 嵌入式设计与开发科目“模拟液位检测告警系统”设计任务书

添加链接描述## 赛题要求

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

下面是代码部分

主函数部分

#include "stm32f10x.h"
#include "lcd.h"
#include "led.h"
#include "stdio.h"
#include "key.h"
#include "adc.h"
#include "mytim.h"
#include "myiic.h"
#include "myusart.h"

u32 TimingDelay = 0;
uint16_t ADC_value =0;
u8 key_state=1; // 1 ËÉ¿ª¿É°´ÏÂ״̬ 0 °´ÏÂ״̬²»¿É°´ÏÂ
u8 ADC_Char[20]={
    
    0};

void Delay_Ms(u32 nTime);
void KEY_Scan(void);
void Display_LCD(void);
void Level_Derive(void);
void IIC_preservation(void);
void IIC_Read_Data(void);
u8 Height[20]={
    
    0};
u8 ADC[20]={
    
    0};
u8 LEVEL[20]={
    
    0};
u16 Height_num=0;
float ADC_num=0;
u16 Level_num =0;
u8 wink_number =0 ;//ÉÁ˸5´ÎרÓüÆÊý±äÁ¿
_Bool wink_switch= 0;//¶¨ÒåÉÁ˸µÄ¿ª¹Ø 
u8 Thre_1[20]={
    
    0};  //ãÐÖµ1 2 3 
u8 Thre_2[20]={
    
    0};
u8 Thre_3[20]={
    
    0};
u8 Thre_1_num = 30;
u8 Thre_2_num = 50;
u8 Thre_3_num = 70;
u8 Thre_num = 1;

_Bool  Par_set_up =0 ;//ãÐÖµÉèÖýçÃæ B1°´ÏÂÇл» 0 ½çÃæÊÇÏÔʾ½çÃæ 1ÊÇãÐÖµ½çÃæ
_Bool  run_mode = 0;//ÔËÐÐ״̬
_Bool  UART_Mode =0 ;//ͨѶ״̬
u8  I2C_Second = 0;
//Main Body
int main(void)
{
    
    
	SysTick_Config(SystemCoreClock/1000);
  KEY_Init();
	MY_ADC_Init();
	STM3210B_LCD_Init();
	LCD_Clear(Blue);
	LED_Init();
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	MYTIM2_Init();
	i2c_init();
	Delay_Ms(2);
	if(My_IIC_Read(0xfc)!=20)
	{
    
    
			my_iic_write(0xff,30);
			Delay_Ms(20);
			my_iic_write(0xfe,50);
			Delay_Ms(20);
			my_iic_write(0xfd,70);
			Delay_Ms(20);
			my_iic_write(0xfc,20);
			Delay_Ms(20);
	}
	IIC_Read_Data();
	while(1)
	{
    
    

			ADC_value= ADC_GetConversionValue(ADC1);
			ADC_num  = ADC_value;
			ADC_num = ADC_num/4095*3.3;
			Height_num = ADC_num/3.3*100;
			Level_Derive();
			Display_LCD();
			LCD_DisplayStringLine(Line4,ADC_Char);
			KEY_Scan();

	}
	
}
void Display_LCD(void)
{
    
    
	if(Par_set_up)
	{
    
    

			LCD_DisplayStringLine(Line0,"   Parameter Setup  ");
		  sprintf(Thre_1," Threshold1 : %d cm ",Thre_1_num);
		  sprintf(Thre_2," Threshold2 : %d cm ",Thre_2_num);
		  sprintf(Thre_3," Threshold2 : %d cm ",Thre_3_num);
					  switch(Thre_num)
				{
    
    

					case 1:
						LCD_SetTextColor(Red);
			      LCD_DisplayStringLine(Line3,Thre_1);
						LCD_SetTextColor(White);
					  LCD_DisplayStringLine(Line5,Thre_2);
			      LCD_DisplayStringLine(Line7,Thre_3);
					break;
					case 2:
						LCD_SetTextColor(Red);
			      LCD_DisplayStringLine(Line5,Thre_2);
						LCD_SetTextColor(White);
					  LCD_DisplayStringLine(Line3,Thre_1);
			      LCD_DisplayStringLine(Line7,Thre_3);
					break;
					case 3:
						LCD_SetTextColor(Red);
			      LCD_DisplayStringLine(Line7,Thre_3);
						LCD_SetTextColor(White);
					  LCD_DisplayStringLine(Line5,Thre_2);
			      LCD_DisplayStringLine(Line3,Thre_1);
					break;
				}
	
			
	}else
	{
    
    

      run_mode = 1;
			LCD_DisplayStringLine(Line0,"     Liquid Level ");
			sprintf(Height,"   Height : %d ",Height_num);
			sprintf(ADC,"   ADC :  %.2f V ",ADC_num);
			sprintf(LEVEL,"   Level :  %d ",Level_num);	

			LCD_DisplayStringLine(Line3,Height);
			LCD_DisplayStringLine(Line5,ADC);
			LCD_DisplayStringLine(Line7,LEVEL);
	}
}
//
void Delay_Ms(u32 nTime)
{
    
    
	TimingDelay = nTime;
  while(TimingDelay != 0);	
}

//
void KEY_Scan(void)
{
    
    
 u8 key_value = 0;
 key_value = KEY_Read();  
q	if(key_state==1)
	switch(key_value)
 {
    
    

		 case 1:
			 Par_set_up= !Par_set_up;
		 if(Par_set_up)
		 {
    
      
			wink_switch = 1; //ÉÁ˸¿ªÊ¼
			 run_mode = 0;
       UART_Mode =0 ;
		 }
		 else IIC_preservation(); //ÍƳöÉèÖÃ×Ô¶¯±£´æ

		 	LCD_Clear(Blue);
			 key_state=0;
		 break;		 
		 case 2:
			 Thre_num++;
		 if(Thre_num>3)Thre_num=1;
			 key_state=0;
		 break;		 
		 case 3:  //°´ÏÂÈýãÐÖµ¼Ó
			  switch(Thre_num)
				{
    
    
					case 1:
						if((Thre_1_num<Thre_2_num)&&(Thre_1_num<Thre_3_num))
						Thre_1_num +=5;
					if(Thre_1_num>95)Thre_1_num=95;
					break;
					case 2:
					if((Thre_1_num<=Thre_2_num)&&(Thre_2_num<Thre_3_num))
						Thre_2_num +=5;
					if(Thre_2_num>95)Thre_2_num=95;
					break;
					case 3:
				  if((Thre_1_num<=Thre_3_num)&&(Thre_2_num<=Thre_3_num))
						Thre_3_num +=5;
					if(Thre_3_num>95)Thre_3_num=95;
					break;
				}
		 key_state=0;
		 break;		 
		 case 4:
			 			  switch(Thre_num)
				{
    
    
					case 1:
						if((Thre_1_num<=Thre_2_num)&&(Thre_1_num<=Thre_3_num))						
						Thre_1_num -=5;
					if(Thre_1_num<5)Thre_1_num=5;
					break;
					case 2:
						if((Thre_1_num<Thre_2_num)&&(Thre_2_num<=Thre_3_num))
						Thre_2_num -=5;
					if(Thre_2_num<5)Thre_2_num=5;
					break;
					case 3:
						if((Thre_1_num<Thre_3_num)&&(Thre_2_num<Thre_3_num))
						Thre_3_num -=5;
					if(Thre_3_num<5)Thre_3_num=5;
					break;
				}
			key_state=0;
		 break;		 
	 }
 }
void Level_Derive(void)
{
    
    

	if(Height_num<Thre_1_num)
		Level_num = 0;
	else if((Height_num>=Thre_1_num)&&(Height_num<Thre_2_num))
		Level_num = 1;
	else if((Height_num>=Thre_2_num)&&(Height_num<=Thre_3_num))
		Level_num = 2;
	else if(Height_num>Thre_3_num)
		Level_num = 3;
}
void IIC_preservation(void)
{
    
    

	my_iic_write(0xff,Thre_1_num);
	Delay_Ms(20);
	my_iic_write(0xfe,Thre_2_num);
	Delay_Ms(20);
	my_iic_write(0xfd,Thre_3_num);
	Delay_Ms(20);
}
void IIC_Read_Data(void)
{
    
    
	Thre_1_num=My_IIC_Read(0xff);
	Delay_Ms(20);
  Thre_2_num=My_IIC_Read(0xfe);
	Delay_Ms(20);
	Thre_3_num=My_IIC_Read(0xfd);
	Delay_Ms(20);
}

LED部分

#include "led.h"
void LED_Init(void)
{
    
    
	
	GPIO_InitTypeDef GPIO_InitStrure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitStrure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStrure.GPIO_Pin=GPIO_Pin_2;
	GPIO_InitStrure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOD,&GPIO_InitStrure);
	GPIO_InitStrure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStrure.GPIO_Pin=GPIO_Pin_All;
	GPIO_InitStrure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStrure);
	
}
void LED_Control(u16  led)
{
    
    
	GPIO_SetBits(GPIOC,GPIO_Pin_All);
	GPIO_ResetBits(GPIOC,led<<8);

	GPIO_SetBits(GPIOD,GPIO_Pin_2);	
	GPIO_ResetBits(GPIOD,GPIO_Pin_2);

}



按键部分

#include "key.h"
extern  u8 key_state;
extern void Delay_Ms(u32 nTime);
extern _Bool LCD_wink_bug;
void KEY_Init(void)
{
    
    
		GPIO_InitTypeDef GPIO_InitStrure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitStrure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStrure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2;
	GPIO_InitStrure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStrure);
	GPIO_InitStrure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStrure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_8;
	GPIO_InitStrure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStrure);
	
}

u8 KEY_Read(void)
{
    
    
	if(KEY_0&&KEY_1&&KEY_2&&KEY_3)key_state = 1;
	else 
	{
    
    
	  Delay_Ms(10);
    if(KEY_0&&KEY_1&&KEY_2&&KEY_3)return 0;
		else{
    
    

				if(KEY_0==0) return 1;
				if(KEY_1==0) return 2;
				if(KEY_2==0) return 3;
				if(KEY_3==0) return 4;
			
			
		}
		
	}

	return 0;
}


定时器部分

#include "mytim.h"
_Bool led_twinkle =0;
	u32 TIM2_nTime =0;
extern u16 twinkle_number  ;//ÉÁ˸5´ÎרÓüÆÊý±äÁ¿
extern _Bool  Par_set_up  ;//ãÐÖµÉèÖýçÃæ B1°´ÏÂÇл» 0 ½çÃæÊÇÏÔʾ½çÃæ 1ÊÇãÐÖµ½çÃæ
extern _Bool  run_mode ;//ÔËÐÐ״̬
extern _Bool  UART_Mode  ;//ͨѶ״̬
extern u8 wink_number;
extern _Bool wink_switch ;
void MYTIM2_Init(void)
{
    
    
	/*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
  NVIC_InitTypeDef NVIC_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* GPIOC clock enable */
  /* System Clocks Configuration */

  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  /* GPIO Configuration */

  /* ---------------------------------------------------------------
    TIM2 Configuration: Output Compare Timing Mode:
    TIM2 counter clock at 6 MHz
    CC1 update rate = TIM2 counter clock / CCR1_Val = 146.48 Hz
    CC2 update rate = TIM2 counter clock / CCR2_Val = 219.7 Hz
    CC3 update rate = TIM2 counter clock / CCR3_Val = 439.4 Hz
    CC4 update rate = TIM2 counter clock / CCR4_Val = 878.9 Hz
  --------------------------------------------------------------- */

  /* Compute the prescaler value */

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 1000-1; // 1 000 000 = 1 ms
  TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //72 /72 =1M = 1 000 000 000
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  /* Prescaler configuration */

  /* Output Compare Timing Mode configuration: Channel1 */




  /* TIM IT enable */
  TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE);

  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
    
    
	static u8 wink_number = 0;
  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  {
    
    
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  
				if(Par_set_up)
				{
    
    
					if(wink_switch)
					{
    
    
            if(wink_number>10){
    
    
							wink_switch=0;
							wink_number=0;
							LED_Control(0x00);
						}
			     		TIM2_nTime++;
							if(TIM2_nTime >200)
							{
    
    	
							  wink_number++;
								TIM2_nTime =0;
								led_twinkle = !led_twinkle;

							}
								if(led_twinkle)LED_Control(0x02);
								else LED_Control(0x00);
						}
				}
				else	if(UART_Mode)
				{
    
    
					TIM2_nTime++;
					
							if(TIM2_nTime>200)
							{
    
    
								TIM2_nTime =0 ;
								led_twinkle = !led_twinkle;

							}

							
								if(led_twinkle)LED_Control(0x03);
								else LED_Control(0x00);
							
					}
						else	if(run_mode)
				{
    
    
					TIM2_nTime++;
					if(TIM2_nTime > 1000)
					{
    
    
						TIM2_nTime =0 ;
						led_twinkle = !led_twinkle;
					}
					if(led_twinkle)LED_Control(0x01);
					else LED_Control(0x00);
				}
    /* Pin PC.06 toggling with frequency = 73.24 Hz */
  }
  
}

IIC部分

/*
  ³ÌÐò˵Ã÷: CT117EǶÈëʽ¾ºÈü°åGPIOÄ£ÄâI2C×ÜÏßÇý¶¯³ÌÐò
  Èí¼þ»·¾³: Keil uVision 4.10 
  Ó²¼þ»·¾³: CT117EǶÈëʽ¾ºÈü°å
  ÈÕ    ÆÚ: 2011-8-9
*/

#include "stm32f10x.h"
#include "myiic.h"
/** I2C ×ÜÏß½Ó¿Ú */
#define I2C_PORT GPIOB
#define SDA_Pin	GPIO_Pin_7
#define SCL_Pin GPIO_Pin_6

#define FAILURE 0
#define SUCCESS 1

//ÅäÖÃSDAÐźÅÏßΪÊäÈëģʽ
void SDA_Input_Mode()
{
    
    
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Pin = SDA_Pin;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;	 

  	GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}

//ÅäÖÃSDAÐźÅÏßΪÊä³öģʽ
void SDA_Output_Mode()
{
    
    
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Pin = SDA_Pin;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  	GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}

//
void SDA_Output( uint16_t val )
{
    
    
	if ( val ) {
    
    
		GPIO_SetBits(I2C_PORT,SDA_Pin);
	} else {
    
    
		GPIO_ResetBits(I2C_PORT,SDA_Pin);
	}
}

//
void SCL_Output( uint16_t val )
{
    
    
	if ( val ) {
    
    
		GPIO_SetBits(I2C_PORT,SCL_Pin);
	} else {
    
    
		GPIO_ResetBits(I2C_PORT,SCL_Pin);
	}
}

//
uint8_t SDA_Input()
{
    
    
	return GPIO_ReadInputDataBit( I2C_PORT, SDA_Pin);
}

//ÑÓʱ³ÌÐò
void delay1(unsigned int n)
{
    
    
	unsigned int i;
	for ( i=0;i<n;++i);
}

//I2C×ÜÏßÆô¶¯
void I2CStart(void)
{
    
    
	SDA_Output(1);delay1(500);
	SCL_Output(1);delay1(500);
	SDA_Output(0);delay1(500);
	SCL_Output(0);delay1(500);
}

//I2C×ÜÏßÍ£Ö¹
void I2CStop(void)
{
    
    
	SCL_Output(0); delay1(500);
	SDA_Output(0); delay1(500);
	SCL_Output(1); delay1(500);
	SDA_Output(1); delay1(500);

}

//µÈ´ýÓ¦´ð
unsigned char I2CWaitAck(void)
{
    
    
	unsigned short cErrTime = 5;
	SDA_Input_Mode(); 
	delay1(500);
	SCL_Output(1);delay1(500);
	while(SDA_Input())
	{
    
    
		cErrTime--;
		delay1(500);
		if (0 == cErrTime)
		{
    
    
			SDA_Output_Mode();
			I2CStop();
			return FAILURE;
		}
	}
	SDA_Output_Mode();
	SCL_Output(0);delay1(500); 
	return SUCCESS;
}

//·¢ËÍÓ¦´ðλ
void I2CSendAck(void)
{
    
    
	SDA_Output(0);delay1(500);
	delay1(500);
	SCL_Output(1); delay1(500);
	SCL_Output(0); delay1(500);

}

//
void I2CSendNotAck(void)
{
    
    
	SDA_Output(1);
	delay1(500);
	SCL_Output(1); delay1(500);
	SCL_Output(0); delay1(500);

}

//ͨ¹ýI2C×ÜÏß·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý
void I2CSendByte(unsigned char cSendByte)
{
    
    
	unsigned char  i = 8;
	while (i--)
	{
    
    
		SCL_Output(0);delay1(500); 
		SDA_Output(cSendByte & 0x80); delay1(500);
		cSendByte += cSendByte;
		delay1(500); 
		SCL_Output(1);delay1(500); 
	}
	SCL_Output(0);delay1(500); 
}

//´ÓI2C×ÜÏß½ÓÊÕÒ»¸ö×Ö½ÚÊý¾Ý
unsigned char I2CReceiveByte(void)
{
    
    
	unsigned char i = 8;
	unsigned char cR_Byte = 0;
	SDA_Input_Mode(); 
	while (i--)
	{
    
    
		cR_Byte += cR_Byte;
		SCL_Output(0);delay1(500); 
		delay1(500); 
		SCL_Output(1);delay1(500); 
		cR_Byte |=  SDA_Input(); 
	}
	SCL_Output(0);delay1(500); 
	SDA_Output_Mode();
	return cR_Byte;
}

//I2C×ÜÏß³õʼ»¯
void i2c_init()
{
    
    
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

	GPIO_InitStructure.GPIO_Pin = SDA_Pin | SCL_Pin;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;	 // **

  	GPIO_Init(I2C_PORT, &GPIO_InitStructure);
	
}
void my_iic_write(u8 addr,u8 info)
{
    
    
	I2CStart();
	I2CSendByte(0xa0);
	I2CWaitAck();
	
	I2CSendByte(addr);
	I2CWaitAck();
	I2CSendByte(info);
	I2CWaitAck();
	I2CStop();
}

u8 My_IIC_Read(u8 addr)
{
    
    
	u8 mydata=0;
	I2CStart();
	I2CSendByte(0xa0);
  I2CWaitAck();
	
	I2CSendByte(addr);
	I2CWaitAck();
	
	I2CStart();
	I2CSendByte(0xa1);
	I2CWaitAck();
	
	mydata = I2CReceiveByte();
	I2CWaitAck();
	I2CStop();
	return mydata;
	
	

	
}

源代码

猜你喜欢

转载自blog.csdn.net/m0_46179894/article/details/108897757