[SOLVED] Atmega328 IO PORT Bit Access

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

I'm having some problems with Atmega328 port. I'm bit banging onewire bus on one of the pins of PORTB I have some other device connected to this port as well. From time to time it seems that the onewire device is interrupted by the other devices activity. I suspect it is related with the way I manipulate the pins or with the PORT itself. When I place the OneWire bus on a completely different PORT, not used by any other device, everything seems to work fine.

I have two questions.

1. Does rewriting even the same valueto the PORT have some physical implications on the voltage on the pins whatsoever ?

2. I created a gpio "abstraction" layer to manipulate the pins. The code is pretty straight forward but just to be sure could you guys take a look on it ?


typedef struct _gpio_pin {
	volatile uint8_t *port;
	uint8_t pin;
} gpio_pin;


#define GPIO_CONFIGURE_AS_OUTPUT(__gpio) \
	*(GET_DDRX_FROM_PORTX((__gpio)->port)) |= _BV((__gpio)->pin)
	

#define GPIO_CONFIGURE_AS_INPUT(__gpio) \
	*(GET_DDRX_FROM_PORTX((__gpio)->port)) &= ~_BV((__gpio)->pin)


#define GPIO_GET(__gpio) \
	(*(GET_PINX_FROM_PORTX((__gpio)->port)) & _BV((__gpio)->pin))


#define GPIO_SET_LOW(__gpio) \
	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { \
		(*(__gpio)->port) &= ~_BV((__gpio)->pin); \
	}


#define GPIO_SET_HIGH(__gpio) \
	ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { \
		(*(__gpio)->port) |= _BV((__gpio)->pin); \
	}

A single GPIO is declared the following way

gpio_pin pinx;
pinx.port = &PORTB;
pinx.pin = 3;

... and used

GPIO_CONFIGURE_AS_OUTPUT(&pinx);
GPIO_SET_LOW(&pinx);
GPIO_SET_HIGH(&pinx);
Last Edited: Fri. Dec 20, 2013 - 04:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys, problem can be marked as solved. I had a timer output compare enabled on the same pin which was interfering with the OW bus. My bad.

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

dagon666 wrote:
Hi guys, problem can be marked as solved. I had a timer output compare enabled on the same pin which was interfering with the OW bus. My bad.

You can edit your first post and place a [Solved] in front of the title

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown