STM32duino:【玩STM32】STM32Duino的引脚映射,ADC,PWM等介绍

基于迷你强的STM32duino文章,感觉非常有价值,这小单片机做Arduino实在太强了,甩AVR的几条街,结合Maple Mini研究了一下,总结下面基础资料:
STM32duino:【玩STM32】STM32Duino的引脚映射,ADC,PWM等介绍_第1张图片
STM32duino:【玩STM32】STM32Duino的引脚映射,ADC,PWM等介绍_第2张图片

0、编程语法参考

http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/latest/language.html

1、引脚对应关系:

32个GPIO,也可以用序号表达。

序号 物理引脚

0        PA0
1        PA1
2        PA2
3        PA3
4        PA4
5        PA5
6        PA6
7        PA7
8        PA8
9        PA9
10        PA10
11        PA11
12        PA12
13        PA13
14        PA14
15        PA15
16        PB0
17        PB1
18        PB2
19        PB3
20        PB4
21        PB5
22        PB6
23        PB7
24        PB8
25        PB9
26        PB10
27        PB11
28        PB12
29        PB13
30        PB14
31        PB15
32        PC13
33        PC14
34        PC15

2、允许PWM引脚:

序号 物理引脚

0        PA0
1        PA1
2        PA2
3        PA3
6        PA6
7        PA7
8        PA8
9        PA9
10        PA10
16        PB0
22        PB6
23        PB7

PWM引脚简易测试程序,总共有12个PWM引脚,16bit即65536级PWM,很精密了:

void setup()
{
        pinMode(PB0, PWM);
        pinMode(PA7, PWM);
        pinMode(PA6, PWM);
        pinMode(PA3, PWM);
        pinMode(PA2, PWM);
        pinMode(PA1, PWM);
        pinMode(PA0, PWM);
        pinMode(PB7, PWM);
        pinMode(PB6, PWM);
        pinMode(PA10, PWM);
        pinMode(PA9, PWM);
        pinMode(PA8, PWM);
}
void loop()
{
        for (int i = 0; i < 65536; i++)
        {
                delayMicroseconds(40);
                pwmWrite(PB0, i);
                pwmWrite(PA7, i);
                pwmWrite(PA6, i);
                pwmWrite(PA3, i);
                pwmWrite(PA2, i);
                pwmWrite(PA1, i);
                pwmWrite(PA0, i);
                pwmWrite(PB7, i);
                pwmWrite(PB6, i);
                pwmWrite(PA10, i);
                pwmWrite(PA9, i);
                pwmWrite(PA8, i);
        }
}

PWM精度太高了,驱动LED没必要那么高,再来一个指数式适合驱动LED的,看起来亮度更线性。

void setup() {
  pinMode(PB0, PWM);
  pinMode(PA7, PWM);
  pinMode(PA6, PWM);
  pinMode(PA3, PWM);
  pinMode(PA2, PWM);
  pinMode(PA1, PWM);
  pinMode(PA0, PWM);
  pinMode(PB7, PWM);
  pinMode(PB6, PWM);
  pinMode(PA10, PWM);
  pinMode(PA9, PWM);
  pinMode(PA8, PWM);
}
void loop() {
  //指数式增加亮度,适合驱动LED,看起来亮度更线性
  for (int i = 0; i < 256; i++)
  {
    delay(20);
    pwmWrite(PB0, i * i);
    pwmWrite(PA7, i * i);
    pwmWrite(PA6, i * i);
    pwmWrite(PA3, i * i);
    pwmWrite(PA2, i * i);
    pwmWrite(PA1, i * i);
    pwmWrite(PA0, i * i);
    pwmWrite(PB7, i * i);
    pwmWrite(PB6, i * i);
    pwmWrite(PA10, i * i);
    pwmWrite(PA9, i * i);
    pwmWrite(PA8, i * i);
  }
}

或者用序号表示:

int pins[12] = {0, 1, 2, 3, 6, 7, 8, 9, 10, 16, 22, 23};
void setup()
{
  for (int i = 0; i < 12; i++)
  {
    pinMode(pins[i], PWM);
  }
}
void loop()
{
  for (int i = 0; i < 100; i++)
  {
    for (int j = 0; j < 12; j++)
    {
      pwmWrite(pins[j], i * i);
    }
    delay(20);
  }
}

3、允许的ADC引脚:

ADC总共有10个通道,12bit即4096级。引脚如下
物理引脚,ADC通道

PA0        CH0
PA1        CH1
PA2        CH2
PA3        CH3
PA4        CH4
PA5        CH5
PA6        CH6
PA7        CH7
PB0        -(不能用序号表达)
PB1        -(不能用序号表达)

ADC测试程序:

void setup()
{
  Serial.begin(115200);
  pinMode(PB0, INPUT_ANALOG);
  pinMode(PA7, INPUT_ANALOG);
  pinMode(PA6, INPUT_ANALOG);
  pinMode(PA5, INPUT_ANALOG);
  pinMode(PA4, INPUT_ANALOG);
  pinMode(PA3, INPUT_ANALOG);
  pinMode(PA2, INPUT_ANALOG);
  pinMode(PA1, INPUT_ANALOG);
  pinMode(PA0, INPUT_ANALOG);
  pinMode(PB1, INPUT_ANALOG);
}
void loop()
{
  delay(50);
  Serial.print("\tPB0="); Serial.print(analogRead(PB0));
  Serial.print("\tPA7="); Serial.print(analogRead(PA7));
  Serial.print("\tPA6="); Serial.print(analogRead(PA6));
  Serial.print("\tPA5="); Serial.print(analogRead(PA5));
  Serial.print("\tPA4="); Serial.print(analogRead(PA4));
  Serial.print("\tPA3="); Serial.print(analogRead(PA3));
  Serial.print("\tPA2="); Serial.print(analogRead(PA2));
  Serial.print("\tPA1="); Serial.print(analogRead(PA1));
  Serial.print("\tPA0="); Serial.print(analogRead(PA0));
  Serial.print("\tPB1="); Serial.println(analogRead(PB1));
}

4、允许的串口

总共3个硬件外接串口Serial1、Serial2、Serial3,一个USB虚拟串口Serial:

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);  //TX=PA9,RX=PA10
  Serial2.begin(115200);  //TX=PA2,RX=PA3
  Serial3.begin(115200);  //TX=PB10,RX=PB11
}
void loop()
{
  delay(100);
  Serial.println("Test Serial");
  Serial1.println("Test Serial1");
  Serial2.println("Test Serial2");
  Serial3.println("Test Serial3");
}

你可能感兴趣的:(5.,Arduino,stm32,arduino)