LCD副屏-CPU,内存,时显,日期显示
以前的旧模块搁置很久没用,最近看到圈子很多倒腾电脑副屏的,咱虽然没钱,但是闲啊,刚好手头有这些东西,开干。
目的:
- 显示电脑CPU占用
- 内存占用等信息
- 后台消息屏闪提示
- 开机自启
- 显示时间日期
- 背光可调
- 蓝牙断线重连
材料清单:LCD一块,HC-04蓝牙模块,10K可调电阻,杜邦线,PC,51单片机最小系统板一块(其余种类也可,有串口即可)
51单片机的STC8G系列完成硬件逻辑,蓝牙模块负责收发,项目框架如下:
51接收端资料:
1 #include <STC8.H> 2 #include "LCD1602.h" 3 4 // #include "Display1602.h" 5 6 7 #define uchar unsigned char 8 #define uint unsigned int 9 10 sbit res_vcc = P3^0; 11 sbit res_gnd = P3^1; 12 13 sbit back1_vcc = P3^2; 14 sbit back1_gnd = P3^3; 15 16 sbit Time_vcc = P1^0; 17 sbit Time_gnd = P1^4; 18 19 int Time[3] = {0, 55, 16}; //秒,分,时 20 uint Data[3] = {20,11,2022}; //年,月,日 21 uint Week[7] = {0 ,1, 2, 3, 4, 5, 6}; 22 enum WEEK{SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}; 23 uint count; 24 uchar num; 25 char sep = '-'; 26 27 uchar i,flag,j,show_time; 28 uchar Info_buffer[16]; 29 uchar Info_buffer1[16]; 30 31 32 //uchar DS1302_Time_test[]={27,1,19,12,00,00,1}; 33 //char DS1302_Time_test[] = "27,1,19,12,00,00,1"; 34 uchar code Data_array[10] = "2022-06-14"; 35 uchar code Week_array[3] = "WED"; 36 uchar code Time_array[8] = "11:08:09"; 37 uchar code Word_array[12] = "Starry"; 38 uchar code Tem_array[] = "32.1 C"; 39 40 //void Init_Timer0(); 41 //void TimeShow(void); 42 void DataTransfer(); 43 44 void Init_UART() 45 { 46 PCON &= 0x7F; //波特率不倍速 47 SCON = 0x50; //8位数据,可变波特率 48 AUXR &= 0xBF; //定时器时钟12T模式 49 AUXR &= 0xFE; //串口1选择定时器1为波特率发生器 50 TMOD &= 0x0F; //设置定时器模式 51 TMOD |= 0x20; //设置定时器模式 52 TL1 = 0xFD; //设置定时初始值 53 TH1 = 0xFD; //设置定时重载值 54 ET1 = 0; //禁止定时器%d中断 55 TR1 = 1; //定时器1开始计时 56 EA = 1; 57 ES = 1; 58 59 } 60 61 void main() 62 { 63 P1M0 = 0; 64 P1M1 = 0; 65 66 P3M0 = 0; 67 P3M1 = 0; 68 69 P2M0 = 0; 70 P2M1 = 0; 71 72 res_vcc = 1; 73 back1_vcc = 1; 74 Time_vcc = 1; 75 76 res_gnd = 0; 77 back1_gnd = 0; 78 Time_gnd = 0; 79 80 81 LCD_set(); 82 Init_UART(); 83 // Init_Timer0(); 84 85 while(1) 86 { 87 //TimeShow(); 88 LCD_write_Chars(0,0,Info_buffer); 89 LCD_write_Chars(0,1,Info_buffer1); 90 91 if(flag == 1) 92 { 93 //LCD_write_com(0x01); 94 LCD_write_Chars(0,0,Info_buffer); 95 LCD_write_Chars(0,1,Info_buffer1); 96 for(i=0;i<16;i++) 97 Info_buffer[i]=0x00; 98 for(i=0;i<16;i++) 99 Info_buffer1[i]=0x00; 100 flag = 0; 101 } 102 103 } 104 } 105 106 void Init_Timer0() 107 { 108 EA = 1; 109 ET0 = 1; 110 TMOD = 0x01; 111 TH0 = (65536-10000)/256; 112 TL0 = (65536-10000)%256; 113 TR0 = 1; 114 } 115 116 void TimeShow(void) 117 { 118 LCD_writ_Number(0, 1, 2, Time[2]); // 显示小时 119 lcd_delay(1); 120 121 LCD_writ_Char(2, 1, '-'); 122 lcd_delay(1); 123 124 LCD_writ_Number(3, 1, 2, Time[1]); // 显示分钟 125 lcd_delay(1); 126 127 LCD_writ_Char(5 ,1 , '-'); 128 lcd_delay(1); 129 130 LCD_writ_Number(6, 1, 2, Time[0]); // 显示秒钟 131 lcd_delay(1); 132 //----------------------------------------------------- 133 LCD_writ_Number(0, 0, 4, Data[2]); // 显示日期 134 lcd_delay(1); 135 136 LCD_writ_Char(4, 0, '-'); 137 lcd_delay(1); 138 139 LCD_writ_Number(5, 0, 2, Data[1]); // 显示月份 140 lcd_delay(1); 141 142 LCD_writ_Char(7 ,0 , '-'); 143 lcd_delay(1); 144 145 LCD_writ_Number(8, 0, 2, Data[0]); // 显示年份 146 lcd_delay(1); 147 } 148 149 void UART_Interrupt() interrupt 4 150 { 151 ES = 0; 152 for(j=0;SBUF!=0X0A;j++) 153 { 154 while(!RI); 155 if(SBUF==0X0A) 156 goto A; 157 if(j<16) 158 Info_buffer[j]= SBUF; 159 else 160 Info_buffer1[j-16]= SBUF; 161 RI = 0; 162 } 163 A: RI=0; 164 flag = 1; 165 ES = 1; 166 }
电脑发送端资料:
1 #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) 2 #include <stdio.h> 3 #include <Windows.h> 4 #include<time.h> 5 #include <tchar.h> 6 #include "readport.h" 7 8 #define DIV 1024 9 #define WIDTH 7 10 #define GB(x) (((x).HighPart << 2) + ((DWORD)(x).LowPart) / 1024.0 / 1024.0 / 1024.0) 11 12 #pragma warning(disable: 4996) 13 14 #define true 1 15 typedef unsigned char U_CHAR; 16 17 //获取系统时间 18 void getSystemTime(char* buffer) { 19 20 char buf[128]; 21 time_t rawtime; 22 struct tm * timeinfo; 23 time(&rawtime); 24 timeinfo = localtime(&rawtime); 25 strftime(buf, sizeof(buf), " %m-%d %H:%M:%S", timeinfo); 26 strcpy(buffer, buf); 27 } 28 29 //CPU利用率 30 FILETIME m_preidleTime; 31 FILETIME m_prekernelTime; 32 FILETIME m_preuserTime; 33 34 __int64 CompareFileTime2(FILETIME time1, FILETIME time2) 35 { 36 __int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime; 37 __int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime; 38 39 40 return b - a; 41 } 42 43 void GetCpuUseage() 44 { 45 GetSystemTimes(&m_preidleTime, &m_prekernelTime, &m_preuserTime); 46 Sleep(1000); 47 } 48 49 double CpuUseage() 50 { 51 FILETIME idleTime; 52 FILETIME kernelTime; 53 FILETIME userTime; 54 GetSystemTimes(&idleTime, &kernelTime, &userTime); 55 56 57 int idle = CompareFileTime2(m_preidleTime, idleTime); 58 int kernel = CompareFileTime2(m_prekernelTime, kernelTime); 59 int user = CompareFileTime2(m_preuserTime, userTime); 60 61 62 if (kernel + user == 0) 63 return 0.0; 64 //(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率 65 double cpu = abs((kernel + user - idle) * 100 / (kernel + user)); 66 67 m_preidleTime = idleTime; 68 m_prekernelTime = kernelTime; 69 m_preuserTime = userTime; 70 return cpu; 71 } 72 73 //读取操作系统的名称 74 void GetSystemName(char* osname) 75 { 76 char buf[128]; 77 SYSTEM_INFO info; //用SYSTEM_INFO结构判断64位AMD处理器 78 GetSystemInfo(&info); //调用GetSystemInfo函数填充结构 79 OSVERSIONINFOEX os; 80 os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); 81 82 strcpy(buf, "unknown OperatingSystem."); 83 84 if (GetVersionEx((OSVERSIONINFO *)&os)) 85 { 86 //下面根据版本信息判断操作系统名称 87 switch (os.dwMajorVersion)//判断主版本号 88 { 89 case 4: 90 switch (os.dwMinorVersion)//判断次版本号 91 { 92 case 0: 93 if (os.dwPlatformId == VER_PLATFORM_WIN32_NT) 94 strcpy(buf, "Microsoft Windows NT 4.0"); //1996年7月发布 95 else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) 96 strcpy(buf, "Microsoft Windows 95"); 97 break; 98 case 10: 99 strcpy(buf, "Microsoft Windows 98"); 100 break; 101 case 90: 102 strcpy(buf, "Microsoft Windows Me"); 103 break; 104 } 105 break; 106 107 case 5: 108 switch (os.dwMinorVersion) //再比较dwMinorVersion的值 109 { 110 case 0: 111 strcpy(buf, "Microsoft Windows 2000"); //1999年12月发布 112 break; 113 114 case 1: 115 strcpy(buf, "Microsoft Windows XP"); //2001年8月发布 116 break; 117 118 case 2: 119 if (os.wProductType == VER_NT_WORKSTATION 120 && info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) 121 { 122 strcpy(buf, "Microsoft Windows XP Professional x64 Edition"); 123 } 124 else if (GetSystemMetrics(SM_SERVERR2) == 0) 125 strcpy(buf, "Microsoft Windows Server 2003"); //2003年3月发布 126 else if (GetSystemMetrics(SM_SERVERR2) != 0) 127 strcpy(buf, "Microsoft Windows Server 2003 R2"); 128 break; 129 } 130 break; 131 132 case 6: 133 switch (os.dwMinorVersion) 134 { 135 case 0: 136 if (os.wProductType == VER_NT_WORKSTATION) 137 strcpy(buf, "Microsoft Windows Vista"); 138 else 139 strcpy(buf, "Microsoft Windows Server 2008"); //服务器版本 140 break; 141 case 1: 142 if (os.wProductType == VER_NT_WORKSTATION) 143 strcpy(buf, "Microsoft Windows 7"); 144 else 145 strcpy(buf, "Microsoft Windows Server 2008 R2"); 146 break; 147 } 148 break; 149 } 150 }//if(GetVersionEx((OSVERSIONINFO *)&os)) 151 152 strcpy(osname, buf); 153 } 154 155 int main() { 156 157 WZSerialPort w; 158 //这里是选择端口号,其他波特率信息可在头文件修改,或者在下面重新赋值。 159 if (w.open("COM6")) 160 { 161 //cout << "打开成功" << endl; 162 //cout << "在这里我发送:恭喜发财" << endl; 163 164 //w.send("恭喜发财"); 165 //w.close(); 166 } 167 else 168 { 169 cout << "打开失败" << endl; 170 } 171 172 //获取系统时间 173 char buffer[128]; 174 getSystemTime(buffer); 175 //puts(buffer); 176 177 //系统运行时间 178 clock_t start, finish; 179 double duration; 180 181 start = clock(); 182 183 184 //系统版本名称 185 char osname[20]; 186 GetSystemName(osname); 187 //printf("%s\n", osname); 188 189 while (true) 190 { 191 // 192 193 //CPU利用率 194 U_CHAR sysStateCpu[5]; 195 double cpu = CpuUseage(); 196 sprintf((char*)sysStateCpu, "%.lf", cpu); 197 //printf("CPU:%s%%\n", sysStateCpu); 198 199 //系统时间 200 char buffer[128]; 201 getSystemTime(buffer); 202 puts(buffer); 203 204 // 系统运行时间 205 finish = clock(); 206 duration = (double)(finish - start) / CLOCKS_PER_SEC; 207 //printf("%.0f 秒\n", duration); 208 209 //内存使用率 210 char bufPreCPU[10]; 211 MEMORYSTATUSEX statex; 212 statex.dwLength = sizeof(statex); 213 GlobalMemoryStatusEx(&statex); 214 sprintf(bufPreCPU, "MEM:%ld%% CPU:%2s%% %s%\n", statex.dwMemoryLoad, sysStateCpu, buffer); 215 216 217 218 //puts(bufPreCPU); 219 w.send(bufPreCPU); 220 Sleep(1000); 221 } 222 printf("hello world!\n"); 223 system("pause"); 224 225 }
项目全部文件:
个人博客下方私信领取
http://potatoworld.top:5800/
- 为了实现开机自启,最开始可谓是煞费苦心,想了很多办法,什么计划任务,看门狗等等......最后,最方便最直接的方法还是把上位机直接扔进开机自启文件夹.....
启动文件夹开机自启:https://blog.csdn.net/misterdo/article/details/106898689
- HC-04蓝牙模块在win10系统下,怎么通过程序给它发消息?这个问题加起来快赶上写程序的时间了,网上也没有找到满意的答案。
上述图示过后,得到了蓝牙模块的端口,那就一切好说了,直接电脑打开串口软件,往里面丢东西便是。
- 串口软件发送hex数据,在选中确认为hex模式后,无需再添加0X,否则会有几个乱码。
- 如果细心一点,应该能看见图片中蓝牙模块上面有两个二极管......是的,5V转3.3V,我偷懒了,直接接了两个二极管,每个压降0.7V,加起来差不多。nice~