ATMega32 digitalRead and write trouble

I've managed to get an Atmega32 bootloaded with the Arduino bootloader and the blink program works on a breadboard. I've updated the core files including pins_arduino.c, I've also updated boards.txt too. The trouble is I can't read or write on analogue pins PA0 - PA7. digitalWrite seems OK with the other pin but when using digitalRead with this code

for (int i=0; i<31; ++){
  Serial.print(i);
}
Seriail.println("");

I get output like this

[noting connected]
10011000000000000000000000000000

[try a pin]
10011000000111100000000000000000

[try adjacent pin]
10011000000111000000000000000000

[try adjacent pin again]
10011000000110000000000000000000

Any ideads what could be causing this? Is the problem in the core or bootloader or somewhere else?

Thnanks

(deleted)

Yes, I have a pulldown resistor attached and I'm testing all the pins one at a time because I only have one switch circuit.

Show us the code, how the pins are declared, pinMode usage, etc..

I would look up the pin mappings for ATMega32. That is search for the Arduino.h file that applies to ATMega32. If you turn on "verbose" mode for compilation you can see where the IDE pulls the files from. On my computer this is somewhere in /usr/share/arduino/hardware/variants/

Thanks Udo, that's a good suggestion; I'll try that now and report back. :slight_smile: I have checked the pin mappings and I've used 2 different versions of pins_arduino.c written for ATMega32 and got the same result

Hi Crossroads, here's the code; it couldn't really get any simpler. :slight_smile:

void setup() {

	Serial.begin(9600);

	for (int i=0; i<31; i++)
		pinMode(i,INPUT);

}

void loop() {

	for (int i=0; i<31; i++){
		button = digitalRead(i);
		Serial.print(button);
	}

	Serial.println("-");
}

And the wiring

// * = connected
//     (D  8) PB0  1|    |40  PA0 (AI 24)
//     (D  9) PB1  2|    |39  PA1 (AI 25)
//     (D 10) PB2  3|    |38  PA2 (AI 26)
//     (D 11) PB3  4|    |37  PA3 (AI 27)
// PWM (D 12) PB4  5|    |36  PA4 (AI 28)
//     (D 13) PB5  6|    |35  PA5 (AI 29)
//     (D 14) PB6  7|    |34  PA6 (AI 30)
//     (D 15) PB7  8|    |33  PA7 (AT 31)
//  *       RESET  9|    |32  AREF *
//  *         VCC 10|    |31  GND *
//  *         GND 11|    |30  AVCC
//  *       XTAL2 12|    |29  PC7 (D 23) PWM
//  *       XTAL1 13|    |28  PC6 (D 22) PWM
//      (D 0) PD0 14|    |27  PC5 (D 21 PWM
//      (D 1) PD1 15|    |26  PC4 (D 20)
//      (D 2) PD2 16|    |25  PC3 (D 19)
//      (D 3) PD3 17|    |24  PC2 (D 18)
//      (D 4) PD4 18|    |23  PC1 (D 17)
//      (D 5) PD5 19|    |22  PC0 (D 16)
//      (D 6) PD6 20|    |21  PD7 (D  7)
// I have one single switch that I move and test each pin separatelu

// This is the output when I have nothing connected; I'm expectig it to be all 0.
0101000000000000100010000000000-

// This PD7 when I press my switch, note 2 inputs are marked as true!  (physical pin 21)
0101000010000000100010010000000-

// PC0 with switch (physical pin 22)
0101000011100000100010010000000-

// PC1 (physical pin 23)
0101000011110000100010010000000-

Any other suggestions will be greatly appreciated. :slight_smile:

You need to determine if the problem is hardware or software. Print the PINs...

  Serial.println( PINA, BIN );
  Serial.println( PINB, BIN );
  ...

...and compare to the digitalReads. If the values match, the problem is hardware. Otherwise, the problem is software.

Try turning on the internal pullup resistors:

for (int i=0; i<31; i++)
pinMode(i,INPUT);
digitalWrite(i, HIGH); // << add this

Move this After the pinModes so the Tx pin is not declared an Input after you turn on the Serial library/function:

Serial.begin(9600);

Thanks for your suggestion coding badly! That has shown that my single input seems to be weirdly triggering more than one bit:

Serial.print( PINA, BIN );
Serial.print("-");
Serial.print( PINB, BIN );
Serial.print("-");
Serial.print( PINC, BIN );
Serial.print("-");
Serial.println( PIND, BIN );
Serial.println("");

// When nothing is active (The 2 trues are probably due to Serial data? TX, RX pins)
0-0-0-10001

// PD7 set high (physical pin 21)
0-0-1-10010001

// PC0  set high (physical pin 22)
0-0-111-10010001 // for a about 3 seconds
0-0-11-10010001 // for a about 3 seconds
0-0-1-10010001 // settles

// PC1 (physical pin 23)
0-0-1111-10010001 // for a about 3 seconds
0-0-111-10010001 // for a about 3 seconds
0-0-110-10010001 // for a about 3 seconds
0-0-10-10010001// settles

WTF?!  :(

Says to me your inputs are floating...

CrossRoads:
Try turning on the internal pullup resistors:

for (int i=0; i<31; i++)
pinMode(i,INPUT);
digitalWrite(i, HIGH); // << add this

Move this After the pinModes so the Tx pin is not declared an Input after you turn on the Serial library/function:

Serial.begin(9600);

Thanks for this suggestion this seems to give even more confusing results

//Nothing pressed
1111111-11111111-11111111-1111101


// PD7 set hight
1111111-11111111-11111111-11111101

// PC0 - when plugged into switch but that isn't pressed; essentially: pc0->10k->ground
1111111-11111111-11111110-11111101

// PCO switch active
1111111-11111111-11111111-11111101

// PC1- when plugged into switch but that isn't pressed; essentially: pc0->10k->ground
1111111-11111111-11111111-11111101

// PC1- switch active
1111111-11111111-11111111-11111101

CrossRoads:
Says to me your inputs are floating...

It looks that way but how can it be if the circuit is so simple? :roll_eyes:

              _|_
---pin------+   +---+5v
            |
            S 10k
            |
            | 0v

Try a simpler connection setup:

I think D1 will read low as that is the Serial output pin, and it will only go high when it is sending a '1' out.

I'm using an Atmega32A so that pin doesn't map to an RX or TX. However, I think with your kind help resolved this one for now. It looks like because I was only connecting one input at a time and leaving the other unconnected, that's why I was getting floating values on the other ports! Obvious right! :blush: I also found that my pin numbering was wrong! So far all my input appear to be working OK. I'm checking each one individually whilst ignoring the other values. Seems like such an easy fix looking back now :~

Thanks for everyone's help it's 2AM now :frowning: =(

"« Reply #14 on: Today at 08:46:25 PM »"

"it's 2AM now "

Your clock's a little fast 8)

UK Time :frowning:

Not that - you called the top of the hour 15 minutes early ... or 45 minutes late :wink: