1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| LEDB = 5 LEDR = 6 LEDG = 7 BEEP = 8 --设置 IO 口模式 gpio.mode(LEDB, gpio.OUTPUT) gpio.mode(LEDR, gpio.OUTPUT) gpio.mode(LEDG, gpio.OUTPUT) gpio.mode(BEEP, gpio.OUTPUT) --初始设置输出低电平,所有灯都不亮 gpio.write(LEDB, gpio.LOW) gpio.write(LEDR, gpio.LOW) gpio.write(LEDG, gpio.LOW) --初始设置输出低电平,蜂鸣器不响 gpio.write(BEEP, gpio.HIGH) pwm.setup(LEDR, 100, 512) pwm.start(LEDR) function led(r, g, b) pwm.setduty(LEDB, b) pwm.setduty(LEDR, r) pwm.setduty(LEDG, g) end --初始化 COUNTER COUNTER = 0 REVERSE = 0 NEXT = 0 --注册定时器 0 tmr.register(0, 200, tmr.ALARM_AUTO, function() if REVERSE == 0 then COUNTER = COUNTER + 20; else COUNTER = COUNTER - 20; end if COUNTER >= 1020 then REVERSE = 1 COUNTER = 1020 end if COUNTER <= 0 then COUNTER = 0 REVERSE = 0 end led(COUNTER, 1023, 1023) print("----LEDR is Changing----"..COUNTER) end ) --启动定时器 0 tmr.start(0)
|
PWM Module
pwm.setup()
原型: pwm.setup(pin, clock, duty)
作用: 将引脚设置为PWM模式,最多只能设置6个引脚
参数:
- 1〜12,IO引脚
- 1〜1000,pwm频率
- 0〜1023,pwm占空比,最大值1023(10bit)
pwm.start( )
原型: pwm.start(pin)
作用: PWM启动时,波形被施加到GPIO引脚。
参数: 1〜12,IO引脚
pwm.setduty()
原型: pwm.setduty(pin, duty)
作用: 设置引脚占空比
参数:
- 1〜12,IO引脚
- 0〜1023,pwm占空比,最大值1023(10bit)
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| -- D1 is connected to green led -- D2 is connected to blue led -- D3 is connected to red led pwm.setup(1, 500, 512) pwm.setup(2, 500, 512) pwm.setup(3, 500, 512) pwm.start(1) pwm.start(2) pwm.start(3) function led(r, g, b) pwm.setduty(1, g) pwm.setduty(2, b) pwm.setduty(3, r) end led(512, 0, 0) -- set led to red led(0, 0, 512) -- set led to blue.
|