Atmega 32 USART

Hi, I’m trying to get an arduino sketch running on an Atmega32 16Pu chip with an external 16mhz clock, everything seems to be working, including I2C communications with a nano, but the hardware serial i can’t get up and running.

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("hello world");
}

If i run this on the nano, i can read it over the USB’s virtual COM port.
So I tried the standalone chip with a max232 and a physical COM port, and on a scope i can see data, however, the computer doesn’t seem to be getting anything.

Shorting the RX and TX pin on the max232 (without the arduino attached) results in an echo, as to be expected, so the max232 circuit is working. However, i can not get the atmega32 16pu chip to communicate with the pc.

I’ve read somewhere that HardwareSerial.cpp needs to be modified with some extra defines. However, those defines get never defined with my boards.txt selection: (I’m not entirely sure how the defines relate to the settings in boards.txt either, is it the string from build.board padded with __ on both sides?)

mega32-16.name=ATmega32-External 16mhz
mega32-16.build.board=AVR_MEGA32_16
mega32-16.build.mcu=atmega32
mega32-16.build.f_cpu=16000000L
mega32-16.build.core=arduino:arduino
mega32-16.build.variant=mega32

mega32-16.upload.tool=avrdude
mega32-16.upload.maximum_size=32000

mega32-16.bootloader.low_fuses=0xEF
mega32-16.bootloader.high_fuses=0x89
#mega32-16.bootloader.extended_fuses=0xFD

What am I overlooking/not getting here?

I've put together a core where most AVR DIP-40s (including ATmega32) are supported. Just remember to burn the bootloader before you upload the code. I've had no problems using Serial.print with the ATmega32 :)

Download it at https://github.com/MCUdude/MightyCore

Please report back if it doesn't work

I've changed the low fuses in my download from 0xff to 0xf7 for the faster clock 1284/644/324/164 chip settings. There've been much less complaints over software installs with Full Swing Crystal Oscillator selected. There's a whole multi page long discussion in the forum over that.

I haven't actually thought of using the full swing oscillator option. Could you please link to the post you are mentioning? I'd like to run some tests :) does this affects the power consumption?

http://forum.arduino.cc/index.php?topic=146773.0 and see reply #1.

I don't know how much difference there is in power draw between the crystal settings. I just know what reliably works.

I'm not entirely sure, but what's a "core"? Is that the libraries used for the compiler or a bootloader?

I'm guessing the first, so i dump this in the arduino/hardware/* folder, select the 16mhz board and build again and pray it works?

Yes, a core is all the basic libraries that's used to compile code + the bootloader and pin definitions. Since the ATmega32 isn't officially supported by the Arduino team, you'll need to add a core that makes the ATmega32 work with the Arduino IDE.

But this ATmega of yours, have you loaded it with a bootloader? You're not able to upload a sketch to it with a USB to serial adapter. You can either upload the code using a programmer (USBasp, USBtinyISP) by holding shift while you upload your sketch, or burn the bootloader to be able to upload using a USB to serial adapter

i’m uploading with an ISP, but the programmer (ArduinoISP) isn’t in the list of your programmers.
Am i supposed to use that core on top of the default arduino core folder? or replace it?
Putting it in Documents\Arduino\hardware… doesn’t seem to be doing anything good:

Warning: platform.txt from core 'MightyCore' contains deprecated recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{build.path}/{archive_file}" "{object_file}", automatically converted to recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}". Consider upgrading this core.
Could not find boards.txt in C:\Users\stino\Documents\Arduino\hardware\avr\bootloaders. Is it pre-1.5?
Could not find boards.txt in C:\Users\stino\Documents\Arduino\hardware\avr\cores. Is it pre-1.5?
Could not find boards.txt in C:\Users\stino\Documents\Arduino\hardware\avr\libraries. Is it pre-1.5?
Could not find boards.txt in C:\Users\stino\Documents\Arduino\hardware\avr\variants. Is it pre-1.5?

And i can’t upload the bootloader with the default arduino core because the checksum doesn’t seem to be what avrdude expects, but no clue where it gets the checksum from, it’s not in boards.txt.

EDIT: seems like i need to unzip it to hardware<something>\ not just hardware.

Result is: it compiles, it uploads, my TX led blinks but no data on my COM port, not even random noise.

The directory should be /Users/[username]/Documents/Arduino/hardware/MightyCore-master

The error saying that the core should be updated is supposed to be there. The error message only appear in Arduino IDE 1.6.6. If I fix the error, the core wouldn't be backwards compatible with 1.6.5 and older. (stupid, indeed!)

Did you manage to compile your code? :)

It compiles, it even uploads. But i still dont see the data coming out of the tx port. LED still flashes, but nothing on the COM port.

If you got an LED on the TX pin and it flashes when the program is running, it means that the Serial.print are working just fine. Try hooking up a logic analyzer or an oscilloscope to see what actually happens on that pin. Maybe you got a bad driver on your PC? What kind of USB to serial adapter are you using?

The USB-Serial adapter seems to be working if I test it with the Arduino Nano. Also, the LED-flashing frequency/duty cycle seems to be different. With the Nano it's almost constant-on while with the Atmega32 I can see it blink, although fast, still slow enough to see that they are individual pulses.

So I'm guessing somehow that 9600 isn't 9600, but how much slower I can't tell. The scope I have is an analog one, so, no memory to capture the bittrain.

Compare these 2:

Program on the Atmega32 16PU

long delayTime = 1000;
void loop() {
  digitalWrite(28, HIGH);
  delayMicroseconds(delayTime/2);  
  digitalWrite(28, LOW);
  delayMicroseconds(delayTime/2);
}

Program on the Arduino NANO 328

long delayTime = 81930;
void loop() {
  digitalWrite(12, HIGH);
  delayMicroseconds(delayTime/2);  
  digitalWrite(12, LOW);
  delayMicroseconds(delayTime/2);
}

both generate an almost identical pulse width after some tweaking of the delayTime value.

So I'm guessing the atmega32's clock pulse is about 81.93 times slower? Might explain why the COM can't read the "not so 9600" baud rate ..

It's wired up with a 16Mhz crystal between pin 12 and 13 (XTAL 1 and 2) with 22pF's to the ground. and to be sure about that: (https://upload.wikimedia.org/wikipedia/commons/b/b9/Capacitors_(7189597135).jpg) the 15, 10 and 7 capacitors look the same as the 22 I've used.

I'm not seeing any signal with the scope on either pin of the crystal though, not sure if that's normal or not.

then I'm afraid I don't know whats wrong. I also got an ATmega32A lying around, and I hooked it up to my test board. Selected 16MHz, and burned the bootloader. Uploaded this sketch with an FTDI adapter:

void setup() {
pinMode(0,OUTPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(0,HIGH);
Serial.println("On");
delay(1000);
digitalWrite(0,LOW);
Serial.println("Off");
delay(1000);
}

The LED attached to pin 0 are turning on and off every second, and the output from the serial monitor is:

On
Off
On
Off
On
Off
On
Off
On
Off

Everything works fine out of the box for me. :confused:

The CKSEL register, how does it get set for external? is that the bootloader's job? Becaue there's no bootloader on the chip, just directly programmed via ICSP.