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
main.cpp@3:29a231eba2e0, 2015-05-12 (annotated)
- Committer:
- Foxnec
- Date:
- Tue May 12 08:56:09 2015 +0000
- Revision:
- 3:29a231eba2e0
- Parent:
- 2:10add9ee8c4b
Changes in comments;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| dousape2 | 1:533ef4c5f7ab | 1 | /********************************************************************************** |
| dousape2 | 1:533ef4c5f7ab | 2 | * @file main.cpp |
| dousape2 | 2:10add9ee8c4b | 3 | * @author Petr Dousa |
| dousape2 | 1:533ef4c5f7ab | 4 | * @version V0.1 |
| dousape2 | 1:533ef4c5f7ab | 5 | * @date 09-March-2015 |
| dousape2 | 1:533ef4c5f7ab | 6 | * @brief Read value from pins and write it to serial. |
| Foxnec | 3:29a231eba2e0 | 7 | * Some settings can be set through serial. |
| dousape2 | 1:533ef4c5f7ab | 8 | * Serial speed is set to 115200. |
| dousape2 | 1:533ef4c5f7ab | 9 | ***********************************************************************************/ |
| dousape2 | 1:533ef4c5f7ab | 10 | |
| dousape2 | 2:10add9ee8c4b | 11 | /**************************************************************************************************************************************************/ |
| dousape2 | 2:10add9ee8c4b | 12 | /* Table how to find 0xE000 */ |
| dousape2 | 2:10add9ee8c4b | 13 | /**************************************************************************************************************************************************/ |
| dousape2 | 2:10add9ee8c4b | 14 | /* Num. | 15 | 14 | 13 | 12 || 11 | 10 | 9 | 8 || 7 | 6 | 5 | 4 || 3 | 2 | 1 | 0 */ |
| dousape2 | 2:10add9ee8c4b | 15 | /* Num. of pin | PC_15 | PC_14 | PC_13 | PC_12 || PC_11 | PC_10 | PC_9 | PC_8 || PC_7 | PC_6 | PC_5 | PC_4 || PC_3 | PC_2 | PC_1 | PC_0 */ |
| dousape2 | 2:10add9ee8c4b | 16 | /* Num. in BIN | 1 | 1 | 1 | 0 || 0 | 0 | 0 | 0 || 0 | 0 | 0 | 0 || 0 | 0 | 0 | 0 */ |
| dousape2 | 2:10add9ee8c4b | 17 | /* Num. in HEX | E || 0 || 0 || 0 */ |
| dousape2 | 2:10add9ee8c4b | 18 | /**************************************************************************************************************************************************/ |
| dousape2 | 2:10add9ee8c4b | 19 | |
| dousape2 | 1:533ef4c5f7ab | 20 | /* Includes ----------------------------------------------------------------------*/ |
| dousape2 | 0:786207d961f4 | 21 | #include "mbed.h" |
| dousape2 | 0:786207d961f4 | 22 | |
| dousape2 | 2:10add9ee8c4b | 23 | /* Defines -----------------------------------------------------------------------*/ |
| dousape2 | 2:10add9ee8c4b | 24 | |
| dousape2 | 2:10add9ee8c4b | 25 | /* Function prototypes -----------------------------------------------------------*/ |
| dousape2 | 2:10add9ee8c4b | 26 | |
| dousape2 | 1:533ef4c5f7ab | 27 | /* Variables ---------------------------------------------------------------------*/ |
| Foxnec | 3:29a231eba2e0 | 28 | int pin =0; |
| Foxnec | 3:29a231eba2e0 | 29 | int printout =0; |
| Foxnec | 3:29a231eba2e0 | 30 | bool start = true; |
| dousape2 | 0:786207d961f4 | 31 | |
| dousape2 | 1:533ef4c5f7ab | 32 | //mbed - initialization of peripherals |
| dousape2 | 1:533ef4c5f7ab | 33 | PortIn myIOs(PortC, 0xFFFF); // PC_all |
| Foxnec | 3:29a231eba2e0 | 34 | Serial pc(SERIAL_TX, SERIAL_RX); // initialize Serial to connect to PC |
| Foxnec | 3:29a231eba2e0 | 35 | Ticker toggle_ticker; // initialize ticker |
| Foxnec | 3:29a231eba2e0 | 36 | DigitalOut led(LED1); // initialize LED |
| dousape2 | 1:533ef4c5f7ab | 37 | |
| dousape2 | 1:533ef4c5f7ab | 38 | /* Functions----------------------------------------------------------------------*/ |
| dousape2 | 0:786207d961f4 | 39 | |
| dousape2 | 1:533ef4c5f7ab | 40 | /*********************************************************************************** |
| dousape2 | 1:533ef4c5f7ab | 41 | * Function Name : toggle. |
| dousape2 | 1:533ef4c5f7ab | 42 | * Description : Read value from pins, blink with LED and send value to serial. |
| dousape2 | 1:533ef4c5f7ab | 43 | * Input : None. |
| dousape2 | 1:533ef4c5f7ab | 44 | * Output : None. |
| dousape2 | 1:533ef4c5f7ab | 45 | * Return : None. |
| dousape2 | 1:533ef4c5f7ab | 46 | ***********************************************************************************/ |
| dousape2 | 0:786207d961f4 | 47 | void toggle() |
| dousape2 | 0:786207d961f4 | 48 | { |
| dousape2 | 0:786207d961f4 | 49 | int meas,z; |
| Foxnec | 3:29a231eba2e0 | 50 | meas=myIOs.read(); // read pins |
| dousape2 | 0:786207d961f4 | 51 | |
| Foxnec | 3:29a231eba2e0 | 52 | led= !led; //blink led |
| Foxnec | 3:29a231eba2e0 | 53 | if (meas == pin ) { // If the value is as we want |
| Foxnec | 3:29a231eba2e0 | 54 | start = true; //start send data to Serial |
| dousape2 | 0:786207d961f4 | 55 | } |
| dousape2 | 0:786207d961f4 | 56 | if(start) { |
| Foxnec | 3:29a231eba2e0 | 57 | if(printout==1) { // write value in bin |
| dousape2 | 0:786207d961f4 | 58 | while(!pc.writeable()); |
| dousape2 | 0:786207d961f4 | 59 | for (z =32768; z > 0; z >>= 1) { |
| dousape2 | 0:786207d961f4 | 60 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 61 | pc.printf(((meas & z) == z) ? "1" : "0"); |
| dousape2 | 0:786207d961f4 | 62 | } |
| dousape2 | 0:786207d961f4 | 63 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 64 | pc.printf("\n"); |
| Foxnec | 3:29a231eba2e0 | 65 | } else if(printout==2) { // write value in decimal |
| dousape2 | 0:786207d961f4 | 66 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 67 | pc.printf("%d\n", meas); |
| Foxnec | 3:29a231eba2e0 | 68 | } else if(printout==3) { // write value in oct |
| dousape2 | 0:786207d961f4 | 69 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 70 | pc.printf("%o\n", meas); |
| Foxnec | 3:29a231eba2e0 | 71 | } else if(printout==4) { // write value in hex |
| dousape2 | 0:786207d961f4 | 72 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 73 | pc.printf("%x\n", meas); |
| Foxnec | 3:29a231eba2e0 | 74 | } else { // write value in all options |
| dousape2 | 0:786207d961f4 | 75 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 76 | pc.printf("DEC: %d, HEX: %x, OCT: %o, BIN: ", meas,meas,meas); |
| dousape2 | 0:786207d961f4 | 77 | for (z =32768; z > 0; z >>= 1) { |
| dousape2 | 0:786207d961f4 | 78 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 79 | pc.printf(((meas & z) == z) ? "1" : "0"); |
| dousape2 | 0:786207d961f4 | 80 | } |
| dousape2 | 0:786207d961f4 | 81 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 82 | pc.printf("\n"); |
| dousape2 | 0:786207d961f4 | 83 | } |
| dousape2 | 0:786207d961f4 | 84 | } |
| dousape2 | 0:786207d961f4 | 85 | } |
| dousape2 | 0:786207d961f4 | 86 | |
| dousape2 | 1:533ef4c5f7ab | 87 | /*********************************************************************************** |
| dousape2 | 1:533ef4c5f7ab | 88 | * Function Name : flushSerialPort. |
| Foxnec | 3:29a231eba2e0 | 89 | * Description : Serial flush routine. |
| dousape2 | 1:533ef4c5f7ab | 90 | * Input : None. |
| dousape2 | 1:533ef4c5f7ab | 91 | * Output : None. |
| dousape2 | 1:533ef4c5f7ab | 92 | * Return : None. |
| dousape2 | 1:533ef4c5f7ab | 93 | ***********************************************************************************/ |
| dousape2 | 0:786207d961f4 | 94 | void flushSerialPort() |
| dousape2 | 0:786207d961f4 | 95 | { |
| dousape2 | 0:786207d961f4 | 96 | while(pc.readable()) |
| dousape2 | 0:786207d961f4 | 97 | pc.getc(); |
| dousape2 | 0:786207d961f4 | 98 | return; |
| dousape2 | 0:786207d961f4 | 99 | } |
| dousape2 | 0:786207d961f4 | 100 | |
| dousape2 | 1:533ef4c5f7ab | 101 | /*********************************************************************************** |
| dousape2 | 1:533ef4c5f7ab | 102 | * Function Name : menu. |
| dousape2 | 1:533ef4c5f7ab | 103 | * Description : Print menu to serial. |
| dousape2 | 1:533ef4c5f7ab | 104 | * Input : None. |
| dousape2 | 1:533ef4c5f7ab | 105 | * Output : None. |
| dousape2 | 1:533ef4c5f7ab | 106 | * Return : None. |
| dousape2 | 1:533ef4c5f7ab | 107 | ***********************************************************************************/ |
| dousape2 | 1:533ef4c5f7ab | 108 | void menu() |
| dousape2 | 1:533ef4c5f7ab | 109 | { |
| Foxnec | 3:29a231eba2e0 | 110 | while(!pc.writeable()); // wait for the serial to be available for sending data |
| dousape2 | 1:533ef4c5f7ab | 111 | pc.printf("HELP - MENU\n");// send text to serial |
| dousape2 | 1:533ef4c5f7ab | 112 | while(!pc.writeable()); |
| Foxnec | 3:29a231eba2e0 | 113 | pc.printf("Data to PC are in dec format.\n"); |
| dousape2 | 1:533ef4c5f7ab | 114 | while(!pc.writeable()); |
| Foxnec | 3:29a231eba2e0 | 115 | pc.printf("Input format: \"xx yy\", where xx is the setting and yy is the value.\n"); |
| dousape2 | 1:533ef4c5f7ab | 116 | while(!pc.writeable()); |
| Foxnec | 3:29a231eba2e0 | 117 | pc.printf("01 y.yyy - set period [seconds] to send data to PC and start reading value, example: 01 0.01\n"); |
| dousape2 | 1:533ef4c5f7ab | 118 | while(!pc.writeable()); |
| Foxnec | 3:29a231eba2e0 | 119 | pc.printf("02 0 - stop reading value, example: 02 0\n"); |
| dousape2 | 1:533ef4c5f7ab | 120 | while(!pc.writeable()); |
| Foxnec | 3:29a231eba2e0 | 121 | pc.printf("03 yyyy - Wait for pin to get to high level. yyyy write in decimal, example: 03 2000\n"); |
| dousape2 | 1:533ef4c5f7ab | 122 | while(!pc.writeable()); |
| Foxnec | 3:29a231eba2e0 | 123 | pc.printf("04 yy - print format yy: 00-default all, 01-bin, 02-dec, 03-oct, 04-hex, example: 04 00\n"); |
| dousape2 | 1:533ef4c5f7ab | 124 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 125 | pc.printf("end HELP\n"); |
| dousape2 | 1:533ef4c5f7ab | 126 | } |
| dousape2 | 0:786207d961f4 | 127 | |
| dousape2 | 1:533ef4c5f7ab | 128 | /*********************************************************************************** |
| dousape2 | 1:533ef4c5f7ab | 129 | * Function Name : main. |
| dousape2 | 1:533ef4c5f7ab | 130 | * Description : Main routine. |
| dousape2 | 1:533ef4c5f7ab | 131 | * Input : None. |
| dousape2 | 1:533ef4c5f7ab | 132 | * Output : None. |
| dousape2 | 1:533ef4c5f7ab | 133 | * Return : None. |
| dousape2 | 1:533ef4c5f7ab | 134 | ***********************************************************************************/ |
| dousape2 | 0:786207d961f4 | 135 | int main() |
| dousape2 | 0:786207d961f4 | 136 | { |
| dousape2 | 1:533ef4c5f7ab | 137 | myIOs.mode(PullNone); // Modes: PullDown PullUp PullNone OpenDrain |
| dousape2 | 0:786207d961f4 | 138 | |
| dousape2 | 1:533ef4c5f7ab | 139 | // variables to read from serials |
| dousape2 | 0:786207d961f4 | 140 | int prijData=0; |
| dousape2 | 0:786207d961f4 | 141 | int Data1=0; |
| dousape2 | 0:786207d961f4 | 142 | float Data2=0; |
| dousape2 | 1:533ef4c5f7ab | 143 | |
| dousape2 | 1:533ef4c5f7ab | 144 | //set serial |
| dousape2 | 0:786207d961f4 | 145 | pc.baud(115200); |
| dousape2 | 1:533ef4c5f7ab | 146 | pc.printf("\nLogic sond.\n"); |
| dousape2 | 1:533ef4c5f7ab | 147 | menu(); //print menu |
| dousape2 | 1:533ef4c5f7ab | 148 | |
| Foxnec | 3:29a231eba2e0 | 149 | toggle_ticker.detach(); // detach all functions from ticker |
| Foxnec | 3:29a231eba2e0 | 150 | toggle_ticker.attach(&toggle, 1); // call function toggle every single second |
| dousape2 | 1:533ef4c5f7ab | 151 | |
| dousape2 | 0:786207d961f4 | 152 | |
| dousape2 | 0:786207d961f4 | 153 | while(1) { |
| dousape2 | 1:533ef4c5f7ab | 154 | //accepted data from serial |
| dousape2 | 1:533ef4c5f7ab | 155 | prijData=pc.scanf("%d",&Data1); // read number from serial |
| Foxnec | 3:29a231eba2e0 | 156 | if(prijData==1 && (Data1>=1 && Data1<=3)) { // test if number was read and if it's between 1 and 3 |
| dousape2 | 1:533ef4c5f7ab | 157 | prijData=pc.scanf("%f",&Data2); // read float number from serial |
| dousape2 | 1:533ef4c5f7ab | 158 | if(prijData==1) { |
| dousape2 | 1:533ef4c5f7ab | 159 | prijData=2; // set variable to set measure of values |
| dousape2 | 1:533ef4c5f7ab | 160 | } else { |
| Foxnec | 3:29a231eba2e0 | 161 | flushSerialPort(); // clear serial port, discard all data in the buffer |
| dousape2 | 1:533ef4c5f7ab | 162 | prijData=0; // set variable to print menu |
| dousape2 | 0:786207d961f4 | 163 | } |
| dousape2 | 0:786207d961f4 | 164 | } else { |
| Foxnec | 3:29a231eba2e0 | 165 | flushSerialPort(); // clear serial port, discard all data in the buffer |
| dousape2 | 1:533ef4c5f7ab | 166 | prijData=0; // set variable to print menu |
| dousape2 | 1:533ef4c5f7ab | 167 | } |
| dousape2 | 0:786207d961f4 | 168 | |
| dousape2 | 1:533ef4c5f7ab | 169 | if(prijData==2) { |
| dousape2 | 1:533ef4c5f7ab | 170 | if(Data1==1) { |
| Foxnec | 3:29a231eba2e0 | 171 | toggle_ticker.detach(); // detach all functions from ticker |
| dousape2 | 1:533ef4c5f7ab | 172 | toggle_ticker.attach(&toggle, Data2); // Data2 seconds was passed, call function toggle |
| dousape2 | 1:533ef4c5f7ab | 173 | } else if(Data1==2) { |
| Foxnec | 3:29a231eba2e0 | 174 | toggle_ticker.detach(); // detach all functions from ticker |
| dousape2 | 1:533ef4c5f7ab | 175 | } else if(Data1==3) { |
| Foxnec | 3:29a231eba2e0 | 176 | pin=(int)Data2; //set variables, which are used in toggle |
| dousape2 | 1:533ef4c5f7ab | 177 | start = false; |
| dousape2 | 1:533ef4c5f7ab | 178 | } else if(Data1==4) { |
| Foxnec | 3:29a231eba2e0 | 179 | printout=(int)Data2; //set variables to set printf, which are used in toggle |
| dousape2 | 1:533ef4c5f7ab | 180 | } |
| dousape2 | 1:533ef4c5f7ab | 181 | } else { |
| Foxnec | 3:29a231eba2e0 | 182 | toggle_ticker.detach(); // detach all functions from ticker |
| Foxnec | 3:29a231eba2e0 | 183 | menu(); // print menu |
| Foxnec | 3:29a231eba2e0 | 184 | flushSerialPort(); // clear serial port, discard all data in the buffer |
| dousape2 | 0:786207d961f4 | 185 | } |
| dousape2 | 0:786207d961f4 | 186 | } |
| dousape2 | 0:786207d961f4 | 187 | } |