Exercises the DAC on the STM32F303K8 NUCLEO32 BOARD

Dependencies:   mbed

Fork of Nucleo_FrequencyCounter_Timed by Sam Walsh

Committer:
EmbeddedSam
Date:
Thu Feb 25 14:22:29 2016 +0000
Revision:
2:29a5b859318a
Parent:
1:85b1605ed4e0
This just shows how to exercise the DAC on the Nucleo F303K8 it changes a PWM duty cycle and makes the DAC correspond to the value.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EmbeddedSam 1:85b1605ed4e0 1 /*
EmbeddedSam 1:85b1605ed4e0 2 Title: Mbed Motor Controller
EmbeddedSam 1:85b1605ed4e0 3 Author: Samuel.Walsh@manchester.ac.uk
EmbeddedSam 1:85b1605ed4e0 4 Date: 27/01/16
EmbeddedSam 1:85b1605ed4e0 5 Description: Using School of EEE Motor Driver Board and STM32F303K8 Nucleo32
EmbeddedSam 1:85b1605ed4e0 6 Unipolar PWM Controlling a DC Brushed Motor with PWM on D3, PWM is inverse so
EmbeddedSam 1:85b1605ed4e0 7 1.0 = motor off and 0.0 = motor full
EmbeddedSam 1:85b1605ed4e0 8 */
EmbeddedSam 0:fb3e19364d48 9
EmbeddedSam 1:85b1605ed4e0 10 #include "mbed.h"
EmbeddedSam 2:29a5b859318a 11
EmbeddedSam 0:fb3e19364d48 12
EmbeddedSam 1:85b1605ed4e0 13 #define CURRENT_CHECK_PERIOD_US 50 //Checks ths current of the motor every 100us
EmbeddedSam 1:85b1605ed4e0 14 #define TICKS_PER_REVOLUTION 12.0 //The encoder has 12 slots
EmbeddedSam 2:29a5b859318a 15 #define TICK_CHECK_PERIOD_US 10000 //Checks ths speed every 10ms
EmbeddedSam 1:85b1605ed4e0 16 #define TICKS_PER_SECOND_MAX 1800.0 //This was found through experimentation with motor, max speed = 8900RPM = ~1800 ticks per second
EmbeddedSam 1:85b1605ed4e0 17 #define TICKS_PER_SECOND_MIN 1*TICKS_PER_REVOLUTION //This is equal to 1RPS (60RPM) at however many ticks per revolution
EmbeddedSam 1:85b1605ed4e0 18 #define SETPOINT_RPM 4000 //Arbitrarily picked
EmbeddedSam 1:85b1605ed4e0 19
EmbeddedSam 1:85b1605ed4e0 20 /* Peripherals */
EmbeddedSam 1:85b1605ed4e0 21 PwmOut motorPWM(D6);
EmbeddedSam 2:29a5b859318a 22 AnalogOut PWM_Duty_Cycle_DAC_Out(A5); //Used for plotting motor resonse on a scope
EmbeddedSam 2:29a5b859318a 23 Ticker periodicTimer1;
EmbeddedSam 1:85b1605ed4e0 24
EmbeddedSam 1:85b1605ed4e0 25 //Peripherals used for debugging
EmbeddedSam 1:85b1605ed4e0 26 Serial pc(USBTX, USBRX); // tx, rx just used for debugging
EmbeddedSam 0:fb3e19364d48 27
EmbeddedSam 2:29a5b859318a 28 float normalise(float real_min, float real_max, float scaled_min, float scaled_max, int value){
EmbeddedSam 2:29a5b859318a 29 return (((value - real_min) * (scaled_max - scaled_min)) / (real_max - real_min)) + scaled_min;
EmbeddedSam 0:fb3e19364d48 30 }
EmbeddedSam 0:fb3e19364d48 31
EmbeddedSam 1:85b1605ed4e0 32 void Print_Variables_To_PC(){
EmbeddedSam 2:29a5b859318a 33 //pc.printf("put whatever you want here to be sent to PC once a second %d, %d, %f",var1,var2,var3);
EmbeddedSam 0:fb3e19364d48 34 }
EmbeddedSam 0:fb3e19364d48 35
EmbeddedSam 0:fb3e19364d48 36 int main() {
EmbeddedSam 1:85b1605ed4e0 37 //Setup PWM
EmbeddedSam 1:85b1605ed4e0 38 motorPWM.period_us(100); //Set up for 10KHz PWM Switching Frequency
EmbeddedSam 1:85b1605ed4e0 39
EmbeddedSam 2:29a5b859318a 40 //Setup Debugging
EmbeddedSam 2:29a5b859318a 41 periodicTimer1.attach(&Print_Variables_To_PC, 1.0); //Display stuff on serial port once a second
EmbeddedSam 2:29a5b859318a 42
EmbeddedSam 2:29a5b859318a 43 motorPWM = 0;
EmbeddedSam 2:29a5b859318a 44 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 45
EmbeddedSam 2:29a5b859318a 46 float wait_time = 0.5;
EmbeddedSam 1:85b1605ed4e0 47
EmbeddedSam 2:29a5b859318a 48 while(1) {
EmbeddedSam 2:29a5b859318a 49 wait(wait_time);
EmbeddedSam 2:29a5b859318a 50 motorPWM = 0.1;
EmbeddedSam 2:29a5b859318a 51 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 52
EmbeddedSam 2:29a5b859318a 53 wait(wait_time);
EmbeddedSam 2:29a5b859318a 54 motorPWM = 0.2;
EmbeddedSam 2:29a5b859318a 55 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 56
EmbeddedSam 2:29a5b859318a 57 wait(wait_time);
EmbeddedSam 2:29a5b859318a 58 motorPWM = 0.3;
EmbeddedSam 2:29a5b859318a 59 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 60
EmbeddedSam 2:29a5b859318a 61 wait(wait_time);
EmbeddedSam 2:29a5b859318a 62 motorPWM = 0.4;
EmbeddedSam 2:29a5b859318a 63 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 64
EmbeddedSam 2:29a5b859318a 65 wait(wait_time);
EmbeddedSam 2:29a5b859318a 66 motorPWM = 0.5;
EmbeddedSam 2:29a5b859318a 67 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 68
EmbeddedSam 2:29a5b859318a 69 wait(wait_time);
EmbeddedSam 2:29a5b859318a 70 motorPWM = 0.6;
EmbeddedSam 2:29a5b859318a 71 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 72
EmbeddedSam 2:29a5b859318a 73 wait(wait_time);
EmbeddedSam 2:29a5b859318a 74 motorPWM = 0.7;
EmbeddedSam 2:29a5b859318a 75 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 76
EmbeddedSam 2:29a5b859318a 77 wait(wait_time);
EmbeddedSam 2:29a5b859318a 78 motorPWM = 0.8;
EmbeddedSam 2:29a5b859318a 79 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 80
EmbeddedSam 2:29a5b859318a 81 wait(wait_time);
EmbeddedSam 2:29a5b859318a 82 motorPWM = 0.9;
EmbeddedSam 2:29a5b859318a 83 PWM_Duty_Cycle_DAC_Out = motorPWM;
EmbeddedSam 2:29a5b859318a 84 }
EmbeddedSam 1:85b1605ed4e0 85
EmbeddedSam 0:fb3e19364d48 86 }