Maker Pro
Maker Pro

Help Using 74HC594 Shift Register (Please)

I've been trying to interface a SN74HC594 chip to an AVR 2313 (using
avr-gcc)
and having a tough time with it.

I have the following function, but it appears as if only the MSB get's
set
every time it's called.


void write7SegChar(char displayValue) {

// Turn bit on PORTX |= BIT(x)
// Turn bit off PORTX &= ~BIT(x)
// Toggle bit PORTX ^= ~BIT(x)
//
// Load PB0 with serial value (MSB First)
// Pulse SCLK (PB1) with each value
// Pulse RCLK (PB2) after all eight bits are gone
//
// PB3 is RCLR
// PB4 is SCLR


PORTB &= ~(_BV(PB3) | _BV(PB4)); // bring RCLR & SCLR LOW for clear
PORTB |= (_BV(PB3) | _BV(PB4)); // bring high again

for (int i = 0; i < sizeof(char); i++) {
if((displayValue << i) & 0x80) {
PORTB |= _BV(PB0);
} else {
PORTB &= ~_BV(PB0);
}

PORTB |= _BV(PB1); // strobe SCLK
PORTB &= ~_BV(PB1);
}
PORTB |= _BV(PB2); // strobe RCLK
PORTB &= ~_BV(PB2);
return;
}

I have pull up resistors on all the clr and clock pins (PB1-PB4) so I
don't
think it's an open collector problem.

VCC is 5VDC, and the uP clock is at 4MHz so I shouldn't need any delay
when strobing clock or clear.

I can't see what I'm doing wrong here. Any suggestions are welcome.

Thanks,

Kevin
 
R

Rich Webb

I've been trying to interface a SN74HC594 chip to an AVR 2313 (using
avr-gcc)
and having a tough time with it.

I have the following function, but it appears as if only the MSB get's
set
every time it's called.


void write7SegChar(char displayValue) {

// Turn bit on PORTX |= BIT(x)
// Turn bit off PORTX &= ~BIT(x)
// Toggle bit PORTX ^= ~BIT(x)

Presuming that it's
#define BIT(a) (1 << (a))
shouldn't the toggle be "PORTX ^= BIT(x)"?
//
// Load PB0 with serial value (MSB First)
// Pulse SCLK (PB1) with each value
// Pulse RCLK (PB2) after all eight bits are gone
//
// PB3 is RCLR
// PB4 is SCLR


PORTB &= ~(_BV(PB3) | _BV(PB4)); // bring RCLR & SCLR LOW for clear
PORTB |= (_BV(PB3) | _BV(PB4)); // bring high again

for (int i = 0; i < sizeof(char); i++) {

I wouldn't go overboard on "portable-izing" the code with the sizeof
operator in an embedded app, since it's so wired to the underlying
hardware anyway.

In this case, the extra effort you took to make the code portable came
back and bit you -- remember that, by definition, sizeof(char) is 1.
 
That was the problem. Thank you!

The rub is I wasn't even trying to make it portable. That was
simply force of habit and not thinking it thru.

I really appreciate the second pair of eyes spotting the
obvious problem!

Thanks again.

Kevin
 
Top