/* Arduino Fiber Optics Example Code * Copyright 2010 SparkFun Electronics * Written by Hilary Hoops * This code is example code for sending a fiber optic signal at over 0.1Mbps. * This code runs though the ASCII tables starting with the character "!" and * increments every 1/2 second. The character is first encoded in bi-phase and * then start and stop frames are added. The data is shifted out over the * register for port D to acheive the necessary speed. So, only pins 0-7 can * be used without changing the code of course. */ //Example Character: K //Character Represented in Binary with Start/Stop Frames (start=16 0's, end 8 1's 8 0's) //00000000 00000000 01001011 11111111 00000000 //Biphase Encoded: 0010110010110101 //Biphase Encoded with Start/Stop Frames: //0011001100110011 0011001100110011 0010110010110101 0101010101010101 0011001100110011 const int out = 4; // Fiber Optic TX pin int BParray[16]; // BiPhase Encoded Character int OutputArray[80]; // BiPhase Encoded Character with Start/Stop Frames char input='!'; // Character to Send void setup() { Serial.begin(9600); pinMode(out,OUTPUT); } void loop() { // Encode the Character using BiPhase Encoding with Start/Stop Frames BiPhase(input); // Send out data for(int m=79; m>=0;m--){ PORTD = (OutputArray[m]<=0;m--){ Serial.print(OutputArray[m]); //print data sending for debug } */ delay(500); input++; //move to next character on ASCII table } void BiPhase(char x){ //Biphase encode the character //http://en.wikipedia.org/wiki/Biphase_mark_code char mychar= x; int a=0; int i=7; int j=15; while(i>=0){ int temp =bitRead(mychar,i); if (temp==1){ BParray[j]=bitRead(a,0); j--; a=~a; BParray[j]=bitRead(a,0); j--; a=~a; } else{ BParray[j]=bitRead(a,0); j--; BParray[j]=bitRead(a,0); j--; a=~a; } i--; } //choose headers for biphase encoded character //write to output array int ZeroLast[16]= {0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1}; int ZeroFirst[16]={1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0}; int OneLast[16]= {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0}; int OneFirst[16]= {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}; for(int m=79; m>63;m--){ if (BParray[15]==1) OutputArray[m]=ZeroLast[m-64]; else if(BParray[15]==0) OutputArray[m]=ZeroFirst[m-64]; } for(int m=63; m>47;m--){ if (BParray[15]==1) OutputArray[m]=ZeroLast[m-48]; else if(BParray[15]==0) OutputArray[m]=ZeroFirst[m-48]; } int k=15; for(int m=47; m>31;m--){ OutputArray[m]=BParray[k]; k--; } for(int m=31; m>15;m--){ if (BParray[0]==1) OutputArray[m]=OneLast[m-16]; else if(BParray[0]==0) OutputArray[m]=OneFirst[m-16]; } for(int m=15; m>=0;m--){ if (BParray[0]==1) OutputArray[m]=ZeroFirst[m]; else if(BParray[0]==0) OutputArray[m]=ZeroLast[m]; } }