3.24 haas506 2.0开发教程-example-74HC595

最新案例教程点击下方链接跳转,CSDN已停止更新

点击跳转HaaS506官方最新案例







案例说明

案例使用两个74HC595模块控制16个灯的开关,每个74HC595最多可以控制8个.

注意:控制GPIO引脚需要5v电压

1.逻辑原理图

在这里插入图片描述

2.代码

main.py

'''
@Date : 2021/08/05 20:51:50
'''
import utime as time
from driver_595 import dr595

global com_data_l
global com_data_h
global led_i
led_i = 0
com_data_h = 0xff
com_data_l = 0xff
ledshow = [0xff,0xff]
drv = dr595('data_595','clk_595','cs_595')
def flow_led():
    global led_i
    led_i+=1
    if led_i == 5:
        led_i = 1
    if led_i == 1:
    	#改变数值可以控制1~16个不同的灯光亮灭
        led_open(13)
        led_close(14)
        led_close(15)
        led_close(16)
    if led_i == 2:
        led_close(13)
        led_open(14)
        led_close(15)
        led_close(16)
    if led_i == 3:
        led_close(13)
        led_close(14)
        led_open(15)
        led_close(16)
    if led_i == 4:
        led_close(13)
        led_close(14)
        led_close(15)
        led_open(16)

def led_closeall():
    ledshow2 = [0xff,0xff]
    drv.senddata(ledshow2,2)

def led_open(i):
    global com_data_l
    global com_data_h
    i = i - 1
    if i >= 0 and i <= 7:
        com_data_l = com_data_l & ~(2**i)
        ledshow[1] = com_data_l
        ledshow[0] = com_data_h
    elif i > 7 and i <= 15:
        ledshow[1] = com_data_l
        com_data_h = com_data_h & ~(2**(i-8))
        ledshow[0] = com_data_h
    drv.senddata(ledshow,2)

def led_close(i):
    global com_data_l
    global com_data_h
    i = i -1
    if i >= 0 and i <= 7:
        com_data_l = com_data_l | (2**i)
        ledshow[1] = com_data_l
        ledshow[0] = com_data_h
    elif i > 7 and i <= 15:
        ledshow[1] = com_data_l
        com_data_h = com_data_h | (2**(i-8))
        ledshow[0] = com_data_h
    drv.senddata(ledshow,2)  

def main():
    while True:
        time.sleep(1)
        flow_led()
        
if __name__ == '__main__':
    main()    
    

driver_595.py

# -*- coding: UTF-8 -*-

from driver import GPIO
import utime as time

class dr595(object):
    def __init__(self,data,clk,cs):
        self.clk = GPIO()
        self.cs = GPIO()
        self.data = GPIO()
        self.clk.open(clk)
        self.cs.open(cs)
        self.data.open(data)
        pass

    def __del__(self):
        self.clk.close()
        self.cs.close()
        self.data.close()

    def __cs(self):
        self.cs.write(0)
        time.sleep_us(10)
        self.cs.write(1)

    def __sendbyte(self,byte):
        for i in range(8):
            if byte & 0x80:
                self.data.write(1)
            else:
                self.data.write(0)
            
            self.clk.write(0)
            time.sleep_us(1)
            self.clk.write(1)
            byte <<= 1

    def senddata(self,data,len):
        for i in range(len):
            self.__sendbyte(data[i])
        self.__cs()

board.json


{
    "version": "1.0.0",
    "io": {
        "clk_595": {
            "type": "GPIO",
            "port": 4,
            "dir": "output",
            "pull": "pullup"
        },
        "data_595": {
            "type": "GPIO",
            "port": 24,
            "dir": "output",
            "pull": "pullup"
        },
        "cs_595": {
            "type": "GPIO",
            "port": 44,
            "dir": "output",
            "pull": "pullup"
        },
      "serial1":{
        "type":"UART",
        "port":0,
        "dataWidth":8,
        "baudRate":9600,
        "stopBits":1,
        "flowControl":"disable",
        "parity":"none"
      },
      "serial2":{
        "type":"UART",
        "port":1,
        "dataWidth":8,
        "baudRate":4800,
        "stopBits":1,
        "flowControl":"disable",
        "parity":"none"
      },
      "serial3":{
        "type":"UART",
        "port":2,
        "dataWidth":8,
        "baudRate":9600,
        "stopBits":1,
        "flowControl":"disable",
        "parity":"none"
      }
    },
    "debugLevel": "error",
    "repl":"disable"
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值