Control memory sections using AVR GCC

If you programming AVR microcontrollers in C usually you don’t think how compiled program is stored in microcontrollers flash memory. Compiler organises data in the way it looks optimal. But sometimes you are working with programs where you need you code chunks located in specific program memory locations. For instance faced this problem while developing an AVR controlled signal generator. I wanted to make efficient and compiler independent main loop where the signal has to be read from flash memory and transferred to port. I managed to use in line ASM function which does the job: //…………………………………………………….. void static inline signalOUT(const uint8_t *signal, uint8_t ad2, uint8_t ad1, uint8_t ad0) { asm volatile( “eor r18, r18 ;r18

Continue reading

How to measure signal period using microcontrollers

Measuring signal period is common problem in embedded systems. This can be measuring time between two events or measuring signal frequency f=1/T and so on. Measuring of time interval or period is based on comparing of event time t with discrete time usually produced by timer. This usually is done by filling the event time t with discrete time intervals Δt. According to this, discrete time signal period has to be much shorter than event time: Δt

Continue reading

Why to move from ASM to C

ASM language is a low level programming language. It takes tons of time to develop embedded programs. Now even 8 bit microcontrollers arent as smal as they were earlier. The program memories are climbing to megabyte(s). Program structure becoming more complicated because of bigger functionality demand. This is why it is better to use higher level programming languages like C. By using C language you are not overwhelmed by details. You don’t have always to think about hardware logic to be able to program its restricted tasks. It is better to give this work to C compiler which helps you to avoid bugs in silicon level. Another C language benefit against ASM language is portability. Lets say you work on one embedded system architecture and decide to move to other maybe more advanced. If your previous program were written in ASM language, then you will need to rewrite (modify) this code from scratch. Using C language you are able tu run program on different microcontroller without significant modifications. This also reduces the costs of your project upgrade. Continuing the thought it is good to mention, that using C it is easy to save specific hardware routines to libraries which are…

Continue reading

Shelling The Intel 8-bit Hex File Format

Intel 8-bit Hex File Format is the most common hex file format used in the world as far as I know. There is also Motorola Hex file format and maybe other. Creating applications with AVR-GCC we usually select ihex output file format what means Intel hex file format. Lets go through it and see whats inside. It is simple as 6 and 6 (six and six), because each Hex file line consists of six parts. And there can be 6 record types in hex file. Lets go through all six parts of each line: Start code is always character ‘:’; Byte count takes one byte (hex pair) indicating a number of bytes in a data field of the lin. Usually there are 16 or 32 bytes of data in each line; Address takes two bytes (16 bits – four hex digits). Address shows the beginning of memory position for the data. 16 bits gives a limit of 64kilobytes. This is worked around by specifying higher bits via other record types; Record type takes one byte (two hex digits). It defines the type of data field; Data is a sequence of n bytes (2*n hex digits); Checksum is one byte (two…

Continue reading

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