Reading a port value

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
uint8_t set_magazine_size(uint8_t m_size){
	uint8_t temp=0;
	temp = PORTD;
	temp &= 0x18;	//clear unwanted PortD values 
                          only read (PD3 & PD4)
	switch(temp){
		case 0: m_size = 10;
		case 8:	m_size = 15;
		case 16: m_size = 20;
		case 24: m_size = 25;
		default : m_size = 5;
	}
	return m_size;
}

Can I do a direct read of a port value? Or is there another register used to read the current value of the port? Because this does not seem to be working, the value keeps getting set to default. PD3 and 4 are connected to a dip switch and I verified they being pulled low when I close the circuit, so its a software issue.

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

note that PORTD is an output control register, if you want to read the state of the inputs you need to read PIND.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

I bet that switch() will work a bit better when you add the break;s - remember that without break; it'll fall through.

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

Wow, I soooo forgot the breaks, that's what happens when you program at 5am after work and full time school and a fiancée, lol. BTW, I can only read one pin at a time with PIND can I not? I would think there would be a way to read an entire port, because in assembly you can directly read port values in PIC's(PORTx reflects the actual values of the PORT whether input or output unlike the data direction register TRISnx which is equivalent to DDRx for Atmel). I'd think Atmel would have that capability on their Mega's.

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

Quote:

I can only read one pin at a time with PIND can I not?

Err no, the usual complaint most people have is that you get 8 pins at a time with an 8bit wide register like PIND. Each time you read it you read all eight. The trick is to isolate only those bits you are interested in (hence the "101" thread in tutorial) but basically (in C) you do it with & masks such as:

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

to read the state of PD3 alone amongst all the PIND bits. In Asm you might be used to reading PIND (IN) and then ANDI'ing to pick out individual bits. In C the & just does the same thing. What's more, in Asm you may be used to "SBIS PIND,3" kind of tests, as it happens that line of C I gave above may boil down to this single instruction if the optimiser is in a happy mood.

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

Ok, I guess the naming scheme is a little misleading for me, I have read from individual pins in many instances such as this definition:

#define Bolt_Sensor (PINC&_BV(PINC5))

, however I never really understood why I had to use PINC&_BV, I thought it should have been PORTC&_BV. But PORTC is the control register, where PINC is the bit status register. I understand that clearly now. I guess its my fault for not having investigated why, I just used what I knew made it work without understanding how it worked. Thanks for the clear up. That helps a great deal.

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

What? You've never read Bit manipulation 101 ?

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

Quote:
But PORTC is the control register, where PINC is the bit status register.

No, PORTC is the output register (that also controls the internal pull-ups when the port is in input mode), and PINC is the input register. DDRC is the control register.
Quote:
You've never read Bit manipulation 101 ?

Or the datasheet?

Regards,
Steve A.

The Board helps those that help themselves.