Mbed OS 文档翻译 之 参考(API(驱动(DigitalIn)))

DigitalIn

使用 DigitalIn 接口读取数字输入引脚的值。逻辑电平为 1 或 0。

您可以使用任何编号的 Arm Mbed 引脚可以用作 DigitalIn。

DigitalIn 类参考

API 摘要

mbed::DigitalIn 类参考

公共成员函数
 DigitalIn (PinName pin)
 DigitalIn (PinName pin, PinMode mode)
int read ()
void mode (PinMode pull)
int is_connected ()
 operator int ()
受保护的属性
gpio_t gpio

DigitalIn hello, world

main.cpp                                                                                                                                               导入到 Mbed IDE

/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"

DigitalIn  mypin(SW2); // change this to the button on your board
DigitalOut myled(LED1);

int main()
{
    // check mypin object is initialized and connected to a pin
    if(mypin.is_connected()) {
        printf("mypin is connected and initialized! \n\r");
    }
    
    // Optional: set mode as PullUp/PullDown/PullNone/OpenDrain
    mypin.mode(PullNone); 
    
    // press the button and see the console / led change
    while(1) {
        printf("mypin has value : %d \n\r", mypin.read());
        myled = mypin; // toggle led based on value of button
        wait(0.25);
    }
}

DigitalIn 示例

要处理中断,请参阅 InterruptIn

逻辑函数的示例 - 布尔逻辑 NOT,AND,OR,XOR:

main.cpp                                                                                                                                              导入到 Mbed IDE

#include "mbed.h"
 
DigitalIn a(D0);
DigitalIn b(D1);
DigitalOut z_not(LED1);
DigitalOut z_and(LED2);
DigitalOut z_or(LED3);
DigitalOut z_xor(LED4);
 
int main() {
    while(1) {
        z_not = !a;
        z_and = a && b;
        z_or = a || b;
        z_xor = a ^ b;
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PCA9685是一款16通道PWM控制器,常用于控制舵机、LED灯等设备。mbed提供了一个名为"PwmOut"的类,可以用于控制PCA9685。 首先,需要连接PCA9685到mbed板子上。连接时需要将PCA9685的地址引脚(A0、A1、A2)连接到VCC或GND,以设置其I2C地址。然后将PCA9685的SCL和SDA引脚连接到mbed板子上对应的引脚。 接下来,需要使用mbed的I2C库进行通信。首先需要创建一个I2C对象,指定其通信的引脚和地址。然后可以使用该对象进行读写操作。 以下是一个简单的例子: ```cpp #include "mbed.h" #include "PwmOut.h" // 设置PCA9685的I2C地址为0x40 const int PCA9685_ADDR = 0x40; int main() { // 创建I2C对象,设置通信引脚并指定地址 I2C i2c(D14, D15); // SDA, SCL i2c.frequency(400000); // 设置通信速率为400kHz PwmOut servo(i2c, PCA9685_ADDR, 0); // 创建PwmOut对象,指定I2C对象、地址和通道号 // 设置PWM周期为20ms servo.period_ms(20); // 设置舵机角度为90度 servo.pulsewidth_ms(1.5); while (1) { // 循环控制舵机运动 servo.pulsewidth_ms(1.0); // 最小角度 wait(1); servo.pulsewidth_ms(1.5); // 中间角度 wait(1); servo.pulsewidth_ms(2.0); // 最大角度 wait(1); } } ``` 在这个例子中,使用了mbed的PwmOut类来控制PCA9685的PWM输出。在创建PwmOut对象时,需要指定I2C对象、PCA9685的地址和PWM通道号。然后可以使用PwmOut对象的方法来设置PWM周期和占空比,从而控制舵机的角度。 请注意,具体的代码可能会因为使用的mbed板子型号、PCA9685的I2C地址等因素而有所不同。但是基本的思路和方法是相似的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值