Changes

Jump to navigation Jump to search
16,196 bytes added ,  15:58, 28 April 2014
Created page with "<div>#include <EEPROM.h></div><div><br/></div><div>// start reading from the first byte (address 0) of the EEPROM</div><div>int address = 0;</div><div><br/></div><div>//..."
<div>#include &lt;EEPROM.h&gt;</div><div><br/></div><div>// start reading from the first byte (address 0) of the EEPROM</div><div>int address = 0;</div><div><br/></div><div>// available modes</div><div>byte mode;</div><div>byte BITSI &nbsp; &nbsp; &nbsp;= 0;</div><div>byte BITSI_extend = 1;</div><div>boolean NewCode &nbsp; &nbsp; &nbsp; = false;</div><div><br/></div><div><br/></div><div>unsigned long TimeoutPulseLength = 10; &nbsp;/* PulseLength in ms &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</div><div>unsigned long TimeSend &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 0; &nbsp; /* Debounce interval in ms &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</div><div>unsigned long TimeToSendOut &nbsp; &nbsp; &nbsp;= 988;</div><div>unsigned long TimeoutPacingLED &nbsp; = 500; /* Onboard Pacing LED interval in ms */</div><div><br/></div><div>/* Reserved hardware wiring for enabling/disabling the HCT244 input buffer to I[8] */</div><div>int pinEnableBufIn = 13;</div><div><br/></div><div>/* Reserved hardware wiring for forcing 27KOhm pull-up / pull-down to I[8] */</div><div>int pinPullUpDown &nbsp;= 19;</div><div><br/></div><div>/* There are 8 associated input bits D10=pin10, D11=pin11, D12=pin12</div><div>&nbsp;* A0=pin14/AI0, A1=pin15/AI1 ,A2=pin16/AI2, A3=pin17/AI3, A4=pin18/AI3 */</div><div>//leonardo</div><div>//byte I[8] = { 10, 11, 19, 20, 21, 22, 23, 24 };</div><div>//uno</div><div>byte I[8] = { 10, 11, 12, 14, 15, 16, 17, 18};</div><div><br/></div><div>/* declare SerialInChr to receive the desired Parallel_Out value in */</div><div>byte SerialInChr = 1;</div><div>byte SerialOutChr;</div><div><br/></div><div>/* declare a time varable to remember the time when the Parallel_Out was set and to compare with TimeNow,</div><div>&nbsp;* this way Parallel_Out can be implemented as a pulse with a desireded fixed pulseLength */</div><div>unsigned long TimeOnsetOut = 0;</div><div><br/></div><div>/* declare bit state memory booleans to find out if input values have changed since the last looptest */</div><div>/*boolean State_I[8] &nbsp; &nbsp; &nbsp;= {&nbsp;</div><div>&nbsp; LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};</div><div>boolean Prev_State_I[8] = {&nbsp;</div><div>&nbsp; LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};*/</div><div>boolean State_I[8] &nbsp; &nbsp; &nbsp;= {&nbsp;</div><div>&nbsp; HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};</div><div>boolean Prev_State_I[8] = {&nbsp;</div><div>&nbsp; HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};</div><div><br/></div><div><br/></div><div><br/></div><div>void setup()</div><div>{</div><div>&nbsp; // configure input and output ports</div><div>&nbsp; OutPortOutputMode();</div><div>&nbsp; OutPortWrite(0);</div><div><br/></div><div>&nbsp; InPortInputMode();</div><div><br/></div><div>&nbsp; // configure onboard devices</div><div><br/></div><div>&nbsp; // enable input buffer</div><div>&nbsp; pinMode(pinEnableBufIn, OUTPUT);&nbsp;</div><div>&nbsp; digitalWrite(pinEnableBufIn, LOW);</div><div><br/></div><div>&nbsp; // enable pullup resistors</div><div>&nbsp; pinMode(pinPullUpDown, OUTPUT);</div><div>&nbsp; digitalWrite(pinPullUpDown, HIGH);</div><div><br/></div><div>&nbsp; // blink stim leds</div><div>&nbsp; /*SerialOutChr = B00000001;</div><div>&nbsp; for (byte i = 0; i < 9; i++) {</div><div>&nbsp; &nbsp;OutPortWrite(SerialOutChr);</div><div>&nbsp; &nbsp;SerialOutChr = SerialOutChr << 1;</div><div>&nbsp; &nbsp;delay(70);</div><div>&nbsp; &nbsp;}*/</div><div>&nbsp; // read a byte from the current address of the EEPROM</div><div>&nbsp; //mode = EEPROM.read(address);</div><div>&nbsp; mode = BITSI;</div><div><br/></div><div>&nbsp; // visual show mode to user</div><div>&nbsp; /*for (byte i = 0; i < 6; i++) {</div><div>&nbsp; &nbsp;OutPortWrite(mode);</div><div>&nbsp; &nbsp;delay(250);</div><div>&nbsp; &nbsp;OutPortWrite(0);</div><div>&nbsp; &nbsp;delay(150);</div><div>&nbsp; &nbsp;}</div><div>&nbsp; &nbsp;*/</div><div>&nbsp; // what mode are we running?</div><div>&nbsp; if (mode == BITSI) {</div><div>&nbsp; &nbsp; Serial.begin(115200);</div><div>&nbsp; &nbsp; while (!Serial) ;</div><div>&nbsp; &nbsp; Serial.println("BITSI mode, Ready!");</div><div>&nbsp; }</div><div>&nbsp; else if (mode == BITSI_extend) {</div><div>&nbsp; &nbsp; Serial.begin(115200);</div><div>&nbsp; &nbsp; while (!Serial) ;</div><div>&nbsp; &nbsp; Serial.println("BITSI_extend mode, Ready!");</div><div>&nbsp; }</div><div><br/></div><div>}</div><div><br/></div><div><br/></div><div>//--- Forever Loop method -----------------------------------------------------------------------------------------------------------</div><div><br/></div><div><br/></div><div>void loop()</div><div>{</div><div>&nbsp; byte index = 0;</div><div><br/></div><div>&nbsp; //--- buttonbox_stream ---------------------------------------------------------------------------------------------</div><div>&nbsp; /*if (mode == buttonbox_stream) {</div><div>&nbsp; &nbsp; /* check if data has been sent from the computer&nbsp;</div><div>&nbsp; &nbsp; if (Serial.available()) {</div><div>&nbsp; &nbsp; &nbsp; //--- read the most recent byte</div><div>&nbsp; &nbsp; &nbsp; SerialInChr = Serial.read();</div><div>&nbsp; &nbsp; &nbsp; OutPortWrite(SerialInChr);</div><div>&nbsp; &nbsp; &nbsp; NewCode = true;</div><div>&nbsp; &nbsp; }&nbsp;</div><div><br/></div><div>&nbsp; &nbsp; //--- sync every ms-----------------</div><div>&nbsp; &nbsp; while (micros () < TimeSend) {</div><div>&nbsp; &nbsp; }</div><div><br/></div><div>&nbsp; &nbsp; Serial.print(readInPort());</div><div>&nbsp; &nbsp; TimeSend = micros() + TimeToSendOut;</div><div>&nbsp; }&nbsp;</div><div><br/></div><div>&nbsp; //--- buttonbox_event ---------------------------------------------------------------------------------------------</div><div>&nbsp; else if (mode == buttonbox_event){</div><div>&nbsp; &nbsp; /* check if data has been sent from the computer&nbsp;</div><div>&nbsp; &nbsp; if (Serial.available()) {</div><div>&nbsp; &nbsp; &nbsp; //--- read the most recent byte</div><div>&nbsp; &nbsp; &nbsp; SerialInChr = Serial.read();</div><div>&nbsp; &nbsp; &nbsp; OutPortWrite(SerialInChr);</div><div>&nbsp; &nbsp; &nbsp; NewCode = true;</div><div>&nbsp; &nbsp; }&nbsp;</div><div><br/></div><div>&nbsp; &nbsp; //--- sync every ms-----------------</div><div>&nbsp; &nbsp; while (micros () < TimeSend) {</div><div>&nbsp; &nbsp; }</div><div><br/></div><div>&nbsp; &nbsp; /* send data if there is any change&nbsp;</div><div>&nbsp; &nbsp; if (SerialOutChr != readInPort()){</div><div>&nbsp; &nbsp; &nbsp; SerialOutChr = readInPort();</div><div>&nbsp; &nbsp; &nbsp; if (digitalRead(10) == 1){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Serial.write(0b00000001);</div><div>&nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; else if (digitalRead(11) == 1){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Serial.write(0b00000010);</div><div>&nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; else {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Serial.write(0b00000100);</div><div>&nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; TimeSend = micros() + TimeToSendOut;</div><div>&nbsp; }*/</div><div><br/></div><div>&nbsp; //--- BITSI ------------------------------------------------------------------------------------------------------</div><div>&nbsp; if (mode == BITSI){</div><div><br/></div><div>&nbsp; &nbsp; /* the debouncing or asking for the time takes to much processor time, events do not have the same time accuracy anymore</div><div>&nbsp; &nbsp; &nbsp;tested this with ms timing of the buttonbox_stream, when it was implemented &nbsp;ms timing was not reached*/</div><div>&nbsp; &nbsp; byte index = 0;</div><div>&nbsp; &nbsp; /* Process incoming serial characters</div><div>&nbsp; &nbsp; &nbsp;*</div><div>&nbsp; &nbsp; &nbsp;* The output bits are set immediately to reflect the value of the incoming serial character.</div><div>&nbsp; &nbsp; &nbsp;*/</div><div><br/></div><div>&nbsp; &nbsp; //--- sync every ms-----------------</div><div>&nbsp; &nbsp; while (micros () < TimeSend) {</div><div>&nbsp; &nbsp; }</div><div><br/></div><div>&nbsp; &nbsp; /* check if data has been sent from the computer */</div><div>&nbsp; &nbsp; if (Serial.available()) {</div><div>&nbsp; &nbsp; &nbsp; //--- read the most recent byte</div><div>&nbsp; &nbsp; &nbsp; SerialInChr = Serial.read();</div><div>&nbsp; &nbsp; &nbsp; //OutPortWrite(SerialInChr ^ 0b11111111);</div><div>&nbsp; &nbsp; &nbsp; OutPortWrite(SerialInChr);</div><div>&nbsp; &nbsp; &nbsp; NewCode = true;</div><div>&nbsp; &nbsp; }</div><div><br/></div><div>&nbsp; &nbsp; /* Process input bits</div><div>&nbsp; &nbsp; &nbsp;*</div><div>&nbsp; &nbsp; &nbsp;* The input bits are monitored for state changes. If bit 0 goes from low to high, a capital 'A' character</div><div>&nbsp; &nbsp; &nbsp;* is sent back to the PC over the serial line.</div><div>&nbsp; &nbsp; &nbsp;* When it changes back from high to low, a lowercase 'a' character is sent to the PC.</div><div>&nbsp; &nbsp; &nbsp;* For bits 0 to 7, the characters A-H and a-h are sent respectively.</div><div>&nbsp; &nbsp; &nbsp;*</div><div>&nbsp; &nbsp; &nbsp;* It is possible to connect mechanical switches to each of the input, because the input bits are debounced.</div><div>&nbsp; &nbsp; &nbsp;* After a bit changes state, it will be ignored for a debouncing interval of [TimeoutDebounce] miliseconds.</div><div>&nbsp; &nbsp; &nbsp;*/</div><div><br/></div><div>&nbsp; &nbsp; /* loop over the bits, to check their states */</div><div>&nbsp; &nbsp; for (index = 0; index < 8; index = index + 1) {</div><div>&nbsp; &nbsp; &nbsp; State_I[index] = digitalRead(I[index]);</div><div>&nbsp; &nbsp; &nbsp; /* check for bit state change = egde, but not within debouncing interval */</div><div>&nbsp; &nbsp; &nbsp; if (Prev_State_I[index] != State_I[index]) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; /* respond with the corresponding character */</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (State_I[index] == HIGH) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Serial.print(char(97 + index));</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; else {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Serial.print(char(65 + index));</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div><br/></div><div>&nbsp; &nbsp; &nbsp; &nbsp; /* save new previous bit state */</div><div>&nbsp; &nbsp; &nbsp; &nbsp; Prev_State_I[index] = State_I[index];</div><div>&nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; TimeSend = micros() + TimeToSendOut;</div><div><br/></div><div>&nbsp; }</div><div><br/></div><div><br/></div><div>&nbsp; //--- change mode ---------------------------------------------------------------------------------------------</div><div>&nbsp;//00x &nbsp; &nbsp;= buttonbox_stream</div><div>&nbsp; //000x &nbsp; = buttonbox_event</div><div>&nbsp; //0000x &nbsp;= BITSI</div><div>&nbsp; //00000x = personal implementation</div><div>&nbsp; &nbsp;/*if ((SerialInChr == 0) && (NewCode)){</div><div>&nbsp; &nbsp;NewCode = false;</div><div>&nbsp; &nbsp;change_mode++;</div><div>&nbsp; &nbsp;Serial.print(char(change_mode + 48));</div><div>&nbsp; &nbsp;}</div><div>&nbsp; &nbsp;else if ((change_mode > 1) && (SerialInChr != 0)) {</div><div>&nbsp; &nbsp;if (change_mode == 2) {</div><div>&nbsp; &nbsp;EEPROM.write(0, buttonbox_stream);</div><div>&nbsp; &nbsp;Serial.print("new mode: stream, reboot!");</div><div>&nbsp; &nbsp;setup();</div><div>&nbsp; &nbsp;}</div><div>&nbsp; &nbsp;else if (change_mode == 3) {</div><div>&nbsp; &nbsp;EEPROM.write(0, buttonbox_event);</div><div>&nbsp; &nbsp;Serial.print("new mode: event, reboot!");</div><div>&nbsp; &nbsp;setup();</div><div>&nbsp; &nbsp;}</div><div>&nbsp; &nbsp;else if (change_mode == 4) {</div><div>&nbsp; &nbsp;EEPROM.write(0, BITSI);</div><div>&nbsp; &nbsp;Serial.print("new mode: BITSI, reboot!");</div><div>&nbsp; &nbsp;setup();</div><div>&nbsp; &nbsp;}</div><div>&nbsp; &nbsp;else if (change_mode == 5) {</div><div>&nbsp; &nbsp;EEPROM.write(0, pers_impl);</div><div>&nbsp; &nbsp;Serial.print("new mode: personal, reboot!");</div><div>&nbsp; &nbsp;setup();</div><div>&nbsp; &nbsp;}</div><div>&nbsp; &nbsp;change_mode = 0;</div><div>&nbsp; &nbsp;}*/</div><div>}</div><div><br/></div><div><br/></div><div>// todo: implement writing to the input port</div><div>void InPortWrite(byte code)</div><div>{</div><div><br/></div><div>}</div><div><br/></div><div>void OutPortWrite(byte code)</div><div>{</div><div>&nbsp; byte b, d;</div><div><br/></div><div>&nbsp; // perform bit calculations before updating the ports</div><div>&nbsp; b = (code & B11000000) >> 6 | (PINB & B11111100);</div><div>&nbsp; d = (code & B00111111) << 2;</div><div><br/></div><div>&nbsp; // update the ports</div><div>&nbsp; PORTB = b;</div><div>&nbsp; PORTD = d;</div><div>}</div><div><br/></div><div>void OutPortOutputMode()</div><div>{</div><div>&nbsp; pinMode(2, OUTPUT);</div><div>&nbsp; pinMode(3, OUTPUT);</div><div>&nbsp; pinMode(4, OUTPUT);</div><div>&nbsp; pinMode(5, OUTPUT);</div><div>&nbsp; pinMode(6, OUTPUT);</div><div>&nbsp; pinMode(7, OUTPUT);</div><div>&nbsp; pinMode(8, OUTPUT);</div><div>&nbsp; pinMode(9, OUTPUT);</div><div>}</div><div><br/></div><div>void InPortOutputMode()</div><div>{</div><div>&nbsp; pinMode(10, OUTPUT);</div><div>&nbsp; pinMode(11, OUTPUT);</div><div>&nbsp; pinMode(12, OUTPUT);</div><div>&nbsp; pinMode(14, OUTPUT);</div><div>&nbsp; pinMode(15, OUTPUT);</div><div>&nbsp; pinMode(16, OUTPUT);</div><div>&nbsp; pinMode(17, OUTPUT);</div><div>&nbsp; pinMode(18, OUTPUT);</div><div>}</div><div><br/></div><div>void InPortInputMode()</div><div>{</div><div>&nbsp; // configure all the pins of the input port as inputs</div><div>&nbsp; pinMode(10, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(11, INPUT);</div><div>&nbsp; pinMode(12, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(14, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(15, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(16, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(17, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(18, INPUT);</div><div><br/></div><div>&nbsp; // disable internal pullup</div><div>&nbsp; digitalWrite(10, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(11, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(12, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(14, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(15, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(16, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(17, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(18, HIGH);</div><div>}</div><div><br/></div><div>void OutPortInputMode()</div><div>{</div><div>&nbsp; // configure all the pins of the output port as inputs ;-)</div><div>&nbsp; pinMode(2, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(3, INPUT);</div><div>&nbsp; pinMode(4, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(5, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(6, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(7, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(8, INPUT); &nbsp; &nbsp;</div><div>&nbsp; pinMode(9, INPUT);</div><div><br/></div><div>&nbsp; // enable internal pullup</div><div>&nbsp; digitalWrite(2, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(3, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(4, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(5, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(6, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(7, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(8, HIGH); &nbsp;</div><div>&nbsp; digitalWrite(9, HIGH);</div><div>}</div><div><br/></div><div><br/></div><div>byte readInPort()</div><div>{</div><div>&nbsp; byte b, c;</div><div><br/></div><div>&nbsp; b = PINB;</div><div>&nbsp; c = PINC;</div><div><br/></div><div>&nbsp; return (((b & B00011100) >> 2) | ((c & B00011111) &nbsp; &nbsp; &nbsp;<< 3)) ^ (B11111111);</div><div>&nbsp; /* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^ &nbsp;*/</div><div><br/></div><div>}</div><div><br/></div><div>byte readOutPort()</div><div>{</div><div>&nbsp; byte c, d;</div><div><br/></div><div>&nbsp; c = PINC;</div><div>&nbsp; d = PIND;</div><div><br/></div><div>&nbsp; return ((d & B11111100) >> 2) |</div><div>&nbsp; &nbsp; ((c & B00000011) << 6);</div><div>&nbsp; /* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^ &nbsp;*/</div><div><br/></div><div>}</div><div><br/></div>
Anonymous user

Navigation menu