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

Dependencies:   mbed

main.cpp

Committer:
askksa12543
Date:
2015-03-23
Revision:
4:39a777388acd
Parent:
3:027d30718bbc
Child:
5:8c012e2c1ba8

File content as of revision 4:39a777388acd:

//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

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;
unsigned char data[100] = "Hi!"; //data output
int done = 0, i = 0, j = 128; //increment variables

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 = 2000;
    sksecs = 1800;
    
    //output preamble
    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 = (pre / 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;
            j = 128;   
        }
    }    
    
    //reset done
    done = 0;

    //output a clock pulse and data.
    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 = (data[i] / 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 bit sent - reset/increment variables
        if(j==0)
        {
            j=128;
            i++;
        }
        //finished sending data
        if(i>2)
        {
            done = 1;    
        }
    }
    //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.