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

题目如下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
主要代码如下

//main.c
#include "includeall.h"

vu32 TimingDelay = 0;

void Init(void)
{
	Usart2_Init(9600);	
	SysTick_Config(SystemCoreClock/1000);//初始化滴答定时器时钟
	
	Delay_Ms(200);

	LED_Init();	
	STM3210B_LCD_Init();//初始化LCD显示屏
	Beep_Init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	
	i2c_init();
	Key_Init();
	//EXTI0_Init();
	//EXTI8_Init();
	TIM2_Init(1999, 7199);//200ms
	TIM3_Init(79, 7199);//8ms
	ADC8_Init();
	RTC_Init();
}
//Main Body
int main(void)
{
	Init();
		
	LCD_Clear(White);//清屏
	LCD_SetBackColor(White);//改背景
	LCD_SetTextColor(Black);//改字色
	
	
	while(1)
	{	
		LCD_Show();
		KeyMonitor();
		LED_Shine();
		usartDataJudge();
		
	}
}

//
void Delay_Ms(u32 nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);	
}
//usart.c
#include "includeall.h"
#include "../Driver/USART/usart.h"

void Usart2_Init(u32 baud)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);	
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
    GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = baud;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	
	USART_Init(USART2, &USART_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//
	
	USART_Cmd(USART2, ENABLE);
}

void USART2_SendByte(u8 data)
{
	USART_ClearFlag(USART2, USART_FLAG_TC);
	USART_SendData(USART2, data);
	while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == 0);//
}

void USART2_SendString(u8 *str)
{
	u8 i=0, num = 0;
	
	while(str[i]!=0)
		i++;
	num = i;
	for(i=0; i<num; i++)
	{
		USART2_SendByte(str[i]);
	}
		
}

void USART2_IRQHandler(void)
{
	u8 res = 0;	
	
	if( USART_GetITStatus(USART2, USART_IT_RXNE) )
	{
		TIM_SetCounter(TIM3, 0);
		res = USART_ReceiveData(USART2);
		
		if(g_stopreceive == 0)
		{
			g_usartrec[g_usartnum] = res;
			g_usartnum++;
		}
		else
		{
			TIM_Cmd(TIM3, ENABLE);
			g_usartnum = 0;
			g_stopreceive = 0;
			memset((void *)g_usartrec, 0, 50);
			g_usartrec[g_usartnum] = res;
			g_usartnum++;
		}
	}
}


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_TC) == RESET)
  {}

  return ch;
}

//includeall.c
#include "includeall.h"

volatile double g_V = 0;
volatile u8 g_K = 1;

volatile u8 g_LS = 1;
volatile u8 g_LK = 1;

vu8 g_TimeDisplay = 0;
volatile Tim_str g_timtemp = {23, 59, 55};
volatile Tim_str g_settime = {0, 0, 0};

volatile u8 g_page = MAIN;

volatile u8 g_underline = 0;

volatile u8 g_usartrec[50];
volatile u8 g_stopreceive = 1;
volatile u8 g_usartnum = 0;
volatile u8 g_iicsr = 0;

void LCD_DisplayADC(void)
{
	u8 strtem[10];
	u8 i = 0, num = 0;
	
	//USART2_SendString("LCD_DisplayADC1");
	sprintf((char *)strtem, "%5.2lfV", ADC_Measure() );
	//USART2_SendString("LCD_DisplayADC2");
	while(strtem[i]!=0)i++;
	num = i;
	//USART2_SendString("LCD_DisplayADC");
	for(i=0; i<num; i++)
		LCD_DisplayChar(Line5, 319-16*(4+i), strtem[i]);
	//LCD_DisplayStringLine(Line6 ,(unsigned char *)strtem);
}


void My_LCD_DisplayChar(u8 Line, u16 Add, u8 Ascii)
{
	LCD_DisplayChar(Line, 319-16*Add, Ascii);
}


void My_LCD_DisplayStringLine(u8 Line, u16 Add, u8 *ptr)
{
	u32 i = 0;

	while ((*ptr != 0) && (i < 20))	 //	20
	{
		My_LCD_DisplayChar(Line, Add+i, *ptr);
		ptr++;
		i++;
	}
}

double ADC_Measure(void)
{
	g_V = ADC_Get_Ave(8,5)/4096.0*3.3;
	return g_V;
}


void LED_Shine(void)
{
	if (g_LK == 1)
	{
		if(g_V > 0.33*g_K)
		{
			g_LS = 1;
		}
		else
		{
			g_LS = 0;
		}
	}
	else
	{
		LEDOff(LED1);
		g_LS = 0;
	}
}

void LCD_Show(void)
{
	u8 strtemp[20];
	
	if(g_page == MAIN)
	{
		sprintf( (char *)strtemp, "  V1:%4.2lfV", ADC_Measure());
		LCD_DisplayStringLine(Line2, strtemp);
		sprintf( (char *)strtemp, "  k:0.%d", g_K);
		LCD_DisplayStringLine(Line3, strtemp);
		if(g_LK)
			LCD_DisplayStringLine(Line4, (u8 *)"  LED:ON ");
		else
			LCD_DisplayStringLine(Line4, (u8 *)"  LED:OFF ");
		Time_Show();
		sprintf( (char *)strtemp, "  T:%2d-%2d-%2d", g_timtemp.hh, g_timtemp.mm, g_timtemp.ss);
		LCD_DisplayStringLine(Line5, strtemp);
		
	}
	else if(g_page == SETT)
	{
		LCD_DisplayStringLine(Line3, (u8 *)"    Setting ");
		sprintf( (char *)strtemp, "  %2d-%2d-%2d", g_settime.hh, g_settime.mm, g_settime.ss);
		LCD_DisplayStringLine(Line5, strtemp);
		if(g_underline%3 == 2)
			LCD_DisplayStringLine(Line6, (u8 *)"  __        ");
		else if(g_underline%3 == 1)
			LCD_DisplayStringLine(Line6, (u8 *)"     __     ");
		else if(g_underline%3 == 0)
			LCD_DisplayStringLine(Line6, (u8 *)"        __  ");
	}
}


void ReportVoltage(void)
{
	u8 data[25];
	
	sprintf((char*)data, "%.2lf+0.%d+%2d%2d%2d\n", g_V, g_K, g_timtemp.hh, g_timtemp.mm, g_timtemp.ss);
	USART2_SendString(data);
}

void KeyMonitor(void)
{
	if(g_page == MAIN)
	{
		switch(Key_Scan(0) ){
			case K1_PRES:
			{
				//printf("K1_PRES\r\n");
				g_LK = !g_LK;
			}
			break;
			case K2_PRES:
			{
				//printf("K2_PRES\r\n");
				//USART2_SendString("K22_PRES\r\n");
				LCD_Clear(White);
				g_page = SETT;			
			}			
		}	
	}
	else if(g_page == SETT)
	{		
		switch(Key_Scan(0) ){
			case K1_PRES:
			{
				g_LK = !g_LK;
			}
			break;
			case K2_PRES:
			{
				RTC_SetAlarm(Time_Regulate(g_settime.hh, g_settime.mm, g_settime.ss) );
				RTC_WaitForLastTask();
				LCD_Clear(White);
				g_page = MAIN;
			}
			break;
			case K3_PRES:
			{
				g_underline++;
			}
			break;
			case K4_PRES:
			{
				k4settime();				
			}
		}
	}
}


void k4settime(void)
{
	if(g_underline%3 == SUL)
	{
		if(g_settime.ss != 59 )
		{
			g_settime.ss++;
		}
		else
		{
			g_settime.ss=0;
			if(g_settime.mm != 59)
				g_settime.mm++;
			else
			{
				g_settime.mm = 0;
				g_settime.hh++;
			}							
		}
	}
	else if(g_underline%3 == MUL)
	{
		if(g_settime.mm != 59 )
		{
			g_settime.mm++;
		}
		else
		{
			g_settime.mm=0;
			if(g_settime.hh!=23)
				g_settime.hh++;
			else
			{
				g_settime.mm=0;
				g_settime.hh=0;
			}
		}				
	}
	else if(g_underline%3 == HUL)
	{
		if(g_settime.hh != 23 )
		{
			g_settime.hh++;
		}
		else
		{
			g_settime.hh=0;
		}
	}
}

void usartDataJudge(void)
{
	//static u8 i = 1;
	if(g_stopreceive && g_iicsr)
	{
		g_iicsr = 0;
		if( (g_usartrec[0] == 'k') && (g_usartrec[1] == '0') && (g_usartrec[2] == '.') 
			&& (g_usartrec[3] >='1' && (g_usartrec[3] <='9') ))
		{
//			
			USART2_SendString("ok\n");
			IIC24_SendOneData(0, g_usartrec[3]-48);
			Delay_Ms(10);
			g_K = IIC24_RecOneData(0);
			//USART2_SendByte(g_K);
			
			
		}
			
	}

}




//adc.c
#include "includeall.h"
#include "../Driver/ADC/adc.h"

void ADC8_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	ADC_InitTypeDef ADC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
	RCC_ADCCLKConfig(RCC_PCLK2_Div6); 
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	ADC_DeInit(ADC1);
	
	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);
	
	/* 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));
	
}

u16 ADC_Get(u8 ch)
{
	ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_55Cycles5);
	
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);
	while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0);
	
	return ADC_GetConversionValue(ADC1);
}

u16 ADC_Get_Ave(u8 ch, u8 times)
{
	u8 i = 0;
	u16 value = 0;
	
	for(i=0; i<times; i++)
	{
		value = value + ADC_Get(ch);
		Delay_Ms(5);
	}
	
	return value/times;
}




//rtc.c
#include "includeall.h"
#include "../Driver/RTC/rtc.h"



void RTC_Init(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	/* Enable PWR and BKP clocks */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
	/* Allow access to BKP Domain */
	PWR_BackupAccessCmd(ENABLE);
	
	/* Enable the LSI OSC */
	RCC_LSICmd(ENABLE);	
	
	//USART2_SendString("Wait LSI\r\n");
	/* Wait till LSI is ready */
	while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
	{}
	/* Select the RTC Clock Source */
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
	/* Enable RTC Clock */
	RCC_RTCCLKCmd(ENABLE);
	/* Wait for RTC registers synchronization */
	RTC_WaitForSynchro();
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
		
	NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
	
	if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A4)
	{
		/* Backup data register value is not correct or not yet programmed (when
		   the first time the program is executed) */

		//USART2_SendString("RTC not yet configured....\r\n");

		/* RTC Configuration */		
		/* Reset Backup Domain */
		//BKP_DeInit();

		/* Enable the RTC Second */
		RTC_ITConfig(RTC_IT_SEC|RTC_IT_ALR, ENABLE);

		/* Wait until last write operation on RTC registers has finished */
		RTC_WaitForLastTask();

		/* Set RTC prescaler: set RTC period to 1sec */
		RTC_SetPrescaler(39999);

		/* Wait until last write operation on RTC registers has finished */
		RTC_WaitForLastTask();

		//USART2_SendString("RTC configured....\r\n");

		/* Adjust time by values entered by the user on the hyperterminal */
		Time_Adjust(23, 59, 55);

		BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
	}
	else
	{
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP|RCC_APB1Periph_PWR,ENABLE); //RTC是存在后备区域中的,所以是要开启后备区域和电源时钟的
		PWR_BackupAccessCmd(ENABLE);	   //电源控制寄存器(PWR_CR)的BPB位使能对后备区域和RTC的访问
		RCC_RTCCLKCmd(ENABLE);
		//USART2_SendString("No need to configure RTC....\r\n");
		/* Wait for RTC registers synchronization */

		RTC_WaitForSynchro();

		RTC_SetAlarm(Time_Regulate(0, 0, 0));
		/* Enable the RTC Second */
		RTC_ITConfig(RTC_IT_SEC|RTC_IT_ALR, ENABLE);

		/* Wait until last write operation on RTC registers has finished */
		RTC_WaitForLastTask();
		
	}

	//Time_Show();

	//USART2_SendString("RTC end \r\n");
}



uint32_t Time_Regulate(u8 hh, u8 mm, u8 ss)
{
	uint32_t Tmp_HH = 0xFF, Tmp_MM = 0xFF, Tmp_SS = 0xFF;
	
	Tmp_HH = hh;
	
	Tmp_MM = mm;
	
	Tmp_SS = ss;
	

	/* Return the value to store in RTC counter register */
	return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
}

void Time_Adjust(u8 hh, u8 mm, u8 ss)
{
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
	/* Change the current time */
	RTC_SetCounter(Time_Regulate(hh, mm, ss));
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
}

void Time_Display(uint32_t TimeVar)
{
	//uint32_t THH = 0, TMM = 0, TSS = 0;
	u8 Time[30];

	/* Reset RTC Counter when Time is 23:59:59 */
	if (RTC_GetCounter() == 0x00015180)//7f变80
	{
	 RTC_SetCounter(0x0);
	 /* Wait until last write operation on RTC registers has finished */
	 RTC_WaitForLastTask();
	}

	/* Compute  hours */
	 g_timtemp.hh = (TimeVar / 3600)<24?(TimeVar / 3600):0;
	/* Compute minutes */
	g_timtemp.mm = (TimeVar % 3600) / 60;
	/* Compute seconds */
	g_timtemp.ss = (TimeVar % 3600) % 60;
	sprintf((char*)Time, "Time: %2d:%2d:%2d\r\n", g_timtemp.hh, g_timtemp.mm, g_timtemp.ss);
}

/**
  * @brief  Shows the current time (HH:MM:SS) on the Hyperterminal.
  * @param  None
  * @retval None
  */   
void Time_Show(void)
{
		
	/* If 1s has been elapsed */
	if (g_TimeDisplay == 1)
	{
	  /* Display current time */
		//USART2_SendString("Time_Display\r\n");
		Time_Display(RTC_GetCounter());
		g_TimeDisplay = 0;
	}	
}

void RTC_IRQHandler(void)
{
	if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
	{
		//USART2_SendString("RTCirq\r\n");
		/* Clear the RTC Second interrupt */
		RTC_ClearITPendingBit(RTC_IT_SEC);

		/* Toggle LED2 */
		//LEDInvert(LED5|LED6);

		/* Enable time update */
		g_TimeDisplay = 1;
		
		/* Wait until last write operation on RTC registers has finished */
		RTC_WaitForLastTask();

	}
	else if(RTC_GetITStatus(RTC_IT_ALR) )
	{
		ReportVoltage();
		RTC_ClearITPendingBit(RTC_IT_ALR);
	}
}




// A code block

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值