NUCLEO-F042K6 Simple demo with ADC use

Dependencies:   mbed

Committer:
vodsejak
Date:
Sun Feb 11 18:09:41 2018 +0000
Revision:
1:f152bb2a4718
Parent:
0:d52d3729f434
v1.1; commentary

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vodsejak 0:d52d3729f434 1 #include "mbed.h"
vodsejak 0:d52d3729f434 2 /*******************************************************************************
vodsejak 0:d52d3729f434 3
vodsejak 0:d52d3729f434 4 EXAMPLE DESCRIPTION
vodsejak 0:d52d3729f434 5
vodsejak 0:d52d3729f434 6 Initialize ADC on pin PA_0 and PWM out on PA_8. Every 1 ms reads voltage on
vodsejak 0:d52d3729f434 7 PA_0 and converts it to DC of PWM on PA_8 (2 ms period, range 0-2000 us of
vodsejak 0:d52d3729f434 8 active pulse). The best use is to connect LED to pin PA_8 (using wiring
vodsejak 0:d52d3729f434 9 with transitor) to see effect of PWM.
vodsejak 0:d52d3729f434 10
vodsejak 0:d52d3729f434 11 *******************************************************************************/
vodsejak 0:d52d3729f434 12
vodsejak 0:d52d3729f434 13 AnalogIn ADCpin(PA_0); // definition of ADC pin
vodsejak 0:d52d3729f434 14 PwmOut PWM(PA_8); // definition of PWM pin
vodsejak 0:d52d3729f434 15
vodsejak 0:d52d3729f434 16 int main()
vodsejak 0:d52d3729f434 17 {
vodsejak 0:d52d3729f434 18 int DC = 0;
vodsejak 0:d52d3729f434 19
vodsejak 1:f152bb2a4718 20 // set PWM
vodsejak 0:d52d3729f434 21 PWM.period_ms(2); // 500 Hz
vodsejak 0:d52d3729f434 22 PWM.pulsewidth_us(DC); // duration of active pulse
vodsejak 0:d52d3729f434 23
vodsejak 0:d52d3729f434 24 while(1) {
vodsejak 0:d52d3729f434 25 DC= ADCpin.read_u16(); // read voltage on A0
vodsejak 0:d52d3729f434 26 DC=(DC*2000)/0xffff; // change of range
vodsejak 0:d52d3729f434 27 PWM.pulsewidth_us(DC); // duration of active pulse
vodsejak 0:d52d3729f434 28 wait_ms(1); // wait 1 ms
vodsejak 0:d52d3729f434 29 }
vodsejak 0:d52d3729f434 30 }