Strange thing happens on Port D bit 4.

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

Hi there,
I would like to connect 6 IR sensor modules to an ATMega8, read the status of each sensor and send the result to PC using USART.
Those sensors were handmade and well tested (eg. dark surface = outputs '0', while white one outputs '1').
Six sensors were attached at PortD 7,6,5,4,3,2. USART at PortD 1,0 connected to PC using MAX232.
When all sensors had dark surface, the value should be '00000000' but the terminal says '00010000'.
Notice the '1' in bit 4. No mather how it always '1'.
I've logic probed the sensor at bit 4 again and it says '1' (the micro still attached). When i released the micro from socket, the sensor at bit 4 says '0'. Hmm, strange?! Again, the sensors were well tested again and still fine.
Another test, i released the sensors and attached the micro. I probed the PortD pins that used to be read the sensors. All says '0' except PortD bit 4 still says '1'. How can this would be happen??? I've changed with all Mega8's i've had (2 or 3 chips) and the result was same. Looks like the bit 4 love to be logic '1' :).
I overlooked several times at my code but still no clue. All i know from datasheet that PortD bit 4 is also labeled XCK/T0.
Please anyone let me know what happen? Thanks.
Here's my code:

#include 
#include 
#include 
#include 
#include 

void USART_Init( unsigned int baud )
{
	/* Set baud rate */
	UBRRH = (unsigned char)(baud>>8);
	UBRRL = (unsigned char)baud;
	/* Enable Receiver and Transmitter */
	UCSRB = (1<<RXEN)|(1<<TXEN);
	/* Set frame format: 8data, 2stop bit */
	UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

void USART_Transmit( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( !( UCSRA & (1<<UDRE)) )
		;
	/* Put data into buffer, sends the data */
	UDR = data;
}

uint8_t ir_sensor()
{
	uint8_t tmp;
	DDRD = 0;		//set as input
	PORTD = 0;		//no pull ups
	tmp=PIND & 0xFC;	//masking
	return tmp;
}
int main()
{
	DDRD = 0;		//set as input
	PORTD = 0;		//no pull ups
	
	timerInit();
	USART_Init(51);
	sei();
	
	while(1)
	{
		USART_Transmit(ir_sensor());
		timerPause(100);
	}
}

KISS - Keep It Simple Stupid!

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

A stupid question maybe, but have you checked electrical connections? If you're using a DIP, the D4 pin is located next to Vcc... Any chance for a short between the two pins?

Btw. have you shown all your code? You have enabled global interrupts, but no interrupt sources and there's no interrupt handlers.

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

Quote:
have you shown all your code?

That's my code. Global Interrupt Enable is used by timerPause(100); to run properly. The timerPause procedure uses sleep while the timer counts to make some delay so the current consumption is low. Short between bit 4 and VCC? Yes there were such possibilities, but i've checked. The micro were released from socket while the sensors still attached at the board. And i measure the logic of PortD bit 4. The result? it's good, '0' at dark and '1' at white. So i think it is not short circuit with VCC. Also, i tried to swap the sensor at that bit with another one, the result still the same.
One of my friend told me that maybe my sensor module can't make logic low with enough sinking current capability. Now i'm trying to fix that.

KISS - Keep It Simple Stupid!

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

You haven't listed all of your code (timerPause?) so this might be a silly question...

Is the USART somehow being setup in synchronous mode? It looks fine, but is it worth a shot to disable to USART and see what happens?

The alternate functions of PD4 are the first things I'd look into.

EDIT: What happens when you use your USART_tx routine to send a known value? if you send 0, do you get 0b00000000? do you have a free pin you can use to check the value of PD4 (with an led or something)?

I have too many hobbies.
s-conductor.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include  
...
   timerInit(); 

Why are you including a .c file and how is timerInit() defined?

Regards,
Steve A.

The Board helps those that help themselves.