I have a device programmable via an 8 bit digital parallel bus. I would like an Arduino to translate from a parallel 4 bit output of an existing device to 8 bit by intentionally reducing the resuloution. Unfortunately, I can't just use the most significant bits as I want to change the step size. The idea is to use an Arduino that ready the 4 digital input bits and then computs the necessary 8 bit output which it output to the 8 bit bus. However, this need to be as fast as 1 us. Is this possible with an Arduino? If so which board would you recommend? I guess I can't use the digitalWrite command as this will be too slow though. Any suggestions?
1 Answers
You can do this with an Uno, provided it has nothing else to do. I would
program it low-level, skipping the Arduino core since, as you said,
digitalWrite() would be too slow for this application.
Here is my proposed approach: read the 4-bit input from one port, use a look-up table to translate it to an 8-bit output, and write that output to another port. I would use PORTD (pins 0–7) for the output, as it is the only full 8-bit port on the Uno, then the first bits of PORTB (pins 8–11) for the input:
/* Translation look-up table (dummy example). */
const uint8_t lut[16] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
};
int main() {
DDRB = 0x00; // set port B as input
DDRD = 0xff; // set port D as output
for (;;) {
uint8_t input = PINB & 0x0f; // grab the lowest 4 bits
PORTD = lut[input]; // set the output
}
}
I compiled and disassembled this code to count the CPU cycles: the main loop takes 10 cycles per iteration. Given that the Uno is clocked at 16 MHz, this is one update of the output every 0.625 µs.
Edit: This is a straightforward approach. At the cost of some extra complexity, it could be optimized down to 7 cycles per iteration (0.438 µs), or even 6 cycles (0.375 µs) in assembly.
- 45,094
- 4
- 42
- 81