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

Dependencies:   mbed

main.cpp

Committer:
askksa12543
Date:
2015-03-15
Revision:
2:11f32d8cfa11
Parent:
1:4c0c28cc2b2c
Child:
3:027d30718bbc

File content as of revision 2:11f32d8cfa11:

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

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
char data[100] = "Hi!"; //data output
int 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 a clock pulse and data.
    while(i < 3)
    {
        //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++;
        }
    }
}

//crc = x^4+x+1