Pages: [1] 2   Go Down
Author Topic: ATMega32 digitalRead and write trouble  (Read 1299 times)
0 Members and 1 Guest are viewing this topic.
Cardiff, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
Programmer of all sorts
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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
Logged

My Arduino 7 segment LCD/LED library

http://code.google.com/p/arduino-seven-segment/

Scotland
Online Online
Edison Member
*
Karma: 24
Posts: 1230
Have you had your Arduino fix today?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you got pull up/down resisters installed?
Logged

Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

Cardiff, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
Programmer of all sorts
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

My Arduino 7 segment LCD/LED library

http://code.google.com/p/arduino-seven-segment/

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26442
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3470
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

Check out my experiments http://blog.blinkenlight.net

Cardiff, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
Programmer of all sorts
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Udo, that's a good suggestion; I'll try that now and report back. smiley  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.  smiley-kitty

Code:
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

Code:

// * = 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)
« Last Edit: January 09, 2013, 05:24:31 pm by supercrab » Logged

My Arduino 7 segment LCD/LED library

http://code.google.com/p/arduino-seven-segment/

Cardiff, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
Programmer of all sorts
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

// 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. smiley
Logged

My Arduino 7 segment LCD/LED library

http://code.google.com/p/arduino-seven-segment/

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
  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.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26442
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Cardiff, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
Programmer of all sorts
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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?!  :(


Logged

My Arduino 7 segment LCD/LED library

http://code.google.com/p/arduino-seven-segment/

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26442
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Says to me your inputs are floating...
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Cardiff, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
Programmer of all sorts
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
//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







Logged

My Arduino 7 segment LCD/LED library

http://code.google.com/p/arduino-seven-segment/

Cardiff, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
Programmer of all sorts
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Says to me your inputs are floating...

It looks that way but how can it be if the circuit is so simple?  smiley-roll
Code:
             _|_
---pin------+   +---+5v
            |
            S 10k
            |
            | 0v
« Last Edit: January 09, 2013, 06:52:29 pm by supercrab » Logged

My Arduino 7 segment LCD/LED library

http://code.google.com/p/arduino-seven-segment/

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 524
Posts: 26442
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


* Arduino_switch.jpg (46.65 KB, 960x720 - viewed 17 times.)
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Cardiff, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
Programmer of all sorts
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!   smiley-red  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  smiley-confuse

Thanks for everyone's help it's 2AM now smiley-sad  smiley-cry
Logged

My Arduino 7 segment LCD/LED library

http://code.google.com/p/arduino-seven-segment/

Pages: [1] 2   Go Up
Jump to: