/* * 74HCT4094 shift register driver * * Copyright (C) 2013 Michael Buesch * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include "4094.h" #include #include /* Hardware pin configuration */ #define SR4094_DATA_PORT PORTB #define SR4094_DATA_DDR DDRB #define SR4094_DATA_BIT 2 #define SR4094_CLOCK_PORT PORTB #define SR4094_CLOCK_DDR DDRB #define SR4094_CLOCK_BIT 3 #define SR4094_STROBE_PORT PORTB #define SR4094_STROBE_DDR DDRB #define SR4094_STROBE_BIT 0 void sr4094_put_data(sr4094_t old_state, sr4094_t new_state) { sr4094_t mask = (sr4094_t)1 << (sizeof(sr4094_t) * 8 - 1); sr4094_t current_state = old_state; if (new_state == old_state) { /* Neue Daten sind bereits aktiv. */ return; } /* Daten in das Schieberegister laden. */ SR4094_STROBE_PORT &= ~(1 << SR4094_STROBE_BIT); do { current_state <<= 1; /* Datenpin setzen. */ if (new_state & mask) { SR4094_DATA_PORT |= (1 << SR4094_DATA_BIT); current_state |= 1; } else SR4094_DATA_PORT &= ~(1 << SR4094_DATA_BIT); /* Taktflanke ausgeben. */ SR4094_CLOCK_PORT |= (1 << SR4094_CLOCK_BIT); _NOP(); SR4094_CLOCK_PORT &= ~(1 << SR4094_CLOCK_BIT); mask >>= 1; } while (new_state != current_state); SR4094_STROBE_PORT |= (1 << SR4094_STROBE_BIT); } void sr4094_init(void) { /* Ports initialisieren. */ SR4094_DATA_PORT &= ~(1 << SR4094_DATA_BIT); SR4094_DATA_DDR |= (1 << SR4094_DATA_BIT); SR4094_CLOCK_PORT &= ~(1 << SR4094_CLOCK_BIT); SR4094_CLOCK_DDR |= (1 << SR4094_CLOCK_BIT); SR4094_STROBE_PORT |= (1 << SR4094_STROBE_BIT); SR4094_STROBE_DDR |= (1 << SR4094_STROBE_BIT); /* Alle Bits loeschen. */ sr4094_put_data((sr4094_t)-1ul, 0); }