ATMega 8 internal pull-up and PINx

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am playing around with a servo and trying to operate it with an ATMega 8. I am writing some code that is intended to rotate the servo to (1) full left (-90 degrees), (2) center (0 degrees) or (3) full rithgt (+90 degrees) --- depending on the input on pins PC3, PC4 or PC5.

What I plan to do/happen is this:

  • Set up breadboard with the servo attached to pin PB1 (OCR1)
  • Configure pin PC3, PC4 and PC5 as inputs (with internal pull-ups enabled)
  • Pull down one of the input pins with a jumper (leaving the other 2 pins unattached)
  • Reset circuit to run code (my ATMega 8 is wired to circuit with a reset button attached to RESET)
  • If no input pin is pulled down, PC2 (configure as output) lights an LED to indicate no instruction to servo
The result of the above is to run code and rotate the servo depending on which input pin is pulled down when chip resets. The problem is that it doesn't work. :(

When I run the Simulator in AVR Studio, the values of pins PC3, PC4 and PC5 in PINC never indicate high. It was my understanding that PINC should reflect the last status of the pins on PORTC - and should have a "1" if outputted HIGH (where pin is output) or if input on pin was high (where pin is input). Since I set pins PC3, PC4 and PC5 to input and enabled their internal pull-ups, shouldn't bits 3, 4 and 5 of PINC have values of "1"?

So, if the value of PINC is not as anticipated above, the "while" loop doesn't work since the first conditional ("") always evaluates to "true." I tested code on circuit (woondering id Simulator was giving bad info) with no input pulled down - the result should have been that the LED attached to PC2 should have lit. It didn't - which I believe supports the conclusion that the values of PINC are not what I thought they would be.

Am I incorrect in my understanding of how my code should work with the ATMega 8? Or have I made an error in the code? Or is there some other error/problem that my ignorance is oblivious to?

Here is the Code (note code configuring Timer1 and PWM mode in not included):

#include 

//Declare and initialize some variables

int PinStatus; //hold input values on pins PC3, PC4 and PC5

//Declare and define main function

int main (void)
{

//configure pin PB1(OC1A) on Port B as output

	DDRB |= (1 << 1);

//configure pins PC3, PC4 and PC5 on Port C as input and enable internal pull-up resistors

	DDRC = 0;
	PORTC = (1 << 5) | (1 << 4) | (1 << 3);
	
//configure pin PC2 on Port C as output and initialize as low

	DDRC |= (1 << 2);
	
//Configure Timer1 and PWM Mode

//loop to read input values of PC3, PC4 and PC5, and then set duty cycle to operate servo

	while (1)
	{

//read status of Pin PC#, PC4 and PC5; result stored in "PinStatus"		

		PinStatus = PINC;
		
		if(~(PinStatus & 0x8)) //PC3 is low?
			{
				
				OCR1A = 0x258; //rotate to -90 degress (left)


			} //end if for PC3

		else if(~(PinStatus & 0x10)) //PC4 is low?
			{
				
				OCR1A = 0x5DC; //rotate to 0 degress (center)


			} //end elseif for PC4

		else if(~(PinStatus & 0x20)) //PC5 is low?
			{
				
				OCR1A = 0x960; //rotate to +90 degress (right)


			} //end elseif for PC5

		else  //no input pins low
			{

				PORTC |= (1 << 2); //no servo rotation attempted

			} //end else

	} //end while

} //end main

Thanks for any assistance.

Russ

Last Edited: Sat. Jul 11, 2015 - 12:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you have a sign extension problem.

int PinStatus;              // signed 16bit  
...
      PinStatus = PINC;     // assign unsigned 8 bit
                            // e.g. = 0x00FF for no key
...
      if(~(PinStatus & 0x8)) //PC3 is low?
                            // ~(0x0008) = 0xFFF7 TRUE
...

I prefer

      PinStatus = PINC;     // assign unsigned 8 bit
      if((PinStatus & 0x8) == 0) //PC3 is low?
...

I prefer working with positive logic most times. So why not:

      PinStatus = PINC ^ 0xFF;   // key down is high
      if((PinStatus & 0x8))      //PC3 is pressed?
...

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should at least use ! instead of ~, explanation can be found elsewhere or in any beginner C book.

Also, the emulator may not behave exactly like the real chip would, so you may have to actually set the PINC bits high yourself first. It runs code fine, but emulation of the peripherals may be incomplete, buggy or missing.

The emulator might just think that since you did not pull the PIN bits up, you want them to be pulled down. It does not know what you want or who is driving them.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you, David and Japael - what you have provided is very helpful. The concept that I had intended was the "!", but I misunderstood the use of the "~".

Japael wrote:
The emulator might just think that since you did not pull the PIN bits up, you want them to be pulled down. It does not know what you want or who is driving them.

I had assumed that the simulator would have known that the 3, 4 and 5 bits of PINC should be "High" since the internal pull-up resistors were set.

Russ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The emulator basically knows nothing about "pullup". Enabling a pullup does not make the pin high. Its just one of "those things".

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
The emulator basically knows nothing about "pullup". Enabling a pullup does not make the pin high. Its just one of "those things".

You mean it does not make the pin high for purposes of the emulator - but from a logic standpoint, it is high, correct?

Russ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have to set it high if that is what it is. Ugly but its what I do.

I set a break about the end of initialization and manually put those pins high. As far as the emulator is concerned, the pullups seem to have no effect.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gatoruss wrote:

You mean it does not make the pin high for purposes of the emulator - but from a logic standpoint, it is high, correct?

Think it in any way you like, like for example the emulator is 100% correct and does understands pull-ups, but the emulator just assumes at power-up that it has been told that button is driving the pin low, because nobody has told it the pin is high via pull-ups.

If you had a external pull down and pushing a button would pull the pin high, you would not be complaining about it because the emulator had been magically assuming your button hardware correctly at powerup.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I apologize, but I am confused.

I understand that the emulator doesn't know that that pull-ups make the pins high.

But for purposes of the actual code and the circuit, pins PC3, PC4 and PC 5 are high, right? For example, in my code if none of the pins were attached to ground and I were able to print out the value of PINC after start-up, it would be 00111000, correct?

Thank you for your patience in helping me work this thru.

Russ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

it would be 00111000, correct?

Not quite - it would be:

??111???

You can't predict what the non-pulled up, tristate, floating pins will read.

BTW the simulator is pretty hopeless so I wouldn't trust anything you see there bu what you could do is build the code for mega88 rather than mega8 then simulate that in simulator V2 which is much more likely to be accurate.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Quote:

it would be 00111000, correct?

Not quite - it would be:

??111???

You can't predict what the non-pulled up, tristate, floating pins will read.


Ok, I see...that makes sense to me. Thank you.

Russ

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which is why unconnected pins should be at least set as inputs with pull-ups on, or even set as outputs (low or high does not matter). But usually this is not a problem.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone.. I am working with atmega8A for a while and i have found a very dumb problem.. Every site, tutorials i have checked states that if you want to use any port pin as input then simply 0 the DDRXn bit and for using pulling up, 1 the PORTXn.. Then to check the input states use PINX register..

 

Now my problem is when i use PORTD0 as in put with enabled pull up.. The microcontroller just doesn' t repond.. My program is as follows:-

 

#define F_CPU 1000000

#include <avr/io.h>
#include<util/delay.h>

int main(void)
{
    DDRD = 0;
    DDRB = 255;
    PORTD = 255;
    PORTB = 0;

    while (1)
    {
        if (bit_is_clear(PIND,0))
        {
            PORTB = 255;
        }
         if (bit_is_set(PIND,0))
         {
             PORTB = 0;
             _delay_ms(50);
             PORTB = 255;
             _delay_ms(50);
         }
    }
}

 

In the above program, the microcontroller simply keeps running the second condition i.e. toggler of 50ms in case of PORTD0 is high.. Even if i pull down the D0 pin it just keeps running the toggler..

help me out in this.. Actually i was working on pwm of microcontroller and faced this issue so to check i developed this basic problem and here also the problem is same.. I tried in AVR studio 6.2 debugging in simulator mode.. Even there also, the input pins when written 1 doesn' t show any thing on PINx register.. For the only thing PINx register shows up is when the PORT is defined as OUTPUT.. If i check the status of PORTB during manual changing in debugging mode in PIND0 bit, there along with PORTB = 255, PINB = 255, but in case of doing PORTD(input) writing PORTD = 255 doesn't show PIND = 255.. For sure there is something that i don' t know or i am unaware about.. PLease help me if anyone knows about this problem.. 

Kunal.. :-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1) Do not cross post. This question is already being answered in another thread.

2) Do not hijack other peoples threads.

Regards,
Steve A.

The Board helps those that help themselves.

Topic locked