jenswilly.dk


Archive for November, 2009

Timers

17. November 2009 21:39 by Jens Willy Johannsen
Categories: ATmega

Here is a link to an excellent tutorial about timers: Newbie's Guide to AVR Timers (also available as PDF here: Newbie's Guide to AVR Timers PDF.

After reading that tutorial, understanding the following ATmega168 code should pose no problems whatsoever:

// OC1A pin is automatically toggled due to the COM1A0 setting on TCCR1A
// B0 pin is toggled by way of compare A match interrupt

int main(void)
{
    // setup
    DDRB |= _BV( PB1 ) | _BV( PB0 );   // B1 (OC1A, pin 15, Arduino pin DIG9) and B0 (pin 14, Arduino DIG8) -> output
    PORTB |= _BV( PB0 );               // B0 HIGH
    PORTB &= ~_BV( PB1 );              // B1 LOW
    TCCR1B = 0;                        // stop timer1 by shutting off the clock - just to be sure
    TCNT1 = 0;                         // force count to start from scratch - just to be sure

    // configure timer1
    TCCR1A = _BV( COM1A0 );            // toggle OC1A on compare match
    OCR1A = 62499;                     // compare value for 1 Hz: val = F_CPU/prescaler/target freq. - 1: 16000000/256/1 - 1
    TIMSK1 |= _BV( OCIE1A );           // enable interrupt for timer1 output compare A match

    // start timer
    sei();                               // enable interrupts
    TCCR1B = _BV( WGM12 ) | _BV( CS12 ); // start timer1: waveform generation mode = 4 (TOP=OCR1A), clock source = system clock w/ prescaler 256

    // loop
    while( 1 )
        ;

    return 0;
}

ISR( TIMER1_COMPA_vect )
{
    PINB |= _BV( PB0 );   // toggle port B0 by writing a HIGH to the input bit of an output pin
}
No comments No comments »

TextMate Bundle for Arduino 0017

17. November 2009 16:07 by Jens Willy Johannsen
Categories: Arduino

TextMate Bundle to build and upload Arduino sketches

Far be it from me in any way, shape or form to criticize the Arduino IDE which is simple, easy to use and works flawlessly (at least I have never had any problems). However, there are times when I find it preferable to write the Arduino sketches in TextMate (which is the text editor for Mac, in case you didn't know) since it has better... well, text editing functions. Until yesterday I frequently wrote/edited the sketches in TextMate and then copied the text over to the Arduino IDE in order to upload the sketch.
There are several guides on compiling and uploading from the command line and also several TextMate bundles for doing this but I couldn't get any of them to work on my setup (Mac OS X 10.6.1, Arduino Duemilanove and Arduino 0017) so I had to hack one together myself using bits and pieces from the other available bundles and documentation from various forums.

Here it is: TextMate bundle for Arduino 0017

Assumptions
This bundle assumes that the Arduino IDE version 0017 is installed in /Applications and it named "Arduino". If this is not the case you need to change a path in the makefile (refer to the section about messing with the Makefile).

How to use it

  • Extract the zip and double-click the .tmbundle file to install it in TextMate (which, of course, you will need to have installed beforehand).
  • Create a new document in TextMate, write some Arduino code (or copy/paste in the sample Blink code) and save the file with a .pde extension
  • Hit Cmd-R to compile and upload the sketch

You can also hit Cmd-B just to compile without uploading (to check your syntax).

Note that a folder named "applet" will be created in the same location as the .pde file during compilation. (You can delete it if you want or leave it in place.)

MCU and F_CPU settings
The Makefile needs to know what AVR processor and which clock speed to compile to. The defaults are ATmega328 and 16 MHz (i.e. a standard Arduino Duemilanove).
These values can be overridden by setting other values in TextMate's preferences. In the TextMate preferences, choose the "Advanced" tab and click "Shell Variables". Add the following variables to override the default values:

Variable Value
TM_ARDUINO_MCU atmega328p|atmega168|...
TM_ARDUINO_F_CPU 16000000|8000000|...

For example, the following settings will compile for an ATmega168 running at 8 MHz:
TextMate preferences

If one or both of the above variables are deleted or disabled, the default values will be used.

Messing with the Makefile
If you need to change something in the Makefile (e.g. the path to the Arduino tools) go right ahead and do it.
The Makefile is located in the bundle. The bundle is located in ~/Library/Application Support/TextMate/Bundles. Option-click and choose "Show Package Contents" and then open the "Support" folder and edit the Makefile to your heart's content.

Comments 5 comments »

Time, Gentlemen!

15. November 2009 23:08 by Jens Willy Johannsen
Categories: General

A microprocessor needs a component that generates clock signals which basically control when and to which beat everything happens.

The ATmega MCUs have a built-in "RC oscillator" but you can also use an external resonator or crystal. The easier the solution, the less precise clock signals will be generated (ain't no such thing as a free lunch :-).

The easiest solution is to use the built-in RC oscillator, the second easiest is to use an external resonator which doesn't require any other components and the lastly you can use an external crystal and a pair of "load capacitors".

If you don't need precise timing the internal RC oscillator is completely adequate but if you use serial communication or other tasks where precise timing is required, you should use a crystal or a resonator.
Read this excellent guide to choosing a clock source: Why you need a clock source - an introduction to choosing and using clock sources.

When using a crystal two capacitors need to be connected from the crystal to GND. Usually a "load capacitance" is specified in the crystal's datasheet but this is total load capacitance so you need a different capacitance for the two parallel capacitors. To find the required capacitance (rLC), the following formula can be used:

rLC = 2 x (LC-PC)

where LC is the specificed total load capacitance and PC is parasitic capacitance (from nearby wires and circuit board etc.) which is usually between 7 and 10 pF.

Another useful formula is this one which is used to calculate the load capacitance when using two capacitors of a capacitance of rLC:

LC = rLC^2 / 2rLC + PC

An example: this 16.000 MHz crystal requires a load capacitance of 30pF. Using the first formula yields using a rLC of 40 pF:
rLC = 2 x (30pF - 10pF) => rLC = 40pF

However, if you don't have all exact size capacitors in stock (I know I don't) you can choose the nearest and use the second formular to check what the load capacitance will be. So for two capacitors of 39 pF (again using a value of 10pF for PF) we get:

LC = 39 pF x 39 pF / 2 x 39 pF + 10 pF => LC = 1521 pF / 78 pF + 10 pF => LC = 29.5 pF

which is close enough.

No comments No comments »