Changes
Jump to navigation
Jump to search
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 <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>// available modes</div><div>byte mode;</div><div>byte BITSI = 0;</div><div>byte BITSI_extend = 1;</div><div>boolean NewCode = false;</div><div><br/></div><div><br/></div><div>unsigned long TimeoutPulseLength = 10; /* PulseLength in ms */</div><div>unsigned long TimeSend = 0; /* Debounce interval in ms */</div><div>unsigned long TimeToSendOut = 988;</div><div>unsigned long TimeoutPacingLED = 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 = 19;</div><div><br/></div><div>/* There are 8 associated input bits D10=pin10, D11=pin11, D12=pin12</div><div> * 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> * 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] = { </div><div> LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};</div><div>boolean Prev_State_I[8] = { </div><div> LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};*/</div><div>boolean State_I[8] = { </div><div> HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};</div><div>boolean Prev_State_I[8] = { </div><div> 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> // configure input and output ports</div><div> OutPortOutputMode();</div><div> OutPortWrite(0);</div><div><br/></div><div> InPortInputMode();</div><div><br/></div><div> // configure onboard devices</div><div><br/></div><div> // enable input buffer</div><div> pinMode(pinEnableBufIn, OUTPUT); </div><div> digitalWrite(pinEnableBufIn, LOW);</div><div><br/></div><div> // enable pullup resistors</div><div> pinMode(pinPullUpDown, OUTPUT);</div><div> digitalWrite(pinPullUpDown, HIGH);</div><div><br/></div><div> // blink stim leds</div><div> /*SerialOutChr = B00000001;</div><div> for (byte i = 0; i < 9; i++) {</div><div> OutPortWrite(SerialOutChr);</div><div> SerialOutChr = SerialOutChr << 1;</div><div> delay(70);</div><div> }*/</div><div> // read a byte from the current address of the EEPROM</div><div> //mode = EEPROM.read(address);</div><div> mode = BITSI;</div><div><br/></div><div> // visual show mode to user</div><div> /*for (byte i = 0; i < 6; i++) {</div><div> OutPortWrite(mode);</div><div> delay(250);</div><div> OutPortWrite(0);</div><div> delay(150);</div><div> }</div><div> */</div><div> // what mode are we running?</div><div> if (mode == BITSI) {</div><div> Serial.begin(115200);</div><div> while (!Serial) ;</div><div> Serial.println("BITSI mode, Ready!");</div><div> }</div><div> else if (mode == BITSI_extend) {</div><div> Serial.begin(115200);</div><div> while (!Serial) ;</div><div> Serial.println("BITSI_extend mode, Ready!");</div><div> }</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> byte index = 0;</div><div><br/></div><div> //--- buttonbox_stream ---------------------------------------------------------------------------------------------</div><div> /*if (mode == buttonbox_stream) {</div><div> /* check if data has been sent from the computer </div><div> if (Serial.available()) {</div><div> //--- read the most recent byte</div><div> SerialInChr = Serial.read();</div><div> OutPortWrite(SerialInChr);</div><div> NewCode = true;</div><div> } </div><div><br/></div><div> //--- sync every ms-----------------</div><div> while (micros () < TimeSend) {</div><div> }</div><div><br/></div><div> Serial.print(readInPort());</div><div> TimeSend = micros() + TimeToSendOut;</div><div> } </div><div><br/></div><div> //--- buttonbox_event ---------------------------------------------------------------------------------------------</div><div> else if (mode == buttonbox_event){</div><div> /* check if data has been sent from the computer </div><div> if (Serial.available()) {</div><div> //--- read the most recent byte</div><div> SerialInChr = Serial.read();</div><div> OutPortWrite(SerialInChr);</div><div> NewCode = true;</div><div> } </div><div><br/></div><div> //--- sync every ms-----------------</div><div> while (micros () < TimeSend) {</div><div> }</div><div><br/></div><div> /* send data if there is any change </div><div> if (SerialOutChr != readInPort()){</div><div> SerialOutChr = readInPort();</div><div> if (digitalRead(10) == 1){</div><div> Serial.write(0b00000001);</div><div> }</div><div> else if (digitalRead(11) == 1){</div><div> Serial.write(0b00000010);</div><div> }</div><div> else {</div><div> Serial.write(0b00000100);</div><div> }</div><div> }</div><div> TimeSend = micros() + TimeToSendOut;</div><div> }*/</div><div><br/></div><div> //--- BITSI ------------------------------------------------------------------------------------------------------</div><div> if (mode == BITSI){</div><div><br/></div><div> /* the debouncing or asking for the time takes to much processor time, events do not have the same time accuracy anymore</div><div> tested this with ms timing of the buttonbox_stream, when it was implemented ms timing was not reached*/</div><div> byte index = 0;</div><div> /* Process incoming serial characters</div><div> *</div><div> * The output bits are set immediately to reflect the value of the incoming serial character.</div><div> */</div><div><br/></div><div> //--- sync every ms-----------------</div><div> while (micros () < TimeSend) {</div><div> }</div><div><br/></div><div> /* check if data has been sent from the computer */</div><div> if (Serial.available()) {</div><div> //--- read the most recent byte</div><div> SerialInChr = Serial.read();</div><div> //OutPortWrite(SerialInChr ^ 0b11111111);</div><div> OutPortWrite(SerialInChr);</div><div> NewCode = true;</div><div> }</div><div><br/></div><div> /* Process input bits</div><div> *</div><div> * The input bits are monitored for state changes. If bit 0 goes from low to high, a capital 'A' character</div><div> * is sent back to the PC over the serial line.</div><div> * When it changes back from high to low, a lowercase 'a' character is sent to the PC.</div><div> * For bits 0 to 7, the characters A-H and a-h are sent respectively.</div><div> *</div><div> * It is possible to connect mechanical switches to each of the input, because the input bits are debounced.</div><div> * After a bit changes state, it will be ignored for a debouncing interval of [TimeoutDebounce] miliseconds.</div><div> */</div><div><br/></div><div> /* loop over the bits, to check their states */</div><div> for (index = 0; index < 8; index = index + 1) {</div><div> State_I[index] = digitalRead(I[index]);</div><div> /* check for bit state change = egde, but not within debouncing interval */</div><div> if (Prev_State_I[index] != State_I[index]) {</div><div> /* respond with the corresponding character */</div><div> if (State_I[index] == HIGH) {</div><div> Serial.print(char(97 + index));</div><div> }</div><div> else {</div><div> Serial.print(char(65 + index));</div><div> }</div><div><br/></div><div> /* save new previous bit state */</div><div> Prev_State_I[index] = State_I[index];</div><div> }</div><div> }</div><div> TimeSend = micros() + TimeToSendOut;</div><div><br/></div><div> }</div><div><br/></div><div><br/></div><div> //--- change mode ---------------------------------------------------------------------------------------------</div><div> //00x = buttonbox_stream</div><div> //000x = buttonbox_event</div><div> //0000x = BITSI</div><div> //00000x = personal implementation</div><div> /*if ((SerialInChr == 0) && (NewCode)){</div><div> NewCode = false;</div><div> change_mode++;</div><div> Serial.print(char(change_mode + 48));</div><div> }</div><div> else if ((change_mode > 1) && (SerialInChr != 0)) {</div><div> if (change_mode == 2) {</div><div> EEPROM.write(0, buttonbox_stream);</div><div> Serial.print("new mode: stream, reboot!");</div><div> setup();</div><div> }</div><div> else if (change_mode == 3) {</div><div> EEPROM.write(0, buttonbox_event);</div><div> Serial.print("new mode: event, reboot!");</div><div> setup();</div><div> }</div><div> else if (change_mode == 4) {</div><div> EEPROM.write(0, BITSI);</div><div> Serial.print("new mode: BITSI, reboot!");</div><div> setup();</div><div> }</div><div> else if (change_mode == 5) {</div><div> EEPROM.write(0, pers_impl);</div><div> Serial.print("new mode: personal, reboot!");</div><div> setup();</div><div> }</div><div> change_mode = 0;</div><div> }*/</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> byte b, d;</div><div><br/></div><div> // perform bit calculations before updating the ports</div><div> b = (code & B11000000) >> 6 | (PINB & B11111100);</div><div> d = (code & B00111111) << 2;</div><div><br/></div><div> // update the ports</div><div> PORTB = b;</div><div> PORTD = d;</div><div>}</div><div><br/></div><div>void OutPortOutputMode()</div><div>{</div><div> pinMode(2, OUTPUT);</div><div> pinMode(3, OUTPUT);</div><div> pinMode(4, OUTPUT);</div><div> pinMode(5, OUTPUT);</div><div> pinMode(6, OUTPUT);</div><div> pinMode(7, OUTPUT);</div><div> pinMode(8, OUTPUT);</div><div> pinMode(9, OUTPUT);</div><div>}</div><div><br/></div><div>void InPortOutputMode()</div><div>{</div><div> pinMode(10, OUTPUT);</div><div> pinMode(11, OUTPUT);</div><div> pinMode(12, OUTPUT);</div><div> pinMode(14, OUTPUT);</div><div> pinMode(15, OUTPUT);</div><div> pinMode(16, OUTPUT);</div><div> pinMode(17, OUTPUT);</div><div> pinMode(18, OUTPUT);</div><div>}</div><div><br/></div><div>void InPortInputMode()</div><div>{</div><div> // configure all the pins of the input port as inputs</div><div> pinMode(10, INPUT); </div><div> pinMode(11, INPUT);</div><div> pinMode(12, INPUT); </div><div> pinMode(14, INPUT); </div><div> pinMode(15, INPUT); </div><div> pinMode(16, INPUT); </div><div> pinMode(17, INPUT); </div><div> pinMode(18, INPUT);</div><div><br/></div><div> // disable internal pullup</div><div> digitalWrite(10, HIGH); </div><div> digitalWrite(11, HIGH); </div><div> digitalWrite(12, HIGH); </div><div> digitalWrite(14, HIGH); </div><div> digitalWrite(15, HIGH); </div><div> digitalWrite(16, HIGH); </div><div> digitalWrite(17, HIGH); </div><div> digitalWrite(18, HIGH);</div><div>}</div><div><br/></div><div>void OutPortInputMode()</div><div>{</div><div> // configure all the pins of the output port as inputs ;-)</div><div> pinMode(2, INPUT); </div><div> pinMode(3, INPUT);</div><div> pinMode(4, INPUT); </div><div> pinMode(5, INPUT); </div><div> pinMode(6, INPUT); </div><div> pinMode(7, INPUT); </div><div> pinMode(8, INPUT); </div><div> pinMode(9, INPUT);</div><div><br/></div><div> // enable internal pullup</div><div> digitalWrite(2, HIGH); </div><div> digitalWrite(3, HIGH); </div><div> digitalWrite(4, HIGH); </div><div> digitalWrite(5, HIGH); </div><div> digitalWrite(6, HIGH); </div><div> digitalWrite(7, HIGH); </div><div> digitalWrite(8, HIGH); </div><div> digitalWrite(9, HIGH);</div><div>}</div><div><br/></div><div><br/></div><div>byte readInPort()</div><div>{</div><div> byte b, c;</div><div><br/></div><div> b = PINB;</div><div> c = PINC;</div><div><br/></div><div> return (((b & B00011100) >> 2) | ((c & B00011111) << 3)) ^ (B11111111);</div><div> /* ^^^^^^^^ */</div><div><br/></div><div>}</div><div><br/></div><div>byte readOutPort()</div><div>{</div><div> byte c, d;</div><div><br/></div><div> c = PINC;</div><div> d = PIND;</div><div><br/></div><div> return ((d & B11111100) >> 2) |</div><div> ((c & B00000011) << 6);</div><div> /* ^^^^^^^^ */</div><div><br/></div><div>}</div><div><br/></div>