NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » Control DIPSWITCH with 74HC156

October 19, 2011
by scottmc94
scottmc94's Avatar

(Note - This is my first attempt at using Eagle to make a schematic, and I am not a fancy coder. I am sure there are more elegant ways to do this.. But would love to hear critiques, especially if something doesn't make sense.)

base idea from !(http://wardyprojects.blogspot.com/2011/05/74hc165-piso-shift-register-arduino.html)

PURPOSE Design a project to not use many pins on the MCU to read a DIPSWITCH and display the settings on the LCD. The 74HC165 IC is an 8-Bit Parallel-In/Serial-Out Shift Register which was used to limit the number of pins required

SCHEMATIC

74HC165

CODE

    // Dipswitch and 74HC165 PISO test
    // NerdKit with ATmega328p
    // scottmc94

    #define F_CPU 14745600

    #include <avr/io.h>
    #include <inttypes.h>
    #include <avr/pgmspace.h>
    #include "../libnerdkits/lcd.h"
    #include "../libnerdkits/delay.h"
    #include "../libnerdkits/io_328p.h"

    // PIN DEFINITIONS:
    #define PIN_PL (1<<PC0) //pin 23 on atmega328p goes to pin 1 of 74HC165
    #define PIN_CP (1<<PC1) //pin 22 on atmega328p goes to pin 2 of 74HC165
    #define PIN_Q7 (1<<PC2) //pin 21 on atmega328p goes to pin 9 of 74HC165
    // GND to pin  8 of 74HC165
    // GND to pin 15 of 74HC165
    // +5v to pin 16 of 74HC165

    int CB(var,pos) {
        //Check bit to see if the dip switch is set
        int t;
        t = ((var) & (1<<(pos)));
        if (t > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    int main() {

        //set as output
        DDRC |= PIN_PL;
        DDRC |= PIN_CP;

        //set as input
        DDRC &= ~PIN_Q7;

        //set OUTPUT HIGH
        PORTC |= PIN_PL;

        //set OUPUT LOW
        PORTC &= ~PIN_CP;

        int i;
        int j;
        int value;

        //  Check the dipswitch 10 times to make sure stable
        for(i = 0 ; i < 10 ; i++)
        {
            //load logic bits from the DIP switches
            PORTC &= ~PIN_PL; //LOAD BITS
            //reset "load" line, this freezes the internal buffer on both chips
            PORTC |= PIN_PL;
            value = 0;

            for(j=0; j<8; j++)
            {
                if(PINC & PIN_Q7) {
                    // pin high set bit
                    value |= (1<<j);
                } else {
                    // pin low - do nothing
                }

                //read next "switch"
                PORTC |= PIN_CP;
                delay_ms(5);
                PORTC &= ~PIN_CP;
            }
        }
        delay_ms(200);
        lcd_init();
        FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
        lcd_home();
        lcd_write_string(PSTR("DIPSWITCH: "));
    // the chip reads the bits from 7-0,
    //so I changed the display to match the dip switch.
    fprintf_P(&lcd_stream, PSTR("%d%d%d%d%d%d%d%d"),
    CB(value,7),CB(value,6),CB(value,5),
    CB(value,4),CB(value,3),CB(value,2),
    CB(value,1),CB(value,0));

    while(1) {

    }

    return 0;
}

Thanks for looking at this.

Scott

October 20, 2011
by Ralphxyz
Ralphxyz's Avatar

Nice, thank you.

Ralph

October 20, 2011
by Ralphxyz
Ralphxyz's Avatar

Hey Scott, you should contribute this to the Nerdkit Community Library.

Probable in the Library/How Do I section.

Ralph

October 20, 2011
by scottmc94
scottmc94's Avatar

I thought about that since it was a pretty controlled and limited test, but I wanted to see if I didn't have any major flaws that others saw first :)

Scott

October 21, 2011
by Ralphxyz
Ralphxyz's Avatar

Well I like your schematic, I looked at the code but I am not a programmer so I can not help with a critique or optimization.

If it works I'd definitely like to see it in the Library (that way I can find it when I need it, which I will :-).

Ralph

October 21, 2011
by scottmc94
scottmc94's Avatar

I will look at doing that, I have never done a library post before.

Post a Reply

Please log in to post a reply.

Did you know that negative numbers are represented in two's complement notation in binary? Learn more...