Saturday, October 5, 2013

Getting the Atmega1284P to work with Arduino, SD card and MsTimer2

For a project I have running at the moment I needed a microcontroller with more ports and memory (Flash and RAM) than the Atmega328P (as for example on the Arduino Uno) but didn't need the full features of the 2560 as in the Arduino Mega. For easy construction of the prototype board on veroboard, the Atmega1284P offered itself. Here I am documenting the procedure for getting it to run and the solution to a few smaller problems I ran into.

 Software to get


First I downloaded the ScratchMonkey ISP[1] from
https://github.com/microtherion/ScratchMonkey/archive/ScratchMonkey-1.0.zip#
and unpacked it into the ~/Documents/Arduino directory.

I followed the directions at http://microtherion.github.io/ScratchMonkey/GettingStarted.html
and copied the hardware directory in ~/Documents/Arduino/hardware.

I downloaded the maniacbug Mighty1284p[2] bootloader from http://maniacbug.wordpress.com/2011/11/27/arduino-on-atmega1284p-4/.

After unpacking it in ~/Documents/Arduino/hardware and renaming the new directory to just mighty-1284p, the Arduino IDE has to be restarted.

Hardware Setup


The Atmega1284P is set up on stripboard with a 16 MHz crystal between pins 12 and 13, two 22 pF capacitors to ground from the crystal leads, 5V to pins 10, 30 and 32, GND to 11 and 31 and bypass capacitors (and all the other connections and hardware you might need).
Wiring the Atmega1284P up to the Arduino Uno was easily done by connecting it according to http://microtherion.github.io/ScratchMonkey/ISP.html.

Burning the Bootloader


Now the ScratchMonkey.ino sketch was loaded on the Arduino Uno. After that ``Mighty1284p 16 MHz using Optiboot'' was chosen as my board to program and ScratchMonkey as the programmer.
Then I burned the bootloader; the operation ran through without fuss.

After that I broke out a USB to serial converter and connected it as follows:
Converter   Atmega1284P
GND         GND
+5V         +5V
TX          Pin 14 (RX0)
RX          Pin 15 (TX0)
EXT Reset   Pin 9 (RST)


and started flashing some sketches and trying my new microcontroller. Now most every program runs without problems.

Low-pass Filter


In the beginning, sketches would not always upload directly, the process would hang at different places. On the boxtec forum [3], microtherion proposed the following solution from [4]:
``10k series resistor in line and 100pf cap to ground. ``

Some Problems and Their Solutions

SD cards:

One of my sketches is using the original Arduino SD library. This sketch (and the furnished examples) wouldn't run and no errors were thrown. The code compiled without fuss and loaded on the chip but would hang in the initialization phase (setup).
The solution turned out to be a missing definition of the chip in
/Applications/Arduino.app/Contents/Resources/Java/libraries/SD/utility/Sd2PinMap.h

The following needs to be added to the line mentioning the atmega644:
|| defined(__AVR_ATmega1284P__)
so that it reads:

#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__)|| defined(__AVR_ATmega1284P__)


Once this was changed, my code compiled and it now seems to run without a hitch (I left a note on the maniacbug blog).

Timer:
I am using the MsTimer2[3] library for some timing functions. There too,
|| defined(__AVR_ATmega1284P__)
has to be added to the Mstimer2.cpp file in several places, as follows:
#if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) || defined (__AVR_ATmega1284P__)

I verified that this was working on the furnished example blinking sketch. With an on and off period of 1 ms each I got a perfect 500 Hz square wave.



Thank you very much Microtherion[1] for providing ScratchMonkey and to manicabug[2] for providing a bootloader for the Atmega1284P.

[1]http://microtherion.github.io/ScratchMonkey/index.html, http://samelimmat.blogspot.ch/2013/06/announcing-scratchmonkey-10.html
[2]http://maniacbug.wordpress.com/2011/11/27/arduino-on-atmega1284p-4/
[3]http://forum.boxtec.ch/index.php/topic,2323.msg3095.html#msg3095
[4]http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=107115
[5]http://www.pjrc.com/teensy/td_libs_MsTimer2.html

Saturday, August 17, 2013

MCP4706 Library for Arduino

For a small project I got a MCP4706 8 bit I²C-DAC. In order to use it, I modified the +Adafruit Industries MCP4725 library which is for a 12 bit DAC.
The MCP4706 library can be found at my github page.
The data sheet for can be found here at the Microchip homepage.