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@2:10add9ee8c4b, 2015-03-22 (annotated)
- Committer:
- dousape2
- Date:
- Sun Mar 22 14:53:35 2015 +0000
- Revision:
- 2:10add9ee8c4b
- Parent:
- 1:533ef4c5f7ab
- Child:
- 3:29a231eba2e0
snad hotovo
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. |
| dousape2 | 1:533ef4c5f7ab | 7 | * It could be set some settings 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 ---------------------------------------------------------------------*/ |
| dousape2 | 1:533ef4c5f7ab | 28 | int pin =0; //pins to compare and start print |
| dousape2 | 1:533ef4c5f7ab | 29 | int vypis =0; // which output will be provided |
| dousape2 | 1:533ef4c5f7ab | 30 | bool start = true; // if true it print value to serial |
| dousape2 | 0:786207d961f4 | 31 | |
| dousape2 | 1:533ef4c5f7ab | 32 | //mbed - initialization of peripherals |
| dousape2 | 1:533ef4c5f7ab | 33 | PortIn myIOs(PortC, 0xFFFF); // PC_all |
| dousape2 | 1:533ef4c5f7ab | 34 | Serial pc(SERIAL_TX, SERIAL_RX); // inicialize Serial to connect to PC |
| dousape2 | 1:533ef4c5f7ab | 35 | Ticker toggle_ticker; // inicialize ticker |
| dousape2 | 1:533ef4c5f7ab | 36 | DigitalOut led(LED1); // inicialize 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; |
| dousape2 | 1:533ef4c5f7ab | 50 | meas=myIOs.read(); // read pins |
| dousape2 | 0:786207d961f4 | 51 | |
| dousape2 | 1:533ef4c5f7ab | 52 | led= !led; //blink led |
| dousape2 | 1:533ef4c5f7ab | 53 | if (meas == pin ) { // If the value is as we want |
| dousape2 | 1:533ef4c5f7ab | 54 | start = true; //start send data to Serial |
| dousape2 | 0:786207d961f4 | 55 | } |
| dousape2 | 0:786207d961f4 | 56 | if(start) { |
| dousape2 | 1:533ef4c5f7ab | 57 | if(vypis==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"); |
| dousape2 | 1:533ef4c5f7ab | 65 | } else if(vypis==2) { // write value in decimal |
| dousape2 | 0:786207d961f4 | 66 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 67 | pc.printf("%d\n", meas); |
| dousape2 | 1:533ef4c5f7ab | 68 | } else if(vypis==3) { // write value in oct |
| dousape2 | 0:786207d961f4 | 69 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 70 | pc.printf("%o\n", meas); |
| dousape2 | 1:533ef4c5f7ab | 71 | } else if(vypis==4) { // write value in hex |
| dousape2 | 0:786207d961f4 | 72 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 73 | pc.printf("%x\n", meas); |
| dousape2 | 1:533ef4c5f7ab | 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. |
| dousape2 | 1:533ef4c5f7ab | 89 | * Description : Serial flush rountine. |
| 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 | { |
| dousape2 | 1:533ef4c5f7ab | 110 | while(!pc.writeable()); // wait to be serial available for sending data |
| dousape2 | 1:533ef4c5f7ab | 111 | pc.printf("HELP - MENU\n");// send text to serial |
| dousape2 | 1:533ef4c5f7ab | 112 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 113 | pc.printf("Data send to PC is in dec format.\n"); |
| dousape2 | 1:533ef4c5f7ab | 114 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 115 | pc.printf("Set data exactly.\n"); |
| dousape2 | 1:533ef4c5f7ab | 116 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 117 | pc.printf("Write to console: \"xx yy\", where xx is a code of seting and yy his value.\n"); |
| dousape2 | 1:533ef4c5f7ab | 118 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 119 | pc.printf("01 y.yyy - set period[s] to send data to PC and start reading value, example:01 0.01\n"); |
| dousape2 | 1:533ef4c5f7ab | 120 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 121 | pc.printf("02 0 - stop reading value, example:02 0\n"); |
| dousape2 | 1:533ef4c5f7ab | 122 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 123 | pc.printf("03 yyyy - Wait for pin to get to log.1. yyyy write in dec example:03 2000\n"); |
| dousape2 | 1:533ef4c5f7ab | 124 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 125 | pc.printf("04 yy - print yy: 00-default all, 01-bin, 02-dec, 03-oct, 04-hex, example:04 00\n"); |
| dousape2 | 1:533ef4c5f7ab | 126 | while(!pc.writeable()); |
| dousape2 | 1:533ef4c5f7ab | 127 | pc.printf("end HELP\n"); |
| dousape2 | 1:533ef4c5f7ab | 128 | } |
| dousape2 | 0:786207d961f4 | 129 | |
| dousape2 | 1:533ef4c5f7ab | 130 | /*********************************************************************************** |
| dousape2 | 1:533ef4c5f7ab | 131 | * Function Name : main. |
| dousape2 | 1:533ef4c5f7ab | 132 | * Description : Main routine. |
| dousape2 | 1:533ef4c5f7ab | 133 | * Input : None. |
| dousape2 | 1:533ef4c5f7ab | 134 | * Output : None. |
| dousape2 | 1:533ef4c5f7ab | 135 | * Return : None. |
| dousape2 | 1:533ef4c5f7ab | 136 | ***********************************************************************************/ |
| dousape2 | 0:786207d961f4 | 137 | int main() |
| dousape2 | 0:786207d961f4 | 138 | { |
| dousape2 | 1:533ef4c5f7ab | 139 | myIOs.mode(PullNone); // Modes: PullDown PullUp PullNone OpenDrain |
| dousape2 | 0:786207d961f4 | 140 | |
| dousape2 | 1:533ef4c5f7ab | 141 | // variables to read from serials |
| dousape2 | 0:786207d961f4 | 142 | int prijData=0; |
| dousape2 | 0:786207d961f4 | 143 | int Data1=0; |
| dousape2 | 0:786207d961f4 | 144 | float Data2=0; |
| dousape2 | 1:533ef4c5f7ab | 145 | |
| dousape2 | 1:533ef4c5f7ab | 146 | //set serial |
| dousape2 | 0:786207d961f4 | 147 | pc.baud(115200); |
| dousape2 | 1:533ef4c5f7ab | 148 | pc.printf("\nLogic sond.\n"); |
| dousape2 | 1:533ef4c5f7ab | 149 | menu(); //print menu |
| dousape2 | 1:533ef4c5f7ab | 150 | |
| dousape2 | 1:533ef4c5f7ab | 151 | toggle_ticker.detach(); // do NOT call function |
| dousape2 | 1:533ef4c5f7ab | 152 | toggle_ticker.attach(&toggle, 1); // 1 second was passed, call function toggle |
| dousape2 | 1:533ef4c5f7ab | 153 | |
| dousape2 | 0:786207d961f4 | 154 | |
| dousape2 | 0:786207d961f4 | 155 | while(1) { |
| dousape2 | 1:533ef4c5f7ab | 156 | //accepted data from serial |
| dousape2 | 1:533ef4c5f7ab | 157 | prijData=pc.scanf("%d",&Data1); // read number from serial |
| dousape2 | 1:533ef4c5f7ab | 158 | if(prijData==1 && (Data1>=1 && Data1<=3)) { // test if number was read and it is between 1 and 3 |
| dousape2 | 1:533ef4c5f7ab | 159 | prijData=pc.scanf("%f",&Data2); // read float number from serial |
| dousape2 | 1:533ef4c5f7ab | 160 | if(prijData==1) { |
| dousape2 | 1:533ef4c5f7ab | 161 | prijData=2; // set variable to set measure of values |
| dousape2 | 1:533ef4c5f7ab | 162 | } else { |
| dousape2 | 1:533ef4c5f7ab | 163 | flushSerialPort(); // iscard data from serial |
| dousape2 | 1:533ef4c5f7ab | 164 | prijData=0; // set variable to print menu |
| dousape2 | 0:786207d961f4 | 165 | } |
| dousape2 | 0:786207d961f4 | 166 | } else { |
| dousape2 | 1:533ef4c5f7ab | 167 | flushSerialPort(); // discard data from serial |
| dousape2 | 1:533ef4c5f7ab | 168 | prijData=0; // set variable to print menu |
| dousape2 | 1:533ef4c5f7ab | 169 | } |
| dousape2 | 0:786207d961f4 | 170 | |
| dousape2 | 1:533ef4c5f7ab | 171 | if(prijData==2) { |
| dousape2 | 1:533ef4c5f7ab | 172 | if(Data1==1) { |
| dousape2 | 1:533ef4c5f7ab | 173 | toggle_ticker.detach(); // do NOT call function |
| dousape2 | 1:533ef4c5f7ab | 174 | toggle_ticker.attach(&toggle, Data2); // Data2 seconds was passed, call function toggle |
| dousape2 | 1:533ef4c5f7ab | 175 | } else if(Data1==2) { |
| dousape2 | 1:533ef4c5f7ab | 176 | toggle_ticker.detach(); // do NOT call function |
| dousape2 | 1:533ef4c5f7ab | 177 | } else if(Data1==3) { |
| dousape2 | 1:533ef4c5f7ab | 178 | pin=(int)Data2; //set variables, which are use in toggle |
| dousape2 | 1:533ef4c5f7ab | 179 | start = false; |
| dousape2 | 1:533ef4c5f7ab | 180 | } else if(Data1==4) { |
| dousape2 | 1:533ef4c5f7ab | 181 | vypis=(int)Data2; //set variables to set printf, which are use in toggle |
| dousape2 | 1:533ef4c5f7ab | 182 | } |
| dousape2 | 1:533ef4c5f7ab | 183 | } else { |
| dousape2 | 1:533ef4c5f7ab | 184 | toggle_ticker.detach(); // do NOT call function |
| dousape2 | 1:533ef4c5f7ab | 185 | menu(); //print menu |
| dousape2 | 1:533ef4c5f7ab | 186 | flushSerialPort(); // discard data from serial |
| dousape2 | 0:786207d961f4 | 187 | } |
| dousape2 | 0:786207d961f4 | 188 | } |
| dousape2 | 0:786207d961f4 | 189 | } |