发表了主题帖:
补充【得捷电子Follow Me第二期】
这个帖子是补充 [得捷电子Follow Me第二期提交任务](https://bbs.eeworld.com.cn/thread-1263126-1-1.html) 的NUA7802 ADC模块的使用,选择在任务3中进行补充。
### 任务3:控制WS2812B
> 任务要求:使用按键控制板载`NeoPixel LED` 的显示和颜色切换
- 本次任务使用到了板载的按键、`NeoPixel LED`和额外的ADC模块,程序逻辑为开发板驱动ADC模块进行采集,当ADC模块采集的信号超出阈值时,变化板载`NeoPixel LED`的颜色,同时当检测到按键按下时,固定当前的屏幕显示和板载LED的颜色,再次按下按键时,继续进行采集。
- 准备工作:
因为新增了ADC模块使用,所以需要准备好[adafruit-circuitpython-register](https://github.com/adafruit/Adafruit_CircuitPython_Register/releases)和[circuitpython-nau7802](https://github.com/adafruit/CircuitPython_NAU7802/releases/tag/2.1.1)这两个库,将它们放到开发板的`/lib`目录下。
- `code.py`代码如下:
```python
# 导入所需模块
import board
import time
import displayio
from digitalio import DigitalInOut, Direction, Pull
import neopixel
from adafruit_led_animation.animation.blink import Blink
from adafruit_led_animation.color import JADE, BLACK, ORANGE, GOLD, OLD_LACE,RED
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
from cedargrove_nau7802 import NAU7802
pixel_pin = board.NEOPIXEL
num_pixels = 1
led_color = 0
# 初始化像素灯
pixels = neopixel.NeoPixel(
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=neopixel.GRB
)
# 配置ADC模块
nau7802 = NAU7802(board.I2C(), address=0x2A, active_channels=1)
# 定义ADC初始化所需函数
def zero_channel():
nau7802.calibrate("INTERNAL")
nau7802.calibrate("OFFSET")
def read_raw_value(samples=2):
sample_sum = 0
sample_count = samples
while sample_count > 0:
while not nau7802.available():
pass
sample_sum = sample_sum + nau7802.read()
sample_count -= 1
return int(sample_sum / samples)
blink = Blink(pixels, speed=0.5, color=RED)
btn = DigitalInOut(board.BOOT0)
btn.direction = Direction.INPUT
btn.pull = Pull.UP
screen = board.DISPLAY
text = displayio.Group()
font = bitmap_font.load_font("/font/fusion_pixel.pcf")
text_0 = "红色"
text_color = RED
bt_status = 1
bt_cnt = 0
# 使能nau7802数字和模拟电源
enabled = nau7802.enable(True)
time.sleep(2)
# 校准和归零通道1
nau7802.channel = 1
zero_channel()
while True:
# 当检测到按键按下时,判断系统下次运行状态
if not btn.value:
time.sleep(0.1)
if not btn.value & bt_cnt:
bt_status = not bt_status
bt_cnt = 0
else:
pass
if bt_status:
nau7802.channel = 1
value = read_raw_value()
if value > 300 & value < 3000:
text_color = JADE
elif value < 300 & value > -3000:
text_color = ORANGE
elif alue < -3000 & value > -6000:
text_color = GOLD
elif value < -6000 & value > -8000:
text_color = OLD_LACE
elif value < -8000 & value > -10000:
text_color = RED
else:
pass
blink = Blink(pixels, speed=1, color=text_color)
text_0 = "NAU7802 read is :\r\n %7.0f" %value
text = displayio.Group()
content_0 = label.Label(font, text=text_0,x=10, y=50, color = text_color, scale=3)
text.append(content_0)
screen.show(text)
screen.refresh()
time.sleep(0.1) # sleep for debounce
# 执行led闪烁动态效果
blink.animate()
bt_cnt = 1
else:
time.sleep(0.1)
```
- 效果如下:
- 使用手指靠近ADC模块的差分输入端,ADC的采样值会发生变化,LED颜色变成红色,按下按键后固定当前状态
![image-20231228000033001](https://taotie-pic.oss-cn-guangzhou.aliyuncs.com/picture/image-20231228000033001.png)
- 将手指拿开ADC模块,按下按键,让系统采集ADC模块的值,LED随ADC的采样值变成绿色,再次按下按键固定当前值
![image-20231228000307926](https://taotie-pic.oss-cn-guangzhou.aliyuncs.com/picture/image-20231228000307926.png)