Digital pins read

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

Hello,

 

I would like to toggle LED on the xplainedpro board . The LED would toggle whenever it receives '1' as input. The below code doesnt work. May I know the reason. I provide '1' input from arduino's 3.3v output pin.

 

int main ()
 
 
{
	DDRD = 0x00; //input port
	DDRB = 0xFF; //output port
	//PORTD = 0x00;
	PORTB = 0x00;
	
	if (PORTD0 == 1) //0th pin of port D is connected with '1' tried PORTD=0x01 too ; when I equal it to 0 LED blinks (without the arduino input)
      while (1)
         {
	
			PORTB = 0xFF; //Turns ON All LEDs
			_delay_ms(100); //1 second delay
			PORTB = 0x00; //Turns OFF All LEDs
			_delay_ms(100); //1 second delay
		}
	}
}

 

This topic has a solution.
Last Edited: Thu. Apr 19, 2018 - 11:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Assuming Atmel Studio: PORTD0 will be defined as 0, so after preprocessing the if (PORTD0 == 1)is if(0 == 1) which will never be true.

You probably need something like if ((PIND & (1 << PIND0)) == 1)

or preferably just if (PIND & (1 << PIND0))

 

Edit: typo

Edit2: as pointed out by ki0bk in post #4, I changed PORTD to PIND blush

David (aka frog_jr)

Last Edited: Wed. Apr 18, 2018 - 04:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pika wrote:
PORTD0

What magic is being used to make this mean bit 0 of PORTD?

IOW how is PORTD0 defined?

 

I would use something like this:

if ((PIND & 0x01) == 1){
    .....
}

masking the bit from PIND , see tutorial topic  bit manipulation 101

 

Also

 

pika wrote:
_delay_ms(100); //1 second delay

How is this not a 100ms delay?

 

Jim

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

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

frog_jr wrote:
or preferably just if (PORTD & (1 << PORTD0))

Close but, PIN registers are used for input!    But you knew that smiley

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

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

ki0bk wrote:
Close but, PIN registers are used for input!
Oops, time for more coffee (will edit my post for future viewers).

David (aka frog_jr)

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

1 second delay of my comment is mistake. I thought we can directly access pins of any port by portxy and directly euating to 0 like in C. I do use Atmel studio

 

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

If you want to use something like "if (pind0)" and have it actually work I have a way to achieve that:

 

https://www.avrfreaks.net/forum/...

 

in that I define symbols such as:

#define pind_b1 pSFR->_PIND.bits.b1

which means you can then write code like:

if (pind_b1) { ...

However note that this is a very "non standard" way of writing AVR code and so you won't get a lot of help or see other examples. Doing it the "C standard" way:

if (PIND & (1 << 5)) {

or whatever the bit number is, is the way 99% of folks do it so you can get tons of help doing things like that.

Last Edited: Wed. Apr 18, 2018 - 04:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pika wrote:
can directly access pins of any port by portxy

Where did you read that?   AVR's use three registers for ports,  PORTx for output, PINx for input, DDRx to set the direction of a port pin.

Did you not get some warnings or errors when you compiled?

 

Jim

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

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

oh okay , but I dont need it specifically , I did not use the "PIN" for input. The follwing statement is already true:

if ((PIND & 0x01) == 1)

   Only when it is put to 0 and LOW signal is given ,the condition works

if ((PIND & 0x01) == 0)

Is it not possible to achieve it vice versa ? If PIN=0 condiiton FALSE and if PIN=1 condition true ? the internal pull-ups seems to be the concern here .

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

I do not remember where I read . No, there were no errors.

 

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Forget the ==1 or ==0 in any of that. The fact is that in a conditional like if() the C language treats 0 as "false" and anything else as "true". So say you wanted to read bit 5 of PIND then:

PIND & (1 << 5)

will actually return 0x20 if the input bit is high and 0x00 if it is low. But you don't need to use a test like:

if ((PIND & (1 << 5)) == (1 << 5)) {

because you can just use the fact that 0x20 is non-zero so it is "true". Therefore

if (PIND & (1 << 5)) {

is quite sufficient to make the test.

 

If you want it "upside down" - that is it executes the conditional code when then input is low/0 and not high/1 then you can just throw a NOT operator ('!') into that:

if (!(PIND & (1 << 5))) {

now it executes the conditional code when the bit is 0 and skips it when the input bit 5 is high (as might be the case if you use a switch with a pull-up which means it reads 1 when the button is not pressed and 0 when it is).

 

There is a thread in the tutorial forum called "Bit Manipulation 101" - it explains all this stuff - suggest you find / read it.

EDIT: that tutorial thread is here: https://www.avrfreaks.net/forum/...

Last Edited: Wed. Apr 18, 2018 - 04:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the clear explanation.!

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

Hello all,

 

One simple issue i currently noticed is ,the internal pull - ups cannot be deactivated ? why so? Im using xplained pro board ,

 

int main ()
 {
	
	DDRE = 0x00; //input direction
	DDRB = 0xFF; //output
	PORTE = 0x00; // porte = 0  

	
	  while (1)
	  {
		  if ((PINE & (1<<2))) 
		  //(PINE & (1<<2))
		  {
			  PORTB = 0xFF; //Turns ON All LEDs
			  _delay_ms(100); //  delay
			  PORTB = 0x00; //Turns OFF All LEDs
			  _delay_ms(100); //  delay
		  }  
		  _delay_ms(100);
	  };
}

 

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

pika wrote:
,the internal pull - ups cannot be deactivated ?
Nothing in the code you then show is making any attempt to enable a pull-up - so why are you surprised.

 

The code appears to be telling us that all 8 pins of port E are input and it's then clear that you are actually using bit 2. If so then to enable the pull-up on bit 2 of PORTE you would be using:

PORTE = (1 << 2); // enable pull up on pin 2

but what you actually have is:

PORTE = 0x00; // porte = 0  

But 0x00 is already the default state of that register and it means "all pull-ups disabled". The pull-ups are only enabled in the positions where you write 1 bits.

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

This one I expected to deactivate the pull ups ;

PORTE = 0x00;

But it is not actually deactivating the pull-ups. Only if I connect pine2 to ground the led does not blink, otherwise it keeps blinking. Something like this

 

https://electronics.stackexchange.com/questions/197671/pin-value-of-a-gpio-pin-set-as-high-impedance

 

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

the xplainedpro board

Which one?

 

The SE page you link to is for the STM32F4.  Is that what you have?  That's a competitor's product.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Ya , from the link I wanted to say that , board gets into high impedance state if pullups are deactivated. I have atmega256rfr2 xplained pro board , I dont know if it happens to my board or is genreal.

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

But it is not actually deactivating the pull-ups. Only if I connect pine2 to ground the led does not blink, otherwise it keeps blinking. Something like this

Tie PE2 to ground via a 1 mega-ohm resistor.  Does it still flash?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

If you disable internal pull ups you need to tie the pin to gnd via a resistor as joeymorin mentioned. This is because with no internal pull up the pin is left floating, ie it isn't at 0v nor is it at vcc.

Also you mentioned that you are using and arduino to provide the signal, did you remember to connect the ground plain of the 2 boards together?

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

Ya, grounds are connected but the pull-up pins need to be connected to a ground like the floating values, maybe i have to connect resistor to make the inpur pin '0'

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

I suggested a 1M external pull-down resistor so that you could confirm that the internal pull-up resistors were in fact disabled.  If they remained enabled, their lower value of about 35K would pull the pin high.  If they were disabled, the 1M external would be sufficient to pull the pin low.

 

Lesson:  A floating pin can take on any voltage/value at any time.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]