PWM output with serial

Dependencies:   mbed

Committer:
Foxnec
Date:
Tue May 12 09:52:07 2015 +0000
Revision:
1:dff553988426
Parent:
0:9e52247c0dd2
Changes to comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dousape2 0:9e52247c0dd2 1 /**********************************************************************************
dousape2 0:9e52247c0dd2 2 * @file main.cpp
dousape2 0:9e52247c0dd2 3 * @author Petr Dousa
dousape2 0:9e52247c0dd2 4 * @version V1.00
dousape2 0:9e52247c0dd2 5 * @date 30-March-2015
Foxnec 1:dff553988426 6 * @brief Blinks every single second LED and on PB_3 generate PWM
dousape2 0:9e52247c0dd2 7 * Serial speed is set to 115200.
dousape2 0:9e52247c0dd2 8 ***********************************************************************************/
dousape2 0:9e52247c0dd2 9
dousape2 0:9e52247c0dd2 10 /**********************************************************************************/
dousape2 0:9e52247c0dd2 11 /* Table of PWM pins on Nucleo F303 (LQFP64) */
dousape2 0:9e52247c0dd2 12 /**********************************************************************************/
dousape2 0:9e52247c0dd2 13 /* LQFP64 pin | Nucleo pin | ST Pin | PWM number | Channel */
dousape2 0:9e52247c0dd2 14 /* 15 | A1 | PA_1 | PWM15 | 1 */
dousape2 0:9e52247c0dd2 15 /* 26 | A3 | PB_0 | PWM1 | 2 */
dousape2 0:9e52247c0dd2 16 /* 9 | A4 | PC_1 | PWM1 | 2 */
dousape2 0:9e52247c0dd2 17 /* 8 | A5 | PC_0 | PWM1 | 1 */
dousape2 0:9e52247c0dd2 18 /* 17 | D0 | PA_3 | PWM15 | 2 */
dousape2 0:9e52247c0dd2 19 /* 16 | D1 | PA_2 | PWM15 | 1 */
dousape2 0:9e52247c0dd2 20 /* 43 | D2 | PA_10 | PWM1 | 3 */
dousape2 0:9e52247c0dd2 21 /* 55 | D3 | PB_3 | PWM8 | 1 */
dousape2 0:9e52247c0dd2 22 /* 57 | D4 | PB_5 | PWM17 | 1 */
dousape2 0:9e52247c0dd2 23 /* 56 | D5 | PB_4 | PWM16 | 1 */
dousape2 0:9e52247c0dd2 24 /* 41 | D7 | PA_8 | PWM1 | 1 */
dousape2 0:9e52247c0dd2 25 /* 42 | D8 | PA_9 | PWM1 | 2 */
dousape2 0:9e52247c0dd2 26 /* 38 | D9 | PC_7 | PWM3 | 2 */
dousape2 0:9e52247c0dd2 27 /* 58 | D10 | PB_6 | PWM16 | 1 */
dousape2 0:9e52247c0dd2 28 /* 23 | D11 | PA_7 | PWM15 | 2 */
dousape2 0:9e52247c0dd2 29 /* 22 | D12 | PA_6 | PWM15 | 1 */
dousape2 0:9e52247c0dd2 30 /* 62 | D14 | PB_9 | PWM17 | 1 */
dousape2 0:9e52247c0dd2 31 /* 61 | D15 | PB_8 | PWM16 | 1 */
dousape2 0:9e52247c0dd2 32 /* 46 | | PA_13 | PWM16 | 1 */
dousape2 0:9e52247c0dd2 33 /* 49 | | PA_14 | PWM8 | 2 */
dousape2 0:9e52247c0dd2 34 /* 59 | | PB_7 | PWM17 | 1 */
dousape2 0:9e52247c0dd2 35 /* 2 | | PC_13 | PWM1 | 1 */
dousape2 0:9e52247c0dd2 36 /* 10 | | PC_2 | PWM1 | 3 */
dousape2 0:9e52247c0dd2 37 /* 11 | | PC_3 | PWM1 | 4 */
dousape2 0:9e52247c0dd2 38 /* 40 | | PC_9 | PWM3 | 4 */
dousape2 0:9e52247c0dd2 39 /* 39 | | PC_8 | PWM3 | 3 */
dousape2 0:9e52247c0dd2 40 /* 37 | | PC_6 | PWM3 | 1 */
dousape2 0:9e52247c0dd2 41 /* 45 | | PA_12 | PWM16 | 1 */
dousape2 0:9e52247c0dd2 42 /* 44 | | PA_11 | PWM1 | 4 */
dousape2 0:9e52247c0dd2 43 /* 27 | | PB_1 | PWM1 | 3 */
dousape2 0:9e52247c0dd2 44 /* 36 | | PB_15 | PWM17 | 1 */
dousape2 0:9e52247c0dd2 45 /* 35 | | PB_14 | PWM16 | 1 */
dousape2 0:9e52247c0dd2 46 /**********************************************************************************/
dousape2 0:9e52247c0dd2 47
dousape2 0:9e52247c0dd2 48 /* Includes ----------------------------------------------------------------------*/
dousape2 0:9e52247c0dd2 49 #include "mbed.h"
dousape2 0:9e52247c0dd2 50
dousape2 0:9e52247c0dd2 51 /* Defines -----------------------------------------------------------------------*/
dousape2 0:9e52247c0dd2 52 /* Function prototypes -----------------------------------------------------------*/
dousape2 0:9e52247c0dd2 53 /* Variables ---------------------------------------------------------------------*/
dousape2 0:9e52247c0dd2 54 //mbed - initialization of peripherals
dousape2 0:9e52247c0dd2 55 Serial pc(SERIAL_TX, SERIAL_RX);
dousape2 0:9e52247c0dd2 56 DigitalOut my_led(LED1); // blink LED
dousape2 0:9e52247c0dd2 57 PwmOut my_pwm(PB_3); // PWM output
dousape2 0:9e52247c0dd2 58 /* Functions----------------------------------------------------------------------*/
dousape2 0:9e52247c0dd2 59
dousape2 0:9e52247c0dd2 60
dousape2 0:9e52247c0dd2 61
dousape2 0:9e52247c0dd2 62 /***********************************************************************************
dousape2 0:9e52247c0dd2 63 * Function Name : flushSerialPort.
dousape2 0:9e52247c0dd2 64 * Description : Serial flush rountine.
dousape2 0:9e52247c0dd2 65 * Input : None.
dousape2 0:9e52247c0dd2 66 * Output : None.
dousape2 0:9e52247c0dd2 67 * Return : None.
dousape2 0:9e52247c0dd2 68 ***********************************************************************************/
dousape2 0:9e52247c0dd2 69 void flushSerialPort()
dousape2 0:9e52247c0dd2 70 {
dousape2 0:9e52247c0dd2 71 while(pc.readable())
dousape2 0:9e52247c0dd2 72 pc.getc();
dousape2 0:9e52247c0dd2 73 return;
dousape2 0:9e52247c0dd2 74 }
dousape2 0:9e52247c0dd2 75
dousape2 0:9e52247c0dd2 76 /***********************************************************************************
dousape2 0:9e52247c0dd2 77 * Function Name : menu.
dousape2 0:9e52247c0dd2 78 * Description : Print menu to serial.
dousape2 0:9e52247c0dd2 79 * Input : None.
dousape2 0:9e52247c0dd2 80 * Output : None.
dousape2 0:9e52247c0dd2 81 * Return : None.
dousape2 0:9e52247c0dd2 82 ***********************************************************************************/
dousape2 0:9e52247c0dd2 83 void menu()
dousape2 0:9e52247c0dd2 84 {
Foxnec 1:dff553988426 85 while(!pc.writeable()); // wait for serial to be free to send
Foxnec 1:dff553988426 86 pc.printf("HELP - MENU\n"); // send text to serial
dousape2 0:9e52247c0dd2 87 while(!pc.writeable());
Foxnec 1:dff553988426 88 pc.printf("Input format: \"xx yy\", where xx is the setting and yy is the value.\n");
dousape2 0:9e52247c0dd2 89 while(!pc.writeable());
dousape2 0:9e52247c0dd2 90 pc.printf("01 yyy - set up duty-cycle from 0 to 100, example:01 80\n");
dousape2 0:9e52247c0dd2 91 while(!pc.writeable());
Foxnec 1:dff553988426 92 pc.printf("02 yyyyy - set up period in ms from 0 to 10000, example:02 10\n");
dousape2 0:9e52247c0dd2 93 while(!pc.writeable());
dousape2 0:9e52247c0dd2 94 pc.printf("end HELP\n");
dousape2 0:9e52247c0dd2 95 }
dousape2 0:9e52247c0dd2 96
dousape2 0:9e52247c0dd2 97 /***********************************************************************************
dousape2 0:9e52247c0dd2 98 * Function Name : main.
Foxnec 1:dff553988426 99 * Description : Main routine.
dousape2 0:9e52247c0dd2 100 * Input : None.
dousape2 0:9e52247c0dd2 101 * Output : None.
dousape2 0:9e52247c0dd2 102 * Return : None.
dousape2 0:9e52247c0dd2 103 ***********************************************************************************/
dousape2 0:9e52247c0dd2 104 int main()
dousape2 0:9e52247c0dd2 105 {
Foxnec 1:dff553988426 106 int rcvdData=0;
dousape2 0:9e52247c0dd2 107 int Data1=0;
dousape2 0:9e52247c0dd2 108 int Data2=0;
dousape2 0:9e52247c0dd2 109
dousape2 0:9e52247c0dd2 110 pc.baud(115200);
Foxnec 1:dff553988426 111 pc.printf("\nPWM.\n");
dousape2 0:9e52247c0dd2 112 menu(); //print menu
dousape2 0:9e52247c0dd2 113 // Set PWM
dousape2 0:9e52247c0dd2 114 my_pwm.period_ms(10);
dousape2 0:9e52247c0dd2 115 my_pwm.write(0.5);
dousape2 0:9e52247c0dd2 116
dousape2 0:9e52247c0dd2 117 while(1) {
dousape2 0:9e52247c0dd2 118 //accepted data from serial
Foxnec 1:dff553988426 119 rcvdData=pc.scanf("%d",&Data1); // read number from serial
Foxnec 1:dff553988426 120 if(rcvdData==1 && (Data1>=1 && Data1<=2)) { // test if number was read and if it's between 1 and 2
Foxnec 1:dff553988426 121 rcvdData=pc.scanf("%d",&Data2); // read int number from serial
Foxnec 1:dff553988426 122 if(rcvdData==1 && Data2<=100 && Data2>=0 && Data1==1) {
Foxnec 1:dff553988426 123 rcvdData=2; // set variable to 2 - parse data
Foxnec 1:dff553988426 124 } else if(rcvdData==1 && Data2>=0 && Data2<=10000 && Data1==2) {
Foxnec 1:dff553988426 125 rcvdData=2; // set variable to 2 - parse data
dousape2 0:9e52247c0dd2 126 } else {
Foxnec 1:dff553988426 127 flushSerialPort(); // discard all data from serial
Foxnec 1:dff553988426 128 rcvdData=0; // set variable to print menu
dousape2 0:9e52247c0dd2 129 }
dousape2 0:9e52247c0dd2 130 } else {
Foxnec 1:dff553988426 131 flushSerialPort(); // discard all data from serial
Foxnec 1:dff553988426 132 rcvdData=0; // set variable to print menu
dousape2 0:9e52247c0dd2 133 }
dousape2 0:9e52247c0dd2 134
Foxnec 1:dff553988426 135 if(rcvdData==2) {
dousape2 0:9e52247c0dd2 136 if(Data1==1) {
Foxnec 1:dff553988426 137 my_pwm.write((double)Data2/(double)100); // set duty-cycle
dousape2 0:9e52247c0dd2 138 } else if(Data1==2) {
dousape2 0:9e52247c0dd2 139 my_pwm.period_ms(Data2); // set period in ms
dousape2 0:9e52247c0dd2 140 }
dousape2 0:9e52247c0dd2 141 } else {
Foxnec 1:dff553988426 142 menu(); // print menu
dousape2 0:9e52247c0dd2 143 pc.printf("\n Bad data.\n");
Foxnec 1:dff553988426 144 flushSerialPort(); // discard all data from serial
dousape2 0:9e52247c0dd2 145 }
dousape2 0:9e52247c0dd2 146 }
dousape2 0:9e52247c0dd2 147 }