Skip to content

Firmware

Maik Wöhl edited this page May 17, 2020 · 3 revisions

I will cover some details of the firmware implementation on this page.

Interfacing a shift register

The 74HC595 is a really simple shift register that has a shift register stage and an output stage. So you can shift a value into the register and then output it on the bus without concerning about undesired behaviour on the bus.

According to the truth table above, we can easily shift the values into the register with SI (SDA) and SCK. With RCK the value is latched out.

A code for that would be this:

#define WAIT_MS 10  // wait for 10ms before clocking the next value.
int sr_push(uint8_t data) 
{
    for (uint8_t i = 0; i < sizeof(uint8_t)*8; ++i)
    {
        switch ((data>>i) & 1)
        {
            case 1:
                DATAPORT |= SDA;
                break;
            case 0:
            default:
                DATAPORT &= ~SDA;
                break;
        }
        CLKPORT |= SCK;
        _delay_ms( WAIT_MS );
        CLKPORT &= ~SCK;
    }
}

The 10ms time span before shifting the next value in is very big. But even a crappy oscilloscope can catch up with this.

Simplifcations

For simplifications I have put the main() function into the future.c file and implemented it like this:

extern int setup();
extern int loop();
int main(void)
{
    setup();
    while (1) { loop(); }
    return 1;
}

That makes me able to call setup() and loop in the main.c file:

int setup() 
{
    board_init();

    return 1;
}

int loop()
{
    PORTB |= (1<<PB2);
    _delay_ms(500);
    PORTB &= ~(1<<PB2);
    _delay_ms(500);

    return 0;
}
Clone this wiki locally