2021物联网国赛通用库开发——D卷

2021物联网国赛通用库开发——D卷

本次所需要实现的功能如下

2021物联网国赛通用库开发——D卷_第1张图片

一:导入竞赛选手所需要使用到的头文件
#inclide "adc_reader.h"				//导入ADC头尾文件
#include "hal_oled.h"				//OLED头文件
二:使用字模取模软件进行取模
char zhi[32]={0x10,0x94,0x53,0x32,0x1E,0x32,0x52,0x10,0x00,0x7E,0x42,0x42,0x42,0x7E,0x00,0x00,0x00,0x00,0x00,0xFF,0x49,0x49,0x49,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,0x00};	//智

char neng[32]={0x08,0xCC,0x4A,0x49,0x48,0x4A,0xCC,0x18,0x00,0x7F,0x88,0x88,0x84,0x82,0xE0,0x00,0x00,0xFF,0x12,0x12,0x52,0x92,0x7F,0x00,0x00,0x7E,0x88,0x88,0x84,0x82,0xE0,0x00}; //能

char deng[32]={0x80,0x70,0x00,0xFF,0x20,0x10,0x04,0x04,0x04,0x04,0xFC,0x04,0x04,0x04,0x04,0x00,0x80,0x60,0x18,0x07,0x08,0x30,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00}; //灯

char qiang[32]={0x02,0xE2,0x22,0x22,0x3E,0x00,0x80,0x9E,0x92,0x92,0xF2,0x92,0x92,0x9E,0x80,0x00,0x00,0x43,0x82,0x42,0x3E,0x40,0x47,0x44,0x44,0x44,0x7F,0x44,0x44,0x54,0xE7,0x00}; //强

char du[32]={0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00,0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00}; //度
三:显示函数封装
void OLED_display_file()
{
	OLED_ShowCHineseArray(30,0,zhi);					//在OLED屏上初始化智能灯
	OLED_ShowCHineseArray(50,0,neng);
	OLED_ShowCHineseArray(70,0,deng);
}
四:添加变量
uint8_t Oled_Data_Show[10];			//OLED屏数据数组
五:主函数(核心代码)
int main( void )
{
   		Init();						
		OLED_Init();			//OLED屏初始化
		ADCS_Init();			//ADC初始化
		OLED_display_file();
    while( 1 )
    {
	AdcScanChannel();			//获取ADC值	
	//sprintf((char*)Oled_Data_Show,":%.2fV",AdcReadCh0());		//获取当前电压值
	sprintf((char *)Oled_Data_Show,":%.3f",(5/2.0)*(AdcReadCh0()*100));	//使用计算出光照的值并以保留三位小数的格式保存在Oled_Data_Show数组当中并输出在OLED屏上
	int gz=(5/2.0)*(AdcReadCh0()*100);				//当光照小于100时LED2亮否则灭
	if(gz<100)
	{
				
		GpioWrite(&Led2,0);
				
	}else{
			
		GpioWrite(&Led2,1);
				
	}
		OLED_ShowCHinese(0,4,29);				//输出光照字符 使用的是库中的字模
		OLED_ShowCHinese(20,4,30);
		//OLED_ShowCHineseArray(40,4,qiang);
		//OLED_ShowCHineseArray(60,4,du);
		OLED_ShowString(40,4,Oled_Data_Show);
		HAL_Delay(1000);			//在末尾一定要进行延时!!!不然会获取太快!!!!有影响显示效果
				
    }
}

注:光照是使用ADC协议来进行获取的 ADC的详细情况后期会进行总结发布!!

实现本案例时务必使用温湿度光照三合一传感器!!

特别补充:光照的计算格式如下

光照值(Lux) = ( 5 / 2.0 ) * ( 电压值 ) * 100.0 )
简化成
电压值*250
调用通用库当中的AdcReadCh0()就是获取当前的电压值!!!

最后:若读者有更好的方法,欢迎在评论区打出互相学习!!
如有纰漏,请多指教!!

你可能感兴趣的:(物联网,c语言,嵌入式硬件)