蓝桥杯嵌入式-第七届省赛-模拟液位检测告警系统

 main.c

/*
  程序说明: 蓝桥杯大赛嵌入式-第七届省赛-模拟液位检测告警系统
  软件环境: Keil uVision 4.11 
  硬件环境: CT117E嵌入式竞赛板(代码兼容ILI932X系列、uc8230液晶控制器)
  日    期: 2020年10月15日
  作    者: lishan
*/
#include "stm32f10x.h"
#include "lcd.h"
#include "stdio.h"
#include "led.h"
#include "timer.h"
#include "adc.h"
#include "key.h"
#include "i2c.h"
#include "uart.h"
#include "string.h"

//first为1时,先保存默认值到eeprom存储器,再从存储器加载数据
//first为0时,直接从存储器加载数据
#define first 0

u32 TimingDelay = 0;

u8 data_buf[20];
u16 data = 0;

u16 adc_value = 0;
float VR37_value = 0;

u8 Threshold_value[3] = {30,50,70};

u8 Height = 50;
u8 Level_old = 1;
u8 Level_new = 1;
u8 Level_change = 0;

void Delay_Ms(u32 nTime);
void Show_Level(void);
void Show_Setup(void);
void Time_Polling(void);
void ReceiveData_Process(void);
void Save_Data(void);
void Load_Data(void);

//Main Body
int main(void)
{
	SysTick_Config(SystemCoreClock/1000);
	Delay_Ms(200);
	STM3210B_LCD_Init();
	LCD_Clear(Blue);
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	LED_Init();
	TIM4_Init();
	ADC_Channel_8_Init();
	KEY_Init();
	UART2_Init();
	i2c_init();
	Load_Data();
	while(1)
	{
		if(Set)
		{
			Show_Setup();	
		}
		else
		{
			Show_Level();	
		}
		ReceiveData_Process();	
		Time_Polling();
		Save_Data();
	}
}

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

void ReceiveData_Process(void)
{
	if(Rx_flag)
	{
		Rx_flag = 0;
		if(RxCounter1 == 1)
		{
			if(RxBuffer1[0]	== 'C')
			{
				printf("C:H%02d+L%d\r\n",Height,Level_new);	
			}
			else if(RxBuffer1[0] == 'S')
			{
				printf("S:TL%02d+TM%02d+TH%02d\r\n",Threshold_value[0],Threshold_value[1],Threshold_value[2]);	
			}
		}
		RxCounter1 = 0;
		memset(RxBuffer1,0,sizeof(RxBuffer1));	
	}	
}

void Load_Data(void)
{
	if(first)
	{
		Write_AT24c02(0x00,Threshold_value[0]);
		Delay_Ms(5);
		Write_AT24c02(0x01,Threshold_value[1]);
		Delay_Ms(5);
		Write_AT24c02(0x02,Threshold_value[2]);
		Delay_Ms(5);	
	}
	else
	{
		Threshold_value[0] = Read_AT24c02(0x00);
		Threshold_value[1] = Read_AT24c02(0x01);
		Threshold_value[2] = Read_AT24c02(0x02);	
	}
}

void Save_Data(void)
{
	if(Save)
	{
		Save = 0;
		Write_AT24c02(0x00,Threshold_value[0]);
		Delay_Ms(5);
		Write_AT24c02(0x01,Threshold_value[1]);
		Delay_Ms(5);
		Write_AT24c02(0x02,Threshold_value[2]);
		Delay_Ms(5);	
	}
}


void Time_Polling(void)
{
	if(flag_10ms)
	{
		flag_10ms = 0;
		KEY_Read();
		KEY_Process();
		Status_Update();
	}
	if(flag_200ms && Level_change)
	{
		static u8 cnt = 0;
		flag_200ms = 0;
		LED_Toggling(LD2);
		if(++cnt >= 10)
		{
			cnt = 0;
			Level_change = 0;		
		}
	}
	if(flag_1000ms)
	{
		u8 i = 0;
		float adc_sample = 0;
		flag_1000ms = 0;
		if(Set == 0)
		{
			LED_Toggling(LD1);	
		}
		for(i=0; i<20; i++)
		{
			adc_sample += ADC_GetConversionValue(ADC1);
			Delay_Ms(1);
		}
		adc_sample /= 20;
		adc_value =  (u16)adc_sample;
		VR37_value = adc_value * 3.3 / 4095;	
		Height = (u8)(VR37_value * 100 / 3.3);
		if(Height <= Threshold_value[0])
		{
			Level_new = 0;	
		}
		else if(Height > Threshold_value[0] && Height <= Threshold_value[1])
		{
			Level_new = 1;	
		}
		else if(Height > Threshold_value[1] && Height <= Threshold_value[2])
		{
			Level_new = 2;	
		}
		else if(Height > Threshold_value[2])
		{
			Level_new = 3;	
		}
		if(Level_old != Level_new)
		{
			Level_change = 1;
			if(Level_new < Level_old)
			{
				printf("A:H%02d+L%d+D\r\n",Height,Level_new);
			}
			else
			{
				printf("A:H%02d+L%d+U\r\n",Height,Level_new);
			}
			Level_old = Level_new;	
		}							
	}
}
		
void Show_Level(void)
{
	LCD_DisplayStringLine(Line2,"    Liquid Level    ");
	
	sprintf((char*)data_buf,"   Height: %dcm    ",Height);
	LCD_DisplayStringLine(Line4,data_buf);
	
	sprintf((char*)data_buf,"   ADC: %.2fV    ",VR37_value);
	LCD_DisplayStringLine(Line5,data_buf);
	
	sprintf((char*)data_buf,"   Level: %d    ",Level_new);
	LCD_DisplayStringLine(Line6,data_buf);	
}


	
void Show_Setup(void)
{
	LCD_DisplayStringLine(Line2,"  Parameter Setup   ");

	if(Choose == 0)
	{
		LCD_SetBackColor(Green);
		LCD_SetTextColor(Red);

		sprintf((char*)data_buf," Threshold1: %02dcm     ",Threshold_value[0]);
		LCD_DisplayStringLine(Line4,data_buf);

		LCD_SetBackColor(Blue);
		LCD_SetTextColor(White);

		sprintf((char*)data_buf," Threshold2: %02dcm    ",Threshold_value[1]);
		LCD_DisplayStringLine(Line5,data_buf);
													  
		sprintf((char*)data_buf," Threshold3: %02dcm    ",Threshold_value[2]);
		LCD_DisplayStringLine(Line6,data_buf);		
	}
	else if(Choose == 1)
	{
		sprintf((char*)data_buf," Threshold1: %02dcm    ",Threshold_value[0]);
		LCD_DisplayStringLine(Line4,data_buf);

		LCD_SetBackColor(Green);
		LCD_SetTextColor(Red);

		sprintf((char*)data_buf," Threshold2: %02dcm    ",Threshold_value[1]);
		LCD_DisplayStringLine(Line5,data_buf);

		LCD_SetBackColor(Blue);
		LCD_SetTextColor(White);

		sprintf((char*)data_buf," Threshold3: %02dcm    ",Threshold_value[2]);
		LCD_DisplayStringLine(Line6,data_buf);		
	}
	else if(Choose == 2)
	{
		sprintf((char*)data_buf," Threshold1: %02dcm    ",Threshold_value[0]);
		LCD_DisplayStringLine(Line4,data_buf);

		sprintf((char*)data_buf," Threshold2: %02dcm    ",Threshold_value[1]);
		LCD_DisplayStringLine(Line5,data_buf);

		LCD_SetBackColor(Green);
		LCD_SetTextColor(Red);
		
		sprintf((char*)data_buf," Threshold3: %02dcm    ",Threshold_value[2]);
		LCD_DisplayStringLine(Line6,data_buf);	
		
		LCD_SetBackColor(Blue);
		LCD_SetTextColor(White);	
	}		
}



key.c

/*
  程序说明: CT117E嵌入式竞赛板KEY驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT117E嵌入式竞赛板(代码兼容ILI932X系列、uc8230液晶控制器)
  日    期: 2013-10-9
*/
#include "key.h"
#include "lcd.h"
#include "led.h"

u8 Trg = 0;
u8 Cont = 0;
u8 Set = 0;
u8 Choose = 0;
u8 Save = 0;

u8 key1_press = 0;
u8 key2_press = 0;
u8 key3_press = 0;
u8 key4_press = 0;

u8 key1_time = 0;
u8 key2_time = 0;
u8 key3_time = 0;
u8 key4_time = 0;

void KEY_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);	

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOB, &GPIO_InitStructure);	
}

void KEY_Read(void)
{
	u8 ReadData = ~(KEYPORT);
	Trg =  ReadData & (ReadData ^ Cont);
	Cont = ReadData;
}

void KEY_Process(void)
{
	if(Cont == 0x01){++key1_time;}
	else if(Cont == 0x02){++key2_time;}
	else if(Cont == 0x04){++key3_time;}
	else if(Cont == 0x08){++key4_time;}
	if(Trg == 0 && Cont == 0)
	{
		if(key1_time > 0){key1_press = 1;}
		else if(key2_time > 0){key2_press = 1;}
		else if(key3_time > 0){key3_press = 1;}
		else if(key4_time > 0){key4_press = 1;}
	}
}

void Status_Update(void)
{ 
	if(key1_press)
	{
		Set = (Set + 1) % 2;
		LCD_Clear(Blue);
		Choose = 0;	
		if(Set)
		{
			LED_Control(0xFF00,0);	
		}	
		else
		{
			Save = 1;
		}
	}
	else if(Set && key2_press)
	{
		Choose = (Choose + 1) % 3;	
	}
	else if(Set && key3_press)
	{
		Threshold_value[Choose] += 5;
		if(Threshold_value[Choose] >= 95)
		{
			Threshold_value[Choose] = 95;	
		}
		if(Choose != 2 && Threshold_value[Choose] >= Threshold_value[Choose+1])
		{
			Threshold_value[Choose] -= 5;	
		}

	}
	else if(Set && key4_press)
	{
		Threshold_value[Choose] -= 5;
		if(Threshold_value[Choose] <= 5)
		{
			Threshold_value[Choose] = 5;	
		}
		if(Choose != 0 && Threshold_value[Choose] <= Threshold_value[Choose-1])
		{
			Threshold_value[Choose] += 5;	
		}
	}
	if(Trg == 0 && Cont == 0)
	{
		key1_time = 0; key2_time = 0; key3_time = 0; key4_time = 0;
		key1_press = 0; key2_press = 0;	key3_press = 0;	key4_press = 0;	
	}
}

uart.c

/*
  程序说明: CT117E嵌入式竞赛板UART驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT117E嵌入式竞赛板(代码兼容ILI932X系列、uc8230液晶控制器)
  日    期: 2013-10-9
*/
#include "uart.h"

u8 Rx_flag = 0;
u8 RxBuffer1[20];
u8 RxCounter1 = 0;
void UART2_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	/* Enable GPIO clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 

	/* Configure USART Tx as alternate function push-pull */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* Configure USART Rx as input floating */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* Configure the NVIC Preemption Priority Bits */  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
	
	/* Enable the USARTy Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_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;
         
  	/* USART configuration */
	USART_Init(USART2, &USART_InitStructure);
	
	/* Enable USART */
	USART_Cmd(USART2, ENABLE); 

	/* Enable USARTz Receive and Transmit interrupts */
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);
}

int fputc(int ch, char *FILE)
{
	/* Place your implementation of fputc here */
	/* e.g. write a character to the USART */
	USART_SendData(USART2, (uint8_t) ch);
	
	/* Loop until the end of transmission */
	while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
	{}
	
	return ch;
}

void USART2_IRQHandler(void)
{
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
	{
		/* Read one byte from the receive data register */
		RxBuffer1[RxCounter1++] = USART_ReceiveData(USART2);
	}
	if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
	{
		USART2 -> SR;
		USART2 -> DR;	
	   	Rx_flag = 1;
	}
}

 

 

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐观的lishan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值