在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 驱动风扇模块
本帖最后由 jinglixixi 于 2021-8-21 00:45 编辑下边是通过延时调节I/O模拟 PWM的占空比?
引用: littleshrimp 发表于 2021-8-21 08:07 下边是通过延时调节I/O模拟 PWM的占空比?
看错了 好像是通过延时调节频率
引用: littleshrimp 发表于 2021-8-21 08:07 下边是通过延时调节I/O模拟 PWM的占空比?
道理相近
了解更多智能硬件相关产品~请戳:【华为开发者联盟生态市场智能硬件专区https://developer.huawei.com/consumer/cn/market/prod-list?categoryIdL1=7e857be8d4b24a7e97ddf5d941b38ca1&ha_source=mkt-thirdforum】
引用: freebsder 发表于 2021-8-23 17:13 看来楼主买了不少模块啊
哈哈!