蓝桥杯嵌入式-第六届省赛-电压测量监控设备

main.c文件 

/*
  程序说明: 蓝桥杯大赛嵌入式-第六届省赛-电压测量监控设备
  软件环境: Keil uVision 4.11 
  硬件环境: CT117E嵌入式竞赛板(代码兼容ILI932X系列、uc8230液晶控制器)
  日    期: 2020年10月12日
  作    者: lishan
*/

#include "stm32f10x.h"
#include "lcd.h"
#include "stdio.h"
#include "led.h"
#include "timer.h"
#include "key.h"
#include "rtc.h"
#include "adc.h"
#include "uart.h"
#include "i2c.h"
#include "string.h"
#include "stdlib.h"

u32 TimingDelay = 0;

u8 data_buf[20];

float k = 0.1;
float adc_value = 1.84;

u8 Record_HH = 0;
u8 Record_MM = 0;
u8 Record_SS = 0;

u8 THH = 23;
u8 TMM = 59;
u8 TSS = 55;

union eeprom_float
{
	float a;
	u8 b[sizeof(float)];
}float_write,float_read;


void Delay_Ms(u32 nTime);
void LCD_Show_default(void);
void LCD_Show_setting(void);
void ReceivData_Process(void);
void Time_AssignTask(void);
void RTC_Update(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();
	KEY_Init();
	ADC1_Channel_8_Init();
	USART2_Init();
	RTC_Configuration();
	Time_Adjust(23,59,55);
	i2c_init();
	Load_Data();
	while(1)
	{
		if(Set)
		{
			LCD_Show_setting();	
		}
		else
		{
			LCD_Show_default();	
		} 
		ReceivData_Process();
		Time_AssignTask();
		RTC_Update();
	}
}

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

void LCD_Show_default(void)
{
	if(LED_Fucation)
	{
		sprintf((char*)data_buf, "    LED:%s     ", "ON");
		LCD_DisplayStringLine(Line5, data_buf);	
	}
	else
	{
		sprintf((char*)data_buf, "    LED:%s     ", "OFF");
		LCD_DisplayStringLine(Line5, data_buf);	
	}

	sprintf((char*)data_buf, "    V1:%3.2fV     ", adc_value);
	LCD_DisplayStringLine(Line3, data_buf);

	sprintf((char*)data_buf, "    k:%2.1f     ", k);
	LCD_DisplayStringLine(Line4, data_buf);

	sprintf((char*)data_buf, "    T:%02d-%02d-%02d     ", THH, TMM, TSS);
	LCD_DisplayStringLine(Line6, data_buf);	

	sprintf((char*)data_buf, "                  1");
	LCD_DisplayStringLine(Line9, data_buf);	
}

void ReceivData_Process(void)
{
	static u8 data_error = 1;
	static u8 k_value_str[5];
	static u8 i = 0;
	if(rx_flag)
	{
		rx_flag = 0;
		data_error = 1;
		i = 0;				
		memset(k_value_str,0,sizeof(k_value_str));

//      //处理方法一:
		if(RxCounter2 >= 5)
		{
			if(RxBuffer2[0]=='k' && RxBuffer2[1]=='0' && RxBuffer2[2]=='.' && RxBuffer2[RxCounter2-1]=='\n')
			{			
				if(RxBuffer2[3] >= '0' && RxBuffer2[3] <= '9')
				{
					k = (RxBuffer2[3] - '0')/10.f;
					float_write.a = k;
					for(i = 0; i<sizeof(float); i++)
					{
						Write_AT24c02(0x01+i,float_write.b[i]);
						Delay_Ms(5);	
					}
					printf("ok\n");
				}
			}
		}
		
//      //处理方法二:
//		if(RxCounter2 >= 1)
//		{
//			if(RxBuffer2[0]=='k' && RxBuffer2[RxCounter2-1]=='\n')
//			{
//				for(i = 1; i < RxCounter2-1; i++)		  //48 49 50 ...   56
//				{	   
//					if(RxBuffer2[i] == '.' | (RxBuffer2[i] >= '0' && RxBuffer2[i] <= '9'))
//					{
//						data_error = 0; 
//						k_value_str[i-1] = RxBuffer2[i];
//					}
//					else
//					{
//						data_error = 1;	
//					}
//				}	
//			}
//		}
//		if(data_error == 0)
//		{
//			k = atof((char*)k_value_str);
//			if(k >= 0.1 && k <=0.9)
//			{
//				float_write.a = k;
//				for(i = 0; i<sizeof(float); i++)
//				{
//					Write_AT24c02(0x01+i,float_write.b[i]);
//					Delay_Ms(5);	
//				}
//				printf("ok\n");
//			}
//			//printf("%.1f",k);
//		}	
		memset(RxBuffer2,0,sizeof(RxBuffer2));
		RxCounter2 = 0;
	}
}

void Time_AssignTask(void)
{
	if(flag_10ms)
	{
		flag_10ms = 0;
		KEY_Read();
		KEY_Process();
		Status_Update();
	}
	if(flag_100ms)
	{
		flag_100ms = 0;
		adc_value = 3.3*ADC_GetConversionValue(ADC1)/4095;
	}
	if(flag_200ms)
	{
		flag_200ms = 0;
		if(LED_Fucation && adc_value > 3.3*k)
		{
			LED_Toggling(LD1);
		}
		else
		{
			LED_Control(0xFF00,0);	
		}		
	}	
}

void RTC_Update(void)
{
	/* If 1s has been elapsed */
    if (TimeDisplay == 1)
    {
		/* Display current time */
		Time_Display(RTC_GetCounter());
		TimeDisplay = 0;
		if(THH == Record_HH && TMM == Record_MM && TSS == Record_SS)
		{
			printf("%.2f+%.1f+%02d%02d%02d\n",adc_value,k,Record_HH,Record_MM,Record_SS);
		}
    }
}

void Load_Data(void)
{
	static u8 i = 0;
	//先取消存储数据段的注释,编译下载运行,然后添加注释,编译下载运行
	//存储数据
//	float_write.a = k;
//	for(i = 0; i < sizeof(float); i++)
//	{
//		Write_AT24c02(0x01+i,float_write.b[i]);
//		Delay_Ms(5);	
//	}

	//读取数据
	for(i = 0; i < sizeof(float); i++)
	{
		float_read.b[i] = Read_AT24c02(0x01+i);	
	}
	k = float_read.a;
	
	//printf("%.1f",k);
}

void LCD_Show_setting(void)
{
	sprintf((char*)data_buf, "      Setting     ");
	LCD_DisplayStringLine(Line3, data_buf);

//	sprintf((char*)data_buf, "   %02d - %02d - %02d     ", Record_HH, Record_MM, Record_SS);
//	LCD_DisplayStringLine(Line5, data_buf);	

	if(Choose == 0)
	{
		LCD_DisplayChar(Line5, 96+16*1, Record_SS%10 + '0');
		LCD_DisplayChar(Line5, 96+16*2, Record_SS/10 + '0');
		LCD_DisplayChar(Line5, 96+16*3, '-');
		LCD_DisplayChar(Line5, 96+16*4, Record_MM%10 + '0');
		LCD_DisplayChar(Line5, 96+16*5, Record_MM/10 + '0');
		LCD_DisplayChar(Line5, 96+16*6, '-');
		LCD_SetBackColor(Yellow);
		LCD_SetTextColor(Red);
		LCD_DisplayChar(Line5, 96+16*7, Record_HH%10 + '0');
		LCD_DisplayChar(Line5, 96+16*8, Record_HH/10 + '0');	
		LCD_SetBackColor(Blue);
		LCD_SetTextColor(White);
	}
	else if(Choose == 1)
	{
		LCD_DisplayChar(Line5, 96+16*1, Record_SS%10 + '0');
		LCD_DisplayChar(Line5, 96+16*2, Record_SS/10 + '0');
		LCD_DisplayChar(Line5, 96+16*3, '-');
		LCD_SetBackColor(Yellow);
		LCD_SetTextColor(Red);
		LCD_DisplayChar(Line5, 96+16*4, Record_MM%10 + '0');
		LCD_DisplayChar(Line5, 96+16*5, Record_MM/10 + '0');
		LCD_SetBackColor(Blue);
		LCD_SetTextColor(White);
		LCD_DisplayChar(Line5, 96+16*6, '-');
		LCD_DisplayChar(Line5, 96+16*7, Record_HH%10 + '0');
		LCD_DisplayChar(Line5, 96+16*8, Record_HH/10 + '0');	
	}
	else if(Choose == 2)
	{
		LCD_SetBackColor(Yellow);
		LCD_SetTextColor(Red);
		LCD_DisplayChar(Line5, 96+16*1, Record_SS%10 + '0');
		LCD_DisplayChar(Line5, 96+16*2, Record_SS/10 + '0');
		LCD_SetBackColor(Blue);
		LCD_SetTextColor(White);
		LCD_DisplayChar(Line5, 96+16*3, '-');
		LCD_DisplayChar(Line5, 96+16*4, Record_MM%10 + '0');
		LCD_DisplayChar(Line5, 96+16*5, Record_MM/10 + '0');
		LCD_DisplayChar(Line5, 96+16*6, '-');
		LCD_DisplayChar(Line5, 96+16*7, Record_HH%10 + '0');
		LCD_DisplayChar(Line5, 96+16*8, Record_HH/10 + '0');	
	}
	else
	{
		LCD_DisplayChar(Line5, 96+16*1, Record_SS%10 + '0');
		LCD_DisplayChar(Line5, 96+16*2, Record_SS/10 + '0');
		LCD_DisplayChar(Line5, 96+16*3, '-');
		LCD_DisplayChar(Line5, 96+16*4, Record_MM%10 + '0');
		LCD_DisplayChar(Line5, 96+16*5, Record_MM/10 + '0');
		LCD_DisplayChar(Line5, 96+16*6, '-');
		LCD_DisplayChar(Line5, 96+16*7, Record_HH%10 + '0');
		LCD_DisplayChar(Line5, 96+16*8, Record_HH/10 + '0');	
	}
	sprintf((char*)data_buf, "                  2");
	LCD_DisplayStringLine(Line9, data_buf);	
}

uart.c文件

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

u8 RxBuffer2[20];
u8 RxCounter2 = 0;
u8 rx_flag = 0;

void USART2_Init(void)
{

	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

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

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

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

}

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
int fputc(int ch, FILE *f)
{
	/* 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 */
		RxBuffer2[RxCounter2++] = USART_ReceiveData(USART2);
	}
	
	if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
	{   
		USART2->SR;
		USART2->DR;
		rx_flag = 1;
	}
}

 key.c

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

u8 Trg = 0, Cont = 0;
u32 Key1_time = 0;
u32 Key2_time = 0;
u32 Key3_time = 0;
u32 Key4_time = 0;

u8 Key1_long = 0;
u8 Key2_long = 0;
u8 Key3_long = 0;
u8 Key4_long = 0;

u8 Key1_short = 0;
u8 Key2_short = 0;
u8 Key3_short = 0;
u8 Key4_short = 0;

u8 LED_Fucation = 1;
u8 Set = 0;
u8 Choose = 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_2MHz;
	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_2MHz;
	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)
	{
		if(++Key1_time >= 50){Key1_long = 1;}
	}
	else if(Cont == 0x02)
	{
		if(++Key2_time >= 50){Key2_long = 1;}
	}
	else if(Cont == 0x04)
	{
		if(++Key3_time >= 50){Key3_long = 1;}
	}
	else if(Cont == 0x08)
	{
		if(++Key4_time >= 50){Key4_long = 1;}
	}
	else if(Trg == 0 && Cont == 0)
	{
		if(Key1_time > 0 && Key1_time < 50 && Key1_long == 0){Key1_short = 1;}
		else if(Key2_time > 0 && Key2_time < 50 && Key2_long == 0){Key2_short = 1;}
		else if(Key3_time > 0 && Key3_time < 50 && Key3_long == 0){Key3_short = 1;}
		else if(Key4_time > 0 && Key4_time < 50 && Key4_long == 0){Key4_short = 1;}
	}
}

void Status_Update(void)
{
	if(Key1_short || Key1_long)	  //按下B1,打开/关闭指示灯报警
	{
		LED_Fucation = (LED_Fucation + 1) % 2;
	}
	else if(Key2_short || Key2_long)	 //按下B2,切换设置模式和显示模式
	{
		Set = (Set + 1) % 2;
		LCD_Clear(Blue);
		Choose = 5;
	}
	else if(Set && Key3_short)
	{
		Choose = (Choose + 1) % 3;			
	}
	else if(Set && (Key4_long || Key4_short))
	{
		if(Choose == 0)
		{
			if(++Record_HH == 24){Record_HH = 0;}	
		}
		else if(Choose == 1)
		{
			if(++Record_MM == 60){Record_MM = 0;}	
		}
		else if(Choose == 2)
		{
			if(++Record_SS == 60){Record_SS = 0;}	
		}		
	}
	
	if(Trg == 0 && Cont == 0)
	{
		Key1_time = 0; Key2_time = 0; Key3_time = 0; Key4_time = 0;
		Key1_long = 0; Key2_long = 0; Key3_long = 0; Key4_long = 0;
		Key1_short = 0; Key2_short = 0; Key3_short = 0; Key4_short = 0;	
	}
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

乐观的lishan

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

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

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

打赏作者

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

抵扣说明:

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

余额充值