STM32G070RBT6基于Arduino串口的使用

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32G070RBT6基于Arduino串口的使用相关的知识,希望对你有一定的参考价值。

STM32G070RBT6基于Arduino串口的使用


📓STM32G070RBT6串口资源介绍

✨STM32G070RBT6总共有4个USART资源。

Serial1 for USART1
Serial2 for USART2
Serial3 for USART3
Serial4 for UART4

🔖默认情况下,只有一个Serialx实例映射到通用Serial名。对于STM32G070RBT6而言,映射的管脚为:PA1PA0

  • 📍在C:\\Users\\Administrator\\AppData\\Local\\Arduino15\\packages\\STMicroelectronics\\hardware\\stm32\\2.3.0\\variants\\STM32G0xx\\G070RBT\\variant_generic.h中可以找到相关定义:
// Default pin used for generic 'Serial' instance
// Mandatory for Firmata
#ifndef PIN_SERIAL_RX
  #define PIN_SERIAL_RX         PA1      
#endif
#ifndef PIN_SERIAL_TX
  #define PIN_SERIAL_TX         PA0       
#endif

✅默认串口使用示例1

#define led1  PC2
#define led2  PC3

#define button1  PC0
#define button2  PC1

// the setup function runs once when you press reset or power the board
void setup() 
  Serial.begin(115200);//默认串口1的输出管脚:PA0 PA1。
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);
   digitalWrite(led1, LOW);
   digitalWrite(led2, HIGH);
     pinMode(button1, INPUT_PULLUP);
   pinMode(button2, INPUT_PULLUP);



// the loop function runs over and over again forever
void loop() 
 digitalToggleFast(PC_2);   // turn the LED on (HIGH is the voltage level)
  digitalToggle(led2);
  delay(1000);                       // wait for a second
  Serial.println("Perseverance51");
  digitalWriteFast(PC_2, !digitalReadFast(PC_2)) ;   // turn the LED off by making the voltage LOW
  digitalWrite(led2, !digitalRead(led2)) ;
  delay(1000);                       // wait for a second
  Serial.println("Arduino STM32G070RBT6");

🌼串口1指定到管脚使用示例2

//                      RX    TX
HardwareSerial Serial1(PA10, PA9);//将串口1的管脚指定到PA10,PA9引脚上

// the setup function runs once when you press reset or power the board
void setup() 
  Serial1.begin(115200);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);
   digitalWrite(led1, LOW);
   digitalWrite(led2, HIGH);
     pinMode(button1, INPUT_PULLUP);
   pinMode(button2, INPUT_PULLUP);



// the loop function runs over and over again forever
void loop() 
 digitalToggleFast(PC_2);   // turn the LED on (HIGH is the voltage level)
  digitalToggle(led2);
  delay(1000);                       // wait for a second
  Serial1.println("Perseverance51");
  digitalWriteFast(PC_2, !digitalReadFast(PC_2)) ;   // turn the LED off by making the voltage LOW
  digitalWrite(led2, !digitalRead(led2)) ;
  delay(1000);                       // wait for a second
  Serial1.println("Arduino STM32G070RBT6");

🌻将串口1复用到PC4和PC5引脚上

🌷示例4

#define led1  PC2
#define led2  PC3

#define button1  PC0
#define button2  PC1

//                     RX    TX
HardwareSerial Serial1(PC5, PC4);

// the setup function runs once when you press reset or power the board
void setup() 
//  HardwareSerial(PA10, PA9);
  Serial1.begin(115200);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);
   digitalWrite(led1, LOW);
   digitalWrite(led2, HIGH);
     pinMode(button1, INPUT_PULLUP);
   pinMode(button2, INPUT_PULLUP);



// the loop function runs over and over again forever
void loop() 
 digitalToggleFast(PC_2);   // turn the LED on (HIGH is the voltage level)
  digitalToggle(led2);
  delay(1000);                       // wait for a second
  Serial1.println("Perseverance51");
  digitalWriteFast(PC_2, !digitalReadFast(PC_2)) ;   // turn the LED off by making the voltage LOW
  digitalWrite(led2, !digitalRead(led2)) ;
  delay(1000);                       // wait for a second
  Serial1.println("Arduino STM32G070RBT6");

📝将串口1复用到PB7和PB6引脚上

示例5

#define led1  PC2
#define led2  PC3

#define button1  PC0
#define button2  PC1

//                     RX    TX
HardwareSerial Serial1(PB7, PB6);

// the setup function runs once when you press reset or power the board
void setup() 

  Serial1.begin(115200);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);
   digitalWrite(led1, LOW);
   digitalWrite(led2, HIGH);
     pinMode(button1, INPUT_PULLUP);
   pinMode(button2, INPUT_PULLUP);



// the loop function runs over and over again forever
void loop() 
 digitalToggleFast(PC_2);   // turn the LED on (HIGH is the voltage level)
  digitalToggle(led2);
  delay(1000);                       // wait for a second
  Serial1.println("Perseverance51");
  digitalWriteFast(PC_2, !digitalReadFast(PC_2)) ;   // turn the LED off by making the voltage LOW
  digitalWrite(led2, !digitalRead(led2)) ;
  delay(1000);                       // wait for a second
  Serial1.println("Arduino STM32G070RBT6");

🛠除了上面定义方式外还有另一种指定串口的方法

void setRx(uint32_t rx)
void setTx(uint32_t tx)
void setRx(PinName rx)
void setTx(PinName tx)

🚩注意点:重载函数名相同,形参注意区分。

  • 🌿variant_generic.h头文件声明了数字引脚
/*----------------------------------------------------------------------------
 *        STM32 pins number
 *----------------------------------------------------------------------------*/
#define PA0                     PIN_A0
#define PA1                     PIN_A1
#define PA2                     PIN_A2
#define PA3                     PIN_A3
#define PA4                     PIN_A4
#define PA5                     PIN_A5
#define PA6                     PIN_A6
#define PA7                     PIN_A7
#define PA8                     8
#define PA9                     9
#define PA10                    10
#define PA11                    11
#define PA12                    12
#define PA13                    13
#define PA14                    14
#define PA15                    15
#define PB0                     PIN_A8
#define PB1                     PIN_A9
#define PB2                     PIN_A10
#define PB3                     19
#define PB4                     20
#define PB5                     21
#define PB6                     22
#define PB7                     23
#define PB8                     24
#define PB9                     25
#define PB10                    PIN_A11
#define PB11                    PIN_A12
#define PB12                    PIN_A13
#define PB13                    29
#define PB14                    30
#define PB15                    31
#define PC0                     32
#define PC1                     33
#define PC2                     34
#define PC3                     35
#define PC4                     PIN_A14
#define PC5                     PIN_A15
#define PC6                     38
#define PC7                     39
#define PC8                     40
#define PC9                     41
#define PC10                    42
#define PC11                    43
#define PC12                    44
#define PC13                    45
#define PC14                    46
#define PC15                    47
#define PD0                     48
#define PD1                     49
#define PD2                     50
#define PD3                     51
#define PD4                     52
#define PD5                     53
#define PD6                     54
#define PD8                     55
#define PD9                     56
#define PF0                     57
#define PF1                     58
#define PA9_R                   59
#define PA10_R                  60

// Alternate pins number
#define PA6_ALT1                (PA6  | ALT1)
#define PA7_ALT1                (PA7  | ALT1)
#define PA7_ALT2                (PA7  | ALT2)
#define PA7_ALT3                (PA7  | ALT3)
#define PA15_ALT1               (PA15 | ALT1)
#define PB0_ALT1                (PB0  | ALT1)
#define PB1_ALT1                (PB1  | ALT1)
#define PB1_ALT2                (PB1  | ALT2)
#define PB6_ALT1                (PB6  | ALT1)
#define PB13_ALT1               (PB13 | ALT1)
#define PB14_ALT1               (PB14 | ALT1)
#define PB15_ALT1               (PB15 | ALT1)
#define PB15_ALT2               (PB15 | ALT2)
#define PC4_ALT1                (PC4  | ALT1)
#define PC5_ALT1                (PC5  | ALT1)
#define PC8_ALT1                (PC8  | ALT1)
#define PC9_ALT1                (PC9  | ALT1)
#define PC10_ALT1               (PC10 | ALT1)
#define PC11_ALT1               (PC11 | ALT1)

#define NUM_DIGITAL_PINS        61
#define NUM_REMAP_PINS          2
  • PinNames.h头文件定义了形参PinName枚举类型
typedef enum 
  // Not connected
  NC = 0xFFFFFFFF,

  // Pin name definition
  PA_0  = (PortA << 4) + 0x00,
  PA_1  = (PortA << 4) + 0x01,
  PA_2  = (PortA << 4) + 0x02,
  PA_3  = (PortA << 4) + 0x03,
  PA_4  = (PortA << 4) + 0x04,
  PA_5  = (PortA << 4) + 0x05,
  PA_6  = (PortA << 4) + 0x06,
  PA_7  = (PortA << 4) + 0x07,
  PA_8  = (PortA << 4) + 0x08,
  PA_9  = (PortA << 4) + 0x09,
  PA_10 = (PortA << 4) + 0x0A,
  PA_11 = (PortA << 4) + 0x0B,
  PA_12 = (PortA << 4) + 0x0C,
  PA_13 = (PortA << 4) + 0x0D,
  PA_14 = (PortA << 4) + 0x0E,
  PA_15 = (PortA << 4) + 0x0F,

  PB_0  = (PortB << 4) + 0x00,
  PB_1  = (PortB << 4) + 0x01,
  PB_2  = (PortB << 4) + 0x02,
  PB_3  = (PortB << 4) + 0x03,
  PB_4  = (PortB << 4) + 0x04,
  PB_5  = (PortB << 4) + 0x05,
  PB_6  = (PortB << 4) + 0x06,
  PB_7  = (PortB << 4) + 0x07,
  PB_8  = (PortB << 4) + 0x08,
  PB_9  = (PortB << 4) + 0x09,
  PB_10 = (PortB << 4) + 0x0A,
  PB_11 = (PortB << 4) + 0x0B,
  PB_12 = (PortB << 4) + 0x0C,
  PB_13 = (PortB << 4) + 0x0D,
  PB_14 = (PortB << 4) + 0x0E,
  PB_15 = (PortB << 4) + 0x0F,
#if defined GPIOC_BASE
  PC_0  = (PortC << 4) + 0x00,
  PC_1  = (PortC << 4) + 0x01,
  PC_2  = (PortC << 4) + 0x02,
  PC_3  = (PortC << 4) + 0x03,
  PC_4  = (PortC << 4) + 0x04,
  PC_5  = (PortC << 4) + 0x05,
  PC_6  = (PortC << 4) + 0x06,
  PC_7  = (PortC << 4) +以上是关于STM32G070RBT6基于Arduino串口的使用的主要内容,如果未能解决你的问题,请参考以下文章

STM32G070RBT6基于Arduino框架下点灯程序

STM32G070RBT6基于Arduino框架GPIO输入输出模式

STM32G070RBT6基于Arduino框架GPIO外部中断

STM32G070RBT6基于Arduino框架ADC输入电压检测

STM32G070RBT6基于Arduino框架下eeprom使用示例

STM32G070RBT6基于STM32CubeMX创建串口中断接收数据