Hello, I need to send a bits 0-7 of a byte using 8 pins of a 3.5 teensy, but since the pins are connected on a shared bus with an external device, i need to setup a tri-state logic, like this:
In the example, D0/D7 pins are 23/16 (A9/A2) pins of a 3.5 teensy (so they are analog pins).
Is this the correct approach? Thanks!
Code:
- set the 8 pins to INPUT mode to disable output
- wait for a certain event to happen
- read a byte
- "prepare" the byte data over 8 pins
- send the byte data setting the pins to OUTPUT MODE
- switch back to INPUT mode to disable output after a while
- loop to start
In the example, D0/D7 pins are 23/16 (A9/A2) pins of a 3.5 teensy (so they are analog pins).
Code:
#define D0_PIN 23
#define D1_PIN 22
#define D2_PIN 21
#define D3_PIN 20
#define D4_PIN 19
#define D5_PIN 18
#define D6_PIN 17
#define D7_PIN 16
void setup(){
[..] //init code not needed to show the problem
disableOutput(); //set all D0,D7 pins to INPUT mode
}
void loop() {
//wait for a certain condition to happen (address bus is stable)
while ((address_current = readAddressBus()) != address_previous){
address_previous = address_current;
}
if(cartEnabled == 1){
returnByte = rom_buffer[address_current];
//write the 8 bits to the pins set to INPUT mode
digitalWrite(D0_PIN,returnByte&1);
digitalWrite(D1_PIN,returnByte&2);
digitalWrite(D2_PIN,returnByte&4);
digitalWrite(D3_PIN,returnByte&8);
digitalWrite(D4_PIN,returnByte&16);
digitalWrite(D5_PIN,returnByte&32);
digitalWrite(D6_PIN,returnByte&64);
digitalWrite(D7_PIN,returnByte&128);
enableOutput(); //set all D0,D7 pins to OUTPUT mode to send the byte data
//wait until a new address is requested
while(readAddressBus() == address_current);
disableOutput(); //disable the output
}
}
inline void enableOutput(){
pinMode(D0_PIN,OUTPUT);
pinMode(D1_PIN,OUTPUT);
pinMode(D2_PIN,OUTPUT);
pinMode(D3_PIN,OUTPUT);
pinMode(D4_PIN,OUTPUT);
pinMode(D5_PIN,OUTPUT);
pinMode(D6_PIN,OUTPUT);
pinMode(D7_PIN,OUTPUT);
}
inline void disableOutput(){
pinMode(D0_PIN,INPUT);
pinMode(D1_PIN,INPUT);
pinMode(D2_PIN,INPUT);
pinMode(D3_PIN,INPUT);
pinMode(D4_PIN,INPUT);
pinMode(D5_PIN,INPUT);
pinMode(D6_PIN,INPUT);
pinMode(D7_PIN,INPUT);
}
unsigned int readAddressBus(){
a0 = digitalRead(A0_PIN);
a1 = digitalRead(A1_PIN);
a2 = digitalRead(A2_PIN);
a3 = digitalRead(A3_PIN);
a4 = digitalRead(A4_PIN);
a5 = digitalRead(A5_PIN);
a6 = digitalRead(A6_PIN);
a7 = digitalRead(A7_PIN);
a8 = digitalRead(A8_PIN);
a9 = digitalRead(A9_PIN);
a10 = digitalRead(A10_PIN);
a11 = digitalRead(A11_PIN);
cartEnabled = digitalRead(E_PIN);
return (a0 | a1 <<1 | a2<<2 | a3<<3 | a4<<4 | a5<<5 | a6<<6 | a7<<7 | a8<<8 | a9<<9 | a10<<10 | a11<<11)&0xFFF;
}
...
Is this the correct approach? Thanks!
Last edited: