STM32|STM32的Arduino的引脚映射

STM32|STM32的Arduino的引脚映射
文章图片

1.普通I/0

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

2、允许PWM引脚:
序号物理引脚 0PA0 1PA1 2PA2 3PA3 6PA6 7PA7 8PA8 9PA9 10PA10 16PB0 22PB6 23PB7

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); } }

【STM32|STM32的Arduino的引脚映射】或者用序号表示:
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通道 PA0CH0 PA1CH1 PA2CH2 PA3CH3 PA4CH4 PA5CH5 PA6CH6 PA7CH7 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"); }

    推荐阅读