Why use watchdog variable timer

Most of embedded microcontrollers contain watchdog timer. Watchdog variable timer is literally a „watchdog“. Watchdog timer continuously inspects the program float. Basically if microcontroller program hangs, then watchdog timer resets it and brings embedded system back to life. The idea is very simple. Lets say, you know, that your program has to be executed during 20ms. And you know that worst case scenario is 30ms. Then you set watchdog variable timer connected to highest priority interrupt – RESET. Once Watchdog timer is triggered, timer counts up to time you set and then it resets the MCU. The only way to avoid reseting is to send command to watchdog timer to start counting over. Technically watchdog variable timer is nothing more that retriggerable one shot multivibrator. The use of watchdog timer may be various. The one mentioned above is like a program execution loop. Once all procedures are performed, the MCU resets and starts over. Another more valuable and mostly used is for system reset if code failure occur. You may set some flags indicating was procedure successful or not. If not, than you can fire watchdog timer to restart MCU and start over. The only problem with watchdog timer is…

Continue reading

How to use inline ASM using WinAVR

I have been working on optimisation of one of my C codes. I needed one function to be as optimal as possible. I decided to use inline ASM to achieve this. I decided to write few lines about this. There are few rules that is necessary to follow. Each ASM statement is divided by colons into 3(up to four parts): Assembler instructions part; A list of output operands (comma separated); A list of input operands (comma separated); Clobbered register – usually left empty.

Continue reading

Marking of AVR microcontrollers

There is quite big variety of AVR microcontrollers that ATMEL company produces. This article is about how to Extract information about AVR microcontroller using its marking on package. Each microcontroller has its own mark on package: numbers and letters. First is microcontroller type. After follows suffix of three fields. First field is one or two digits what indicates maximal operation frequency MHz. Second field is a letter showing package type. And third field shows the temperature range of working environment. There are couple more marking letters „L“ and „V“ after microcontroller type. This means that microcontroller operates at low or very low voltages (and frequencies of course). Table of microcontroller packaging types Letter Package Lead Pitch (mm) A TQFP 0.8 J PLCC 1.27 M MLF 0.5 P PDIP 2.54 S SOIC 1.27 Y SSOP 0.65 There are two types of operating temperature ranges: Letter T, ºC C 0…70 I -40…+85 And couple examples at the end: Marking VCC, (V) Fmax, (MHz) Package Attiny15L-1PI 2.7-5.5 1.6 8pin PDIP Atmega64-16AI 4.5-5.5 16 64lead TQFP Atmega325V-8MC 1.8-5.5 8 64lead MLF Attiny2313-20SI 2.7-5.5 20 20lead SOIC

Continue reading

AVR controlled DDS generator software writing

During my spare time, I am developing the program for the AVR controlled DDS generator. I decided to write software using WinAVR tool-set. How far ahead I have moved with this? I have implemented: Menu system; Reading previous generator configuration from EEPROM; Setting signal mode; Storing last generator configuration to EEPROM to be loaded after reset; Four types of signal output (square, sawtooth, triangle and sine wave); Things I still need to do: Ability to change signal frequency; Implement other signals (listed below); Make program clean-up; Signals in AVR controlled generator: 0 – OUT_|¯|_ – square wave(done); 1 – OUT|/|/| – sawtooth(done); 2 – OUT|\|\| – reverse sawtooth(awaiting); 3 – OUT/\/\/\ – triangle(done); 4 – OUT~~~~ – sine wave(done); 5 – OUT-NOISE – noise signal(awaiting); 20 – PWM-OCC – timer output compare mode(awaiting); 21 – PWM-SINMDS  sine wave modulated PWM(awaiting); 22 – PWMSQSMDS – sine wave modulated PWM. PWM positive, SQ  negative polarity(awaiting); 23 – PWM-CMDS – custom PWM. Will be possible to set custom duty cycle(awaiting); Signal generation Square, sawtooth, triangle and sine waves are generated by reading values from flash memory. Fragment of the sine wave is as follows: //———————————————– //signals saved in flash memory const uint8_t sinewave[]…

Continue reading

Reading AVR button status using WinAVR

If you want to bring some interactivity to your embedded projects, one option is to add buttons. This allows you to control program flow, set parameters and much more. Few words about AVR ports. AVR Port pins can be configured as input or output. See table for all general pin configurations: DDRx register is so called direction register; PORTx – is port output register; PINx – is pin input regsiter; So there can be three options for input and two for output. If you are doing some simple routines with AVR microcontrollers, you maybe are familiar with configuring output from port. Just write ‘1’ to DDRx register and then send data to PORTx register: For instance: DDRD=0x0F; //sets lower nibble as output; PORTD=0x05 //output ‘1’ to PORTD pins 0 and 2; If you want to read input signals there are normally couple ways to do this. For input DDRx register always should be set as input ‘0’ pin values. PORTx register can be set in two ways: If PORTx is set to ‘1’, then internal pull-up resistor is enabled depending on PUD bit in SFIOR register. If PORTx pin is set to ‘0’ then internal pull-up is disabled despite PUD…

Continue reading

Simple routine how to store data in microcontroller flash and read from it using WinAVR

I’ve been asked about how to store data table in flash memory using winavr toolset. I decided to post an answer here. Might be someone find this useful. To demonstrate this I have set up a project using VMLAB simulator. The files you can download from here: Small project using VMLAB simulator So I created project in VMLAB. You should read my previous article ho to do this: Using VMLAB as virtual oscilloscope In project window I have connected 8 LED’s to port D by typing this: D1 VDD PD0 D2 VDD PD1 D3 VDD PD2 D4 VDD PD3 D5 VDD PD4 D6 VDD PD5 D7 VDD PD6 D8 VDD PD7 Then I wrote a simple C program: In program you see, that data can be stored in flash memory using data describing sentence: const uint8_t digits[] PROGMEM={your data}; To make PROGMEM macro work you have to include library pgmspace.h. Bellow you see result of simulation in VMLAB environment:

Continue reading

Internal microcontroller ADC

Many microcontrollers contains internal on-chip ADC. Typical devices would be Atmel Atmega series microcontrollers like Atmega8 and further. Internal ADC are successive way to integrate analog world in to your embedded systems using only one microcontroller die. Many applications doesn’t require high speed or high accuracy ADC conversions, thus on-chip ADC is best choice. Lets look at Atmega8. It contains 10-bit approximation ADC with analog input multiplexer of 8-single-ended input voltage (refers to 0V) channels. Atmega microcontrollers have separate analog supply voltage pins – AVCC (Atmega8 has known bug – digital and analog grounds are interconnected inside chip). Reference voltage is provided inside chip. It is nominally 2.56V or AVCC. AREF pin is used to decouple voltage reference by a capacitor for better noise performance. The reference voltages nay be selected between: Internal reference 2.56V; AVCC (must not differ from VCC more than ±0.3V); Or reference voltage can be connected to external pin AREF. If we have 8 bit ADC, then our conversion would look like this: Digital Code=(Vin/Vref)x256 Lets say we use 5V reference and we measure 3.2V input signal. So result: Result=(Vin*256)/Vref=(3.2V*256)/5V=163=0xA3 Just remember, that on-chip ADC accuracy isn’t perfect. Atmega8 datasheet states that absolute accuracy is ±2LSB.…

Continue reading