Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 1:533ef4c5f7ab
- Parent:
- 0:786207d961f4
- Child:
- 2:10add9ee8c4b
diff -r 786207d961f4 -r 533ef4c5f7ab main.cpp
--- a/main.cpp Fri Mar 13 03:33:26 2015 +0000
+++ b/main.cpp Sat Mar 21 16:07:17 2015 +0000
@@ -1,58 +1,83 @@
+/**********************************************************************************
+* @file main.cpp
+* @author Name
+* @version V0.1
+* @date 09-March-2015
+* @brief Read value from pins and write it to serial.
+* It could be set some settings through serial.
+* Serial speed is set to 115200.
+***********************************************************************************/
+
+/* Includes ----------------------------------------------------------------------*/
#include "mbed.h"
-PortIn myIOs(PortC, 0xFFFF); // PC_all
-
-Serial pc(SERIAL_TX, SERIAL_RX);
-
-Ticker toggle_ticker;
+/* Variables ---------------------------------------------------------------------*/
+int pin =0; //pins to compare and start print
+int vypis =0; // which output will be provided
+bool start = true; // if true it print value to serial
-DigitalOut led(LED1);
+//mbed - initialization of peripherals
+PortIn myIOs(PortC, 0xFFFF); // PC_all
+Serial pc(SERIAL_TX, SERIAL_RX); // inicialize Serial to connect to PC
+Ticker toggle_ticker; // inicialize ticker
+DigitalOut led(LED1); // inicialize LED
+
+/* Functions----------------------------------------------------------------------*/
-int pin =0;
-int vypis =0;
-
-bool start = true;
-
+/***********************************************************************************
+* Function Name : toggle.
+* Description : Read value from pins, blink with LED and send value to serial.
+* Input : None.
+* Output : None.
+* Return : None.
+***********************************************************************************/
void toggle()
{
int meas,z;
- meas=myIOs.read();
+ meas=myIOs.read(); // read pins
- led= !led;
- if (meas == pin ) {
- start = true;
+ led= !led; //blink led
+ if (meas == pin ) { // If the value is as we want
+ start = true; //start send data to Serial
}
if(start) {
- if(vypis==1) {
+ if(vypis==1) { // write value in bin
while(!pc.writeable());
for (z =32768; z > 0; z >>= 1) {
while(!pc.writeable());
- printf(((meas & z) == z) ? "1" : "0");
+ pc.printf(((meas & z) == z) ? "1" : "0");
}
while(!pc.writeable());
- printf("\n");
- } else if(vypis==2) {
+ pc.printf("\n");
+ } else if(vypis==2) { // write value in decimal
while(!pc.writeable());
- printf("%d\n", meas);
- } else if(vypis==3) {
+ pc.printf("%d\n", meas);
+ } else if(vypis==3) { // write value in oct
while(!pc.writeable());
- printf("%o\n", meas);
- } else if(vypis==4) {
+ pc.printf("%o\n", meas);
+ } else if(vypis==4) { // write value in hex
while(!pc.writeable());
- printf("%x\n", meas);
- } else {
+ pc.printf("%x\n", meas);
+ } else { // write value in all options
while(!pc.writeable());
- printf("DEC: %d, HEX: %x, OCT: %o, BIN: ", meas,meas,meas);
+ pc.printf("DEC: %d, HEX: %x, OCT: %o, BIN: ", meas,meas,meas);
for (z =32768; z > 0; z >>= 1) {
while(!pc.writeable());
- printf(((meas & z) == z) ? "1" : "0");
+ pc.printf(((meas & z) == z) ? "1" : "0");
}
while(!pc.writeable());
- printf("\n");
+ pc.printf("\n");
}
}
}
+/***********************************************************************************
+* Function Name : flushSerialPort.
+* Description : Serial flush rountine.
+* Input : None.
+* Output : None.
+* Return : None.
+***********************************************************************************/
void flushSerialPort()
{
while(pc.readable())
@@ -60,60 +85,92 @@
return;
}
+/***********************************************************************************
+* Function Name : menu.
+* Description : Print menu to serial.
+* Input : None.
+* Output : None.
+* Return : None.
+***********************************************************************************/
+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());
+ pc.printf("Data send to PC is in dec format.\n");
+ 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");
+ while(!pc.writeable());
+ pc.printf("01 y.yyy - set period[s] to send data to PC and start reading value, example:01 0.01\n");
+ while(!pc.writeable());
+ pc.printf("02 0 - stop reading value, example:02 0\n");
+ while(!pc.writeable());
+ pc.printf("03 yyyy - Wait for pin to get to log.1. yyyy write in dec example:03 2000\n");
+ while(!pc.writeable());
+ pc.printf("04 yy - print yy: 00-default all, 01-bin, 02-dec, 03-oct, 04-hex, example:04 00\n");
+ while(!pc.writeable());
+ pc.printf("end HELP\n");
+}
+/***********************************************************************************
+* Function Name : main.
+* Description : Main routine.
+* Input : None.
+* Output : None.
+* Return : None.
+***********************************************************************************/
int main()
{
- myIOs.mode(PullNone); // PullDown PullUp PullNone OpenDrain
+ myIOs.mode(PullNone); // Modes: PullDown PullUp PullNone OpenDrain
-
-
+ // variables to read from serials
int prijData=0;
int Data1=0;
float Data2=0;
+
+ //set serial
pc.baud(115200);
- toggle_ticker.detach();
- toggle_ticker.attach(&toggle, 0.01);
- printf("\nLogic sond.\n");
+ pc.printf("\nLogic sond.\n");
+ menu(); //print menu
+
+ toggle_ticker.detach(); // do NOT call function
+ toggle_ticker.attach(&toggle, 1); // 1 second was passed, call function toggle
+
while(1) {
- prijData=pc.scanf("%d %f",&Data1,&Data2);
- if(prijData==2) {
- //pc.printf()
- if(Data1==1) {
- toggle_ticker.detach();
- toggle_ticker.attach(&toggle, Data2);
- } else if(Data1==2) {
- toggle_ticker.detach();
- } else if(Data1==3) {
- pin=(int)Data2;
- start = false;
- } else if(Data1==4) {
- vypis=(int)Data2;
+ //accepted data from serial
+ prijData=pc.scanf("%d",&Data1); // read number from serial
+ if(prijData==1 && (Data1>=1 && Data1<=3)) { // test if number was read and it is between 1 and 3
+ prijData=pc.scanf("%f",&Data2); // read float number from serial
+ if(prijData==1) {
+ prijData=2; // set variable to set measure of values
+ } else {
+ flushSerialPort(); // iscard data from serial
+ prijData=0; // set variable to print menu
}
} else {
- toggle_ticker.detach();
+ flushSerialPort(); // discard data from serial
+ prijData=0; // set variable to print menu
+ }
- while(!pc.writeable());
- printf("HELP - MENU\n");
- while(!pc.writeable());
- printf("Data send to PC is in dec format.\n");
- while(!pc.writeable());
- printf("Set data exactly.\n");
- while(!pc.writeable());
- printf("Write to console: \"xx yy\", where xx is a code of seting and yy his value.\n");
- while(!pc.writeable());
- printf("01 y.yyy - set period[s] to send data to PC and start reading value, example:01 0.01\n");
- while(!pc.writeable());
- printf("02 0 - stop reading value, example:02 0\n");
- while(!pc.writeable());
- printf("03 yyyy - Wait for pin to get to log.1. yyyy write in dec example:03 2000\n");
- while(!pc.writeable());
- printf("04 yy - print yy: 00-default all, 01-bin, 02-dec, 03-oct, 04-hex, example:04 00\n");
- while(!pc.writeable());
- printf("end HELP\n");
- flushSerialPort();
-
+ if(prijData==2) {
+ if(Data1==1) {
+ toggle_ticker.detach(); // do NOT call function
+ toggle_ticker.attach(&toggle, Data2); // Data2 seconds was passed, call function toggle
+ } else if(Data1==2) {
+ toggle_ticker.detach(); // do NOT call function
+ } else if(Data1==3) {
+ pin=(int)Data2; //set variables, which are use in toggle
+ start = false;
+ } else if(Data1==4) {
+ vypis=(int)Data2; //set variables to set printf, which are use in toggle
+ }
+ } else {
+ toggle_ticker.detach(); // do NOT call function
+ menu(); //print menu
+ flushSerialPort(); // discard data from serial
}
- // wait(0.2); // 200 ms
}
}