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.

Microcontroller Programming » Having problem in using PIN as an I/P

February 09, 2010
by banerjen
banerjen's Avatar

Hi,

I'm having a problem in using PC4 and PC5 as an I/P pin.

I used the code ->

PORTC &= ~((1<<PC4) | (1<<PC5));
uint8_t int1, int2;

while (1)
{
   inp1 = (PINC & (1<<PC4)) >> PC4;
   inp2 = (PINC & (1<<PC5)) >> PC5;

   if (inp1)
      // print inp1 value on the LCD

   if (inp2)
      // print inp2 value on the LCD
}

Now, the problem is that it is cycling between 1 and 0 for both the pins in the LCD irrespective of the input given to the PIN.

Nandan.

February 09, 2010
by Rick_S
Rick_S's Avatar

I'm assuming you're using a switch through a resistor to ground for your input or some other method of pulling the two pins you are checking low? If so, did you set your data direction registers (DDRC) and turn on the internal pullups? the code snippet below would do that for the two pins you want to check.

DDRC &= ~(1<<PC4); // set PC4 as input
DDRC &= ~(1<<PC5); // set PC5 as input
PORTC |= (1<<PC4); // turn on internal pull up resistor for PC4
PORTC |= (1<<PC5); // turn on internal pull up resistor for PC5

Otherwise, with my limited knowledge of C, the code (assuming you had something to write to the lcd in place of the comments) looks ok.

Rick

February 09, 2010
by banerjen
banerjen's Avatar

Hi Rick,

I used PB4 and PB5 using the same code and they are working perfectly as input pins. And no, I did enable the pull up resistors. After I write this code and put it in the MCU then without the application of any input the PIN is switching itself between 1 and 0.

I had done the addition using the DIP switches previously by enabling the pull ups and it worked fine for me.

I even changed the pins to PB2 (PC4) and PB3 (PC5) but to no avail.

Thanks. And if I could not make myself clear then please let me know.

Regards, Nandan.

February 09, 2010
by Rick_S
Rick_S's Avatar

That really sounds as if the internal pullups aren't on leaving the pin to float. However, what is really odd is that it still changes even when you ground the pin thru a resistor with the switch.

Is your circuit the same as the DIP switch arithmatic only with the switches going to PB4 and PB5?? Could you take and post an overhead photo of your circuit and maybe post more of the code so we can look it over better?

Right now I'm at a loss.

Rick

February 09, 2010
by banerjen
banerjen's Avatar

Hi Rick,

No, I am sorry. I actually did not enable the pull up resistors. But, earlier when I did the DIP arithmetic circuit I enabled it and it had worked fine then. Should I use the pull up resistors and try??? Cause in the NerdKits guide they gave an example of using the pin without enabling the pull ups. They just configured the PIN as an i/p pin and read the PINx register value to determine the status of the PIN value. I tried the same thing but it has not worked for me. Instead it is switching between 1 and 0 continuously.

But, now I am thinking of enabling the pull ups and using it the way I did on the DIP arithmetic circuit. But, I still can't think of any reason as to why the PIN is switching itself between 1 and 0 without the application of any signal!?

Thanks. I will send you a picture of my circuit soon. I don't have my camera with me right now.

Regards,

Nandan.

February 10, 2010
by Rick_S
Rick_S's Avatar

With the internal pullups off, the pin is floating. You can add your own external pullups by placing a 10k resistor from the pin to the 5v rail then connecting your switch from the pin to ground. This way when the switch is open, the pin is held high by the resistor and when the switch is closed the pin is pulled low by the switch. The resistor is in place to prevent a dead short to ground when you close the switch.

What confused me was when you said - - "Now, the problem is that it is cycling between 1 and 0 for both the pins in the LCD irrespective of the input given to the PIN. - - Even with the pullups off, when you connect the pin to ground, the state of the pin should be held low and the reading should not change. Of course that is if I was interpreting your statement correctly.

Rick

February 10, 2010
by banerjen
banerjen's Avatar

Hi Rick,

I enabled the pull ups and it is working fine. Guess that was the problem, it was probably floating between 1 and 0. Cause when I had given it a signal 1 it showed 1 and when I had connected it to the ground it was showing 0.

One question, why does it switch between 1 and 0 when it is floating? Why does it not settle for one particular value? And, why did two pins remain at zero when they were floating?

Thanks a lot.

Regards,

Nandan.

February 10, 2010
by Rick_S
Rick_S's Avatar

I'm actually not 100% sure but my guess is there are other internal factors that bring it above and below the threshold level thus toggling the bit.

I just try to make a point of always using either internal or external pullups when I'm checking pin state.

Rick

February 10, 2010
by Rick_S
Rick_S's Avatar

BTW, Glad to hear it's working for you!!

Rick

February 10, 2010
by banerjen
banerjen's Avatar

Hi Rick,

One more question. I used four input pins PB2 to PB5. Now, when I enabled the pull up resistors for the four of them and measured the potential at the terminals, I found that PB2 had 4.93 V, PB3 had something around 4.89 V, PB4 had about 3.29 V and PB5 had 0.68 V. So, only the pins PB2, PB3 and PB4 were at a high state. I then connected an external resistor from the 5 V line to the PIN PB5. And it worked fine. I was wondering that even after enabling the pull up resistors, why did the voltage differ like this. Is something wrong with my MCU unit. Because the last time I had used it for the DIP arithmetic circuit this arrangement had worked fine. Also, I am using four outputs to drive an L293D motor driver. Do you think that it has anything to do with this?

Anyway, thanks a lot for helping me out.

Nandan.

February 10, 2010
by Rick_S
Rick_S's Avatar

This is where things begin to elude me as well. I don't know exactly why that would be. I'm sure there are others here with more electronic background than my self taught experiences will provide.

February 12, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi banerjen,

I can't think of a good reason why you would be measuring such wonky voltages on your pins. When you turn on the pull up resistors you should measure 5V with a multimeter on the pin unless something explicitly driving the pin low. Are you sure the pins are set as input pins?

Humberto

February 13, 2010
by banerjen
banerjen's Avatar

Hi Humberto,

I know, I too am surprised. But, when I used an external pull-up resistor, it worked fine for me then. I had once accidentally used 12V directly in the MCU. Did it destroy the internal pull-ups. Because, everything else is working fine. All I need to do is use external pull-ups for inputs and external pull-downs for outputs.

And yes, the pins were set as input pins.

DDRx  &= ~(1<<Pxn);
PORTx |= (1<<Pxn);

Nandan.

Post a Reply

Please log in to post a reply.

Did you know that NerdKits believes in the importance of a mixture of meaningful topics, clear instruction, and engaging projects? Learn more...