PWM output with serial

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Foxnec
Date:
Tue May 12 09:52:07 2015 +0000
Parent:
0:9e52247c0dd2
Commit message:
Changes to comments

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Apr 20 22:22:37 2015 +0000
+++ b/main.cpp	Tue May 12 09:52:07 2015 +0000
@@ -3,7 +3,7 @@
 * @author  Petr Dousa
 * @version V1.00
 * @date    30-March-2015
-* @brief   Every 1 second blink LED and on PB_3 generate PWM
+* @brief   Blinks every single second LED and on PB_3 generate PWM
 *          Serial speed is set to 115200.
 ***********************************************************************************/
 
@@ -82,35 +82,33 @@
 ***********************************************************************************/
 void menu()
 {
-    while(!pc.writeable()); // wait to be serial available for sending data
-    pc.printf("HELP - MENU\n");// send text to serial
+    while(!pc.writeable());         // wait for serial to be free to send
+    pc.printf("HELP - MENU\n");     // send text to serial
     while(!pc.writeable());
-    pc.printf("Set data exactly.\n");
-    while(!pc.writeable());
-    pc.printf("Write to console: \"xx yy\", where xx is a code of seting and yy his value.\n");
+    pc.printf("Input format: \"xx yy\", where xx is the setting and yy is the value.\n");
     while(!pc.writeable());
     pc.printf("01 yyy - set up duty-cycle from 0 to 100, example:01 80\n");
     while(!pc.writeable());
-    pc.printf("02 yyyyy - set up period in ms from 0 to 10000 , example:02 10\n");
+    pc.printf("02 yyyyy - set up period in ms from 0 to 10000, example:02 10\n");
     while(!pc.writeable());
     pc.printf("end HELP\n");
 }
 
 /***********************************************************************************
 * Function Name  : main.
-* Description    : Main rountine.
+* Description    : Main routine.
 * Input          : None.
 * Output         : None.
 * Return         : None.
 ***********************************************************************************/
 int main()
 {
-    int prijData=0;
+    int rcvdData=0;
     int Data1=0;
     int Data2=0;
 
     pc.baud(115200);
-    pc.printf("\nLogic sond.\n");
+    pc.printf("\nPWM.\n");
     menu(); //print menu
     // Set PWM
     my_pwm.period_ms(10);
@@ -118,32 +116,32 @@
 
     while(1) {
         //accepted data from serial
-        prijData=pc.scanf("%d",&Data1);             // read number from serial
-        if(prijData==1 && (Data1>=1 && Data1<=2)) { // test if number was read and it is between 1 and 2
-            prijData=pc.scanf("%d",&Data2);         // read int number from serial
-            if(prijData==1 && Data2<=100 && Data2>=0 && Data1==1) {
-                prijData=2;         // set variable to 2 - parse data
-            } else if(prijData==1 && Data2>=0 && Data2<=10000 && Data1==2) {
-                prijData=2;         // set variable to 2 - parse data
+        rcvdData=pc.scanf("%d",&Data1);                     // read number from serial
+        if(rcvdData==1 && (Data1>=1 && Data1<=2)) {         // test if number was read and if it's between 1 and 2
+            rcvdData=pc.scanf("%d",&Data2);                 // read int number from serial
+            if(rcvdData==1 && Data2<=100 && Data2>=0 && Data1==1) {
+                rcvdData=2;         // set variable to 2 - parse data
+            } else if(rcvdData==1 && Data2>=0 && Data2<=10000 && Data1==2) {
+                rcvdData=2;         // set variable to 2 - parse data
             } else {
-                flushSerialPort();  // discard data from serial
-                prijData=0;         // set variable to print menu
+                flushSerialPort();  // discard all data from serial
+                rcvdData=0;         // set variable to print menu
             }
         } else {
-            flushSerialPort();      // discard data from serial
-            prijData=0;             // set variable to print menu
+            flushSerialPort();      // discard all data from serial
+            rcvdData=0;             // set variable to print menu
         }
 
-        if(prijData==2) {
+        if(rcvdData==2) {
             if(Data1==1) {
-                my_pwm.write((double)Data2/(double)100);    //set duty-cycle
+                my_pwm.write((double)Data2/(double)100);    // set duty-cycle
             } else if(Data1==2) {
                 my_pwm.period_ms(Data2);                    // set period in ms
             }
         } else {
-            menu();                 //print menu
+            menu();                 // print menu
             pc.printf("\n Bad data.\n");
-            flushSerialPort();      // discard data from serial
+            flushSerialPort();      // discard all data from serial
         }
     }
 }