STM32F103 IIC通信实验:BH1750与bmp180的应用

版权申诉
0 下载量 126 浏览量 更新于2024-10-24 收藏 3KB ZIP 举报
本资源集包含了一系列与STM32F103单片机通过IIC(也称为I2C)总线与BH1750光强度传感器和BMP180气压/温度传感器通信的实验和代码示例。这些资源特别适合那些希望在嵌入式系统开发领域深入了解IIC总线协议和传感器集成的开发者。以下将详细介绍资源中的知识点: 1. IIC(I2C)总线协议: IIC(Inter-Integrated Circuit)总线是一种由菲利普半导体(现为恩智浦半导体)开发的多主机串行计算机总线,用于连接低速外围设备到处理器和微控制器。IIC总线具有多主机功能,支持串行数据传输,通常使用两条线:一条串行数据线(SDA)和一条串行时钟线(SCL)。IIC总线支持多个从设备连接至同一总线,每个从设备都有一个独立的地址,而主机设备负责发起通信、产生时钟信号以及终止通信。 2. STM32F103单片机: STM32F103系列是STMicroelectronics(意法半导体)生产的一款基于ARM Cortex-M3内核的高性能微控制器,具有丰富的外设接口,包括多个I2C总线接口。这些微控制器非常适合用于嵌入式应用,特别是当需要通过各种传感器接口与外部设备通信时。STM32F103因其高性能和低功耗特性,在工业控制、医疗设备、消费电子产品等众多领域得到了广泛应用。 3. BH1750光强度传感器: BH1750是一款数字光强度传感器,采用IIC总线接口,能够测量环境光线强度,并将其转换为数字信号输出。该传感器可以测量的光照强度范围为1至65535 lux,精度高,且内置有50/60Hz的杂波滤波器,可以有效减少环境灯光干扰。BH1750广泛应用于环境光检测、LCD屏幕亮度自动调整等场景。 4. BMP180气压/温度传感器: BMP180是一款高精度、超小型的气压/温度传感器,同样使用IIC总线接口,能够提供准确的气压和温度读数。它通常被用于移动设备、户外运动设备、气象站等,以实现高度计、天气预报等功能。BMP180采用16位ADC(模数转换器),与前代产品BMP085相比,尺寸更小,功耗更低,且精度更高。 5. 代码示例和实验: 提供的文件中可能包含了针对STM32F103单片机与BH1750和BMP180传感器通信的代码示例,这些代码可帮助开发者了解如何通过IIC总线对传感器进行初始化、配置以及数据读取等操作。开发者可以利用这些示例来加深对IIC协议通信流程、数据转换以及传感器特性的理解,并在此基础上进行相应的修改和扩展,以适应自己的项目需求。 总结以上,该资源集为开发者提供了一个完整的平台,用以学习和实践IIC总线通信技术以及如何在STM32F103单片机上集成BH1750和BMP180这两种常用的传感器。通过深入研究和操作这些实验和代码,开发者能够更好地掌握传感器数据采集、处理以及应用开发的关键技能,并能将这些技能应用到各种嵌入式系统项目中。

import os from PyQt5.QtCore import Qt from PyQt5.QtGui import QPixmap, QIcon from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout, QTreeView, QFileSystemModel class ImageViewer(QWidget): def init(self, folder_path): super().init() self.folder_path = folder_path self.image_dict = {} self.current_image = None self.setWindowTitle("Image Viewer") self.setFixedSize(1000, 600) self.image_label = QLabel(self) self.image_label.setAlignment(Qt.AlignCenter) self.tree_view = QTreeView() self.tree_view.setMinimumWidth(250) self.tree_view.setMaximumWidth(250) self.model = QFileSystemModel() self.model.setRootPath(folder_path) self.tree_view.setModel(self.model) self.tree_view.setRootIndex(self.model.index(folder_path)) self.tree_view.setHeaderHidden(True) self.tree_view.setColumnHidden(1, True) self.tree_view.setColumnHidden(2, True) self.tree_view.setColumnHidden(3, True) self.tree_view.doubleClicked.connect(self.tree_item_double_clicked) self.main_layout = QHBoxLayout(self) self.main_layout.addWidget(self.tree_view) self.main_layout.addWidget(self.image_label) self.load_images() self.update_image() def load_images(self): for file_name in os.listdir(self.folder_path): if file_name.lower().endswith((".jpg", ".jpeg", ".png", ".gif", ".bmp")): file_path = os.path.join(self.folder_path, file_name) self.image_dict[file_name] = file_path current_image = list(self.image_dict.keys())[0] def update_image(self): if self.current_image is not None: pixmap = QPixmap(self.image_dict[self.current_image]) self.image_label.setPixmap(pixmap.scaled(self.width() - self.tree_view.width(), self.height(), Qt.KeepAspectRatio, Qt.SmoothTransformation)) def tree_item_double_clicked(self, index): file_name = self.model.fileName(index) if file_name in self.image_dict: self.current_image = file_name self.update_image() def keyPressEvent(self, event): if event.key() == Qt.Key_A: self.previous_image() elif event.key() == Qt.Key_D: self.next_image() elif event.key() in [Qt.Key_1, Qt.Key_2, Qt.Key_3, Qt.Key_4, Qt.Key_5]: self.save_text_file(event.key() - Qt.Key_0) def previous_image(self): if self.current_image is not None: file_names = list(self.image_dict.keys()) current_index = file_names.index(self.current_image) if current_index > 0: self.current_image = file_names[current_index - 1] else: self.current_image = file_names[-1] self.update_image() def next_image(self): if self.current_image is not None: file_names = list(self.image_dict.keys()) current_index = file_names.index(self.current_image) if current_index < len(file_names) - 1: self.current_image = file_names[current_index + 1] else: self.current_image = file_names[0] self.update_image() def save_text_file(self, number): if self.current_image is not None: file_name = self.current_image txt_file_path = os.path.join(self.folder_path, os.path.splitext(file_name)[0] + ".txt") with open(txt_file_path, "w") as file: file.write(str(number)) if name == "main": import sys app = QApplication(sys.argv) viewer = ImageViewer("D:/图片/wallpaper") viewer.show() sys.exit(app.exec_())这份代码实现不了使用键盘的A键向上翻页以及D键向下翻页,也实现不了键盘数字键生成相应txt文档,帮我分析一下错在哪里

152 浏览量
182 浏览量

程序提示AttributeError: 'ImageThread' object has no attribute '_dgl',优化程序 def __init__(self, pipeline, color_label, depth_label, interval, color_photo_dir, depth_photo_dir): super().__init__() self.pipeline = pipeline self.color_label = color_label self.depth_label = depth_label self.is_running = True self.interval = interval self.color_photo_dir = color_photo_dir self.depth_photo_dir = depth_photo_dir self.saved_color_photos = 0 self.saved_depth_photos = 0 def save_photo(self, color_image, depth_image): # 保存彩色图和深度图 filename = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S-{}.bmp".format(self.saved_color_photos)) color_image.save(os.path.join(self.color_photo_dir, filename), "BMP") depth_image.save(os.path.join(self.depth_photo_dir, filename), "BMP") # print(self.color_photo_dir) # 更新已保存照片数量标签 self.saved_color_photos += 1 self.saved_depth_photos += 1 self.saved_color_photos_signal.emit(self.saved_color_photos) self.saved_depth_photos_signal.emit(self.saved_depth_photos) def run(self): ROT = 3 while self.is_running: # 从相机获取彩色图和深度图 frames = self.pipeline.wait_for_frames() color_frame = frames.get_color_frame() depth_frame = frames.get_depth_frame() depth_image = np.asanyarray(depth_frame.get_data()) color_image = np.asanyarray(color_frame.get_data()) # 转换成 Qt 图像格式 depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET) # 将深度图像转换为伪彩色图像 color_image = QImage(color_image, color_image.shape[1], color_image.shape[0], color_image.shape[1] * 3, QImage.Format_RGB888) depth_colormap = QImage(depth_colormap, depth_colormap.shape[1], depth_colormap.shape[0], depth_colormap.shape[1] * 3, QImage.Format_RGB888) # 显示图像 self.color_label.setPixmap(QPixmap.fromImage(color_image)) self.depth_label.setPixmap(QPixmap.fromImage(depth_colormap)) v = self._dgl.qpin(ROT) if len(v) > 0: self._count += sum(v) if self._count > self._inspect_step: self.save_photo(color_image, depth_colormap) self._count -= self._inspect_step

112 浏览量