STM32开发例程
Posted kekeoutlook
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32开发例程相关的知识,希望对你有一定的参考价值。
1控制LED和继电器
除了 PA11和PA12其他都可以
void setup() pinMode(PC13, OUTPUT); pinMode(PC14, OUTPUT); pinMode(PC15, OUTPUT); pinMode(PA0, OUTPUT); pinMode(PA1, OUTPUT); pinMode(PA2, OUTPUT); pinMode(PA3, OUTPUT); pinMode(PA4, OUTPUT); pinMode(PA5, OUTPUT); pinMode(PA6, OUTPUT); pinMode(PA7, OUTPUT); pinMode(PA8, OUTPUT); pinMode(PA9, OUTPUT); pinMode(PA10, OUTPUT); // pinMode(PA11, OUTPUT); // CAN t0 不能用 // pinMode(PA12, OUTPUT); // CAN r0 不能用 pinMode(PA15, OUTPUT); pinMode(PB0, OUTPUT); pinMode(PB1, OUTPUT); pinMode(PB2, OUTPUT); pinMode(PB3, OUTPUT); pinMode(PB4, OUTPUT); pinMode(PB5, OUTPUT); pinMode(PB6, OUTPUT); pinMode(PB7, OUTPUT); pinMode(PB8, OUTPUT); pinMode(PB9, OUTPUT); pinMode(PB10, OUTPUT); pinMode(PB11, OUTPUT); pinMode(PB12, OUTPUT); pinMode(PB13, OUTPUT); pinMode(PB14, OUTPUT); pinMode(PB15, OUTPUT); void loop() digitalWrite(PC13, HIGH); digitalWrite(PC14, HIGH); digitalWrite(PC15, HIGH); digitalWrite(PA0, HIGH); digitalWrite(PA1, HIGH); digitalWrite(PA2, HIGH); digitalWrite(PA3, HIGH); digitalWrite(PA4, HIGH); digitalWrite(PA5, HIGH); digitalWrite(PA6, HIGH); digitalWrite(PA7, HIGH); digitalWrite(PA8, HIGH); digitalWrite(PA9, HIGH); digitalWrite(PA10, HIGH); // digitalWrite(PA11, HIGH); // digitalWrite(PA12, HIGH); digitalWrite(PA15, HIGH); digitalWrite(PB0, HIGH); digitalWrite(PB1, HIGH); digitalWrite(PB2, HIGH); digitalWrite(PB3, HIGH); digitalWrite(PB4, HIGH); digitalWrite(PB5, HIGH); digitalWrite(PB6, HIGH); digitalWrite(PB7, HIGH); digitalWrite(PB8, HIGH); digitalWrite(PB9, HIGH); digitalWrite(PB10, HIGH); digitalWrite(PB11, HIGH); digitalWrite(PB12, HIGH); digitalWrite(PB13, HIGH); digitalWrite(PB14, HIGH); digitalWrite(PB15, HIGH); delay(1000); digitalWrite(PC13, LOW); digitalWrite(PC14, LOW); digitalWrite(PC15, LOW); digitalWrite(PA0, LOW); digitalWrite(PA1, LOW); digitalWrite(PA2, LOW); digitalWrite(PA3, LOW); digitalWrite(PA4, LOW); digitalWrite(PA5, LOW); digitalWrite(PA6, LOW); digitalWrite(PA7, LOW); digitalWrite(PA8, LOW); digitalWrite(PA9, LOW); digitalWrite(PA10, LOW); // digitalWrite(PA11, LOW); // digitalWrite(PA12, LOW); digitalWrite(PA15, LOW); digitalWrite(PB0, LOW); digitalWrite(PB1, LOW); digitalWrite(PB2, LOW); digitalWrite(PB3, LOW); digitalWrite(PB4, LOW); digitalWrite(PB5, LOW); digitalWrite(PB6, LOW); digitalWrite(PB7, LOW); digitalWrite(PB8, LOW); digitalWrite(PB9, LOW); digitalWrite(PB10, LOW); digitalWrite(PB11, LOW); digitalWrite(PB12, LOW); digitalWrite(PB13, LOW); digitalWrite(PB14, LOW); digitalWrite(PB15, LOW); delay(1000);
2 PWM控制
PWM引脚简易测试程序,总共有12个PWM引脚,16bit即65536级PWM,很精密了:
延迟给100微妙 多了少了都有问题,不正常转
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 = 1000; i < 65536; i++) //delayMicroseconds(100); // delay(1); delay(20); 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); for (int i = 65536; i > 1000; i--) // delayMicroseconds(100); // delay(1); delay(20); 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);
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);
IO引脚读取
int value = digitalRead(pin);
ADC引脚
实际测试 0-4096 2的10次方
在pa7接一个光敏电阻,完全遮盖4095 手机强光基本几十
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 onChange()
if ( digitalRead(PA8) == LOW )
Serial.println("Key Down");
else
Serial.println("Key UP");
void setup()
Serial.begin(9600); //打开串口
pinMode( PA8, INPUT);//设置管脚为输入
//Enable中断管脚, 中断服务程序为onChange(), 监视引脚变化
/*
LOW 低电平触发
CHANGE 电平变化,高电平变低电平、低电平变高电平
RISING 上升沿触发
FALLING 下降沿触发
HIGH 高电平触发(该中断模式仅适用于Arduino due)
*/
attachInterrupt( PA8, onChange, CHANGE);
void loop()
// 模拟长时间运行的进程或复杂的任务。
for (int i = 0; i < 100; i++)
// 什么都不做,等待10毫秒
delay(10);
允许的串口
总共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");
以上是关于STM32开发例程的主要内容,如果未能解决你的问题,请参考以下文章
STM32F103基于标准库开发串口中断接收数据环形队列例程
STM32中CAN外设的操作是不是等待ISR例程代码的执行?