Kevin Anderson
/
DataCommFreedom
Using FRDM Board to output data and a clock for communications example.
main.cpp@0:7c7f4b30d64f, 2015-02-26 (annotated)
- Committer:
- askksa12543
- Date:
- Thu Feb 26 14:52:18 2015 +0000
- Revision:
- 0:7c7f4b30d64f
- Child:
- 1:4c0c28cc2b2c
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
askksa12543 | 0:7c7f4b30d64f | 1 | #include "mbed.h" |
askksa12543 | 0:7c7f4b30d64f | 2 | #include "stdio.h" |
askksa12543 | 0:7c7f4b30d64f | 3 | |
askksa12543 | 0:7c7f4b30d64f | 4 | DigitalOut myled(LED1); // Led on board |
askksa12543 | 0:7c7f4b30d64f | 5 | DigitalOut clock_pin(D8); //send clock pulse |
askksa12543 | 0:7c7f4b30d64f | 6 | DigitalOut serial_out(D7); //serial tx |
askksa12543 | 0:7c7f4b30d64f | 7 | Timer t; //timer for controlling the clock and data skew |
askksa12543 | 0:7c7f4b30d64f | 8 | int msecs; //clock time needed for data transfer |
askksa12543 | 0:7c7f4b30d64f | 9 | int sksecs; //skew time |
askksa12543 | 0:7c7f4b30d64f | 10 | int skew_flag; //skew flag for while loop |
askksa12543 | 0:7c7f4b30d64f | 11 | |
askksa12543 | 0:7c7f4b30d64f | 12 | int main() { |
askksa12543 | 0:7c7f4b30d64f | 13 | |
askksa12543 | 0:7c7f4b30d64f | 14 | //turn on red led to show programming has worked |
askksa12543 | 0:7c7f4b30d64f | 15 | myled = 0; |
askksa12543 | 0:7c7f4b30d64f | 16 | //initialize output pins |
askksa12543 | 0:7c7f4b30d64f | 17 | clock_pin = 0; |
askksa12543 | 0:7c7f4b30d64f | 18 | serial_out = 0; |
askksa12543 | 0:7c7f4b30d64f | 19 | //skew flag |
askksa12543 | 0:7c7f4b30d64f | 20 | skew_flag = 1; |
askksa12543 | 0:7c7f4b30d64f | 21 | |
askksa12543 | 0:7c7f4b30d64f | 22 | //set timers |
askksa12543 | 0:7c7f4b30d64f | 23 | msecs = 10; |
askksa12543 | 0:7c7f4b30d64f | 24 | sksecs = 8; |
askksa12543 | 0:7c7f4b30d64f | 25 | |
askksa12543 | 0:7c7f4b30d64f | 26 | //output a clock pulse and data. |
askksa12543 | 0:7c7f4b30d64f | 27 | while(1) |
askksa12543 | 0:7c7f4b30d64f | 28 | { |
askksa12543 | 0:7c7f4b30d64f | 29 | //start timer for clock |
askksa12543 | 0:7c7f4b30d64f | 30 | t.start(); |
askksa12543 | 0:7c7f4b30d64f | 31 | //wait until the timer has reached the set time. |
askksa12543 | 0:7c7f4b30d64f | 32 | while(t.read_ms() < msecs) |
askksa12543 | 0:7c7f4b30d64f | 33 | { |
askksa12543 | 0:7c7f4b30d64f | 34 | //switch data just before clock goes high |
askksa12543 | 0:7c7f4b30d64f | 35 | if(!clock_pin && skew_flag && t.read_ms() > sksecs) |
askksa12543 | 0:7c7f4b30d64f | 36 | { |
askksa12543 | 0:7c7f4b30d64f | 37 | //switch data signal |
askksa12543 | 0:7c7f4b30d64f | 38 | serial_out = !serial_out; |
askksa12543 | 0:7c7f4b30d64f | 39 | skew_flag = 0; |
askksa12543 | 0:7c7f4b30d64f | 40 | } |
askksa12543 | 0:7c7f4b30d64f | 41 | } |
askksa12543 | 0:7c7f4b30d64f | 42 | //stop and reset the timer |
askksa12543 | 0:7c7f4b30d64f | 43 | t.stop(); |
askksa12543 | 0:7c7f4b30d64f | 44 | t.reset(); |
askksa12543 | 0:7c7f4b30d64f | 45 | //switch clock signal |
askksa12543 | 0:7c7f4b30d64f | 46 | clock_pin = !clock_pin; |
askksa12543 | 0:7c7f4b30d64f | 47 | //reset skew flag |
askksa12543 | 0:7c7f4b30d64f | 48 | skew_flag = 1; |
askksa12543 | 0:7c7f4b30d64f | 49 | } |
askksa12543 | 0:7c7f4b30d64f | 50 | } |