Using FRDM Board to output data and a clock for communications example.

Dependencies:   mbed

Committer:
askksa12543
Date:
Mon Mar 23 14:20:12 2015 +0000
Revision:
5:8c012e2c1ba8
Parent:
4:39a777388acd
Child:
6:4ef63169c970
address

Who changed what in which revision?

UserRevisionLine numberNew 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 5:8c012e2c1ba8 9 #define ADDRESS 0x02 //address of 00000010
askksa12543 2:11f32d8cfa11 10
askksa12543 2:11f32d8cfa11 11 DigitalOut myled(LED1); // red led on board
askksa12543 2:11f32d8cfa11 12 DigitalOut clock_pin(D8), serial_out(D7); //send clock pulse and tx
askksa12543 0:7c7f4b30d64f 13 Timer t; //timer for controlling the clock and data skew
askksa12543 2:11f32d8cfa11 14 int msecs, sksecs; //clock time needed for data transfer and skew time
askksa12543 0:7c7f4b30d64f 15 int skew_flag; //skew flag for while loop
askksa12543 5:8c012e2c1ba8 16 unsigned char pre = PREAMBLE, add = ADDRESS;
askksa12543 3:027d30718bbc 17 unsigned char data[100] = "Hi!"; //data output
askksa12543 3:027d30718bbc 18 int done = 0, i = 0, j = 128; //increment variables
askksa12543 0:7c7f4b30d64f 19
askksa12543 0:7c7f4b30d64f 20 int main() {
askksa12543 0:7c7f4b30d64f 21
askksa12543 0:7c7f4b30d64f 22 //turn on red led to show programming has worked
askksa12543 0:7c7f4b30d64f 23 myled = 0;
askksa12543 0:7c7f4b30d64f 24 //initialize output pins
askksa12543 0:7c7f4b30d64f 25 clock_pin = 0;
askksa12543 0:7c7f4b30d64f 26 serial_out = 0;
askksa12543 0:7c7f4b30d64f 27 //skew flag
askksa12543 0:7c7f4b30d64f 28 skew_flag = 1;
askksa12543 0:7c7f4b30d64f 29 //set timers
askksa12543 5:8c012e2c1ba8 30 msecs = 1000;
askksa12543 5:8c012e2c1ba8 31 sksecs = 800;
askksa12543 0:7c7f4b30d64f 32
askksa12543 3:027d30718bbc 33 //output preamble
askksa12543 3:027d30718bbc 34 while(!done)
askksa12543 3:027d30718bbc 35 {
askksa12543 3:027d30718bbc 36 //start timer for clock
askksa12543 3:027d30718bbc 37 t.start();
askksa12543 3:027d30718bbc 38 //wait until the timer has reached the set time.
askksa12543 3:027d30718bbc 39 while(t.read_ms() < msecs)
askksa12543 3:027d30718bbc 40 {
askksa12543 3:027d30718bbc 41 //extract data just before clock goes high
askksa12543 3:027d30718bbc 42 if(!clock_pin && skew_flag && t.read_ms() > sksecs)
askksa12543 3:027d30718bbc 43 {
askksa12543 3:027d30718bbc 44 //extract data bit
askksa12543 3:027d30718bbc 45 serial_out = (pre / j) % 2;
askksa12543 3:027d30718bbc 46 skew_flag = 0;
askksa12543 3:027d30718bbc 47 j /= 2; //decrement j to get to next bit location
askksa12543 3:027d30718bbc 48 }
askksa12543 3:027d30718bbc 49 }
askksa12543 3:027d30718bbc 50 //stop and reset the timer
askksa12543 3:027d30718bbc 51 t.stop();
askksa12543 3:027d30718bbc 52 t.reset();
askksa12543 3:027d30718bbc 53 //switch clock signal
askksa12543 3:027d30718bbc 54 clock_pin = !clock_pin;
askksa12543 3:027d30718bbc 55 //reset skew flag
askksa12543 3:027d30718bbc 56 skew_flag = 1;
askksa12543 3:027d30718bbc 57 //last preamble bit sent - reset/increment variables
askksa12543 3:027d30718bbc 58 if(j==0)
askksa12543 3:027d30718bbc 59 {
askksa12543 3:027d30718bbc 60 done = 1;
askksa12543 3:027d30718bbc 61 j = 128;
askksa12543 3:027d30718bbc 62 }
askksa12543 3:027d30718bbc 63 }
askksa12543 3:027d30718bbc 64
askksa12543 3:027d30718bbc 65 //reset done
askksa12543 3:027d30718bbc 66 done = 0;
askksa12543 3:027d30718bbc 67
askksa12543 5:8c012e2c1ba8 68 //output address
askksa12543 5:8c012e2c1ba8 69 while(!done)
askksa12543 5:8c012e2c1ba8 70 {
askksa12543 5:8c012e2c1ba8 71 //start timer for clock
askksa12543 5:8c012e2c1ba8 72 t.start();
askksa12543 5:8c012e2c1ba8 73 //wait until the timer has reached the set time.
askksa12543 5:8c012e2c1ba8 74 while(t.read_ms() < msecs)
askksa12543 5:8c012e2c1ba8 75 {
askksa12543 5:8c012e2c1ba8 76 //extract data just before clock goes high
askksa12543 5:8c012e2c1ba8 77 if(!clock_pin && skew_flag && t.read_ms() > sksecs)
askksa12543 5:8c012e2c1ba8 78 {
askksa12543 5:8c012e2c1ba8 79 //extract data bit
askksa12543 5:8c012e2c1ba8 80 serial_out = (add / j) % 2;
askksa12543 5:8c012e2c1ba8 81 skew_flag = 0;
askksa12543 5:8c012e2c1ba8 82 j /= 2; //decrement j to get to next bit location
askksa12543 5:8c012e2c1ba8 83 }
askksa12543 5:8c012e2c1ba8 84 }
askksa12543 5:8c012e2c1ba8 85 //stop and reset the timer
askksa12543 5:8c012e2c1ba8 86 t.stop();
askksa12543 5:8c012e2c1ba8 87 t.reset();
askksa12543 5:8c012e2c1ba8 88 //switch clock signal
askksa12543 5:8c012e2c1ba8 89 clock_pin = !clock_pin;
askksa12543 5:8c012e2c1ba8 90 //reset skew flag
askksa12543 5:8c012e2c1ba8 91 skew_flag = 1;
askksa12543 5:8c012e2c1ba8 92 //last preamble bit sent - reset/increment variables
askksa12543 5:8c012e2c1ba8 93 if(j==0)
askksa12543 5:8c012e2c1ba8 94 {
askksa12543 5:8c012e2c1ba8 95 done = 1;
askksa12543 5:8c012e2c1ba8 96 j = 128;
askksa12543 5:8c012e2c1ba8 97 }
askksa12543 5:8c012e2c1ba8 98 }
askksa12543 5:8c012e2c1ba8 99
askksa12543 5:8c012e2c1ba8 100 //reset done
askksa12543 5:8c012e2c1ba8 101 done = 0;
askksa12543 5:8c012e2c1ba8 102
askksa12543 0:7c7f4b30d64f 103 //output a clock pulse and data.
askksa12543 3:027d30718bbc 104 while(!done)
askksa12543 0:7c7f4b30d64f 105 {
askksa12543 0:7c7f4b30d64f 106 //start timer for clock
askksa12543 0:7c7f4b30d64f 107 t.start();
askksa12543 0:7c7f4b30d64f 108 //wait until the timer has reached the set time.
askksa12543 0:7c7f4b30d64f 109 while(t.read_ms() < msecs)
askksa12543 0:7c7f4b30d64f 110 {
askksa12543 2:11f32d8cfa11 111 //extract data just before clock goes high
askksa12543 0:7c7f4b30d64f 112 if(!clock_pin && skew_flag && t.read_ms() > sksecs)
askksa12543 0:7c7f4b30d64f 113 {
askksa12543 2:11f32d8cfa11 114 //extract data bit
askksa12543 2:11f32d8cfa11 115 serial_out = (data[i] / j) % 2;
askksa12543 0:7c7f4b30d64f 116 skew_flag = 0;
askksa12543 2:11f32d8cfa11 117 j /= 2; //decrement j to get to next bit location
askksa12543 0:7c7f4b30d64f 118 }
askksa12543 0:7c7f4b30d64f 119 }
askksa12543 0:7c7f4b30d64f 120 //stop and reset the timer
askksa12543 0:7c7f4b30d64f 121 t.stop();
askksa12543 0:7c7f4b30d64f 122 t.reset();
askksa12543 0:7c7f4b30d64f 123 //switch clock signal
askksa12543 0:7c7f4b30d64f 124 clock_pin = !clock_pin;
askksa12543 0:7c7f4b30d64f 125 //reset skew flag
askksa12543 0:7c7f4b30d64f 126 skew_flag = 1;
askksa12543 2:11f32d8cfa11 127 //last bit sent - reset/increment variables
askksa12543 2:11f32d8cfa11 128 if(j==0)
askksa12543 2:11f32d8cfa11 129 {
askksa12543 2:11f32d8cfa11 130 j=128;
askksa12543 2:11f32d8cfa11 131 i++;
askksa12543 2:11f32d8cfa11 132 }
askksa12543 3:027d30718bbc 133 //finished sending data
askksa12543 3:027d30718bbc 134 if(i>2)
askksa12543 3:027d30718bbc 135 {
askksa12543 3:027d30718bbc 136 done = 1;
askksa12543 3:027d30718bbc 137 }
askksa12543 0:7c7f4b30d64f 138 }
askksa12543 3:027d30718bbc 139 //turn off red led to show sending has finished
askksa12543 3:027d30718bbc 140 myled = 1;
askksa12543 1:4c0c28cc2b2c 141 }
askksa12543 1:4c0c28cc2b2c 142
askksa12543 4:39a777388acd 143 //crc = x^4+x+1
askksa12543 4:39a777388acd 144 //put char data into unsigned short temp variable, then shift << 5. % this number by the decimal equivalent of binary representation of
askksa12543 4:39a777388acd 145 //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 146 //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 147 // the crc decimal equivalent, if that equals 0, save the temp character in the permanent character array and receive the next bits.