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. Related:  arduino

» LowPower JeeLabs No, this isn’t a story about bio-rhythms :) One of the challenges I’ll be up against with Room Nodes is how to keep track of time. The fact is that an ATmega is extraordinarily power efficient when turned off, and with it a JeeNode – under a few microamps if you get all the little details right. That leads to battery lifetimes which are essentially only determined by self-discharge! But there are two problems with power off: 1) you need to be 100% sure that some external event will pull the ATmega out of this comatose state again, and 2) you can completely lose track of time. Wireless packets are of no use for power-down mode: the RFM12B consumes many milliamps when turned on to receive packets. Meet the watchdog… Fortunately, the ATmega has a watchdog, which runs on an internal oscillator. There’s a Sleepy class in the Ports library, which manages the watchdog. It’s not enough, though. Meet the pin-change interrupt… Eh… not so fast: Q: What time is it when the pin-change occurred?

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.

Arduino Frequency Counter Tutorial with Circuit Diagrams & Code Almost every electronic hobbyist must have faced a scenario where he or she must measure the frequency of signal generated by a clock or a counter or a timer. We can use oscilloscope to do the job, but not all of us can afford an oscilloscope. We can buy equipment for measuring the frequency but all these devices are costly and are not for everyone. With that in mind we are going to design a simple yet efficient Frequency Counter using Arduino Uno and Schmitt trigger gate. This Frequency Counter is cost effective and can be easily made, we are going to use ARDUINO UNO for the measuring the frequency of signal, UNO is the heart of project here. To test the Frequency Meter, we are going to make a dummy signal generator. With everything in place we will have a Frequency meter and a square wave generator. Required Components: Circuit Explanation: The circuit diagram of the Frequency Meter using Arduino is shown in below figure. Signal Generator using 555 Timer IC: Schmitt Trigger Gate: - Apprenez à tout réparer Theory Overview This circuit is based on a PIC16F628A microcontroller which sends serial data through a series of four 74HC595 eight bit shift registers that in turn drive 32 LED pairs. 2N3904 transistors are used to switch the LEDs on and off so that all LEDs can be simultaneously driven at 20mA each, indefinitely, without damaging the shift registers. One kilobyte of image data is stored in the program memory area of the microcontroller and is read by way of a lookup table. The firmware uses three interrupt routines to do its job: An interrupt to provide the time interval between radial raster lines One to increment a counter for timing the wheel rotation interval And finally, one to reset all counters and update the raster interval value every time the hall effect sensor is triggered In fact, after the initial startup routine, virtually every part of the firmware’s execution runs inside an interrupt routine. Features Flowcharts Main program flowchart: TIMER1 rollover flowchart: Shift registers

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 Dimmer-Arduino – Arduino&stuff Switching an AC load with an Arduino is rather simpel: either a mechanical Relay or a solid state relay with an optically isolated Triac.It becomes a bit more tricky if one wants to dim a mains AC lamp with an arduino: just limiting the current through a Triac is not really possible due to the large power the triac then will need to dissipate, resulting in much heat and it is also not efficient from an energy use point of view. The proper way to do it is through phase control: the Triac then is fully opened, but only during a part of the sinoid AC wave.One could an Arduino just let open the Triac for a number of microseconds, but that has the problem that it is unpredictable during what part of the sinuswave the triac opens and therefore the dimming level is unpredictable. One needs a reference point in the sinus wave.For that a zero crossing detector is necessary. Opening the Triac is done via a tried and tested opto coupled Triac circuit. All one needs then is the software. Like this:

Tout réparer (ou presque) | SUV [sens & utilités en vrac] A l’heure où la grogne contre l’obsolescence programmée devient de plus en plus audible, on me demande souvent comment réparer des produits : ces chaussures irréparables, ces faux contacts dans les écouteurs si frustrants. En tant que passionnée du hack, de la réparation, et du détournement, je n’avais pas de solution pour bien des objets jusqu’à ce que je découvre Sugru, une sorte de pâte à modeler en silicone qui permet de réparer tout ou presque. Cette très belle vidéo de Do the Green Thing donne une petite idée des portes de la créativité qui s’ouvrent à vous. The Joy of Fix from Do the Green Thing on Vimeo. On peut réparer les roues usées de sa valise de voyage, un câble, et beaucoup d’autres choses encore : regardez cette vidéo où on vous fait – avec humour – une démonstration des choses que l’on peut faire. L’idée derrière ce produit: permettre à chacun de réparer et adapter les produits à leurs besoins. Vous ne savez pas comment faire? Une poignée cassée: une réparation discrète