Skip to content

需要注意的问题以及扩展功能 #1

@mysterywolf

Description

@mysterywolf

1.启用PWM不能调用pinMode(led_pin, OUTPUT) 否则pwm会失效

void setup() {
  //Declaring LED pin as output
  //pinMode(led_pin, OUTPUT); //不能设置为OUTPUT,否则PWM会失效
}
void loop() {
  //Fading the LED
  for(int i=0; i<255; i++){
    analogWrite(led_pin, i);
    delay(5);
  }
  for(int i=255; i>0; i--){
    analogWrite(led_pin, i);
    delay(5);
  }
}

因为底层已经将对应的IO设置为模拟输入或者复用推挽,结果pinMode之后把IO模式改成了纯输入输出,因此不好使了。
不需要特别修正,只需要知道调用analogRead和analogWrite的时候不需要设置pinMode即可。一旦调用pinMode,该引脚将丧失analogWrite或者analogRead功能,后续只能当做普通IO使用。
Arduino 官方文档也是这么建议的:

You do not need to call pinMode() to set the pin as an output before calling analogWrite().
The analogWrite function has nothing to do with the analog pins or the analogRead function.

2 扩展API

以下三个函数为非Arduino-AVR中存在,但是在Arduino-STM32中扩展:
analogReadResolution()
analogWriteResolution()
analogWriteFrequency()
RTduino扩展:
mapFloat() 双精度浮点映射

3 包含Arduino.h头文件

调用到Arduino相关函数和宏的源文件,请包含Arduino.h头文件
cannot_find_setuploop
includearduino

4 Serial.begin

目前Serial.begin设置波特率是无效的,串口的波特率跟随RT-Thread的串口设备框架设置的波特率走,默认是115200.
换句话说,即便Serial.begin(9600), 串口的波特率依然是115200

5. Keil AC5

如果使用Keil AC5环境,需要勾选GNU extension。AC6不需要。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions