STM32F103 - Правим примеры

ua6em
ua6em аватар
Offline
Зарегистрирован: 17.08.2016

Столкнулся с тем, что примеры из аддона STM32 на процессоре STM32F103 не работают.
Добавлю исправленные примеры:

BLINK

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
  
  Modified by Roger Clark. www.rogerclark.net for Maple mini 25th April 2015 , where the LED is on PB1
  
 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin PB1 (PC13) as an output.
  pinMode(PC13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(PC13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(PC13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

 

AnalogInOutSerial

/*
  Analog input, analog output, serial output

  Reads an analog input pin, maps the result to a range from 0 to
  65535 and uses the result to set the pulse width modulation (PWM) of
  an output pin.  Also prints the results to the serial monitor.

  (You may need to change the pin numbers analogInPin and analogOutPin
  if you're not using a Maple).

  The circuit:
  * Potentiometer connected to analog pin 15.
    Center pin of the potentiometer goes to the analog pin.
    Side pins of the potentiometer go to +3.3V and ground.
  * LED connected from digital pin 9 to ground

  created 29 Dec. 2008
  by Tom Igoe

  ported to Maple
  by LeafLabs
*/

// These constants won't change.  They're used to give names
// to the pins used:

const int analogInPin = PA0; // Analog input pin that the potentiometer
                            // is attached to

const int pwmOutPin = 9;    // PWM pin that the LED is attached to

// These variables will change:
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM

void setup() {
    // Configure the ADC pin
    pinMode(analogInPin, INPUT_ANALOG);
    // Configure LED pin
    pinMode(pwmOutPin, PWM);
	Serial.begin(115200); // Ignored by Maple. But needed by boards using Hardware serial via a USB to Serial Adaptor
}

void loop() {
    // read the analog in value:
    sensorValue = analogRead(analogInPin);
    // map it to the range of the analog out:
    outputValue = map(sensorValue, 0, 4096, 0, 65535);
    // change the analog out value:
    pwmWrite(pwmOutPin, outputValue);

    // print the results to the serial monitor:
    Serial.print("sensor = " );
    Serial.print(sensorValue);
    Serial.print("\t output = ");
    Serial.println(outputValue);
}

 

/*
  Analog input, serial output

  Reads an analog input pin, prints the results to the serial monitor.

  The circuit:

  * Potentiometer connected to analog pin 15 (на пин A0).
  * Center pin of the potentiometer goes to the analog pin.
  * Side pins of the potentiometer go to +3.3V (VCC) and ground

  created over and over again
  by Tom Igoe and everyone who's ever used Arduino

  Ported to Maple 27 May, 2010 by Bryan Newbold
*/

// Analog input pin.  You may need to change this number if your board
// can't do analog input on pin PA0.
const int analogInputPin = PA0;

void setup() {
  // Declare analogInputPin as INPUT_ANALOG:
  pinMode(analogInputPin, INPUT_ANALOG);
  	Serial.begin(115200); // Ignored by Maple. But needed by boards using Hardware serial via a USB to Serial Adaptor
}

void loop() {
  // Read the analog input into a variable:
  int analogValue = analogRead(analogInputPin);

  // print the result:
  Serial.println(analogValue);
}

 

ua6em
ua6em аватар
Offline
Зарегистрирован: 17.08.2016

Пример работы с сервой, подправил для проверки работы двух функций, перемещение по углу и отработка по длительности импульса ШИМ (из-за нелинейности вычисления получилась простая игрушка - найдите точку бифуркации)

/*
 Controlling a servo position using a potentiometer (variable resistor)
 by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

 modified on 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Knob
 Подправлен скетч для простой игры сервой -
 НАЙДИТЕ ТОЧКУ БИФУРКАЦИИ
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(PB9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 4096, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
  Serial.print("Servo in = " );
  Serial.println(val);
  delay(2000);
  val = analogRead(potpin);
  val = map(val, 0, 4096, 1000, 2000);
   myservo.writeMicroseconds(val); 
   delay(1000);
}

 

b707
Offline
Зарегистрирован: 26.05.2017

ua6em - если хотите, что топик был полезным - в каждом случае пишите что именно поправили и почему. Прямо-таки теряюсь в догадках, что можно было поправитьв Блинке - номер пина? :)))

И про другие примеры тоже интересно

ua6em
ua6em аватар
Offline
Зарегистрирован: 17.08.2016

b707 пишет:

ua6em - если хотите, что топик был полезным - в каждом случае пишите что именно поправили и почему. Прямо-таки теряюсь в догадках, что можно было поправитьв Блинке - номер пина? :)))

И про другие примеры тоже интересно

Действительно!
1. Блинк - изменен номер пина с 33 на номер пина к которому подключен светодиод на плате - PC13
2. AnalogInOutSerial - исправлена строка 48, приведена в соответствии с разрешающей способностью АЦП
3. AnalogInputSerialOutput - исправлены строки 19,20 порты обозначены в соответствии с маркировкой платы
4. Пример работы с сервой - исправлены строки 20,25, добавлены строки 30-34 для проверки отработки фунции   позиционирования по передаваемому значению длительности импульса в микросекундах.