用Arduino玩STM32C8T6(一)

引脚对应

用Arduino玩STM32C8T6(一)_第1张图片
PWM测试程序1

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测试程序2

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

PWM测试程序3

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

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

串口测试程序

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

你可能感兴趣的:(单片机,Arduino)