diff --git a/libraries/QMA7981/README.md b/libraries/QMA7981/README.md new file mode 100644 index 0000000..56d2299 --- /dev/null +++ b/libraries/QMA7981/README.md @@ -0,0 +1,157 @@ +# QMA7981 Accelerometer Driver Documentation + +## Overview + +This document describes how to use the `i2c_qma7981.py` driver to interface with the QMA7981 accelerometer sensor. The driver provides functionalities for sensor initialization, interrupt configuration, acceleration data reading, and step counting. + +## Key Features + +1. Sensor initialization and interrupt pin configuration +2. Configuration of various interrupt types (motion detection, no-motion detection, step counting, hand raise detection, etc.) +3. Three-axis acceleration data reading +4. Step count reading +5. Step counter clearing + +## Quick Start + +### 1. Import Module and Initialize Sensor + +```python +from machine import ExtInt +from i2c_qma7981 import qma7981 + +# Define interrupt callback function +def user_cb(event, data): + if event == qma7981.SIG_MOT_INT: + print("Significant motion interrupt triggered") + elif event == qma7981.ANY_MOT_INT_X: + print("X-axis motion interrupt triggered") + # Other event handling... + +# Initialize sensor with GPIO33 as INT1 interrupt pin, falling edge trigger +sensor = qma7981(user_cb, INT1=ExtInt.GPIO33, INT1_output_mode=qma7981.IRQ_FALLING) +``` + +### 2. Configure Interrupts + +#### Any-Motion Interrupt + +```python +# Enable any-motion interrupt with threshold of 200mg and sample count of 1 +sensor.set_any_motion_intr(True, threshod=200, sample_times=1) +``` + +#### Significant Motion Interrupt (Axis-specific) + +```python +# Enable X-axis significant motion interrupt +sensor.set_sig_motion_intr(True, threshod=300, sample_times=2, axis_direction=0) +``` + +#### No-Motion Interrupt + +```python +# Enable no-motion interrupt with threshold of 100mg and duration of 10 sample periods +sensor.set_no_motion_intr(True, threshod=100, duration_time=10, axis_direction=0x03) +``` + +#### Step Counter Interrupt + +```python +# Enable step counter interrupt +sensor.set_step_intr(True) +``` + +#### Hand Raise Interrupt + +```python +# Enable hand raise detection interrupt +sensor.set_raise_intr(True, wake_sum_th=10, wake_diff_th=1.0) +``` + +### 3. Read Sensor Data + +#### Read Acceleration Data + +```python +acc_data = sensor.readacc() +print(f"X: {acc_data[0]}mg, Y: {acc_data[1]}mg, Z: {acc_data[2]}mg") +``` + +#### Read Step Count + +```python +step_count = sensor.readstep() +print(f"Step count: {step_count}") +``` + +#### Clear Step Counter + +```python +sensor.clearstep() +``` + +## Interrupt Type Constants + +| Constant | Value | Description | +| :------------- | :---- | :--------------------------- | +| SIG_MOT_INT | 0 | Significant motion interrupt | +| ANY_MOT_INT_X | 1 | X-axis motion interrupt | +| ANY_MOT_INT_Y | 2 | Y-axis motion interrupt | +| ANY_MOT_INT_Z | 3 | Z-axis motion interrupt | +| NO_MOT_INT | 4 | No-motion interrupt | +| HAND_RAISE_INT | 5 | Hand raise interrupt | +| HAND_DOWN_INT | 6 | Hand down interrupt | +| STEP_INT | 7 | Step count interrupt | + +## Example Code + +```python +from machine import ExtInt +from i2c_qma7981 import qma7981 +import utime as time + +def sensor_callback(event, data): + if event == qma7981.SIG_MOT_INT: + print("Significant motion detected") + elif event == qma7981.ANY_MOT_INT_X: + print("X-axis motion detected") + elif event == qma7981.STEP_INT: + print(f"Step count updated: {data}") + elif event == qma7981.HAND_RAISE_INT: + print("Hand raise detected") + # Read current acceleration + acc = sensor.readacc() + print(f"Current acceleration - X: {acc[0]}mg, Y: {acc[1]}mg, Z: {acc[2]}mg") + +# Initialize sensor +sensor = qma7981(sensor_callback, INT1=ExtInt.GPIO33, INT1_output_mode=qma7981.IRQ_FALLING) + +# Configure interrupts +sensor.set_any_motion_intr(True, threshod=200) # 200mg threshold +sensor.set_step_intr(True) # Enable step counting +sensor.set_raise_intr(True) # Enable hand raise detection + +# Main loop +while True: + # Read step count every 5 seconds + steps = sensor.readstep() + print(f"Current step count: {steps}") + time.sleep(5) +``` + +## Important Notes + +1. Ensure correct I2C address (determined by AD0 pin: 0x12 or 0x13) +2. Interrupt pin configuration must match actual hardware connection +3. Acceleration unit is in mg by default (1g = 1000mg) +4. Sample period and threshold settings should be adjusted according to application requirements + +## Technical Specifications + +- **Communication Interface**: I2C (supports standard and fast modes, 100kHz to 400kHz) +- **Acceleration Range**: Configurable (default 16g with 1.95mg/LSB resolution) +- **Interrupt Modes**: Latch mode by default +- **Power Mode**: Active mode (0xC0 written to PM_ADDR register during initialization) + +This driver provides a comprehensive interface to utilize all major features of the QMA7981 accelerometer in your embedded projects. The interrupt-based architecture allows for efficient event detection without constant polling. \ No newline at end of file diff --git a/libraries/QMA7981/README_zh.md b/libraries/QMA7981/README_zh.md new file mode 100644 index 0000000..5cccbba --- /dev/null +++ b/libraries/QMA7981/README_zh.md @@ -0,0 +1,150 @@ +# QMA7981 加速度传感器驱动使用说明文档 + +## 概述 + +本文档介绍如何使用 `i2c_qma7981.py` 驱动文件与 QMA7981 加速度传感器进行交互。该驱动提供了初始化传感器、配置中断、读取加速度数据和步数等功能。 + +## 主要功能 + +1. 初始化传感器并配置中断引脚 +2. 设置不同类型的中断(运动检测、静止检测、步数计数、抬手检测等) +3. 读取三轴加速度数据 +4. 读取步数计数 +5. 清除步数计数 + +## 快速开始 + +### 1. 导入模块并初始化传感器 + +```python +from machine import ExtInt +from i2c_qma7981 import qma7981 + +# 定义中断回调函数 +def user_cb(event, data): + if event == qma7981.SIG_MOT_INT: + print("显著运动中断触发") + elif event == qma7981.ANY_MOT_INT_X: + print("X轴运动中断触发") + # 其他事件处理... + +# 初始化传感器,使用GPIO33作为INT1中断引脚,下降沿触发 +sensor = qma7981(user_cb, INT1=ExtInt.GPIO33, INT1_output_mode=qma7981.IRQ_FALLING) +``` + +### 2. 配置中断 + +#### 任意方向运动中断 + +```python +# 启用任意方向运动中断,阈值为200mg,采样次数为1次 +sensor.set_any_motion_intr(True, threshod=200, sample_times=1) +``` + +#### 特定方向显著运动中断 + +```python +# 启用X轴显著运动中断 +sensor.set_sig_motion_intr(True, threshod=300, sample_times=2, axis_direction=0) +``` + +#### 静止检测中断 + +```python +# 启用静止检测中断,阈值为100mg,持续时间为10个采样周期 +sensor.set_no_motion_intr(True, threshod=100, duration_time=10, axis_direction=0x03) +``` + +#### 步数计数中断 + +```python +# 启用步数计数中断 +sensor.set_step_intr(True) +``` + +#### 抬手检测中断 + +```python +# 启用抬手检测中断 +sensor.set_raise_intr(True, wake_sum_th=10, wake_diff_th=1.0) +``` + +### 3. 读取传感器数据 + +#### 读取加速度数据 + +```python +acc_data = sensor.readacc() +print(f"X: {acc_data[0]}mg, Y: {acc_data[1]}mg, Z: {acc_data[2]}mg") +``` + +#### 读取步数 + +```python +step_count = sensor.readstep() +print(f"步数: {step_count}") +``` + +#### 清除步数计数 + +```python +sensor.clearstep() +``` + +## 中断类型常量 + +| 常量 | 值 | 描述 | +| :------------- | :--- | :----------- | +| SIG_MOT_INT | 0 | 显著运动中断 | +| ANY_MOT_INT_X | 1 | X轴运动中断 | +| ANY_MOT_INT_Y | 2 | Y轴运动中断 | +| ANY_MOT_INT_Z | 3 | Z轴运动中断 | +| NO_MOT_INT | 4 | 静止中断 | +| HAND_RAISE_INT | 5 | 抬手中断 | +| HAND_DOWN_INT | 6 | 放手中断 | +| STEP_INT | 7 | 步数中断 | + +## 示例代码 + +```python +from machine import ExtInt +from i2c_qma7981 import qma7981 +import utime as time + +def sensor_callback(event, data): + if event == qma7981.SIG_MOT_INT: + print("检测到显著运动") + elif event == qma7981.ANY_MOT_INT_X: + print("检测到X轴运动") + elif event == qma7981.STEP_INT: + print(f"步数更新: {data}") + elif event == qma7981.HAND_RAISE_INT: + print("检测到抬手动作") + # 读取当前加速度 + acc = sensor.readacc() + print(f"当前加速度 - X: {acc[0]}mg, Y: {acc[1]}mg, Z: {acc[2]}mg") + +# 初始化传感器 +sensor = qma7981(sensor_callback, INT1=ExtInt.GPIO33, INT1_output_mode=qma7981.IRQ_FALLING) + +# 配置中断 +sensor.set_any_motion_intr(True, threshod=200) # 200mg阈值 +sensor.set_step_intr(True) # 启用步数计数 +sensor.set_raise_intr(True) # 启用抬手检测 + +# 主循环 +while True: + # 每5秒读取一次步数 + steps = sensor.readstep() + print(f"当前总步数: {steps}") + time.sleep(5) +``` + +## 注意事项 + +1. 确保I2C地址正确(AD0脚决定地址为0x12或0x13) +2. 中断引脚配置需与实际硬件连接一致 +3. 加速度单位默认为mg(1g=1000mg) +4. 采样周期和阈值设置需根据实际应用场景调整 + +通过以上API,您可以方便地集成QMA7981加速度传感器到您的项目中,实现运动检测、步数计数等功能。 \ No newline at end of file diff --git a/libraries/RC522/README.md b/libraries/RC522/README.md new file mode 100644 index 0000000..580bbb0 --- /dev/null +++ b/libraries/RC522/README.md @@ -0,0 +1,165 @@ +# RC522 RFID Module User Manual + +## Overview + +This document explains how to use the RC522 RFID module based on QuecPython for card reading and writing operations. The RC522 is a contactless read/write chip that supports the ISO14443A protocol, commonly used in access control systems, membership card systems, and other applications. + +## Hardware Connection + +Before using the RC522 module, ensure proper hardware connections: + +- SPI interface connection +- RST pin connected to GPIO12 +- IRQ pin connected to GPIO11 (optional interrupt function) + +## Quick Start + +### 1. Initialize the RC522 Module + +```python +from mfrc522 import Mfrc522_spi +reader = Mfrc522_spi(pin_rst=Pin.GPIO12, pin_irq=Pin.GPIO11) +print("RC522 module initialization complete") +``` + +### 2. Basic Functionality + +#### Read Card ID + +```python +card_id = reader.read_id() +print('Detected card ID: {0}'.format(card_id)) +``` + +#### Write Data to Card + +```python +# Define the block address and data to write +blockAddr = 0x01 # Block address +data = [0x00, 0x0A, 0x10, 0x00, 0x0C, 0x00, 0xA0, 0x05, 0x00, 0x40, 0x40, 0x00, 0x10, 0x20] + +# Perform write operation +reader.Mfrc522_Write(blockAddr, data) +print("Data write complete") +``` + +#### Read Data from Card + +```python +# Read data from the specified block address +read_data = reader.Mfrc522_Read(blockAddr) +print('Read data: {0}'.format(read_data)) +``` + +## Advanced Features + +### 1. Interrupt Mode + +The module supports interrupt-based card detection. Pass the `irq_cb` parameter during initialization to set a custom interrupt callback function: + +```python +def my_callback(para): + print("Card detected nearby!") + card_id = reader.read_id_no_block() + if card_id: + print("Card ID:", card_id) + +reader = Mfrc522_spi(pin_rst=Pin.GPIO12, pin_irq=Pin.GPIO11, irq_cb=my_callback) +``` + +### 2. Multi-Block Read/Write + +The module supports reading and writing multiple data blocks: + +```python +# Define multiple block addresses +block_addrs = [8, 9, 10] + +# Write long text data (automatically distributed across blocks) +text_data = "This is a long text to be stored" +reader.write(text_data) + +# Read multi-block data +id, read_text = reader.read() +print("Read text:", read_text) +``` + +## Notes + +1. The GPIO ports in the text are for reference only. For specific interfaces, please refer to the manual of the actual development board used +2. Ensure stable power supply to the RFID module. +3. Storage structures may vary by card type—refer to the respective card specifications. +4. Some blocks may be key or control blocks—verify before writing. +5. Read/write operations require key authentication. The default key is `[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]`. + +## Troubleshooting + +1. **Unable to Detect Card**: + - Check antenna connection. + - Ensure the card type is ISO14443A. + - Adjust the distance between the card and the module. +2. **Read/Write Failures**: + - Verify if the card is writable. + - Confirm the correct block address. + - Check if the authentication key is correct. +3. **Communication Errors**: + - Inspect SPI connections. + - Verify RST pin configuration. + - Ensure stable power supply. + +## API Reference + +### Key Methods + +- `read_id()`: Read card ID (blocking). +- `read_id_no_block()`: Read card ID (non-blocking). +- `read()`: Read card data (blocking). +- `read_no_block()`: Read card data (non-blocking). +- `write(text)`: Write text data (blocking). +- `write_no_block(text)`: Write text data (non-blocking). +- `Mfrc522_Read(blockAddr)`: Read data from a specified block. +- `Mfrc522_Write(blockAddr, writeData)`: Write data to a specified block. + +## Example Code + +Complete test code: + +```python +import _thread +from mfrc522 import Mfrc522_spi +import utime + +def rc522_test(): + # Initialize RC522 module + reader = Mfrc522_spi(pin_rst=Pin.GPIO12, pin_irq=Pin.GPIO11) + print("RC522 module initialized") + + while True: + # Attempt to read card ID + card_id = reader.read_id_no_block() + if card_id: + print('Detected card ID: {0}'.format(card_id)) + + # Read data + id, text = reader.read_no_block() + if text: + print('Card data:', text) + + # Write data + new_data = "Hello, RFID!" + str(utime.time()) + reader.write(new_data) + print('New data written:', new_data) + + utime.sleep_ms(500) + +# Run test in a new thread +_thread.start_new_thread(rc522_test, ()) +``` + +## Version Information + +- Current Version: v1.0 +- Last Updated: 2023-10-15 +- Compatible Platform: QuecPython + +For further assistance, refer to the full module source code or contact technical support. \ No newline at end of file diff --git a/libraries/RC522/README_zh.md b/libraries/RC522/README_zh.md new file mode 100644 index 0000000..deb5e2f --- /dev/null +++ b/libraries/RC522/README_zh.md @@ -0,0 +1,165 @@ +# RC522 RFID模块示例代码说明文档 + +## 概述 + +本文档介绍如何使用基于QuecPython的RC522 RFID模块进行卡片读写操作。RC522是一种非接触式读写卡芯片,支持ISO14443A协议,常用于门禁系统、会员卡系统等场景。 + +## 硬件连接 + +在使用RC522模块前,请确保正确连接硬件: + +- SPI接口连接 +- RST引脚连接至GPIO12 +- IRQ引脚连接至GPIO11(可选中断功能) + +## 快速开始 + +### 1. 初始化RC522模块 + +```python +from mfrc522 import Mfrc522_spi +reader = Mfrc522_spi(pin_rst=Pin.GPIO12, pin_irq=Pin.GPIO11) +print("RC522模块初始化完成") +``` + +### 2. 基本功能使用 + +#### 读取卡片ID + +```python +card_id = reader.read_id() +print('检测到卡片ID: {0}'.format(card_id)) +``` + +#### 写入数据到卡片 + +```python +# 定义要写入的数据块地址和数据 +blockAddr = 0x01 # 块地址 +data = [0x00,0x0A,0x10,0x00,0x0C,0x00,0xA0,0x05,0x00,0x40,0x40,0x00,0x10,0x20] + +# 执行写入操作 +reader.Mfrc522_Write(blockAddr, data) +print("数据写入完成") +``` + +#### 从卡片读取数据 + +```python +# 从指定块地址读取数据 +read_data = reader.Mfrc522_Read(blockAddr) +print('读取到的数据: {0}'.format(read_data)) +``` + +## 高级功能 + +### 1. 中断模式 + +模块支持中断模式检测卡片,初始化时传入irq_cb参数可设置自定义中断回调函数: + +```python +def my_callback(para): + print("检测到卡片接近!") + card_id = reader.read_id_no_block() + if card_id: + print("卡片ID:", card_id) + +reader = Mfrc522_spi(pin_rst=Pin.GPIO12, pin_irq=Pin.GPIO11, irq_cb=my_callback) +``` + +### 2. 多块读写 + +模块支持连续读写多个数据块: + +```python +# 定义多个块地址 +block_addrs = [8, 9, 10] + +# 写入长文本数据(自动分配到多个块) +text_data = "这是一段要存储的长文本数据" +reader.write(text_data) + +# 读取多块数据 +id, read_text = reader.read() +print("读取到的文本:", read_text) +``` + +## 注意事项 + +1. 文中GPIO口仅为参考,具体接口请参考实际所用开发板说明手册 +2. 确保RFID模块供电稳定 +3. 不同卡片类型的存储结构可能不同,请参考相应卡片规格 +4. 某些块可能是密钥块或控制块,写入前请确认 +5. 读写操作需要验证密钥,默认密钥为[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] + +## 故障排除 + +1. **无法检测到卡片**: + - 检查天线连接 + - 确认卡片类型为ISO14443A + - 调整卡片与模块的距离 +2. **读写失败**: + - 检查卡片是否可写 + - 确认块地址正确 + - 验证密钥是否正确 +3. **通信错误**: + - 检查SPI连接 + - 确认RST引脚配置正确 + - 确保模块供电正常 + +## API参考 + +### 主要方法 + +- `read_id()`: 读取卡片ID(阻塞式) +- `read_id_no_block()`: 读取卡片ID(非阻塞式) +- `read()`: 读取卡片数据(阻塞式) +- `read_no_block()`: 读取卡片数据(非阻塞式) +- `write(text)`: 写入文本数据(阻塞式) +- `write_no_block(text)`: 写入文本数据(非阻塞式) +- `Mfrc522_Read(blockAddr)`: 读取指定块数据 +- `Mfrc522_Write(blockAddr, writeData)`: 写入数据到指定块 + +## 示例代码 + +完整测试代码: + +```python +import _thread +from mfrc522 import Mfrc522_spi +import utime + +def rc522_test(): + # 初始化RC522模块 + reader = Mfrc522_spi(pin_rst=Pin.GPIO12, pin_irq=Pin.GPIO11) + print("RC522模块初始化完成") + + while True: + # 尝试读取卡片ID + card_id = reader.read_id_no_block() + if card_id: + print('检测到卡片ID: {0}'.format(card_id)) + + # 读取数据 + id, text = reader.read_no_block() + if text: + print('卡片数据:', text) + + # 写入数据 + new_data = "Hello, RFID!" + str(utime.time()) + reader.write(new_data) + print('已写入新数据:', new_data) + + utime.sleep_ms(500) + +# 在新线程中运行测试 +_thread.start_new_thread(rc522_test, ()) +``` + +## 版本信息 + +- 当前版本:v1.0 +- 最后更新:2023-10-15 +- 适用平台:QuecPython + +如需更多帮助,请参考模块完整源代码或联系技术支持。 \ No newline at end of file diff --git a/libraries/RDA5807/README.md b/libraries/RDA5807/README.md new file mode 100644 index 0000000..dc19026 --- /dev/null +++ b/libraries/RDA5807/README.md @@ -0,0 +1,165 @@ +# RDA5807 FM Radio Module Driver Documentation + +## Overview + +This document provides usage instructions for the Python driver of the RDA5807 FM radio module. The driver communicates with the RDA5807 chip via I2C interface, offering complete FM radio control functionality. + +## Key Features + +- FM radio initialization and configuration +- Frequency setting and channel scanning +- Volume control and mute function +- Signal strength detection +- Bass boost adjustment + +## Quick Start Guide + +### 1. Hardware Connection + +```python +# Define pin connections +rdaclk = Pin.GPIO2 # Clock line +rdadio = Pin.GPIO3 # Data line +``` + +### 2. Radio Initialization + +```python +from machine import Pin +import RDA5807 # Assuming driver is saved as RDA5807.py + +# Create radio instance +radio = RDA5807.RDA5807(rdaclk=Pin.GPIO2, rdadio=Pin.GPIO3) + +# Initialize radio +radio._rda_init() +print("Radio initialization complete") +``` + +### 3. Basic Operations + +```python +# Enable FM function +radio.fm_enable(1) + +# Set volume (0-15) +radio.vol_set(10) + +# Scan and play channel +current_freq = radio.seek_channel() +print(f"Current frequency: {current_freq}MHz") +``` + +## API Reference + +### Core Methods + +#### `_rda_init()` + +Initialize RDA5807 chip with default parameters + +#### `fm_enable(flag)` + +Enable/disable FM function + +- `flag`: 1=enable, 0=disable + +#### `vol_set(vol)` + +Set volume level + +- `vol`: Volume level (0-15) + +#### `mute_set(mute)` + +Set mute + +- `mute`: 1=mute, 0=unmute + +#### `bass_set(bass)` + +Set bass boost + +- `bass`: 1=enable, 0=disable + +### Channel Control + +#### `freq_set(freq)` + +Set specific frequency + +- `freq`: Frequency value (in 10kHz units), range 6500-10800 (87.0-108.0MHz) + +#### `seek_channel()` + +Auto scan and lock channel, returns found frequency (MHz) + +#### `next_chanl()` + +Scan and play next available channel + +#### `seek_direction(dir)` + +Set scan direction + +- `dir`: 1=up, 0=down + +### Signal Detection + +#### `rssi_get()` + +Get current signal strength (0-127) + +#### `seekth_set(rssi)` + +Set auto-scan signal threshold + +- `rssi`: Threshold (0-15), lower values find more stations + +## Usage Examples + +### Basic Radio Functionality + +```python +# Initialization +radio = RDA5807.RDA5807(rdaclk=Pin.GPIO2, rdadio=Pin.GPIO3) +radio._rda_init() + +# Enable radio +radio.fm_enable(1) +radio.vol_set(12) # Set medium volume + +# Scan and play channel +current_freq = radio.seek_channel() +print(f"Now playing: {current_freq}MHz") + +# Switch to next channel after 5 seconds +time.sleep(5) +radio.next_chanl() +``` + +### Signal Strength Monitoring + +```python +while True: + strength = radio.rssi_get() + print(f"Current signal strength: {strength}") + if strength < 30: + print("Weak signal, rescanning...") + radio.seek_channel() + time.sleep(2) +``` + +## Important Notes + +1. Ensure correct hardware connections, especially I2C pins +2. Wait at least 30ms after initialization before other operations +3. Frequency range is 87.0-108.0MHz (6500-10800) +4. Volume range is 0-15, recommended initial setting 8-10 +5. Channel scanning may take several seconds + +## Troubleshooting + +- If initialization fails, check I2C lines and power supply +- If no sound, check mute setting and volume level +- For poor signal, try adjusting antenna position or lowering scan threshold \ No newline at end of file diff --git a/libraries/RDA5807/README_zh.md b/libraries/RDA5807/README_zh.md new file mode 100644 index 0000000..91975b1 --- /dev/null +++ b/libraries/RDA5807/README_zh.md @@ -0,0 +1,173 @@ +# RDA5807 FM收音机模块驱动使用说明 + +## 概述 + +本文档介绍RDA5807 FM收音机模块的Python驱动使用方法。该驱动通过I2C接口与RDA5807芯片通信,提供完整的FM收音机功能控制。 + +## 主要功能 + +- FM收音机初始化与配置 +- 频率设置与频道搜索 +- 音量控制与静音功能 +- 信号强度检测 +- 高低音调节 + +## 快速入门 + +### 1. 硬件连接 + +```python +# 定义引脚连接 +rdaclk = Pin.GPIO2 # 时钟线 +rdadio = Pin.GPIO3 # 数据线 +``` + +### 2. 初始化收音机 + +```python +from machine import Pin +import RDA5807 # 假设驱动保存为RDA5807.py + +# 创建收音机实例 +radio = RDA5807.RDA5807(rdaclk=Pin.GPIO2, rdadio=Pin.GPIO3) + +# 初始化收音机 +radio._rda_init() +print("收音机初始化完成") +``` + +### 3. 基本操作 + +```python +# 启用FM功能 +radio.fm_enable(1) + +# 设置音量(0-15) +radio.vol_set(10) + +# 搜索并播放频道 +current_freq = radio.seek_channel() +print(f"当前频率: {current_freq}MHz") +``` + +## API详细说明 + +### 核心方法 + +#### `_rda_init()` + +初始化RDA5807芯片,设置默认参数 + +#### `fm_enable(flag)` + +启用/禁用FM功能 + +- `flag`: 1=启用, 0=禁用 + +#### `vol_set(vol)` + +设置音量 + +- `vol`: 音量级别(0-15) + +#### `mute_set(mute)` + +设置静音 + +- `mute`: 1=静音, 0=取消静音 + +#### `bass_set(bass)` + +设置低音增强 + +- `bass`: 1=启用, 0=禁用 + +### 频道控制 + +#### `freq_set(freq)` + +设置特定频率 + +- `freq`: 频率值(单位10kHz),范围6500-10800(即87.0-108.0MHz) + +#### `seek_channel()` + +自动搜索并锁定频道,返回找到的频率(MHz) + +#### `next_chanl()` + +搜索并播放下一个可用频道 + +#### `seek_direction(dir)` + +设置搜索方向 + +- `dir`: 1=向上搜索, 0=向下搜索 + +### 信号检测 + +#### `rssi_get()` + +获取当前信号强度(0-127) + +#### `seekth_set(rssi)` + +设置自动搜台信号阈值 + +- `rssi`: 阈值(0-15),数值越低搜到的台越多 + +## 应用示例 + +### 基本收音机功能 + +```python +# 初始化 +radio = RDA5807.RDA5807(rdaclk=Pin.GPIO2, rdadio=Pin.GPIO3) +radio._rda_init() + +# 启用收音机 +radio.fm_enable(1) +radio.vol_set(12) # 设置中等音量 + +# 搜索并播放频道 +current_freq = radio.seek_channel() +print(f"正在播放: {current_freq}MHz") + +# 5秒后切换到下一个频道 +time.sleep(5) +radio.next_chanl() +``` + +### 信号强度监测 + +```python +while True: + strength = radio.rssi_get() + print(f"当前信号强度: {strength}") + if strength < 30: + print("信号较弱,尝试重新搜索...") + radio.seek_channel() + time.sleep(2) +``` + +## 注意事项 + +1. 确保硬件连接正确,特别是I2C引脚连接 +2. 初始化后等待至少30ms再进行其他操作 +3. 频率设置范围是87.0-108.0MHz(对应6500-10800) +4. 音量设置范围是0-15,建议初始设置为8-10 +5. 搜索频道时可能需要几秒钟时间 + +## 故障排除 + +- 如果初始化失败,检查I2C线路和电源 +- 无声音时检查静音设置和音量级别 +- 信号差时尝试调整天线位置或降低搜索阈值 + +## 技术支持 + +如需进一步帮助,请参考RDA5807芯片数据手册或联系硬件供应商。 + +------ + +> 注意:本文档基于提供的RDA5807驱动代码编写,所有API和示例均来自源代码实现。实际使用时请根据硬件环境调整引脚定义和参数设置。 \ No newline at end of file diff --git a/libraries/RDA5807/rda5807.py b/libraries/RDA5807/rda5807.py index 5838cf5..4e7435e 100644 --- a/libraries/RDA5807/rda5807.py +++ b/libraries/RDA5807/rda5807.py @@ -281,7 +281,7 @@ def seek_channel(self): def fm_enable(self,flag): ''' 使能芯片 - :param flag: 1.使能 0:禁用 + :param flag: 1.使能 0:禁用 ''' tmp = self._ReadReg(RDA_R02) if(flag == 1): diff --git a/libraries/bma250/README.md b/libraries/bma250/README.md index d0f015f..147b453 100644 --- a/libraries/bma250/README.md +++ b/libraries/bma250/README.md @@ -1,172 +1,171 @@ -# bma250 +# BMA250 Accelerometer Sensor Driver Documentation -默认初始化2G量程。 +## Overview -**类引用:** +This document provides usage instructions for the BMA250 accelerometer sensor driver developed by Quectel. The BMA250 is a low-power digital triaxial acceleration sensor with flexible configuration options. -```python -from peripheral.gsensor.bma250 import Bma250 -``` +## Key Features - +- Sensor initialization and reset +- Configurable measurement range (±2g to ±16g) +- Adjustable output data rate (7.81Hz to 1000Hz) +- Multiple interrupt functions (tap, slope, orientation, etc.) +- Acceleration data reading -**实例化参数:** +## Quick Start Guide -| 名称 | 必填 | 类型 | 说明 | -| ----------- | ---- | ------- | ----------------------------------- | -| i2c | 是 | i2c对象 | 如I2C(I2C.I2C1, I2C.STANDARD_MODE) | -| dev_address | 否 | int | 默认0x19 | +### 1. Initialization ```python -i2c_dev = I2C(I2C.I2C1,I2C.STANDARD_MODE) -bma250 = Bma250(i2c_dev) -``` - -**接口函数:** - -l **set_range(range=0)** - -设置量程。 - -参数: - -| 名称 | 必填 | 类型 | 说明 | -| ----- | ---- | ---- | ----------------------------------------------------------- | -| range | 否 | int | 2g:0x03;
4g :0x05;
8g :0x08;
16g:0x0c | - -返回值: - -​ 无。 - -l **set_hz(hz=0x08)** - -设置传感器频率带宽,默认7.81hz。 - -参数: - -| 名称 | 必填 | 类型 | 说明 | -| ---- | ---- | ---- | ------------------------------------------------------------ | -| hz | 否 | int | 7.81hz:0x08;
15.63hz :0x09;
31.25hz :0x0a;
62.5hz:0x0b
125Hz:0x0c
250Hz:0x0d
500Hz:0x0e
1000Hz:0x0f | - -返回值: - -​ 无。 - -l **int_enable(int_code,tap_thr,tap_dur,slop_thr,slop_dur,flat_hold_time)** - -​ 中断使能。 - -参数: - -| 名称 | 必填 | 类型 | 说明 | -| -------------- | ---- | ---- | ------------------------------------------------------------ | -| int_code | 是 | int | 中断类型
单击中断:0x20
双击中断:0x10
倾斜中断:0x01-0x07(x,y,z轴及其组合)
朝向中断:0x80
水平中断:0x04 | -| tap_thr | 否 | int | 单双击中断选配
敲击中断阈值,默认0x03 | -| tap_dur | 否 | int | 双击中断选配
默认0x04 | -| slop_thr | 否 | int | 倾斜选配
阈值,默认0x14 | -| slop_dur | 否 | int | 倾斜选配
默认0x03 | -| flat_hold_time | 否 | int | 水平中断选配
保持时间,默认0x10 | - -返回值: - -​ 0 :成功 - -​ -1 : 失败 - -l **reset()** - -​ 重置。 - -参数: - -​ 无 - -返回值: - -​ 无 - -l **read_acceleration()** +from machine import I2C +from bma250 import Bma250 -​ 读取三轴加速度。 +# Initialize I2C interface +i2c_dev = I2C(I2C.I2C1, I2C.STANDARD_MODE) -参数: - -​ 无。 - -返回值: - -| 名称 | 类型 | 说明 | -| ------- | ----- | --------------------- | -| (x,y,z) | tuple | x, y, z轴加速度,单位G | - - - -l **process_single_tap ()** - -​ 循环读取中断源寄存器,单击中断检测。 - -​ 注:未检测到中断则死循环,谨慎在主线程执行,执行前请确保单击中断使能并配置正确。 - -参数: - -​ 无。 - -返回值: - -​ 1:检测到单击中断 - -l **process_double_tap()** - -​ 循环读取中断源寄存器,双击中断检测。 - -​ 注:未检测到中断则死循环,谨慎在主线程执行,执行前请确保双击中断使能并配置正确。 - -参数: +# Create sensor instance +sensor = Bma250(i2c_dev) +``` -​ 无。 +### 2. Basic Configuration -返回值: +```python +# Set measurement range (default ±2g) +sensor.set_range(Bma250.RANGE_SEL_2G) # Options: RANGE_SEL_2G, RANGE_SEL_4G, RANGE_SEL_8G, RANGE_SEL_16G -​ 1:检测到双击中断 +# Set output data rate (default 7.81Hz) +sensor.set_hz(Bma250.BW_SEL_1000) # Options from BW_SEL_7_81 to BW_SEL_1000 +``` -l **process_slope ()** +### 3. Reading Acceleration Data -​ 循环读取中断源寄存器,倾斜中断检测。 +```python +# Read acceleration values (x, y, z in g) +x, y, z = sensor.read_acceleration() +print(f"X: {x}g, Y: {y}g, Z: {z}g") +``` -​ 注:未检测到中断则死循环,谨慎在主线程执行,执行前请确保倾斜中断使能并配置正确。 +## Interrupt Configuration -参数: +### Available Interrupt Types -​ 无。 +| Interrupt Constant | Description | +| :----------------- | :--------------------------- | +| `slope_en_x` | X-axis slope detection | +| `slope_en_y` | Y-axis slope detection | +| `slope_en_z` | Z-axis slope detection | +| `slope_en_xyx` | Any-axis slope detection | +| `d_tap_en` | Double-tap detection | +| `s_tap_en` | Single-tap detection | +| `orient_en` | Orientation change detection | +| `flat_en` | Flat position detection | +| `low_g_en` | Low-g detection (free fall) | +| `high_g_en_x` | X-axis high-g detection | +| `high_g_en_y` | Y-axis high-g detection | +| `high_g_en_z` | Z-axis high-g detection | +| `high_g_en_xyx` | Any-axis high-g detection | -返回值: +### Example: Configuring Tap Detection -​ 1:检测到倾斜中断 +```python +# Enable single-tap detection +sensor.int_enable(Bma250.s_tap_en) + +# Wait for and process tap event +while True: + if sensor.process_single_tap(): + print("Single tap detected!") + x, y, z = sensor.read_acceleration() + print(f"Current acceleration: X={x}g, Y={y}g, Z={z}g") + break + utime.sleep_ms(10) +``` -l **process_orient()** +### Example: Free Fall Detection -​ 循环读取中断源寄存器,朝向中断检测。 +```python +# Enable low-g (free fall) detection +sensor.int2_enable(Bma250.low_g_en) + +# Wait for free fall event +while True: + if sensor.process_low_g(): + print("Free fall detected!") + break + utime.sleep_ms(10) +``` -​ 注:未检测到中断则死循环,谨慎在主线程执行,执行前请确保朝向中断使能并配置正确。 +## Advanced Configuration -参数: +### Interrupt Parameters -​ 无。 +The interrupt functions accept several configuration parameters: -返回值: +``` +# Example with all parameters (default values shown) +sensor.int_enable( + int_code=Bma250.s_tap_en, + tap_thr=0x03, # Tap threshold + tap_dur=0x04, # Tap duration + slop_thr=0x14, # Slope threshold + slop_dur=0x03, # Slope duration + flat_hold_time=0x10 # Flat position hold time +) + +sensor.int2_enable( + int_code=Bma250.low_g_en, + low_mode=0x81, # Low-g mode + low_th=0x30, # Low-g threshold + low_dur=0x09, # Low-g duration + high_th=0xc0, # High-g threshold + high_dur=0x0f # High-g duration +) +``` -​ 1:检测到朝向中断 +## Error Handling -l **process_flat ()** +The driver raises `CustomError` exceptions for various error conditions: -​ 循环读取中断源寄存器,水平中断检测。 +```python +try: + sensor = Bma250(i2c_dev) + sensor.set_range(Bma250.RANGE_SEL_4G) +except CustomError as e: + print(f"Error: {e}") +``` -​ 注:未检测到中断则死循环,谨慎在主线程执行,执行前请确保水平中断使能并配置正确。 +## Example Application -参数: +```python +from machine import I2C +from bma250 import Bma250 +import utime + +# Initialize +i2c_dev = I2C(I2C.I2C1, I2C.STANDARD_MODE) +sensor = Bma250(i2c_dev) + +# Configure for high sensitivity +sensor.set_range(Bma250.RANGE_SEL_2G) +sensor.set_hz(Bma250.BW_SEL_1000) + +# Enable orientation detection +sensor.int_enable(Bma250.orient_en) + +# Main loop +while True: + if sensor.process_orient(): + x, y, z = sensor.read_acceleration() + print(f"Orientation changed! Current values: X={x}g, Y={y}g, Z={z}g") + + utime.sleep_ms(100) +``` -​ 无。 +## Notes -返回值: +1. The sensor requires proper power supply and I2C pull-up resistors +2. Interrupt pins must be properly configured in hardware +3. Higher data rates consume more power +4. Lower measurement ranges provide better resolution but smaller maximum detectable acceleration -​ 1:检测到水平中断 +For technical support, please contact Quectel Wireless Solutions. \ No newline at end of file diff --git a/libraries/bma250/README_zh.md b/libraries/bma250/README_zh.md new file mode 100644 index 0000000..c9572df --- /dev/null +++ b/libraries/bma250/README_zh.md @@ -0,0 +1,173 @@ + + +# BMA250加速度传感器驱动使用说明 + +## 概述 + +本文档介绍Quectel开发的BMA250加速度传感器驱动使用方法。BMA250是一款低功耗数字三轴加速度传感器,具有灵活的配置选项。 + +## 主要功能 + +- 传感器初始化和复位 +- 可配置测量范围(±2g至±16g) +- 可调输出数据速率(7.81Hz至1000Hz) +- 多种中断功能(敲击、倾斜、方向等) +- 加速度数据读取 + +## 快速入门 + +### 1. 初始化 + +```python +from machine import I2C +from bma250 import Bma250 + +# 初始化I2C接口 +i2c_dev = I2C(I2C.I2C1, I2C.STANDARD_MODE) + +# 创建传感器实例 +sensor = Bma250(i2c_dev) +``` + +### 2. 基本配置 + +```python +# 设置测量范围(默认±2g) +sensor.set_range(Bma250.RANGE_SEL_2G) # 选项: RANGE_SEL_2G, RANGE_SEL_4G, RANGE_SEL_8G, RANGE_SEL_16G + +# 设置输出数据速率(默认7.81Hz) +sensor.set_hz(Bma250.BW_SEL_1000) # 选项从BW_SEL_7_81到BW_SEL_1000 +``` + +### 3. 读取加速度数据 + +```python +# 读取加速度值(x, y, z单位g) +x, y, z = sensor.read_acceleration() +print(f"X: {x}g, Y: {y}g, Z: {z}g") +``` + +## 中断配置 + +### 可用中断类型 + +| 中断常量 | 描述 | +| :-------------- | :------------------- | +| `slope_en_x` | X轴倾斜检测 | +| `slope_en_y` | Y轴倾斜检测 | +| `slope_en_z` | Z轴倾斜检测 | +| `slope_en_xyx` | 任意轴倾斜检测 | +| `d_tap_en` | 双击检测 | +| `s_tap_en` | 单击检测 | +| `orient_en` | 方向变化检测 | +| `flat_en` | 水平位置检测 | +| `low_g_en` | 低重力检测(自由落体) | +| `high_g_en_x` | X轴高重力检测 | +| `high_g_en_y` | Y轴高重力检测 | +| `high_g_en_z` | Z轴高重力检测 | +| `high_g_en_xyx` | 任意轴高重力检测 | + +### 示例:配置敲击检测 + +```python +# 启用单击检测 +sensor.int_enable(Bma250.s_tap_en) + +# 等待并处理敲击事件 +while True: + if sensor.process_single_tap(): + print("检测到单击!") + x, y, z = sensor.read_acceleration() + print(f"当前加速度: X={x}g, Y={y}g, Z={z}g") + break + utime.sleep_ms(10) +``` + +### 示例:自由落体检测 + +```python +# 启用低重力(自由落体)检测 +sensor.int2_enable(Bma250.low_g_en) + +# 等待自由落体事件 +while True: + if sensor.process_low_g(): + print("检测到自由落体!") + break + utime.sleep_ms(10) +``` + +## 高级配置 + +### 中断参数 + +中断函数接受多个配置参数: + +```python +# 包含所有参数的示例(显示默认值) +sensor.int_enable( + int_code=Bma250.s_tap_en, + tap_thr=0x03, # 敲击阈值 + tap_dur=0x04, # 敲击持续时间 + slop_thr=0x14, # 倾斜阈值 + slop_dur=0x03, # 倾斜持续时间 + flat_hold_time=0x10 # 水平位置保持时间 +) + +sensor.int2_enable( + int_code=Bma250.low_g_en, + low_mode=0x81, # 低重力模式 + low_th=0x30, # 低重力阈值 + low_dur=0x09, # 低重力持续时间 + high_th=0xc0, # 高重力阈值 + high_dur=0x0f # 高重力持续时间 +) +``` + +## 错误处理 + +驱动会抛出`CustomError`异常处理各种错误情况: + +```python +try: + sensor = Bma250(i2c_dev) + sensor.set_range(Bma250.RANGE_SEL_4G) +except CustomError as e: + print(f"错误: {e}") +``` + +## 应用示例 + +```python +from machine import I2C +from bma250 import Bma250 +import utime + +# 初始化 +i2c_dev = I2C(I2C.I2C1, I2C.STANDARD_MODE) +sensor = Bma250(i2c_dev) + +# 配置为高灵敏度 +sensor.set_range(Bma250.RANGE_SEL_2G) +sensor.set_hz(Bma250.BW_SEL_1000) + +# 启用方向检测 +sensor.int_enable(Bma250.orient_en) + +# 主循环 +while True: + if sensor.process_orient(): + x, y, z = sensor.read_acceleration() + print(f"方向改变! 当前值: X={x}g, Y={y}g, Z={z}g") + + utime.sleep_ms(100) +``` + +## 注意事项 + +1. 传感器需要正确的电源供应和I2C上拉电阻 +2. 中断引脚必须在硬件上正确配置 +3. 更高的数据速率会消耗更多电量 +4. 更小的测量范围提供更好的分辨率但可检测的最大加速度更小 + +如需技术支持,请联系Quectel无线解决方案。 \ No newline at end of file diff --git a/libraries/gl5516/README.md b/libraries/gl5516/README.md index 0297856..2760be6 100644 --- a/libraries/gl5516/README.md +++ b/libraries/gl5516/README.md @@ -1,60 +1,120 @@ -# gl5516 +# GL5516 Light Dependent Resistor (LDR) Driver Documentation -​ 注意:光敏电阻值的计算跟接入电路有关! +## Overview -**类引用:** +This document describes how to use the GL5516 light dependent resistor (photoresistor) driver to measure ambient light levels by reading resistance values. + +## Features + +- Read raw voltage values from ADC +- Convert voltage to resistance values +- Simple API for light sensing applications + +## Quick Start + +### 1. Import Required Modules ```python -from gl5528 import Gl5528 +from misc import ADC +from gl5516 import Gl5516 +import utime as time ``` - +### 2. Initialize the Sensor -**实例化参数:** +```python +# Create ADC device instance +adc_device = ADC() -| 名称 | 必填 | 类型 | 说明 | -| ------- | ---- | ------- | ---------- | -| adc_dev | 是 | ADC对象 | adc设备 | -| adcn | 是 | ADC通道 | 例ADC.ADC0 | +# Initialize GL5516 sensor on ADC channel 0 +ldr_sensor = Gl5516(adc_device, ADC.ADC0) +``` + +### 3. Read Sensor Data +#### Read Resistance Value + +```python +resistance = ldr_sensor.read() +print(f"Photoresistor resistance: {resistance}Ω") ``` -AdcDevice = ADC() -gl5516=Gl5516(AdcDevice,ADC.ADC0) + +#### Read Raw Voltage Value + +```python +voltage = ldr_sensor.read_volt() +print(f"Raw voltage: {voltage}mV") ``` -**接口函数:** +## API Reference + +### `Gl5516(adc_dev, adcn)` -l **read()** +Constructor to initialize the GL5516 sensor. -​ 读取光敏电阻值。 +**Parameters:** -​ 备注:不同接线光敏电阻值计算方式可能不一致,此方法只适合我们的600s系列。 +- `adc_dev`: ADC device instance +- `adcn`: ADC channel number (e.g., `ADC.ADC0`) -参数: +### `read()` -​ 无。 +Reads and returns the photoresistor resistance value in ohms (Ω). -返回值: +**Returns:** -| 名称 | 类型 | 说明 | -| ---------- | ---- | ------ | -| resistance | 数值 | 电阻值 | +- Photoresistor resistance value (Ω) -l **read_volt ()** +### `read_volt()` -​ 读取电压值。 +Reads and returns the raw voltage value from the ADC. -​ 用于计算光敏电阻值。 +**Returns:** -参数: +- Voltage value in millivolts (mV) + +## Example Application + +```python +from misc import ADC +from gl5516 import Gl5516 +import utime as time + +# Initialize sensor +adc = ADC() +ldr = Gl5516(adc, ADC.ADC0) + +# Continuous light monitoring +while True: + resistance = ldr.read() + voltage = ldr.read_volt() + + print(f"Light Level - Resistance: {resistance}Ω, Voltage: {voltage}mV") + + if resistance > 10000: # Dark condition + print("Dark environment detected") + elif resistance < 2000: # Bright condition + print("Bright environment detected") + + time.sleep(1) +``` -​ 无。 +## Technical Notes -返回值: +1. The resistance calculation assumes a specific voltage divider circuit configuration with: + - 4.7kΩ resistor (R1) + - 40.2kΩ resistor (R2) + - 3.3V supply voltage +2. For different circuit configurations, modify the `_voltage_to_resistance()` method accordingly. +3. The sensor response time is typically in milliseconds, making 1-second intervals suitable for most applications. +4. Typical resistance ranges: + - Bright light: ~1kΩ + - Darkness: ~100kΩ or more -| 名称 | 类型 | 说明 | -| ---- | ---- | ---- | -| volt | 数值 | 电压 | +## Troubleshooting - +- If readings are unstable, check for proper connections and stable power supply +- Ensure the ADC channel is correctly configured +- Verify the voltage divider circuit matches the driver's assumptions +This driver provides a simple interface for light sensing applications using the GL5516 photoresistor, suitable for automatic lighting control, daylight detection, and other light-sensitive applications. \ No newline at end of file diff --git a/libraries/gl5516/README_zh.md b/libraries/gl5516/README_zh.md new file mode 100644 index 0000000..2c8b2aa --- /dev/null +++ b/libraries/gl5516/README_zh.md @@ -0,0 +1,121 @@ +# GL5516光敏电阻传感器驱动文档 + +## 概述 + +本文档介绍如何使用GL5516光敏电阻驱动模块测量环境光照强度,通过读取电阻值变化来感知光线强弱。 + +## 主要特性 + +- 读取ADC原始电压值 +- 将电压值转换为电阻值 +- 简洁易用的光照传感API接口 + +## 快速入门 + +### 1. 导入所需模块 + +```python +from misc import ADC +from gl5516 import Gl5516 +import utime as time +``` + +### 2. 初始化传感器 + +```python +# 创建ADC设备实例 +adc_device = ADC() + +# 在ADC通道0上初始化GL5516传感器 +ldr_sensor = Gl5516(adc_device, ADC.ADC0) +``` + +### 3. 读取传感器数据 + +#### 读取电阻值 + +```python +resistance = ldr_sensor.read() +print(f"光敏电阻阻值: {resistance}Ω") +``` + +#### 读取原始电压值 + +```python +voltage = ldr_sensor.read_volt() +print(f"原始电压值: {voltage}mV") +``` + +## API接口说明 + +### `Gl5516(adc_dev, adcn)` + +构造函数,用于初始化GL5516传感器。 + +**参数说明:** + +- `adc_dev`: ADC设备实例 +- `adcn`: ADC通道号(例如: `ADC.ADC0`) + +### `read()` + +读取并返回光敏电阻的阻值,单位为欧姆(Ω)。 + +**返回值:** + +- 光敏电阻阻值(Ω) + +### `read_volt()` + +读取并返回ADC原始电压值。 + +**返回值:** + +- 电压值,单位为毫伏(mV) + +## 应用示例 + +```python +from misc import ADC +from gl5516 import Gl5516 +import utime as time + +# 初始化传感器 +adc = ADC() +ldr = Gl5516(adc, ADC.ADC0) + +# 持续监测光照强度 +while True: + resistance = ldr.read() + voltage = ldr.read_volt() + + print(f"光照强度 - 电阻值: {resistance}Ω, 电压值: {voltage}mV") + + if resistance > 10000: # 黑暗环境 + print("检测到黑暗环境") + elif resistance < 2000: # 明亮环境 + print("检测到明亮环境") + + time.sleep(1) +``` + +## 技术说明 + +1. 电阻值计算基于特定分压电路配置: + - 4.7kΩ电阻(R1) + - 40.2kΩ电阻(R2) + - 3.3V供电电压 +2. 如需使用不同电路配置,请相应修改`_voltage_to_resistance()`方法。 +3. 传感器响应时间通常在毫秒级,1秒的采样间隔适用于大多数应用场景。 +4. 典型阻值范围: + - 强光环境:约1kΩ + - 黑暗环境:约100kΩ或更高 + +## 常见问题排查 + +- 如果读数不稳定,请检查连接是否可靠和电源是否稳定 +- 确保ADC通道配置正确 +- 确认分压电路与驱动程序的假设相匹配 + +本驱动为使用GL5516光敏电阻的光照传感应用提供了简洁的接口,适用于自动照明控制、日光检测等光敏应用场景。 + diff --git a/libraries/gl5528/README.md b/libraries/gl5528/README.md index 838a330..82002a8 100644 --- a/libraries/gl5528/README.md +++ b/libraries/gl5528/README.md @@ -1,73 +1,148 @@ -# gl5528 +# GL5528 Photoresistor Sensor Driver Documentation -注意:光敏电阻值的计算跟接入电路有关! +## Overview -**类引用:** +This document describes the usage of the GL5528 photoresistor sensor driver, which measures ambient light intensity and converts it to both resistance values and illuminance (lux) levels. + +## Key Features + +- Read raw ADC voltage values +- Convert voltage to resistance values +- Map resistance values to illuminance (lux) using predefined lookup table +- Provides both resistance and illuminance outputs + +## Quick Start Guide + +### 1. Import Required Modules ```python -from peripheral.illsensor.gl5528 import Gl5528 +from misc import ADC +from gl5528 import Gl5528 +import utime as time ``` - +### 2. Initialize the Sensor -**实例化参数:** +```python +# Create ADC device instance +adc_device = ADC() + +# Initialize GL5528 sensor on ADC channel 0 +ldr_sensor = Gl5528(adc_device, ADC.ADC0) +``` -| 名称 | 必填 | 类型 | 说明 | -| ------- | ---- | ------- | ------- | -| adc_dev | 是 | ADC对象 | adc设备 | +### 3. Read Sensor Data + +#### Read Resistance and Illuminance ```python -AdcDevice = ADC() -gl5528=Gl5528(AdcDevice,ADC.ADC0) +resistance, lux = ldr_sensor.read() +print(f"Photoresistor resistance: {resistance}Ω, Illuminance: {lux}lux") ``` -**接口函数:** +#### Read Raw Voltage Value + +```python +voltage = ldr_sensor.read_volt() +print(f"Raw voltage: {voltage}mV") +``` -l **read()** +## API Reference -​ 读取光敏电阻值和光照度值(如果能转换),这里的照度范围(1-1000),会有缺失(None),有一定误差。 +### `Gl5528(adc_dev, adcn)` -​ 备注:不同接线光敏电阻值计算方式可能不一致,此方法只适合我们的600s系列。 +Constructor for initializing the GL5528 sensor. -参数: +**Parameters:** -​ 无。 +- `adc_dev`: ADC device instance +- `adcn`: ADC channel number (e.g., `ADC.ADC0`) -返回值: +### `read()` -| 名称 | 类型 | 说明 | -| ---------------- | ----- | ------------ | -| (resistance,lux) | tuple | 电阻值,照度 | +Reads and returns both photoresistor resistance and illuminance. -l **read_volt ()** +**Returns:** -​ 读取电压值。 +- Tuple (resistance, lux) + - resistance: Photoresistor value in ohms (Ω) + - lux: Illuminance value (may be None if out of lookup table range) -​ 用于计算光敏电阻值。 +### `read_volt()` -参数: +Reads and returns the raw ADC voltage value. -​ 无。 +**Returns:** -返回值: +- Voltage value in millivolts (mV) -| 名称 | 类型 | 说明 | -| ---- | ---- | ---- | -| volt | 数值 | 电压 | +### `r2i(resis)` - +Converts resistance value to illuminance (lux). -l **r2i(resis)** +**Parameters:** -​ 转换光敏电阻值到光照度值(如果能转换),这里的照度范围(1-1000),会有缺失(None),有一定误差。 +- resis: Resistance value in ohms (Ω) -参数: +**Returns:** -​ 无。 +- Illuminance value (lux) or None (if out of lookup table range) -返回值: +## Example Application -| 名称 | 类型 | 说明 | -| --------- | --------- | ---- | -| lux或None | int或None | 照度 | +```python +from misc import ADC +from gl5528 import Gl5528 +import utime as time + +# Initialize sensor +adc = ADC() +ldr = Gl5528(adc, ADC.ADC0) + +# Continuous light monitoring +while True: + resistance, lux = ldr.read() + + if lux: + print(f"Current environment - Resistance: {resistance}Ω, Illuminance: {lux}lux") + else: + print(f"Current environment - Resistance: {resistance}Ω (Out of measurement range)") + + # Light-dependent control logic + if lux and lux > 500: + print("Bright environment detected") + elif lux and lux < 100: + print("Dim environment detected") + + time.sleep(1) +``` +## Technical Specifications + +1. **Resistance Calculation** based on specific voltage divider configuration: + - 4.7kΩ resistor (R1) + - 40.2kΩ resistor (R2) + - 3.3V power supply +2. **Illuminance Conversion** uses predefined resistance-illuminance lookup table (o2i_table) with precise mapping for 400Ω-82kΩ range +3. **Typical Applications**: + - Indoor light monitoring + - Automatic lighting control + - Ambient light sensing devices +4. **Measurement Range**: + - Resistance range: ~400Ω-82kΩ + - Illuminance range: 1-1300lux (depending on lookup table) + +## Troubleshooting + +1. **Unstable Readings**: + - Check circuit connections + - Ensure stable power supply + - Avoid direct exposure to flickering light sources +2. **None Values Returned**: + - Verify resistance is within 400Ω-82kΩ range + - Check ADC configuration +3. **Accuracy Issues**: + - Extend o2i_table for higher precision + - Consider using higher precision ADC module + +This driver provides complete interface for GL5528 photoresistor, particularly suitable for applications requiring both resistance and illuminance measurements. The included lookup table enables direct conversion from resistance to standard lux values for convenient light intensity evaluation. diff --git a/libraries/gl5528/README_zh.md b/libraries/gl5528/README_zh.md new file mode 100644 index 0000000..afb7f74 --- /dev/null +++ b/libraries/gl5528/README_zh.md @@ -0,0 +1,148 @@ +# GL5528光敏电阻传感器驱动文档 + +## 概述 + +本文档介绍GL5528光敏电阻传感器的驱动使用方法,该传感器可测量环境光照强度并转换为电阻值和光照度(lux)值。 + +## 主要特性 + +- 读取ADC原始电压值 +- 将电压值转换为电阻值 +- 通过预设对照表将电阻值转换为光照度(lux) +- 提供电阻值和光照度双输出 + +## 快速入门 + +### 1. 导入所需模块 + +```python +from misc import ADC +from gl5528 import Gl5528 +import utime as time +``` + +### 2. 初始化传感器 + +```python +# 创建ADC设备实例 +adc_device = ADC() + +# 在ADC通道0上初始化GL5528传感器 +ldr_sensor = Gl5528(adc_device, ADC.ADC0) +``` + +### 3. 读取传感器数据 + +#### 读取电阻值和光照度 + +```python +resistance, lux = ldr_sensor.read() +print(f"光敏电阻阻值: {resistance}Ω, 光照度: {lux}lux") +``` + +#### 读取原始电压值 + +```python +voltage = ldr_sensor.read_volt() +print(f"原始电压值: {voltage}mV") +``` + +## API接口说明 + +### `Gl5528(adc_dev, adcn)` + +构造函数,用于初始化GL5528传感器。 + +**参数说明:** + +- `adc_dev`: ADC设备实例 +- `adcn`: ADC通道号(例如: `ADC.ADC0`) + +### `read()` + +读取并返回光敏电阻的阻值和光照度。 + +**返回值:** + +- 元组(resistance, lux) + - resistance: 光敏电阻阻值(Ω) + - lux: 光照度值(可能为None,表示超出对照表范围) + +### `read_volt()` + +读取并返回ADC原始电压值。 + +**返回值:** + +- 电压值,单位为毫伏(mV) + +### `r2i(resis)` + +将电阻值转换为光照度(lux)。 + +**参数:** + +- resis: 电阻值(Ω) + +**返回值:** + +- 光照度值(lux)或None(超出对照表范围) + +## 应用示例 + +```python +from misc import ADC +from gl5528 import Gl5528 +import utime as time + +# 初始化传感器 +adc = ADC() +ldr = Gl5528(adc, ADC.ADC0) + +# 持续监测光照强度 +while True: + resistance, lux = ldr.read() + + if lux: + print(f"当前环境 - 电阻值: {resistance}Ω, 光照度: {lux}lux") + else: + print(f"当前环境 - 电阻值: {resistance}Ω (超出测量范围)") + + # 根据光照度控制逻辑 + if lux and lux > 500: + print("强光环境") + elif lux and lux < 100: + print("弱光环境") + + time.sleep(1) +``` + +## 技术说明 + +1. **电阻值计算**基于特定分压电路配置: + - 4.7kΩ电阻(R1) + - 40.2kΩ电阻(R2) + - 3.3V供电电压 +2. **光照度转换**使用预设的电阻-照度对照表(o2i_table),包含400Ω-820Ω范围内的精确对应关系 +3. **典型应用场景**: + - 室内光照监测 + - 自动照明控制 + - 环境光感应设备 +4. **测量范围**: + - 电阻范围:约400Ω-82kΩ + - 照度范围:1-1300lux(取决于对照表) + +## 常见问题排查 + +1. **读数不稳定**: + - 检查电路连接是否可靠 + - 确保供电电压稳定 + - 避免传感器直接暴露在闪烁光源下 +2. **返回None值**: + - 确认电阻值是否在400Ω-82kΩ范围内 + - 检查ADC配置是否正确 +3. **精度问题**: + - 如需更高精度,可扩展o2i_table对照表 + - 考虑使用更高精度的ADC模块 + +本驱动为GL5528光敏电阻提供了完整的接口,特别适合需要同时获取电阻值和光照度的应用场景。 diff --git a/libraries/lis2dh12/README.md b/libraries/lis2dh12/README.md index cffd059..32e8bfc 100644 --- a/libraries/lis2dh12/README.md +++ b/libraries/lis2dh12/README.md @@ -1,109 +1,136 @@ -# lis2dh12 +# LIS2DH12 Accelerometer Sensor Driver User Manual -默认初始化2G量程。 +## Overview -**类引用:** +This document describes how to use the LIS2DH12 accelerometer sensor driver code provided by Quectel. The LIS2DH12 is a low-power three-axis accelerometer suitable for various motion detection applications. -```python -from usr.lis2dh12 import lis2dh12 -``` - -## **实例化参数:** - -| 名称 | 必填 | 类型 | 说明 | -| ------------- | ---- | ------- | ----------------------------------- | -| i2c_dev | 是 | i2c对象 | 如I2C(I2C.I2C1, I2C.STANDARD_MODE) | -| int_pin | 是 | int | 连接int1的引脚gpio号 | -| slave_address | 否 | int | 默认0x19 | - -```python -i2c_dev = I2C(I2C.I2C1, I2C.STANDARD_MODE) -dev = lis2dh12(i2c_dev,14) -``` - -## **接口函数:** +## Key Features -### **sensor_reset** +- Sensor initialization and reset +- Acceleration data reading +- Interrupt function configuration (single-click, double-click, motion detection, etc.) +- Operation mode settings -重置传感器。 +## Quick Start -参数: +### 1. Initialize the Sensor -​ 无。 +python -返回值: +``` +def __init__(self, i2c_dev, int_pin, slave_address=0x19): + self._address = slave_address + self._i2c_dev = i2c_dev + self._int_pin = int_pin + self._extint = None + self._sensor_init() +``` -​ 无。 +### 2. Read Acceleration Data -### **int_enable** +python -​ 中断使能。 +``` +# Read three-axis acceleration data + def read_acceleration(self): + ''' + read acceleration + :return: x,y,z-axis acceleration + ''' + + while 1: + status = self._read_data(LIS2DH12_STATUS_REG,1)[0] + xyzda = status & 0x08 # if xyz data exists, set 1 + xyzor = status & 0x80 + if not xyzda: + continue + else: + x,y,z = self._acceleration + return (x, y, z) +``` -参数: +### 3. Configure Interrupt Function -| 名称 | 必填 | 类型 | 说明 | -| ------------ | ---- | ---- | ------------------------------------------------------------ | -| int_type | 是 | int | 中断类型(可配单双击中断,自由落体中断)
x单击:0x01,y单击:0x04,z单击:0x10,三轴单击:0x15
x双击:0x02,y双击:0x08,z双击:0x20,三轴双击:0x2A
自由落体:0x95 | -| int_ths | 是 | int | 中断阈值,所有中断均需设置 | -| time_limit | 否 | int | 时间限制(单双击事件),默认0x18 | -| time_latency | 否 | int | 延时(双击事件设置),默认0x12 | -| time_window | 否 | int | 时间窗口(双击事件设置),双击得在该段时间内完成,默认0x55 | -| duration | 否 | int | 延续时间,惯性中断设置,默认0x03 | +text -返回值: +``` +# Define interrupt callback function + def set_int_callback(self, cb): + self._extint = ExtInt(self._int_pin, ExtInt.IRQ_FALLING, ExtInt.PULL_PU, cb) +``` -​ 无 +## API Details -### **start_sensor** +### 1. Constructor `__init__(self, i2c_dev, int_pin, slave_address=0x19):` -​ 启动传感器(使能xyz轴)。 +- Parameters: + - `i2c_dev`: I2C device object + - `int_pin`: GPIO number connected to the sensor interrupt pin + - `slave_address`: Sensor I2C address (default 0x19) -参数: +### 2. Main Methods -​ 无。 +#### `sensor_reset()` -返回值: +Reset the sensor -​ 无 +#### `int_enable(int_type, int_ths=0x12, time_limit=0x18, time_latency=0x12, time_window=0x55, duration=0x03)` -### **read_acceleration** +Enable interrupt function -​ 循环读取 STATUS_REG寄存器,有新数据则输出三轴加速度。 +- Parameters: + - `int_type`: Interrupt type (e.g., XYZ_SINGLE_CLICK_INT) + - `int_ths`: Interrupt threshold + - `time_limit`: Time limit (for click interrupt) + - `time_latency`: Latency time (for double-click interrupt) + - `time_window`: Time window (for double-click interrupt) + - `duration`: Duration -参数: +#### `set_int_callback(cb)` -​ 无。 +Set interrupt callback function -返回值: +- Parameters: + - `cb`: Callback function -| 名称 | 类型 | 说明 | -| ------- | ----- | --------------------- | -| (x,y,z) | tuple | x, y, z轴加速度,单位G | +#### `set_mode(mode)` - +Set operation mode -### **set_int_callback()** +- Parameters: + - `mode`: 0-High resolution mode, 1-Normal mode, 2-Low power mode -​ 设置中断回调。(int1脚) +### 3. Main Properties -参数: +#### `read_acceleration` -​ 无。 +Read current three-axis acceleration values (unit: g) -返回值: +#### `int_processing_data()` -​ 无。 +Process interrupt and return current acceleration values - **set_mode()** +## Interrupt Type Constants -​ 设置中断回调。(int1脚) +| Constant Name | Description | +| :------------------- | :------------------------------ | +| XYZ_SINGLE_CLICK_INT | XYZ-axis single-click interrupt | +| X_SINGLE_CLICK_INT | X-axis single-click interrupt | +| Y_SINGLE_CLICK_INT | Y-axis single-click interrupt | +| Z_SINGLE_CLICK_INT | Z-axis single-click interrupt | +| XYZ_DOUBLE_CLICK_INT | XYZ-axis double-click interrupt | +| MOVE_RECOGNIZE | Motion detection interrupt | +| FF_RECOGNIZE | Free-fall interrupt | -参数: +## Notes -​ 无。 +1. Ensure the I2C bus is properly initialized before use +2. The interrupt pin needs to be correctly configured +3. Check data ready status before reading acceleration data +4. Different operation modes affect power consumption and accuracy -返回值: +## Technical Support -​ 无。 \ No newline at end of file +For further assistance, please contact Quectel technical support team. \ No newline at end of file diff --git a/libraries/lis2dh12/README_zh.md b/libraries/lis2dh12/README_zh.md new file mode 100644 index 0000000..b8f305e --- /dev/null +++ b/libraries/lis2dh12/README_zh.md @@ -0,0 +1,130 @@ + + +# LIS2DH12 加速度传感器驱动使用说明 + +## 概述 + +本文档介绍了如何使用 Quectel 提供的 LIS2DH12 加速度传感器驱动代码。LIS2DH12 是一款低功耗三轴加速度计,适用于各种运动检测应用。 + +## 主要功能 + +- 传感器初始化与重置 +- 加速度数据读取 +- 中断功能配置(单击、双击、运动检测等) +- 工作模式设置 + +## 快速开始 + +### 1. 初始化传感器 + +```python +def __init__(self, i2c_dev, int_pin, slave_address=0x19): + self._address = slave_address + self._i2c_dev = i2c_dev + self._int_pin = int_pin + self._extint = None + self._sensor_init() +``` + +### 2. 读取加速度数据 + +```python +# 读取三轴加速度数据 + def read_acceleration(self): + ''' + read acceleration + :return: x,y,z-axis acceleration + ''' + + while 1: + status = self._read_data(LIS2DH12_STATUS_REG,1)[0] + xyzda = status & 0x08 # if xyz data exists, set 1 + xyzor = status & 0x80 + if not xyzda: + continue + else: + x,y,z = self._acceleration + return (x, y, z) +``` + +### 3. 配置中断功能 + +``` +# 定义中断回调函数 + def set_int_callback(self, cb): + self._extint = ExtInt(self._int_pin, ExtInt.IRQ_FALLING, ExtInt.PULL_PU, cb) +``` + +## API详解 + +### 1. 构造函数 `__init__(self, i2c_dev, int_pin, slave_address=0x19):` + +- 参数: + - `i2c_dev`: I2C设备对象 + - `int_pin`: 连接传感器中断引脚的GPIO号 + - `slave_address`: 传感器I2C地址(默认0x19) + +### 2. 主要方法 + +#### `sensor_reset()` + +重置传感器 + +#### `int_enable(int_type, int_ths=0x12, time_limit=0x18, time_latency=0x12, time_window=0x55, duration=0x03)` + +启用中断功能 + +- 参数: + - `int_type`: 中断类型(如XYZ_SINGLE_CLICK_INT等) + - `int_ths`: 中断阈值 + - `time_limit`: 时间限制(用于点击中断) + - `time_latency`: 延迟时间(用于双击中断) + - `time_window`: 时间窗口(用于双击中断) + - `duration`: 持续时间 + +#### `set_int_callback(cb)` + +设置中断回调函数 + +- 参数: + - `cb`: 回调函数 + +#### `set_mode(mode)` + +设置工作模式 + +- 参数: + - `mode`: 0-高分辨率模式, 1-普通模式, 2-低功耗模式 + +### 3. 主要属性 + +#### `read_acceleration` + +读取当前三轴加速度值(单位: g) + +#### `int_processing_data()` + +处理中断并返回当前加速度值 + +## 中断类型常量 + +| 常量名称 | 描述 | +| :------------------- | :------------ | +| XYZ_SINGLE_CLICK_INT | XYZ轴单击中断 | +| X_SINGLE_CLICK_INT | X轴单击中断 | +| Y_SINGLE_CLICK_INT | Y轴单击中断 | +| Z_SINGLE_CLICK_INT | Z轴单击中断 | +| XYZ_DOUBLE_CLICK_INT | XYZ轴双击中断 | +| MOVE_RECOGNIZE | 运动识别中断 | +| FF_RECOGNIZE | 自由落体中断 | + +## 注意事项 + +1. 使用前确保I2C总线已正确初始化 +2. 中断引脚需要正确配置 +3. 读取加速度数据前建议检查数据就绪状态 +4. 不同工作模式会影响功耗和精度 + +## 技术支持 + +如需更多帮助,请联系Quectel技术支持团队。 \ No newline at end of file diff --git a/libraries/power_meter_chips/BL0939/README.md b/libraries/power_meter_chips/BL0939/README.md new file mode 100644 index 0000000..b4bfee6 --- /dev/null +++ b/libraries/power_meter_chips/BL0939/README.md @@ -0,0 +1,143 @@ +# BL0939 Electric Energy Metering Chip Sample Code Description Document + +## Overview + +This document explains how to use the BL0939 energy measurement chip driver interface provided by Quectel. The BL0939 is a high-precision energy measurement chip capable of measuring AC voltage, current (dual-channel), power, and other parameters. + +------ + +## Quick Start + +### 1. Initialize the BL0939 Object + +```python +from bl0939 import Bl0939 + +# Parameters: port=SPI port number (default: 1), mode=SPI mode (default: 1), clk=clock frequency (default: 0) +bl0939 = Bl0939(port=1, mode=1, clk=0) +``` + +### 2. Read Measurement Data + +```python +# Read all parameters (Channel A current, Channel B current, voltage) +ia, ib, vol = bl0939.read() + +# Read parameters individually +current_a = bl0939.current_a # Channel A current (RMS) +current_b = bl0939.current_b # Channel B current (RMS) +voltage = bl0939.voltage # Voltage (RMS) +``` + +------ + +## Detailed API Reference + +### Class `Bl0939` + +#### Constructor + +```python +Bl0939(port=1, mode=1, clk=0) +``` + +- **Parameters**: + - `port`: SPI port number (default: 1). + - `mode`: SPI mode (default: 1). + - `clk`: SPI clock frequency (default: 0). +- **Function**: Initializes the SPI interface and resets the chip. + +------ + +#### Methods + +##### `read()` + +```python +read() -> (int, int, int) +``` + +- **Returns**: A tuple `(Channel A current, Channel B current, Voltage)`. +- **Function**: Reads all measurements in a single call. + +##### `reset()` + +```python +reset() +``` + +- **Function**: Resets the chip (soft reset). + +------ + +#### Properties + +##### `current_a` + +```python +current_a -> int +``` + +- **Returns**: Channel A current (RMS) (raw register value). +- **Note**: Must be converted to actual current (e.g., mA/A) based on the hardware circuit. + +##### `current_b` + +```python +current_b -> int +``` + +- **Returns**: Channel B current (RMS) (raw register value). + +##### `voltage` + +```python +voltage -> int +``` + +- **Returns**: Voltage (RMS) (raw register value). +- **Note**: Must be converted to actual voltage (e.g., V) based on the voltage divider circuit. + +------ + +## Example Code + +### Continuous Data Reading + +```python +import utime +from bl0939 import Bl0939 + +bl0939 = Bl0939(port=1) + +for i in range(10): + ia, ib, vol = bl0939.read() + print("Channel A Current: {}, Channel B Current: {}, Voltage: {}".format(ia, ib, vol)) + utime.sleep(1) +``` + +### Output Explanation + +- The output values are raw register data and must be converted to physical quantities: + - **Current Conversion**: + `Actual Current = Register Value × (Shunt Resistor Ratio / Calibration Factor)` + - **Voltage Conversion**: + `Actual Voltage = Register Value × (Voltage Divider Ratio / Calibration Factor)` + +------ + +## Notes + +1. **SPI Configuration**: Ensure the SPI port, mode, and clock frequency match the hardware. +2. **Calibration**: Calibrate scaling factors (e.g., pulse constant) for accurate measurements. +3. **Isolation Design**: Use optocouplers or transformers when measuring high voltage. +4. **Unit Conversion**: Register values are unsigned integers; convert them to real-world units as per the datasheet. + +------ + +## Troubleshooting + +- **SPI Communication Failure**: Check wiring (CS/CLK/MOSI/MISO) and port configuration. +- **Abnormal Data**: Verify shunt resistor/voltage divider parameters. + +For further assistance, refer to the BL0939 datasheet or contact Quectel technical support. \ No newline at end of file diff --git a/libraries/power_meter_chips/BL0939/README_zh.md b/libraries/power_meter_chips/BL0939/README_zh.md new file mode 100644 index 0000000..1beb15d --- /dev/null +++ b/libraries/power_meter_chips/BL0939/README_zh.md @@ -0,0 +1,143 @@ +# BL0939 电能计量芯片示例代码说明文档 + +## 概述 + +本文档介绍如何使用 Quectel 提供的 BL0939 电能计量芯片驱动接口。BL0939 是一款高精度电能计量芯片,可测量交流电压、电流(双通道)和功率等参数。 + +------ + +## 快速开始 + +### 1. 初始化 BL0939 对象 + +python + +``` +from bl0939 import Bl0939 + +# 参数说明: port=SPI端口号(默认1), mode=SPI模式(默认1), clk=时钟频率(默认0) +bl0939 = Bl0939(port=1, mode=1, clk=0) +``` + +### 2. 读取测量数据 + +python + +``` +# 读取所有参数(A通道电流、B通道电流、电压) +ia, ib, vol = bl0939.read() + +# 单独读取各参数 +current_a = bl0939.current_a # A通道电流有效值 +current_b = bl0939.current_b # B通道电流有效值 +voltage = bl0939.voltage # 电压有效值 +``` + +## API 详细说明 + +### 类 `Bl0939` + +#### 构造函数 + +```python +Bl0939(port=1, mode=1, clk=0) +``` + +- **参数**: + - `port`: SPI 端口号 (默认1) + - `mode`: SPI 模式 (默认1) + - `clk`: SPI 时钟频率 (默认0) +- **功能**: 初始化 SPI 接口并复位芯片。 + +------ + +#### 方法 + +##### `read()` + +```python +read() -> (int, int, int) +``` + +- **返回**: 三元组 `(A通道电流, B通道电流, 电压)` +- **功能**: 一次性读取所有测量值。 + +##### `reset()` + +```python +reset() +``` + +- **功能**: 复位芯片(软复位)。 + +------ + +#### 属性 + +##### `current_a` + +```python +current_a -> int +``` + +- **返回**: A通道电流有效值(原始寄存器值) +- **注意**: 需根据实际电路转换为实际电流值(如 mA/A)。 + +##### `current_b` + +```python +current_b -> int +``` + +- **返回**: B通道电流有效值(原始寄存器值) + +##### `voltage` + +```python +voltage -> int +``` + +- **返回**: 电压有效值(原始寄存器值) +- **注意**: 需根据分压电路转换为实际电压值(如 V)。 + +------ + +## 示例代码 + +### 连续读取数据 + +```python +import utime +from bl0939 import Bl0939 + +bl0939 = Bl0939(port=1) + +for i in range(10): + ia, ib, vol = bl0939.read() + print("A电流: {}, B电流: {}, 电压: {}".format(ia, ib, vol)) + utime.sleep(1) +``` + +### 输出说明 + +- 输出值为芯片寄存器的原始数据,需根据实际电路转换为物理量: + - **电流转换公式**: + `实际电流 = 寄存器值 × (分流电阻比例 / 校准系数)` + - **电压转换公式**: + `实际电压 = 寄存器值 × (分压电阻比例 / 校准系数)` + +## 注意事项 + +1. **SPI 配置**:确保 SPI 端口、模式和时钟频率与硬件匹配。 +2. **校准**:实际使用时需校准比例系数(如脉冲常数)。 +3. **隔离设计**:测量高压时需添加光耦或互感器隔离。 +4. **单位转换**:寄存器值为无符号整数,需根据数据手册转换为实际单位。 + +------ + +## 故障排查 + +- **SPI 通信失败**:检查接线(CS/CLK/MOSI/MISO)和端口配置。 +- **数据异常**:确认分流电阻/分压电阻参数是否正确。 + +如需进一步帮助,请参考 BL0939 数据手册或联系 Quectel 技术支持 \ No newline at end of file diff --git a/libraries/power_meter_chips/HLW8110/README.md b/libraries/power_meter_chips/HLW8110/README.md new file mode 100644 index 0000000..b167ad1 --- /dev/null +++ b/libraries/power_meter_chips/HLW8110/README.md @@ -0,0 +1,126 @@ +# HLW8110 Energy Measurement Chip Demo Documentation + +## Overview + +This document provides a demonstration guide for using the HLW8110 energy measurement chip via UART interface. The HLW8110 is a high-precision IC for measuring AC/DC voltage, current, power and phase angle. + +## Key Features + +- Measures voltage (0.5V-5V input range) +- Measures current (via external shunt resistor) +- Calculates active power +- Phase angle measurement +- Overvoltage/undervoltage detection +- Zero-crossing detection +- Interrupt capability + +## Hardware Setup + +1. Connect HLW8110 UART pins to MCU: + - TX -> MCU RX + - RX -> MCU TX + - GND -> Common ground +2. Connect voltage input through appropriate divider +3. Connect current shunt resistor (typical 1-10mΩ) + +## Demo Code Explanation + +### 1. Initialization + +```python +hlw8110 = Hlw8110_uart(uart_n=UART.UART2) # Initialize with UART2 +``` + +### 2. Basic Measurements + +#### Read Current + +```python +curr, ic = hlw8110.read_i() # Returns (raw value, conversion coefficient) +``` + +- For actual current: + `I_actual = (raw_value * shunt_ratio) / ic` + +#### Read Voltage + +```python +volt, uc = hlw8110.read_u() +``` + +- For actual voltage: + `V_actual = (raw_value * divider_ratio) / uc` + +#### Read Power + +```python +power, pc = hlw8110.read_power() +``` + +- Active power calculation: + `P_actual = (raw_value * V_ratio * I_ratio) / pc` + +#### Phase Angle + +```python +angle = hlw8110.read_angle() # Returns degrees +``` + +### 3. Advanced Features + +#### Zero-Crossing Detection + +```python +hlw8110.zx_en(zx_type=3) # Enable both rising/falling edge detection +``` + +#### Interrupt Configuration + +```python +# Enable undervoltage interrupt +hlw8110.int_en(int_type=11) + +# Check interrupts +flag = hlw8110.process_int() +if flag & 0x0800: + print("Undervoltage detected!") +``` + +### 4. Full Demo Output + +``` +start reading... +reading complete. +电流寄存器值:1258291, 电流有效值转换系数:32768 +电压寄存器值:3145728, 电压有效值转换系数:32768 +有功功率寄存器值:536870912, 有功功率转换系数:32768 +相角:30.2 +电压欠压中断 just happened +``` + +## Calibration Notes + +1. Voltage measurement: + - Apply known voltage + - Adjust divider ratio until readings match +2. Current measurement: + - Apply known current + - Tune shunt resistor value +3. Power calculation: + - Verify with reference load + - Adjust coefficients if needed + +## Troubleshooting + +- **No readings**: Check UART connections and baud rate (fixed at 9600) +- **Incorrect values**: Verify reference voltages and resistor values +- **Interrupts not firing**: Confirm proper interrupt enable sequence + +## Application Ideas + +- Smart power strips +- Energy monitoring systems +- Power quality analyzers +- Appliance load profiling + +For detailed register descriptions and conversion formulas, refer to the HLW8110 datasheet. \ No newline at end of file diff --git a/libraries/power_meter_chips/HLW8110/README_zh.md b/libraries/power_meter_chips/HLW8110/README_zh.md new file mode 100644 index 0000000..307aeb2 --- /dev/null +++ b/libraries/power_meter_chips/HLW8110/README_zh.md @@ -0,0 +1,126 @@ +# HLW8110电能计量芯片演示文档 + +## 概述 + +本文档提供了通过UART接口使用HLW8110电能计量芯片的演示指南。HLW8110是一款高精度电能计量IC,可用于测量交流/直流电压、电流、功率和相位角。 + +## 主要特性 + +- 电压测量(0.5V-5V输入范围) +- 电流测量(通过外部分流电阻) +- 有功功率计算 +- 相位角测量 +- 过压/欠压检测 +- 过零检测 +- 中断功能 + +## 硬件连接 + +1. 将HLW8110的UART引脚连接到MCU: + - TX -> MCU RX + - RX -> MCU TX + - GND -> 共地 +2. 通过适当的分压电路连接电压输入 +3. 连接电流采样电阻(典型值1-10mΩ) + +## 示例代码说明 + +### 1. 初始化 + +```python +hlw8110 = Hlw8110_uart(uart_n=UART.UART2) # 使用UART2初始化 +``` + +### 2. 基本测量功能 + +#### 读取电流 + +```python +curr, ic = hlw8110.read_i() # 返回(原始值,转换系数) +``` + +- 实际电流计算: + `实际电流 = (原始值 × 分流比例) / 转换系数` + +#### 读取电压 + +```python +volt, uc = hlw8110.read_u() +``` + +- 实际电压计算: + `实际电压 = (原始值 × 分压比例) / 转换系数` + +#### 读取功率 + +```python +power, pc = hlw8110.read_power() +``` + +- 有功功率计算: + `实际功率 = (原始值 × 电压比例 × 电流比例) / 转换系数` + +#### 相位角 + +```python +angle = hlw8110.read_angle() # 返回角度值 +``` + +### 3. 高级功能 + +#### 过零检测 + +```python +hlw8110.zx_en(zx_type=3) # 使能上升沿/下降沿检测 +``` + +#### 中断配置 + +```python +# 使能欠压中断 +hlw8110.int_en(int_type=11) + +# 检查中断 +flag = hlw8110.process_int() +if flag & 0x0800: + print("检测到欠压!") +``` + +### 4. 完整演示输出 + +``` +开始读取... +读取完成。 +电流寄存器值:1258291,电流有效值转换系数:32768 +电压寄存器值:3145728,电压有效值转换系数:32768 +有功功率寄存器值:536870912,有功功率转换系数:32768 +相位角:30.2 +电压欠压中断已触发 +``` + +## 校准说明 + +1. 电压校准: + - 施加已知电压 + - 调整分压比例直到读数匹配 +2. 电流校准: + - 施加已知电流 + - 调整分流电阻值 +3. 功率校准: + - 使用标准负载验证 + - 必要时调整系数 + +## 故障排除 + +- **无读数**:检查UART连接和波特率(固定9600) +- **数值不正确**:确认参考电压和电阻值 +- **中断不触发**:检查中断使能顺序 + +## 应用场景 + +- 智能排插 +- 能源监测系统 +- 电能质量分析 +- 电器负载分析 + +详细寄存器说明和转换公式请参考HLW8110数据手册。 \ No newline at end of file