stm32nucleof401re_03_timer

Dependencies:   mbed

Committer:
perlatecnica
Date:
Fri Feb 07 07:21:29 2020 +0000
Revision:
0:b9a5fdb02a11
v1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
perlatecnica 0:b9a5fdb02a11 1 /* Copyright (c) 2019 Perlatecnica
perlatecnica 0:b9a5fdb02a11 2 *
perlatecnica 0:b9a5fdb02a11 3 * Licensed under the Apache License, Version 2.0 (the "License");
perlatecnica 0:b9a5fdb02a11 4 * you may not use this file except in compliance with the License.
perlatecnica 0:b9a5fdb02a11 5 * You may obtain a copy of the License at
perlatecnica 0:b9a5fdb02a11 6 *
perlatecnica 0:b9a5fdb02a11 7 * http://www.apache.org/licenses/LICENSE-2.0
perlatecnica 0:b9a5fdb02a11 8 *
perlatecnica 0:b9a5fdb02a11 9 * Unless required by applicable law or agreed to in writing, software
perlatecnica 0:b9a5fdb02a11 10 * distributed under the License is distributed on an "AS IS" BASIS,
perlatecnica 0:b9a5fdb02a11 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
perlatecnica 0:b9a5fdb02a11 12 * See the License for the specific language governing permissions and
perlatecnica 0:b9a5fdb02a11 13 * limitations under the License.
perlatecnica 0:b9a5fdb02a11 14 */
perlatecnica 0:b9a5fdb02a11 15
perlatecnica 0:b9a5fdb02a11 16 /****************************************************
perlatecnica 0:b9a5fdb02a11 17 * RAPID PROTOTYPING WITH NUCLEO *
perlatecnica 0:b9a5fdb02a11 18 * Example Code 03: Timer *
perlatecnica 0:b9a5fdb02a11 19 * Author: Mauro D'Angelo *
perlatecnica 0:b9a5fdb02a11 20 * Organization: Perlatecnica *
perlatecnica 0:b9a5fdb02a11 21 *****************************************************/
perlatecnica 0:b9a5fdb02a11 22 /* Serial Client configuration *
perlatecnica 0:b9a5fdb02a11 23 * 9600 bauds, 8-bit data, no parity *
perlatecnica 0:b9a5fdb02a11 24 *****************************************************/
perlatecnica 0:b9a5fdb02a11 25
perlatecnica 0:b9a5fdb02a11 26 #include "mbed.h"
perlatecnica 0:b9a5fdb02a11 27
perlatecnica 0:b9a5fdb02a11 28 // It creates an instance of the Timer class. From now on, we can refer to the timer through its instance
perlatecnica 0:b9a5fdb02a11 29 Timer timer;
perlatecnica 0:b9a5fdb02a11 30
perlatecnica 0:b9a5fdb02a11 31 Serial pc(USBTX, USBRX);
perlatecnica 0:b9a5fdb02a11 32 DigitalOut myled(LED1);
perlatecnica 0:b9a5fdb02a11 33
perlatecnica 0:b9a5fdb02a11 34 // Entry point
perlatecnica 0:b9a5fdb02a11 35 int main() {
perlatecnica 0:b9a5fdb02a11 36 // The timer is started calling its method 'start'. We refer to the Timer by the name of the instance
perlatecnica 0:b9a5fdb02a11 37 timer.start();
perlatecnica 0:b9a5fdb02a11 38
perlatecnica 0:b9a5fdb02a11 39 // It print to the serial client a string
perlatecnica 0:b9a5fdb02a11 40 pc.printf("It works!\r\n");
perlatecnica 0:b9a5fdb02a11 41
perlatecnica 0:b9a5fdb02a11 42 // Infinite loop. The instructions in the loop will be repeated forever
perlatecnica 0:b9a5fdb02a11 43 while(1) {
perlatecnica 0:b9a5fdb02a11 44 wait(1);
perlatecnica 0:b9a5fdb02a11 45
perlatecnica 0:b9a5fdb02a11 46 // The timer is read calling its method read_ms. It returns the elapsed time in ms
perlatecnica 0:b9a5fdb02a11 47 pc.printf("This program runs since %d seconds.\r\n", timer.read_ms()/1000);
perlatecnica 0:b9a5fdb02a11 48
perlatecnica 0:b9a5fdb02a11 49 // It change the led status. We will see the led blinking
perlatecnica 0:b9a5fdb02a11 50 myled = !myled;
perlatecnica 0:b9a5fdb02a11 51 }
perlatecnica 0:b9a5fdb02a11 52 }
perlatecnica 0:b9a5fdb02a11 53
perlatecnica 0:b9a5fdb02a11 54 // EXERCISE: Reset the board and looks what happens