Kevin Anderson
/
DataCommFreedom
Using FRDM Board to output data and a clock for communications example.
main.cpp@4:39a777388acd, 2015-03-23 (annotated)
- Committer:
- askksa12543
- Date:
- Mon Mar 23 13:54:41 2015 +0000
- Revision:
- 4:39a777388acd
- Parent:
- 3:027d30718bbc
- Child:
- 5:8c012e2c1ba8
updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
askksa12543 | 2:11f32d8cfa11 | 1 | //project to include preamble, addressing, control, error control. |
askksa12543 | 2:11f32d8cfa11 | 2 | //currently fixed size data field but doesn't have to be. (post amble if not fixed size) |
askksa12543 | 2:11f32d8cfa11 | 3 | |
askksa12543 | 0:7c7f4b30d64f | 4 | #include "mbed.h" |
askksa12543 | 0:7c7f4b30d64f | 5 | #include "stdio.h" |
askksa12543 | 0:7c7f4b30d64f | 6 | |
askksa12543 | 2:11f32d8cfa11 | 7 | #define MAX 100 //set the size of the character data storage array |
askksa12543 | 3:027d30718bbc | 8 | #define PREAMBLE 0x7E //preamble of 01111110 |
askksa12543 | 2:11f32d8cfa11 | 9 | |
askksa12543 | 2:11f32d8cfa11 | 10 | DigitalOut myled(LED1); // red led on board |
askksa12543 | 2:11f32d8cfa11 | 11 | DigitalOut clock_pin(D8), serial_out(D7); //send clock pulse and tx |
askksa12543 | 0:7c7f4b30d64f | 12 | Timer t; //timer for controlling the clock and data skew |
askksa12543 | 2:11f32d8cfa11 | 13 | int msecs, sksecs; //clock time needed for data transfer and skew time |
askksa12543 | 0:7c7f4b30d64f | 14 | int skew_flag; //skew flag for while loop |
askksa12543 | 3:027d30718bbc | 15 | unsigned char pre = PREAMBLE; |
askksa12543 | 3:027d30718bbc | 16 | unsigned char data[100] = "Hi!"; //data output |
askksa12543 | 3:027d30718bbc | 17 | int done = 0, i = 0, j = 128; //increment variables |
askksa12543 | 0:7c7f4b30d64f | 18 | |
askksa12543 | 0:7c7f4b30d64f | 19 | int main() { |
askksa12543 | 0:7c7f4b30d64f | 20 | |
askksa12543 | 0:7c7f4b30d64f | 21 | //turn on red led to show programming has worked |
askksa12543 | 0:7c7f4b30d64f | 22 | myled = 0; |
askksa12543 | 0:7c7f4b30d64f | 23 | //initialize output pins |
askksa12543 | 0:7c7f4b30d64f | 24 | clock_pin = 0; |
askksa12543 | 0:7c7f4b30d64f | 25 | serial_out = 0; |
askksa12543 | 0:7c7f4b30d64f | 26 | //skew flag |
askksa12543 | 0:7c7f4b30d64f | 27 | skew_flag = 1; |
askksa12543 | 0:7c7f4b30d64f | 28 | //set timers |
askksa12543 | 1:4c0c28cc2b2c | 29 | msecs = 2000; |
askksa12543 | 1:4c0c28cc2b2c | 30 | sksecs = 1800; |
askksa12543 | 0:7c7f4b30d64f | 31 | |
askksa12543 | 3:027d30718bbc | 32 | //output preamble |
askksa12543 | 3:027d30718bbc | 33 | while(!done) |
askksa12543 | 3:027d30718bbc | 34 | { |
askksa12543 | 3:027d30718bbc | 35 | //start timer for clock |
askksa12543 | 3:027d30718bbc | 36 | t.start(); |
askksa12543 | 3:027d30718bbc | 37 | //wait until the timer has reached the set time. |
askksa12543 | 3:027d30718bbc | 38 | while(t.read_ms() < msecs) |
askksa12543 | 3:027d30718bbc | 39 | { |
askksa12543 | 3:027d30718bbc | 40 | //extract data just before clock goes high |
askksa12543 | 3:027d30718bbc | 41 | if(!clock_pin && skew_flag && t.read_ms() > sksecs) |
askksa12543 | 3:027d30718bbc | 42 | { |
askksa12543 | 3:027d30718bbc | 43 | //extract data bit |
askksa12543 | 3:027d30718bbc | 44 | serial_out = (pre / j) % 2; |
askksa12543 | 3:027d30718bbc | 45 | skew_flag = 0; |
askksa12543 | 3:027d30718bbc | 46 | j /= 2; //decrement j to get to next bit location |
askksa12543 | 3:027d30718bbc | 47 | } |
askksa12543 | 3:027d30718bbc | 48 | } |
askksa12543 | 3:027d30718bbc | 49 | //stop and reset the timer |
askksa12543 | 3:027d30718bbc | 50 | t.stop(); |
askksa12543 | 3:027d30718bbc | 51 | t.reset(); |
askksa12543 | 3:027d30718bbc | 52 | //switch clock signal |
askksa12543 | 3:027d30718bbc | 53 | clock_pin = !clock_pin; |
askksa12543 | 3:027d30718bbc | 54 | //reset skew flag |
askksa12543 | 3:027d30718bbc | 55 | skew_flag = 1; |
askksa12543 | 3:027d30718bbc | 56 | //last preamble bit sent - reset/increment variables |
askksa12543 | 3:027d30718bbc | 57 | if(j==0) |
askksa12543 | 3:027d30718bbc | 58 | { |
askksa12543 | 3:027d30718bbc | 59 | done = 1; |
askksa12543 | 3:027d30718bbc | 60 | j = 128; |
askksa12543 | 3:027d30718bbc | 61 | } |
askksa12543 | 3:027d30718bbc | 62 | } |
askksa12543 | 3:027d30718bbc | 63 | |
askksa12543 | 3:027d30718bbc | 64 | //reset done |
askksa12543 | 3:027d30718bbc | 65 | done = 0; |
askksa12543 | 3:027d30718bbc | 66 | |
askksa12543 | 0:7c7f4b30d64f | 67 | //output a clock pulse and data. |
askksa12543 | 3:027d30718bbc | 68 | while(!done) |
askksa12543 | 0:7c7f4b30d64f | 69 | { |
askksa12543 | 0:7c7f4b30d64f | 70 | //start timer for clock |
askksa12543 | 0:7c7f4b30d64f | 71 | t.start(); |
askksa12543 | 0:7c7f4b30d64f | 72 | //wait until the timer has reached the set time. |
askksa12543 | 0:7c7f4b30d64f | 73 | while(t.read_ms() < msecs) |
askksa12543 | 0:7c7f4b30d64f | 74 | { |
askksa12543 | 2:11f32d8cfa11 | 75 | //extract data just before clock goes high |
askksa12543 | 0:7c7f4b30d64f | 76 | if(!clock_pin && skew_flag && t.read_ms() > sksecs) |
askksa12543 | 0:7c7f4b30d64f | 77 | { |
askksa12543 | 2:11f32d8cfa11 | 78 | //extract data bit |
askksa12543 | 2:11f32d8cfa11 | 79 | serial_out = (data[i] / j) % 2; |
askksa12543 | 0:7c7f4b30d64f | 80 | skew_flag = 0; |
askksa12543 | 2:11f32d8cfa11 | 81 | j /= 2; //decrement j to get to next bit location |
askksa12543 | 0:7c7f4b30d64f | 82 | } |
askksa12543 | 0:7c7f4b30d64f | 83 | } |
askksa12543 | 0:7c7f4b30d64f | 84 | //stop and reset the timer |
askksa12543 | 0:7c7f4b30d64f | 85 | t.stop(); |
askksa12543 | 0:7c7f4b30d64f | 86 | t.reset(); |
askksa12543 | 0:7c7f4b30d64f | 87 | //switch clock signal |
askksa12543 | 0:7c7f4b30d64f | 88 | clock_pin = !clock_pin; |
askksa12543 | 0:7c7f4b30d64f | 89 | //reset skew flag |
askksa12543 | 0:7c7f4b30d64f | 90 | skew_flag = 1; |
askksa12543 | 2:11f32d8cfa11 | 91 | //last bit sent - reset/increment variables |
askksa12543 | 2:11f32d8cfa11 | 92 | if(j==0) |
askksa12543 | 2:11f32d8cfa11 | 93 | { |
askksa12543 | 2:11f32d8cfa11 | 94 | j=128; |
askksa12543 | 2:11f32d8cfa11 | 95 | i++; |
askksa12543 | 2:11f32d8cfa11 | 96 | } |
askksa12543 | 3:027d30718bbc | 97 | //finished sending data |
askksa12543 | 3:027d30718bbc | 98 | if(i>2) |
askksa12543 | 3:027d30718bbc | 99 | { |
askksa12543 | 3:027d30718bbc | 100 | done = 1; |
askksa12543 | 3:027d30718bbc | 101 | } |
askksa12543 | 0:7c7f4b30d64f | 102 | } |
askksa12543 | 3:027d30718bbc | 103 | //turn off red led to show sending has finished |
askksa12543 | 3:027d30718bbc | 104 | myled = 1; |
askksa12543 | 1:4c0c28cc2b2c | 105 | } |
askksa12543 | 1:4c0c28cc2b2c | 106 | |
askksa12543 | 4:39a777388acd | 107 | //crc = x^4+x+1 |
askksa12543 | 4:39a777388acd | 108 | //put char data into unsigned short temp variable, then shift << 5. % this number by the decimal equivalent of binary representation of |
askksa12543 | 4:39a777388acd | 109 | //the crc code and save and send it as a separate 5 bits. On the receive side save each byte in the temp character and then the next five |
askksa12543 | 4:39a777388acd | 110 | //bytes in a temp variable - after receiving 13 bits stick data in unsigned short variable << 5 add the recieved 5 bits and then % by |
askksa12543 | 4:39a777388acd | 111 | // the crc decimal equivalent, if that equals 0, save the temp character in the permanent character array and receive the next bits. |