无忧启动论坛

 找回密码
 注册
搜索
WEPE笔记本电脑手机维修小包 IT发烧友的必备工具最纯净的「微PE装机优盘」UEPON大师作品卡瑞飞系统和装机二合一超级U盘
杏雨梨云专业量产版USB-CD启动U盘,装机专用系统gho:最纯净好用系统下载站广告联系 QQ:184822951 微信:wuyouceo
查看: 1259|回复: 7

[转贴] 10.11 使用大量LED(Charlieplexing)

[复制链接]
发表于 2018-10-17 09:10:29 | 显示全部楼层 |阅读模式
Using Lots of LEDs (Charlieplexing)
NOTE
Be sure to check out the accompanying video for this recipe at http://razzpisampler.oreilly.com.
Problem
You want to control lots of LEDs using as few GPIO pins as possible.
Solution
The way to do this is to use a technique called Charlieplexing. The name comes from the inventor, Charlie Allen of the company Maxim, and the technique takes advantage of the feature of GPIO pins that allows them to be changed from outputs to inputs while a program is running. When a pin is changed to be an input, not enough current will flow through it to light an LED or influence other pins connected to the LED that are set as outputs.
Figure 10-12 shows the schematic for controlling six LEDs with three pins.”

00.png

To make this recipe, you will need:
Breadboard and jumper wires (see “Prototyping Equipment”)
Three 470Ω resistors (see “Resistors and Capacitors”)
Six LEDs (see “Opto-Electronics”)
Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called charlieplexing.py.”

01.png


This example code prompts you to enter a number between 0 and 5, which then lights one of the six LEDs:

  1. import RPi.GPIO as GPIO

  2. pins = [18, 23, 24]

  3. pin_led_states = [
  4.   [1, 0, -1], # A
  5.   [0, 1, -1], # B
  6.   [-1, 1, 0], # C
  7.   [-1, 0, 1], # D
  8.   [1, -1, 0], # E
  9.   [0, -1, 1]  # F
  10. ]

  11. GPIO.setmode(GPIO.BCM)

  12. def set_pin(pin_index, pin_state):
  13.     if pin_state == -1:
  14.         GPIO.setup(pins[pin_index], GPIO.IN)
  15.     else:
  16.         GPIO.setup(pins[pin_index], GPIO.OUT)
  17.         GPIO.output(pins[pin_index], pin_state)

  18. def light_led(led_number):
  19.     for pin_index, pin_state in enumerate(pin_led_states[led_number]):
  20.         set_pin(pin_index, pin_state)

  21. set_pin(0, -1)
  22. set_pin(1, -1)
  23. set_pin(2, -1)

  24. while True:
  25.     x = int(raw_input("Pin (0 to 5):"))
  26.     light_led(x)
复制代码


Discussion
To understand how Charlieplexing works, imagine that you want to light LED A in Figure 10-12. An LED will only light when its positive lead is high and its negative lead is low. If the voltage is the other way around, it will not light. To light LED A, you need its lead connected to GPIO 18 (via a resistor) to be high, and the other lead to LED A, connected to GPIO 23 by a resistor, to be low. However, you must also make sure that GPIO 24 is set to be an input; otherwise, LED C or D will also light depending on whether GPIO 24 is high or low.
The array pin_led_states holds the settings for each GPIO for each of the six LEDs. If the value is 0, the pin is low; 1 means high and -1 means set to be an input.
The number of LEDs that can be controlled per GPIO pin is given by the formula:
LEDs = n2 - n
Using four pins, you can have 16, 4, or 12 LEDs, whereas 10 pins would give you a massive 90 LEDs.
In this example, you’re lighting only one LED at a time. To light more than one at a time, you need to run a refresh loop that keeps the desired state of the LEDs in an array and refreshes the display, turning on the LEDs that need to be on before moving on to the next. It must do this fast enough so that it appears that more than one of the LEDs is on at the same time.
The more LEDs you use when it comes to making it appear that more than one LED is on at a time, the less time the LED will actually be lit, and the dimmer the LEDs will become.

See Also
For more information about Charlieplexing, see Wikipedia.


摘录来自: Simon Monk. “Raspberry Pi Cookbook。” Apple Books.




 楼主| 发表于 2018-10-17 09:12:26 | 显示全部楼层
对输入部分进行判断小改,以防出错:

  1. import RPi.GPIO as GPIO
  2. pins = [18,23,24]

  3. pin_led_states = [
  4.     [1,0,-1], #A
  5.     [0,1,-1], #B
  6.     [-1,1,0], #C
  7.     [-1,0,1], #D
  8.     [1,-1,0], #E
  9.     [0,-1,1]  #F
  10.     ]
  11. GPIO.setmode(GPIO.BCM)

  12. def set_pin(pin_index,pin_state):
  13.     if pin_state == -1:
  14.         GPIO.setup(pins[pin_index],GPIO.IN)
  15.     else:
  16.         GPIO.setup(pins[pin_index],GPIO.OUT)
  17.         GPIO.output(pins[pin_index],pin_state)

  18. def light_led(led_number):
  19.     for pin_index,pin_state in enumerate(pin_led_states[led_number]):
  20.         set_pin(pin_index,pin_state)

  21. set_pin(0,-1)
  22. set_pin(1,-1)
  23. set_pin(2,-1)

  24. try:
  25.     while True:
  26.         x = input("Pin (0 to 5) or 'q' to exit:")
  27.         if '5' >= x >= '0':
  28.             light_led(int(x))
  29.         elif  x in ['q' ,'Q']:
  30.             break
  31.         else:
  32.             print('input error number')
  33. finally:
  34.     GPIO.cleanup()
复制代码
回复

使用道具 举报

发表于 2018-10-17 09:33:06 | 显示全部楼层
啥时候开了树莓派版块~

点评

也就是最近一个月的事,欢迎大家一起交流。  详情 回复 发表于 2018-10-17 09:55
回复

使用道具 举报

 楼主| 发表于 2018-10-17 09:55:58 | 显示全部楼层
2010chinawy 发表于 2018-10-17 09:33
啥时候开了树莓派版块~

也就是最近一个月的事,欢迎大家一起交流。
回复

使用道具 举报

发表于 2018-10-17 14:39:20 | 显示全部楼层
        热心回复!
回复

使用道具 举报

发表于 2018-10-17 19:38:06 | 显示全部楼层
玩不转,帮顶...
回复

使用道具 举报

发表于 2018-10-18 10:52:00 | 显示全部楼层
支持一下,就是没时间玩~
回复

使用道具 举报

发表于 2018-10-19 00:01:41 | 显示全部楼层
等版块开了,到时再交流
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2019-7-17 22:35

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表