Easy start with AVR EEPROM using WinAVR

AVR microcontrollers are loaded with some amount of EEPROM (Electronically Erasable Read-Only memory ) memory. This is handy feature allowing developers to store program parameters like service information, constants, menu strings etc. Atmel states that AVR EEPROM memory can be rewritten over 1000000 times. Reading is unlimited. In this article I am going to show how to store data to EEPROM by defining a variables. For this we need to include eeprom.h header from avr directory (#include “avr/eeprom.h” ). Then we can just write simple variable declaration using simple attribute EEMEM: #include “inttypes.h” #include “avr/io.h” #include “avr/iom8.h” #include “avr/eeprom.h” //store initial byte to eeprom uint8_t EEMEM eeprombyte=0x10; //store initial word to eeprom uint16_t EEMEM eepromword=0x5555; //store string to eeprom uint8_t EEMEM eepromstring[5]={“Test\0”}; int main(void) { //RAM byte variable uint8_t RAMbyte; //RAM word variable uint16_t RAMword; //RAM array of bytes uint8_t RAMstring[5]; //read byte from EEPROm and store to RAM RAMbyte = eeprom_read_byte(&eeprombyte); //read word from EEPROM and store to RAM RAMword = eeprom_read_word(&eepromword); //copy string fro mEEPROM to RAM eeprom_read_block ((void *)&RAMstring, (const void *)&eepromstring,5); return (0); }   EEMEM keyword indicates to compiler that variables are stored in EEPROM and it creates separate .eep file which has to be…

Continue reading

Signal sampling mechanism

Sampling is a process when continuous time signal is represented by series of discrete samples while reconstruction is reverse process when these samples re recreating adequate continuous time signal. Bellow the overall process is illustrated. Sampling is a process when continuous time signal is recorded every T seconds by multiplying by an impulse train. If signal is sampled in frequency domain. For this we need signal transformed in to frequency domain. The frequency spectrum has to be band-limited. The impulse train after transformation becomes impulse train with scale in heigh 1/T. Multiplication in time domain becomes a convolution in frequency domain. After convolving the signal is scaled and shifted. The signal in frequency domain becomes periodical. In order to reconstruct the signal x(t) from sampled spectrum first we need to extract the original spectrum. For this ideal filter is needed to take single spectrum from spectrum signal train. Low pass filter fill cut other copies of spectrum. The filter should have cut off at f=±1/(2T) and Gain of T: After this reconstruction can be done. Ideal filter in frequency domain is a sinc function in time domain. This means that multiplying spectra by box in frequency domains means convolving with…

Continue reading

RS232 interface standard overview

This is pretty old standard but stil widely used in embedded systems. Using RS232 interface standard the ata is sent bit by bit. Usually first comes LSB. Receiver receives data by knowing the position of each data piece and delay. In order to ensure the quality of data transmission, we need to control the start of transmission. This is done by acknowledgment procedure. Lets take assymetrical type of interface RS232-C. Transmitter sends RTC (request to send) signal to receiver. In other hand receiver detects this signal, finishes previous operation and then sends to receiver CTS (clear to send) signal, what menas that receiver is ready to accept data. Without CTS transmitter cannot start data transmission. Note: In RS232 interface logical “1“ corresponds to voltages from -3V to -12V and logical “0“ corresponds to voltages from +3V to +12V. The logical level in interval -3V to +3V is undefined. Lets take some example. If we want to send byte 1100111011 This byte is sent assynchronously. This means that receiver doesn’t know when transmitter will start sending data. But anyway there is some means needed to inform about the start of transmission. For this is START bit used at the beginning of…

Continue reading

AVR watchdog reset timer-practical approach

This is continuing of thread Why use watchdog variable timer. This post is about how watchdog timer on AVR microcontroller works and how to control it. As we mentioned earlier, watchdog timer is a distinct timer counter, which generates reset signal when it fill up. After watchdog timer counts up to maximum, it generates a short pulse duration 1 clock cycle. This pulse triggers internal reset timer counting up to tout. AVR watchdog timer is distinct clock generator which runs at 1 MHz. Watchdog timer has a prescaler module. So reset interval can be selected by adjusting the prescaler. Generally there are three things you have to do while controlling watchdog timer: enable, disable, and set prescaler. First of all we need to set watchdog timer prescaler. Prescaler settings can be set in WDTCR(WatchDog Timer Control Register) register(Atmega8). Prescaler is set by setting three bits (WDP2, WDP1, WDP0) of WDTCR register(see table bellow): In order to be able to control watchdog timer, there some logic needs to be preserveed. In order to enable watchdog timer just set WDE bit in WDTCR. Disabling and setting prescalelr is more complicated because some sequence of operations has to be made. Write „1“ to…

Continue reading

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