background preloader

Microcontroller tutorial series: AVR and Arduino timer interrupts

Microcontroller tutorial series: AVR and Arduino timer interrupts
Does your program seem like it’s trying to do too much at once? Are you using a lot of delay() or while() loops that are holding other things up? If so, your project is a good candidate to use timers. In this tutorial, we’ll discuss AVR and Arduino timers and how to use them to write better code. In our prior article, we covered interrupt basics and how to use external interrupts that are triggered by a pin change or similar event. Check it out if you’re looking to brush up on interrupts in general. This chapter moves on to timer interrupts and talks about their applications in Arduino projects or custom AVR circuits. What is a timer? You’re probably familiar with the general concept of a timer: something used to measure a given time interval. The beauty of timers is that just like external interrupts, they run asynchronously, or independently from your main program. For example, say you’re building a security robot. How do timers work? Our timer resolution is one millionth of a second.

EXTERNAL INTERRUPTS ON THE ATmega168/328 - QEEWiki External Interrupt Mask Register External Interrupt Flag Register ATmega168/328 Code: #include <avr/io.h> #include <avr/interrupt.h> int main(void) DDRD &= ~(1 << DDD2); // Clear the PD2 pin // PD2 (PCINT0 pin) is now an input PORTD |= (1 << PORTD2); // turn On the Pull-up // PD2 is now an input with pull-up enabled EICRA |= (1 << ISC00); // set INT0 to trigger on ANY logic change EIMSK |= (1 << INT0); // Turns on INT0 sei(); // turn on interrupts while(1) /*main program loop here */ ISR (INT0_vect) /* interrupt code here */ Figure 2: ATmega168/328 - Pin Change Interrupt Pins One important thing to note, on the older ATmega8 does not have any PCINT pints, therefore, this section of the tutorial only applies to ATmega88 through ATmega328. Pin Change Interrupt Control Register Pin Change Interrupt Flag Register Pin Change Mask Register 0 Pin Change Mask Register 2 Pin Change Mask Register 1 Pin Change Mask Register 0 DDRB &= ~(1 << DDB0); // Clear the PB0 pin // PB0 (PCINT0 pin) is now an input else Cheers

Studfinder: Wire Edition - CraigM Craig Macomber, Milda Zizyte June 10, 2011 Often, humans are faced with the problem of finding that which they cannot see. The five senses are limited and in particular do not extend to wavelengths outside the audible or visible spectrum. Such sensing capabilities are conceivably very important. In particular, humans cannot easily locate electric fields. In fact, the robot Marvin faces the same problem humans do - just as a robot cannot see these electric fields, neither can humans. Our solution combines analog filtering with digital processing to achieve its goal. The analog circuit has three main purposes: To receive and filter an input electrode current into a clear voltage output signalTo convey the output using minimal complexityTo effectively interface with an Arduino Uno for digital processing (1) is the most complex, requiring filtering a current source. (2) is accomplished by the use of three LEDs, denoted “left,” “right,” and “amplitude.” (3) strongly depends on (1) and (2). 1.

scheduler - avrtutorials2 My final year project will be an implementation of a Real Time Operating System (RTOS) for the AVR microcontrollers. Although many have said it isn't wise to write yet another OS (apparently, there are enough of these around), I decided it is the best way to learn, since the source of other OSes weren't as easy to understand as I had expected.To start off, I began with the Scheduler. What is a Scheduler? A scheduler is a piece of code that calls functions (usually refered to as "Tasks") at periodic intervals of time. Schedulers form one of the basic components of an RTOS. For example, let the maximum time for each task to execute be 100ms. The following scheduler is based on an Atmel Application Journal, which unfortunately, seems to be missing some pages. Implementation Code Listing: scheduler.c The Code, Explained A Task Control Block is how a RTOS "sees" a task. A task is STOPPED if it is no longer going to run. The Timer0 Overflow Interrupt is used to provide a tick. Closing Thoughts

Welcome Mastering timer interrupts on the Arduino « Popdevelop – A developer team from Malmö, Sweden Lately I’ve become more and more fan of the Arduino microcontroller. I really like how easy it is to work with; the accompanying IDE plus the huge amount of community support and libraries available. However, for a project I needed to be able to sample input audio at a certain frequency and this required me to understand a bit more deeply on how to configure the ATMEGA328 timer registers. In this post I’ll show you how to call a user function at a period of one millisecond. I decided to use the 8-bit Timer2 which is one of the timers in the ATMEGA328. After some tedious reading in the ATMEGA328 datasheet (and googling!) The code should be pretty clear from the comments but let me just elaborate a bit on the calculations: First of all, the observant reader will notice that the above code will only be accurate for a CPU running at 16MHz since the calculations are based on this frequency. Joy oh joy! Well, what’s the use of this, you might ask? Well, There you have it!

Ready, Set, Oscillate! The Fastest Way to Change Arduino Pins « The Mind of Bill Porter There are many ways to change an output pin. The way we know and love is the famous digitalWrite() function. (Spoiler: Want a faster digitalWrite? Download Here!) But even the Arduino Reference claims that it is not the most efficient. I ran some tests to find out. The estimated CPU cycles is calculated from ½ the waveform period measured divided by the period of 16Mhz, since it takes two write operations to complete a full period in a waveform. The 3 methods I tested were digitalWrite(pin, LOW); digitalWrite(pin, HIGH);CLR(PORTB, 0) ; SET(PORTB, 0);PORTB |= _BV(0); PORTB &= ~(_BV(0)); The macros used: #define CLR(x,y) (x&=(~(1<<y))) #define SET(x,y) (x|=(1<<y)) #define _BV(bit) (1 << (bit)) The results As you can see, digitalWrite takes around 56 cycles to complete, while direct Port addressing takes 2 cycles. Next I tested just flipping a pin. digitalWrite(pin, ! #define sbi(port,bit) (port)|=(1<<(bit)) Wow, the Arduino method takes a whopping 121 cycles to flip a pin!

Tutorial: Using AVR Studio 5 with Arduino projects | EngBlaze Note: This tutorial has been replaced with an updated version that covers the same topic with Atmel Studio 6. Studio 6 makes a lot of improvements over the prior version, so there’s really no reason not to upgrade unless you have a very specific need. We’ve also incorporated a lot of fixes, tips, and great user feedback. Check it out here: Tutorial: Using Atmel Studio 6 with Arduino projects This article explains, step-by-step, how to set up the AVR Studio 5 IDE for use with Arduino projects. Introduction My my, what a temperamental beast we are. The answer: well, partially. Why should I switch? A primary use case for AVR Studio has always been to serve users that have grown out of the integrated Arduino IDE. AVR Studio is a huge step up from those limitations, but for many, making the switch cold turkey is just that: a huge step. So why not have the best of both worlds? Preparing AVR Studio and the Arduino core library First, a few preparation steps. Compiler and linker setup Flash!

Burn a bootloader to a blank atmega328/atmega328p with an Arduino UNO | Build your own optiLoader shield! You cant use your Arduino UNO as programmer unless you modify it, but i didnt want to do that! Some people had luck using a Resistor and Capacitor to disable the auto reset feature but it did not work for me (Using an Arduino UNO R2) So i tried using a parallel port programmer but never got it to work. It seems easy, so if your interested go ahead and try it yourself, but be warned, could lead to frustration :) Fortunately Bill Westfield wrote optiLoader! The readme file doesent tell you how to connect the blank chip so i asumed that you need an oscillator and a pull up resistor and... well... it worked! You need: Working Arduino (with µC and Bootloader) - optiLoader will run on that one16MHz oscillator2 x 22pF ceramic capacitor10KΩ resistor Breadboard setup looks like this: Powering the circuit over pin 9 is questionable, but seems to work well with this setup (look at optiLoader source files for more info on this).

Gestion du temps, Time manager, delay() | Présentation de la gestion du temps Je ne sais pas si vous avez l’habitude d’utiliser la fonction delay() dans vos sources Arduino mais si c’est le cas vous avez sans doute dû vous rendre compte que la fonction est blocante. Ayant directement pour incidence que si vous souhaitez actionner un autre composant que celui en standby, c’est tout simplement impossible. De multiples limites s’ajoutent, empêchant parfois le comportement le plus basique demandé comme, par exemple, faire clignoter 2 LED à des fréquences différentes pouvant à n’importe quel moment vouloir s’eteindre ou s’allumer. Sauf que si on est bloqué dans un delay() et ben c’est le drame. Une carte arduino UNO dispose malheureusement que d’une seule horloge ce qui rend une parfaite parallélisation des tâches impossible. Petit aparté fait, cela ne faisant pas de graves décalages dans notre cas, passons aux choses sérieuses. Pti montage si vous voulez faire de même : Passons à l’étude de l’objet « Tache » : La LED doit pouvoir :

Python Arduino and Python Talking to Arduino over a serial interface is pretty trivial in Python. On Unix-like systems you can read and write to the serial device as if it were a file, but there is also a wrapper library called pySerial that works well across all operating systems. After installing pySerial, reading data from Arduino is straightforward: >>> import serial >>> ser = serial.Serial('/dev/tty.usbserial', 9600) >>> while True: ... print ser.readline() '1 Hello world! Writing data to Arduino is easy too (the following applies to Python 2.x): >>> import serial # if you have not already done so >>> ser = serial.Serial('/dev/tty.usbserial', 9600) >>> ser.write('5') In Python 3.x the strings are Unicode by default. >>> ser.write(b'5') # prefix b is required for Python 3.x, optional for Python 2.x Note that you will need to connect to the same device that you connect to from within the Arduino development environment. Any write() commands issued before the device initialised will be lost. Py2B