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.

Support Forum » Struggling w/ the dip switch calculator project.....

December 15, 2010
by mbobak
mbobak's Avatar

Hi all,

I was successful w/ the temp sensor project, blinking an LED, and even went ahead and combined the LED w/ the temp sensor, and added an LED warning light when temp exceeded a threshold. All that worked fine.

So, I pull the LED and the temp sensor off the board, I put in the bank of DIP switches. I wire the ON side (numbered side) of the switches to ground, and the other side to MCU pins 23-28, as instructed. I write the code, and even with all the switches off, I get 7, 7, 14 as output, i.e., all pins are reading high (on). Switching any of the switches has no effect. The values never change.

I'm pretty sure it's not a software problem, because the completed code that comes w/ the kit does the same thing my code does. It also sees all bits set high.

The wiring change here was trivial. Is there some silly, obvious wiring mistake that would cause everything to read high? I did initially make the mistake of wiring the the side opposite the MCU pins to +5V rail rather than ground. I corrected my mistake, and they all go to ground now, but, could that have damaged something? I'm thinking not, but, hey, what do I know? ;-)

Here's a pic of what it looks like: My current configuration

Anyone have any idea why all pins are reading a '1'??

Thanks!

-Mark

December 15, 2010
by bretm
bretm's Avatar

Connect pin 1 to Vcc to see if that changes anything. I'm a little surprised it works at all like that.

December 15, 2010
by mbobak
mbobak's Avatar

Sorry, still a total newbie here. Vcc is the +5V rail?

-Mark

December 16, 2010
by Rick_S
Rick_S's Avatar

Yes.

December 16, 2010
by mbobak
mbobak's Avatar

Hmm.....seems to have gone from bad to worse. I added a connection from MCU Pin 1 to Vcc, and now my display shows all pixels lit up. That is, a solid rectangle in every character position in every line of the display.

And, it remains that way if I pull out that wire that I just added from Pin 1 to Vcc.

-Mark

December 16, 2010
by Rick_S
Rick_S's Avatar

Also, you stated that " I write the code ". Did you actually write the code or use the code example provided in the code download? If you wrote or modified the code, if you could post it, that may help.

Rick

December 16, 2010
by Rick_S
Rick_S's Avatar

The solid black display sounds like the contrast resistor to the LCD got bumped out of place. Re-check the LCD wiring - especially the resistor tied to pin 3 of the LCD.

December 16, 2010
by mbobak
mbobak's Avatar

Ok, I must have had a loose wire somewhere. The display problem is resolved, display is back to normal.

However, the original issue still exists. I'm getting 7+7=14, all pins high, even though all DIP switches are currently off. Flipping the switches doesn't make any difference.

Oh, and adding the wire from Pin 1 to Vcc had zero impact, behavior is the same.

Any other thoughts, anyone?

-Mark

December 16, 2010
by Rick_S
Rick_S's Avatar

Did you write your own or modify the standard code sample?

December 16, 2010
by mbobak
mbobak's Avatar

And....now it's working. This is the part where I swear I didn't change anything....:-)

Ok, I figured out what was going on. There was a problem w/ my code. My code would read the initial state of the switches and display correctly. But then changes to the switches were not updating. When I compared to behavior of the provided code, I was careless, and assumed that it looked the same. A closer look, and test, with the provided code, showed that it did the right thing. At that point, a quick check of my program, and I spotted my bug and fixed it. So, it's working.

As to the whole Pin 1 to Vcc thing, I did originally have that on my board. At one point I removed it. My (possibly flawed) reasoning for doing so was as follows:

Rather than have to switch off, switch between program/run mode, and then switch on, I thought it would be cool to have a reset button, so I could switch between program and run mode, and just hit the reset button. So, I made my first attempt at reading (and understanding) the data sheet, I was thinking that Pin 1 didn't need to go to Vcc, as the pull-up resistor would take care of that pin being high, by default. Then, I put the pushbutton switch between Pin 1 and G. And, it seems to work.

So, it seems that Pin 1 to Vcc is not required for normal operation? And if you put a switch on Pin 1 to G, you have a reset button.

Anyhow, the last time I rearranged stuff, I pulled the reset button, and had not gotten around to putting it back on, so, that's how the board got into the state as it's pictured above.

Live and learn....

-Mark

data sheet. It sounded to me, like Pin 1 was the R ESET pin. After reading the datasheet, it seemed to indicate that if you take Pin 1 down to ground, that will cause a reboot. So, I removed the wire from Pin 1 to Vcc, and put in a connection from Pin 1 to pushbutton switch and from switch to G. I remembered reading about pull-up resistors, so, I assumed Pin 1 would be high by default, and go low when I pushed the switch. This did seem to work as I expected.

December 16, 2010
by mbobak
mbobak's Avatar

And....now it's working. This is the part where I swear I didn't change anything....:-)

Ok, I figured out what was going on. There was a problem w/ my code. My code would read the initial state of the switches and display correctly. But then changes to the switches were not updating. When I compared to behavior of the provided code, I was careless, and assumed that it looked the same. A closer look, and test, with the provided code, showed that it did the right thing. At that point, a quick check of my program, and I spotted my bug and fixed it. So, it's working.

As to the whole Pin 1 to Vcc thing, I did originally have that on my board. At one point I removed it. My (possibly flawed) reasoning for doing so was as follows:

Rather than have to switch off, switch between program/run mode, and then switch on, I thought it would be cool to have a reset button, so I could switch between program and run mode, and just hit the reset button. So, I made my first attempt at reading (and understanding) the data sheet, I was thinking that Pin 1 didn't need to go to Vcc, as the pull-up resistor would take care of that pin being high, by default. Then, I put the pushbutton switch between Pin 1 and G. And, it seems to work.

So, it seems that Pin 1 to Vcc is not required for normal operation? And if you put a switch on Pin 1 to G, you have a reset button.

Anyhow, the last time I rearranged stuff, I pulled the reset button, and had not gotten around to putting it back on, so, that's how the board got into the state as it's pictured above.

Live and learn....

-Mark

data sheet. It sounded to me, like Pin 1 was the R ESET pin. After reading the datasheet, it seemed to indicate that if you take Pin 1 down to ground, that will cause a reboot. So, I removed the wire from Pin 1 to Vcc, and put in a connection from Pin 1 to pushbutton switch and from switch to G. I remembered reading about pull-up resistors, so, I assumed Pin 1 would be high by default, and go low when I pushed the switch. This did seem to work as I expected.

December 16, 2010
by mbobak
mbobak's Avatar

Sorry for the botched editing job and double post above. Is there any way I can delete my own messages, or is that something only an admin can clear up?

Also, please ignore the incomplete block of text after my name.

Thanks,

-Mark

December 16, 2010
by mbobak
mbobak's Avatar

Rick,

Just noticed your comment about posting the code. It all works now, but I'll post anyways, as I took a quite different approach from what's in the example.

Here's the relevant section of my code:

  // declare the variables to represent each bit, of our two 3 bit numbers
  uint8_t a1;
  uint8_t a2;
  uint8_t a3;
  while(1) {

          // (PINC & (1<<PC0)) returns 8 bit number, 0's except position PA0 which is
          // the bit we want
          // shift it back by PA0 to put the bit we want in the 0th position.
        a1 = a2 = 0;
        a1 |= (PINC & (1<<PC0));
        a1 |= (PINC & (1<<PC1));
        a1 |= (PINC & (1<<PC2));
        a2 |= ((PINC & (1<<PC3)) >> PC3);
        a2 |= ((PINC & (1<<PC4)) >> PC3);
        a2 |= ((PINC & (1<<PC5)) >> PC3);

       a3 = a1 + a2;

          lcd_home();
        fprintf_P(&lcd_stream, PSTR("  %1d%1d%1d bin = %2d dec "),(a1&4)>>2,(a1&2)>>1,a1&1,a1);
        lcd_line_two();
        lcd_write_string(PSTR("+"));
        lcd_line_three();
        fprintf_P(&lcd_stream, PSTR("  %1d%1d%1d bin = %2d dec "),(a2&4)>>2,(a2&2)>>1,a2&1,a2);
        lcd_line_four();
        fprintf_P(&lcd_stream, PSTR("=%1d%1d%1d%1d bin = %2d dec "),(a3&8)>>3,(a3&4)>>2,(a3&2)>>1,a3&1,a3);
        lcd_write_string(PSTR(" "));

  }

Note that I don't use an entire byte for each bit. From a learning point of view, I can see how that may be easier to understand, but, particularly in a microcontroller environment where memory is at a premium, using one byte per bit seems a bit wasteful. I suppose I could have saved even more by putting all six bits into one byte, but that seemed excessive to me. :-)

Also, my code displays both the binary and decimal representation of both numbers, and their sum. I like it, because you can flip a switch and see a 0 change to 1 or 1 to 0, and also see the change in the decimal representation at the same time.

Anyhow, fun project.

Now it's time to get creative.....

-Mark

December 16, 2010
by Rick_S
Rick_S's Avatar

Mark, while having a reset button on pin 1 is a common thing, you don't want to leave pin 1 floating while the chip is in operation. You should add a "pullup resistor" to the circuit that would connect between VCC and pin1. A value of 10K for the resistor will work fine. That way, when you are operating as normal, the resistor will keep pin 1 high. When you press your reset button, the switch will pull pin 1 low.

December 16, 2010
by mbobak
mbobak's Avatar

Hi Rick,

Thanks again for the reply. Still trying to learn a lot here.

I thought the MCU had internal pull-up resistors on all the pins? Is that not the case for all the pins? If not, why does it work at all with the pin floating?

I'm at work at the moment, but I'll try it when I get home.

So, to be overly explicit, you're saying put a 10k resistor between pin 1 and Vcc. In addition, put the pushbutton switch between pin one and G? Then, when I press the pushbutton, I'll get a reset?

Thanks again,

-Mark

December 16, 2010
by bretm
bretm's Avatar

Another thing your code approach does is assume that PC1 is one bit left of PC0, PC2 is left of PC1, etc. I think that's a reasonable assumption, but once you make it it allows you to read the values this way:

a1 = (PINC >> PC3) & 0b111;

where "0b" indicates a binary constant.

December 17, 2010
by mbobak
mbobak's Avatar

Rick,

I've got the 10k pull-up resistor in there now, between PIN1 and Vcc. I also have a switch between PIN1 and G. When I press the button, I get a reset.

Bret,

Thanks for the pointer, you got me thinking. It's actually even simpler than that.

the "& 0b111" is not necessary.

I've adjusted my code, which now consists of:

        a1 = a2 = 0;
        a1 = PINC & 0b111;
        a2 = PINC >> PC3;
        a3 = a1 + a2;

Thanks guys, I'm slowly progressing....I think I mentioned before, I'm a C coder, so the programming part isn't an issue for me, but the electronics are a whole new world.

-Mark

Post a Reply

Please log in to post a reply.

Did you know that signed numbers need to be sign-extended when chaging variable sizes? Learn more...