diff --git a/ReadMe.md b/ReadMe.md index 98faaee..04d0f78 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -38,6 +38,17 @@ uart_temp是用于8051单片机上的一个项目,用于获取温度,并使 4. 打开串口软件,接收来自单片机的数据 +5. 运行`process.py`解析数据生成图片 + + ```bash + # 新建虚拟环境 + virtualenv env + # 安装依赖 + pip install -r requirements.txt + # 运行脚本 + python process.py + ``` + ## 更多的信息 [从Keil迁移到SDCC](https://blog.csdn.net/weixin_44681954/article/details/135286404) diff --git a/process.py b/process.py new file mode 100644 index 0000000..f339251 --- /dev/null +++ b/process.py @@ -0,0 +1,42 @@ +#!./env/bin/python +from datetime import datetime, timedelta +import matplotlib.pyplot as plt +from pylab import mpl + +# 设置显示中文字体 +mpl.rcParams["font.sans-serif"] = ["SimHei"] + +# 初始化一个起始日期(这里假设是今天凌晨0点) +start_date = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) + +with open('minicom.cap', 'r') as file: + lines = file.readlines() + +times = [] +temperatures = [] + +for line in lines: + time_str, temp_str = line.strip().split('temp = ') + time = start_date + timedelta(seconds=int(time_str.split(':')[2])) + \ + timedelta(minutes=int(time_str.split(':')[1])) + \ + timedelta(hours=int(time_str.split(':')[0])) + temperature = float(temp_str) + times.append(time) + temperatures.append(temperature) + +# 确保时间序列是排序的 +times.sort() + +# 绘制折线图 +plt.plot(times, temperatures) +plt.xlabel('时间') +plt.ylabel('温度 (摄氏度)') +plt.title('温度随时间变化图') +plt.xticks(rotation=45) # 旋转x轴标签以更好地展示时间 + +# 自动调整x轴刻度以显示日期时间格式 +plt.gcf().autofmt_xdate() + +# 显示图形 +plt.savefig("img.png") +plt.show() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d5c7910 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +matplotlib==3.8.2 diff --git a/src/main.c b/src/main.c index f8b392e..14d449c 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ void main(void) int pre_temp_value=0; int temp_value=0; u8 temp_buf[5]; + int HH=0, MM=0, SS=0; uart_init(0XFA);//波特率为9600 ds18b20_init();//初始化DS18B20 @@ -59,8 +60,20 @@ void main(void) // } // delay - delay_ms(1000); - printf("temp = %.1f\r\n", temp_value/10.0); + if(temp_value != 0) { + SS ++; + if (SS == 60){ + SS = 0; + MM ++; + } + if(MM == 60){ + MM = 0; + HH ++; + } + delay_ms(1000); + printf("%02d:%02d:%02d temp = %.1f\r\n", HH, MM, SS, temp_value/10.0); + } + } }