1.继电器
?本案例使用了一块两路高电平触发的继电器模块,其触发引脚分别接到了GPIO31和GPIO32。使用了两块一路的低电平触发的继电器模块,其触发引脚分别接到了GPIO19和GPIO20上。使用了4个触摸按键,按键引脚分别接入到了GPIO18、GPIO17、GPIO16、GPIO15上,来实现继电器开和闭。
注意:由于开发板上的io口的驱动能力不同,以5V光耦继电器模块为例,当前案例中的由高电平触发的继电器模块的触发引脚接入到了排母中的IO口上了,低电平触发的继电器模块的触发引脚接入到了 非排母处的IO口上了。
-
只能低电平触发的继电器模块
-
高低电平都可以触发的继电器模块
import utime as time
from driver import GPIO
#中断回调函数
def key1_callback(args):
global flag1
if flag1==1:
led1.write(1)
relay_1.write(1)
flag1=2
elif flag1==2:
led1.write(0)
relay_1.write(0)
flag1=1
print("level of led1:{},relay_1:{}".format(led1.read(),relay_1.read()))
key1.disableIrq()
key1.clearIrq()
def key2_callback(args):
global flag2
if flag2==1:
led2.write(1)
relay_2.write(1)
flag2=2
elif flag2==2:
led2.write(0)
relay_2.write(0)
flag2=1
print("level of led2:{},relay_2:{}".format(led2.read(),relay_2.read()))
key2.disableIrq()
key2.clearIrq()
def key3_callback(args):
global flag3
if flag3==1:
led3.write(1)
relay_3.write(1)
flag3=2
elif flag3==2:
led3.write(0)
relay_3.write(0)
flag3=1
print("level of led3:{},relay_3:{}".format(led3.read(),relay_3.read()))
key3.disableIrq()
key3.clearIrq()
def key4_callback(args):
global flag4
if flag4==1:
led4.write(1)
relay_4.write(1)
flag4=2
elif flag4==2:
led4.write(0)
relay_4.write(0)
flag4=1
print("level of led4:{},relay_4:{}".format(led4.read(),relay_4.read()))
key4.disableIrq()
key4.clearIrq()
if __name__ == '__main__':
global flag1,flag2,flag3,flag4
flag1=1
flag2=1
flag3=1
flag4=1
#实例化key1
key1=GPIO(10,10)
key2=GPIO(10,10)
key3=GPIO(10,10)
key4=GPIO(10,10)
#打开key1
key1.open("key1")
key2.open("key2")
key3.open("key3")
key4.open("key4")
#实例化led1
led1=GPIO()
led2=GPIO()
led3=GPIO()
led4=GPIO()
#打开led1
led1.open('led1')
led2.open('led2')
led3.open('led3')
led4.open('led4')
#实例化relay
relay_1=GPIO()
relay_2=GPIO()
relay_3=GPIO()
relay_4=GPIO()
#打开
relay_1.open("relay_1")
relay_2.open("relay_2")
relay_3.open("relay_3")
relay_4.open("relay_4")
time.sleep(2)
#打开按键使能
while True:
time.sleep(1)
key1.enableIrq(key1_callback)
key2.enableIrq(key2_callback)
key3.enableIrq(key3_callback)
key4.enableIrq(key4_callback)
{
"version": "1.0.0",
"io": {
"led1": {
"type": "GPIO",
"port": 6,
"dir": "output",
"pull": "pullup"
},
"led2": {
"type": "GPIO",
"port": 7,
"dir": "output",
"pull": "pullup"
},
"led3": {
"type": "GPIO",
"port": 8,
"dir": "output",
"pull": "pullup"
},
"led4": {
"type": "GPIO",
"port": 9,
"dir": "output",
"pull": "pullup"
},
"key1": {
"type": "GPIO",
"port": 18,
"dir": "irq",
"pull": "pullup",
"intMode":"rising"
},
"key2": {
"type": "GPIO",
"port": 17,
"dir": "irq",
"pull": "pullup",
"intMode":"rising"
},
"key3": {
"type": "GPIO",
"port": 16,
"dir": "irq",
"pull": "pullup",
"intMode":"rising"
},
"key4": {
"type": "GPIO",
"port": 15,
"dir": "irq",
"pull": "pullup",
"intMode":"rising"
},
"relay_1": {
"type": "GPIO",
"port": 31,
"dir": "output",
"pull": "pullup"
},
"relay_2": {
"type": "GPIO",
"port": 32,
"dir": "output",
"pull": "pullup"
},
"relay_3": {
"type": "GPIO",
"port": 20,
"dir": "output",
"pull": "pullup"
},
"relay_4": {
"type": "GPIO",
"port": 19,
"dir": "output",
"pull": "pullup"
},
"serial1": {
"type": "UART",
"port": 0,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial2": {
"type": "UART",
"port": 1,
"dataWidth": 8,
"baudRate": 9600,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
},
"serial3": {
"type": "UART",
"port": 2,
"dataWidth": 8,
"baudRate": 115200,
"stopBits": 1,
"flowControl": "disable",
"parity": "none",
"timeout": 1000
}
},
"debugLevel": "ERROR"
}
|