Hi,
I want to address 8 lines and one way is to use a 74series 138 3-to-8 line decoder, thereby only needing
3 I/O pins. The requirement is to turn each output ON for a few msec, move to the next, and keep
going round and round indefinitely.
Could someone give me some pointers as to how best to do that?
Regards
bruce
Actually, if you don't want the output pins to "stutter" as you change the address pins, you need 4 pins. One for each of the address pins, and one for the enable (E3) pin. You need to set the enable pin LOW, change the address pins, then set the enable pin HIGH. See the truth table here. Insure you connect E1 and E2 to ground.
Or connect the 138 to 3 contiguous pins and use direct port writes so all the pins change at the same time.
As for the code, write to the pins, wait, repeat until the cows come home.
Rob
You can also avoid stutter by using a Gray code sequence
000
001
011
010
110
111
101
100
And re-arrange the output pin numbering to match.
The code to drive this is
#define S0 ... // address select bit 0
#define S1 ... // address select bit 1
#define S2 ... // address select bit 2
#define del ... // delay in ms
void setup ()
{
pinMode (S0, OUTPUT) ; digitalWrite (S0, LOW) ;
pinMode (S1, OUTPUT) ; digitalWrite (S1, LOW) ;
pinMode (S2, OUTPUT) ; digitalWrite (S2, LOW) ;
...
}
void sequence ()
{
delay (del) ;
digitalWrite (S0, HIGH) ; // change 000 to 001
delay (del) ;
digitalWrite (S1, HIGH) ; // change 001 to 011
delay (del) ;
digitalWrite (S0, LOW) ; // change 011 to 010
delay (del) ;
digitalWrite (S2, HIGH) ; // change 010 to 110
delay (del) ;
digitalWrite (S0, HIGH) ; // change 110 to 111
delay (del) ;
digitalWrite (S1, LOW) ; // change 111 to 101
delay (del) ;
digitalWrite (S0, LOW) ; // change 101 to 100
delay (del) ;
digitalWrite (S2, LOW) ; // change 100 to 000
}
Because you only change a single bit at a time there are no intermediate states, as long as you are happy to go in sequence and not skip a code, and poriet indicated the scanning would be in sequence.
Rob
Of course nothing is ever that simple - a 138 is not guaranteed to be free of race conditions
in the first place, but with Gray code input you reduce any transistion glitches to the shortest
time possible for that chip, probably 5--10ns or so for HC family.
The "stutter" from successive calls to digitalWrite is of the order of 4us, 3 orders of magnitude larger so my thoughts that Gray code could be useful trick.
If that 5--10ns proves problematical you would probably need some dead time as with the enable input.
Normally you would have a flipflops/latchs somewhere to clean up the transitions if it mattered,
Without know what the OP is trying to achieve its not really clear what is actually needed.
Hello all,
Thanks for the replies.
The Gray code idea is very clever and it took me a while to grasp how it was done.
The outputs will enable 8 digital displays ( 7-seg LEDs to begin with ).
I aim to refresh at 100 times a second ( is that a good rate? ) so each output will be ON for about 10msec.
A jitter of a few usec shouldn't be a problem.
I think I have enough now to write the code for this project.
thanks again
Bruce