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

Dependencies:   mbed

main.cpp

Committer:
askksa12543
Date:
2015-04-02
Revision:
7:aeeb441a68b8
Parent:
6:4ef63169c970
Child:
8:2b61de165543

File content as of revision 7:aeeb441a68b8:

//project to include preamble, addressing, control, error control.
//currently fixed size data field but doesn't have to be. (post amble if not fixed size)

#include "mbed.h"
#include "stdio.h"

#define MAX 100 //set the size of the character data storage array
#define PREAMBLE 0x7E //preamble of 01111110
#define ADDRESS 0x02 //address of 00000010

DigitalOut myled(LED1); // red led on board
DigitalOut clock_pin(D8), serial_out(D7); //send clock pulse and tx
Timer t; //timer for controlling the clock and data skew
int msecs, sksecs; //clock time needed for data transfer and skew time
int skew_flag; //skew flag for while loop
unsigned char pre = PREAMBLE, add = ADDRESS;
unsigned char data[100] = "Hi!"; //data output
int done = 0,sent = 0 ,i = 0, j = 0; //increment variables
void send_byte(int byte);

void send_byte(int byte)
{
    //not done(reset)
    done = 0;
    j = 128;

    //output byte
    while(!done) {
        //start timer for clock
        t.start();
        //wait until the timer has reached the set time.
        while(t.read_ms() < msecs) {
            //extract data just before clock goes high
            if(!clock_pin && skew_flag && t.read_ms() > sksecs) {
                //extract data bit
                serial_out = (byte / j) % 2;
                skew_flag = 0;
                j /= 2; //decrement j to get to next bit location
            }
        }
        //stop and reset the timer
        t.stop();
        t.reset();
        //switch clock signal
        clock_pin = !clock_pin;
        //reset skew flag
        skew_flag = 1;
        //last preamble bit sent - reset/increment variables
        if(j==0) {
            done = 1;
        }
    }
}

int main()
{

    //turn on red led to show programming has worked
    myled = 0;
    //initialize output pins
    clock_pin = 0;
    serial_out = 0;
    //skew flag
    skew_flag = 1;
    //set timers
    msecs = 1000;
    sksecs = 800;

    //output preamble
    send_byte(pre);
    //output address
    send_byte(add);
    //output data
    i=0;
    while(!sent) {
        send_byte(data[i]);
        //finished sending data
        if(i>2) {
            sent = 1;
        }
        i++;
    }
    //turn off red led to show sending has finished
    myled = 1;
}

//crc = x^4+x+1
//put char data into unsigned short temp variable, then shift << 5. % this number by the decimal equivalent of binary representation of
//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
//bytes in a temp variable - after receiving 13 bits stick data in unsigned short variable << 5 add the recieved 5 bits and then % by
// the crc decimal equivalent, if that equals 0, save the temp character in the permanent character array and receive the next bits.