【AB32VG1开发板测评】按键控制LED及风扇
[复制链接]
本帖最后由 jinglixixi 于 2021-8-21 00:45 编辑
在AB32VG1开发板上,配有LED和KEY,其原理图如图1所示。
图1 LED与KEY 线路连接
以KEY控制LED的程序如下:
int main(void)
{
uint32_t cnt = 0;
uint8_t pin = rt_pin_get("PE.1");
rt_pin_mode(pin, PIN_MODE_OUTPUT);
uint8_t pin1 = rt_pin_get("PE.4");
rt_pin_mode(pin1, PIN_MODE_OUTPUT);
uint8_t pin2 = rt_pin_get("PA.1");
rt_pin_mode(pin2, PIN_MODE_OUTPUT);
uint8_t S2 = rt_pin_get("PF.1");
rt_pin_mode(S2, PIN_MODE_INPUT_PULLUP );
uint8_t S3 = rt_pin_get("PF.0");
rt_pin_mode(S3, PIN_MODE_INPUT_PULLUP );
uint8_t S4 = rt_pin_get("PA.2");
rt_pin_mode(S4, PIN_MODE_INPUT_PULLUP );
rt_pin_write(pin, PIN_HIGH);
rt_pin_write(pin1, PIN_HIGH);
rt_pin_write(pin2, PIN_HIGH);
while (1)
{
if(rt_pin_read(S2)==PIN_LOW) {
rt_pin_write(pin, PIN_LOW);
} else {
rt_pin_write(pin, PIN_HIGH);
}
if(rt_pin_read(S3)==PIN_LOW) {
rt_pin_write(pin1, PIN_LOW);
} else {
rt_pin_write(pin1, PIN_HIGH);
}
if(rt_pin_read(S4)==PIN_LOW) {
rt_pin_write(pin2, PIN_LOW);
} else {
rt_pin_write(pin2, PIN_HIGH);
}
}
}
该程序的作用是以按键S2~S4分别控制LED_R、LED_G及LED_B的亮灭。
在开发板上配有Arduino接口,利用它可以控制外挂的LED模块,在控制引脚连接PA0的情况下,其控制程序如下:
#include <rtthread.h>
#include "board.h"
int main(void)
{
uint32_t cnt = 0;
uint8_t pin0 = rt_pin_get("PA.0");
rt_pin_mode(pin0, PIN_MODE_OUTPUT);
while (1)
{
if (cnt % 2 == 0) {
rt_pin_write(pin0, PIN_LOW);
} else {
rt_pin_write(pin0, PIN_HIGH);
}
cnt++;
rt_thread_mdelay(500);
}
return 0;
}
图2 驱动LED模块
此外,若将LED模块换为风扇模块则可控制风扇的运转,并可以延时值来调节风扇转速。
图3 驱动风扇模块
|