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

Dependencies:   mbed

Committer:
n02655194
Date:
Sun Mar 29 03:54:41 2015 +0000
Revision:
6:4ef63169c970
Parent:
5:8c012e2c1ba8
Child:
7:aeeb441a68b8
functions requires testing

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
n02655194 6:4ef63169c970 18 int done = 0,sent = 0 ,i = 0, j = 128; //increment variables
n02655194 6:4ef63169c970 19 void send_byte(int byte);
askksa12543 0:7c7f4b30d64f 20
n02655194 6:4ef63169c970 21 void send_byte(int byte)
n02655194 6:4ef63169c970 22 {
n02655194 6:4ef63169c970 23 //not done(reset)
n02655194 6:4ef63169c970 24 done = 0;
n02655194 6:4ef63169c970 25
n02655194 6:4ef63169c970 26 //output byte
n02655194 6:4ef63169c970 27 while(!done) {
askksa12543 3:027d30718bbc 28 //start timer for clock
askksa12543 3:027d30718bbc 29 t.start();
askksa12543 3:027d30718bbc 30 //wait until the timer has reached the set time.
n02655194 6:4ef63169c970 31 while(t.read_ms() < msecs) {
askksa12543 3:027d30718bbc 32 //extract data just before clock goes high
n02655194 6:4ef63169c970 33 if(!clock_pin && skew_flag && t.read_ms() > sksecs) {
askksa12543 3:027d30718bbc 34 //extract data bit
n02655194 6:4ef63169c970 35 serial_out = (byte / j) % 2;
askksa12543 3:027d30718bbc 36 skew_flag = 0;
askksa12543 3:027d30718bbc 37 j /= 2; //decrement j to get to next bit location
askksa12543 3:027d30718bbc 38 }
askksa12543 3:027d30718bbc 39 }
askksa12543 3:027d30718bbc 40 //stop and reset the timer
askksa12543 3:027d30718bbc 41 t.stop();
askksa12543 3:027d30718bbc 42 t.reset();
askksa12543 3:027d30718bbc 43 //switch clock signal
askksa12543 3:027d30718bbc 44 clock_pin = !clock_pin;
askksa12543 3:027d30718bbc 45 //reset skew flag
askksa12543 3:027d30718bbc 46 skew_flag = 1;
askksa12543 3:027d30718bbc 47 //last preamble bit sent - reset/increment variables
n02655194 6:4ef63169c970 48 if(j==0) {
askksa12543 3:027d30718bbc 49 done = 1;
n02655194 6:4ef63169c970 50 j = 128;
askksa12543 5:8c012e2c1ba8 51 }
n02655194 6:4ef63169c970 52 }
n02655194 6:4ef63169c970 53 }
n02655194 6:4ef63169c970 54
n02655194 6:4ef63169c970 55 int main()
n02655194 6:4ef63169c970 56 {
askksa12543 5:8c012e2c1ba8 57
n02655194 6:4ef63169c970 58 //turn on red led to show programming has worked
n02655194 6:4ef63169c970 59 myled = 0;
n02655194 6:4ef63169c970 60 //initialize output pins
n02655194 6:4ef63169c970 61 clock_pin = 0;
n02655194 6:4ef63169c970 62 serial_out = 0;
n02655194 6:4ef63169c970 63 //skew flag
n02655194 6:4ef63169c970 64 skew_flag = 1;
n02655194 6:4ef63169c970 65 //set timers
n02655194 6:4ef63169c970 66 msecs = 1000;
n02655194 6:4ef63169c970 67 sksecs = 800;
n02655194 6:4ef63169c970 68
n02655194 6:4ef63169c970 69 //output preamble
n02655194 6:4ef63169c970 70 send_byte(pre);
n02655194 6:4ef63169c970 71 //output address
n02655194 6:4ef63169c970 72 send_byte(add);
n02655194 6:4ef63169c970 73 //output data
n02655194 6:4ef63169c970 74 i=0;
n02655194 6:4ef63169c970 75 while(!sent) {
n02655194 6:4ef63169c970 76 send_byte(data[i]);
n02655194 6:4ef63169c970 77 //finished sending data
n02655194 6:4ef63169c970 78 if(i>2) {
n02655194 6:4ef63169c970 79 sent = 1;
askksa12543 0:7c7f4b30d64f 80 }
n02655194 6:4ef63169c970 81 i=i+1;
askksa12543 0:7c7f4b30d64f 82 }
askksa12543 3:027d30718bbc 83 //turn off red led to show sending has finished
askksa12543 3:027d30718bbc 84 myled = 1;
askksa12543 1:4c0c28cc2b2c 85 }
askksa12543 1:4c0c28cc2b2c 86
askksa12543 4:39a777388acd 87 //crc = x^4+x+1
askksa12543 4:39a777388acd 88 //put char data into unsigned short temp variable, then shift << 5. % this number by the decimal equivalent of binary representation of
askksa12543 4:39a777388acd 89 //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 90 //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 91 // the crc decimal equivalent, if that equals 0, save the temp character in the permanent character array and receive the next bits.