Kevin Anderson
/
DataCommFreedom
Using FRDM Board to output data and a clock for communications example.
main.cpp@2:11f32d8cfa11, 2015-03-15 (annotated)
- Committer:
- askksa12543
- Date:
- Sun Mar 15 03:02:48 2015 +0000
- Revision:
- 2:11f32d8cfa11
- Parent:
- 1:4c0c28cc2b2c
- Child:
- 3:027d30718bbc
corrected send/receive for multi bytes
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 | 2:11f32d8cfa11 | 8 | |
askksa12543 | 2:11f32d8cfa11 | 9 | DigitalOut myled(LED1); // red led on board |
askksa12543 | 2:11f32d8cfa11 | 10 | DigitalOut clock_pin(D8), serial_out(D7); //send clock pulse and tx |
askksa12543 | 0:7c7f4b30d64f | 11 | Timer t; //timer for controlling the clock and data skew |
askksa12543 | 2:11f32d8cfa11 | 12 | int msecs, sksecs; //clock time needed for data transfer and skew time |
askksa12543 | 0:7c7f4b30d64f | 13 | int skew_flag; //skew flag for while loop |
askksa12543 | 2:11f32d8cfa11 | 14 | char data[100] = "Hi!"; //data output |
askksa12543 | 2:11f32d8cfa11 | 15 | int i = 0, j = 128; //increment variables |
askksa12543 | 0:7c7f4b30d64f | 16 | |
askksa12543 | 0:7c7f4b30d64f | 17 | int main() { |
askksa12543 | 0:7c7f4b30d64f | 18 | |
askksa12543 | 0:7c7f4b30d64f | 19 | //turn on red led to show programming has worked |
askksa12543 | 0:7c7f4b30d64f | 20 | myled = 0; |
askksa12543 | 0:7c7f4b30d64f | 21 | //initialize output pins |
askksa12543 | 0:7c7f4b30d64f | 22 | clock_pin = 0; |
askksa12543 | 0:7c7f4b30d64f | 23 | serial_out = 0; |
askksa12543 | 0:7c7f4b30d64f | 24 | //skew flag |
askksa12543 | 0:7c7f4b30d64f | 25 | skew_flag = 1; |
askksa12543 | 0:7c7f4b30d64f | 26 | //set timers |
askksa12543 | 1:4c0c28cc2b2c | 27 | msecs = 2000; |
askksa12543 | 1:4c0c28cc2b2c | 28 | sksecs = 1800; |
askksa12543 | 0:7c7f4b30d64f | 29 | |
askksa12543 | 0:7c7f4b30d64f | 30 | //output a clock pulse and data. |
askksa12543 | 2:11f32d8cfa11 | 31 | while(i < 3) |
askksa12543 | 0:7c7f4b30d64f | 32 | { |
askksa12543 | 0:7c7f4b30d64f | 33 | //start timer for clock |
askksa12543 | 0:7c7f4b30d64f | 34 | t.start(); |
askksa12543 | 0:7c7f4b30d64f | 35 | //wait until the timer has reached the set time. |
askksa12543 | 0:7c7f4b30d64f | 36 | while(t.read_ms() < msecs) |
askksa12543 | 0:7c7f4b30d64f | 37 | { |
askksa12543 | 2:11f32d8cfa11 | 38 | //extract data just before clock goes high |
askksa12543 | 0:7c7f4b30d64f | 39 | if(!clock_pin && skew_flag && t.read_ms() > sksecs) |
askksa12543 | 0:7c7f4b30d64f | 40 | { |
askksa12543 | 2:11f32d8cfa11 | 41 | //extract data bit |
askksa12543 | 2:11f32d8cfa11 | 42 | serial_out = (data[i] / j) % 2; |
askksa12543 | 0:7c7f4b30d64f | 43 | skew_flag = 0; |
askksa12543 | 2:11f32d8cfa11 | 44 | j /= 2; //decrement j to get to next bit location |
askksa12543 | 0:7c7f4b30d64f | 45 | } |
askksa12543 | 0:7c7f4b30d64f | 46 | } |
askksa12543 | 0:7c7f4b30d64f | 47 | //stop and reset the timer |
askksa12543 | 0:7c7f4b30d64f | 48 | t.stop(); |
askksa12543 | 0:7c7f4b30d64f | 49 | t.reset(); |
askksa12543 | 0:7c7f4b30d64f | 50 | //switch clock signal |
askksa12543 | 0:7c7f4b30d64f | 51 | clock_pin = !clock_pin; |
askksa12543 | 0:7c7f4b30d64f | 52 | //reset skew flag |
askksa12543 | 0:7c7f4b30d64f | 53 | skew_flag = 1; |
askksa12543 | 2:11f32d8cfa11 | 54 | //last bit sent - reset/increment variables |
askksa12543 | 2:11f32d8cfa11 | 55 | if(j==0) |
askksa12543 | 2:11f32d8cfa11 | 56 | { |
askksa12543 | 2:11f32d8cfa11 | 57 | j=128; |
askksa12543 | 2:11f32d8cfa11 | 58 | i++; |
askksa12543 | 2:11f32d8cfa11 | 59 | } |
askksa12543 | 0:7c7f4b30d64f | 60 | } |
askksa12543 | 1:4c0c28cc2b2c | 61 | } |
askksa12543 | 1:4c0c28cc2b2c | 62 | |
askksa12543 | 2:11f32d8cfa11 | 63 | //crc = x^4+x+1 |