STM32驱动OLED显示菜单功能(附带oled硬件iic驱动代码)

STM32驱动OLED显示菜单功能(附带oled硬件iic驱动代码)_第1张图片

 STM32驱动OLED显示菜单功能(附带oled硬件iic驱动代码)_第2张图片

 while循环里面就写菜单退出 菜单进入


	static int flag=1;     //记录你的选择
	//if(return_FLAG==1)return_FLAG=0;
	OLED_GotoXY(10,0);
	OLED_Puts("1 LED_TEST",&Font_11x18,1);
	OLED_GotoXY(10,20);
	OLED_Puts("2 USART_TEST",&Font_11x18,1);
	OLED_GotoXY(10,40);
	OLED_Puts("3 ADC_TEST",&Font_11x18,1);
	OLED_Refresh();
	while(1)
	{
		
		if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==0)       //***下一项***
		{		
			HAL_Delay(10);	  //防抖
			while(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==0); //按下后不松手,则停在这
			HAL_Delay(10);	  //防抖
			flag++;
			if(flag==4) flag=1;			
		}
		
		if(HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin)==0)       //***确认***
		{
			HAL_Delay(10);	  //防抖
			while(HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin)==0); //按下后不松手,则停在这
			HAL_Delay(10);	  //防抖			
			OLED_Clear();   //清屏
			return(flag);   //返回你的选择
		}
		
		switch(flag)      //光标移动
		{
			case 1:
			{
				OLED_GotoXY(120,5);
				OLED_Puts("*",&Font_7x10,1);
				OLED_GotoXY(120,25);
				OLED_Puts(" ",&Font_7x10,1);
				OLED_GotoXY(120,45);
				OLED_Puts(" ",&Font_7x10,1);
				OLED_Refresh();
				
			}break;
			case 2:
			{
				OLED_GotoXY(120,5);
				OLED_Puts(" ",&Font_7x10,1);
				OLED_GotoXY(120,25);
				OLED_Puts("*",&Font_7x10,1);
				OLED_GotoXY(120,45);
				OLED_Puts("",&Font_7x10,1);
				OLED_Refresh();
			}break;
			case 3:
			{
				OLED_GotoXY(120,5);
				OLED_Puts(" ",&Font_7x10,1);
				OLED_GotoXY(120,25);
				OLED_Puts(" ",&Font_7x10,1);
				OLED_GotoXY(120,45);
				OLED_Puts("*",&Font_7x10,1);	
			OLED_Refresh();				
			}break;
		}		
		
	}

	
} 
int menu2_LED(void)
{
	
	
	OLED_Clear();
	OLED_GotoXY(10,20);
	OLED_Puts("!LED_G_ON!",&Font_11x18,1);
	OLED_Refresh();
	while(1)
	{
		if(!return_FLAG)
		{
		HAL_GPIO_WritePin(LED_GREEN_GPIO_Port,LED_GREEN_Pin,RESET);
		HAL_Delay(1000);
		HAL_GPIO_WritePin(LED_GREEN_GPIO_Port,LED_GREEN_Pin,SET);
		HAL_Delay(1000);
		}
		else
		{
			return_FLAG=0;
			return 0;
		}
	}
}
int menu2_USART(void)
{
	OLED_Clear();
	int flag_T=1; //choose to fasong flag
	uint8_t rx_buf[3];
	OLED_GotoXY(5,0);
	OLED_Puts("A",&Font_11x18,1);
	OLED_GotoXY(5,20);
	OLED_Puts("B",&Font_11x18,1);
	OLED_GotoXY(5,40);
	OLED_Puts("C",&Font_11x18,1);
	OLED_GotoXY(50,25);
	OLED_Puts("recive:",&Font_7x10,1);
	OLED_Refresh();
	while(1)
	{
		
		if(!return_FLAG)
			{	
			HAL_UART_Receive_DMA(&huart1,(uint8_t*)rx_buf,3);
			OLED_GotoXY(100,25);
			OLED_Puts(rx_buf,&Font_7x10,1);
		
			if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==0)       //***下一项***
			{		
				HAL_Delay(10);	  //防抖
				while(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==0); //按下后不松手,则停在这
				HAL_Delay(10);	  //防抖
				flag_T++;
				if(flag_T==4) flag_T=1;			
			}
		
			if(HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin)==0)       //***确认***
			{
				HAL_Delay(10);	  //防抖
				while(HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin)==0); //按下后不松手,则停在这
				HAL_Delay(10);	  //防抖			
				if(flag_T==1)printfl("A\r\n");
				if(flag_T==2)printfl("B\r\n");
				if(flag_T==3)printfl("C\r\n");
			}
			
		
		
		
				switch(flag_T)      //光标移动
				{
					case 1:
					{	
						OLED_GotoXY(20,5);
						OLED_Puts("*",&Font_7x10,1);
						OLED_GotoXY(20,25);
						OLED_Puts(" ",&Font_7x10,1);
						OLED_GotoXY(20,45);
						OLED_Puts(" ",&Font_7x10,1);
						OLED_Refresh();
					
					}break;
					case 2:
					{
						OLED_GotoXY(20,5);
						OLED_Puts(" ",&Font_7x10,1);
						OLED_GotoXY(20,25);
						OLED_Puts("*",&Font_7x10,1);
						OLED_GotoXY(20,45);
						OLED_Puts("",&Font_7x10,1);
						OLED_Refresh();
					}break;
					case 3:
					{
						OLED_GotoXY(20,5);
						OLED_Puts(" ",&Font_7x10,1);
						OLED_GotoXY(20,25);
						OLED_Puts(" ",&Font_7x10,1);
						OLED_GotoXY(20,45);
						OLED_Puts("*",&Font_7x10,1);	
					OLED_Refresh();				
					}break;
				}	
		
			}
			
			else
			{
				HAL_UART_DMAStop(&huart1);
				OLED_Clear();
				HAL_Delay(150);//mo ming qi miao de yan shi, deng dai geng xin flag
				return_FLAG=0;
				return 1;
			
			} 
	
	}

}

这里是选择发送ABC 和接受字符

退出回调函数:


void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin==GPIO_PIN_13)
	{
		return_FLAG=1;
		
	}



}

任务三是adc采样 双通道采样 

int menu2_ADC(void)
{
	
	OLED_Clear();
	OLED_GotoXY(5,0);
	OLED_Puts("X:",&Font_16x26,1);
	OLED_GotoXY(5,30);
	OLED_Puts("Y:",&Font_16x26,1);
	OLED_Refresh();
	uint16_t adc_buf[2];
	HAL_ADC_Start_DMA(&hadc1,(uint32_t*)adc_buf,2);
	
	while(1)
	{
		if(!return_FLAG)
		{
			uint8_t Kp_txt1[10];
			sprintf(Kp_txt1,"%.4fV",((float)adc_buf[0]*3.3)/4095);
			OLED_GotoXY(50,5);
			OLED_Puts(Kp_txt1,&Font_11x18,1);
			uint8_t Kp_txt2[10];
			sprintf(Kp_txt2,"%.4fV",((float)adc_buf[1]*3.3)/4095);
			OLED_GotoXY(50,35);
			OLED_Puts(Kp_txt2,&Font_11x18,1);
			OLED_Refresh();
			HAL_Delay(200);
		}
		
		else
		{
			HAL_ADC_Stop_DMA(&hadc1);
			OLED_Clear();
			return_FLAG=0;
			return 0;
		}
		
	}
}

 利用dma采样双通道

HAL_ADC_Start_DMA(&hadc1,(uint32_t*)adc_buf,2);

将32位浮点数转换为8位可展示 

 

 

			uint8_t Kp_txt1[10];
			sprintf(Kp_txt1,"%.4fV",((float)adc_buf[0]*3.3)/4095);

这里就是转换了

下面是oled.c和oled.h文件了 在下章给出

你可能感兴趣的:(stm32,嵌入式硬件,单片机)