miercuri, 26 septembrie 2012

Iniţiere în programare ASM. Stiva. Subrutina. Subrutina cu parametri.


    Initiere in programare ASM

    Entitatea de baza a limbagului ASM este comanda.
    Vom numi program o consecutivitate de instructiuni (comenzi) ce se executa una dupa alta.
   Programarea in limbajul ASM reprezinta accesul la memorie si modificarea volorilor la aceste adrese utilizand setul de comenzi. Limbajul permite gruparea secventelor de comenzi in subrutine, executia conditionata a secventelor de program realizata in baza salturilor conditionate si neconditionate.

    Structura unui program in ASM

    Un microcontroller in orice moment de timp trebuie sa execute o instructiune definita de programator. Atunci cand microcontrollerul executa o comanda definita de programator vom spune ca controllerul se afla sub controlul programului.Si invers, In cazulin care se executa o comanda care nu a fost definita de catre programator vom spune ca microcontrollerul a iesit de sub control.
    Un program ce ruleaza pe microcontroller trebuie sa mentina microcontrollerul intotdeauna sub control ceia ce presupune existenta unei bucle infinite in program.

joi, 20 septembrie 2012

Sistemul de comenzi al microcontrollerului AVR

 
 Microcontrollerul AVR este un MCU tip RISC cu arhitectura Haward fapt ce permite execuţia a majorităţii operaţiilor într-un singur tact. Această performanţă se datoreaz faptului că sunt excluse cheltuielele de timp pentru transferul de date din memoria de program. Excepţii de la regula "execuţia intr-un singur tact" comenzi care operează cu date pe 16 biţi, salturi, transfer a controlului, etc.
    O comandă Assembler presupune o acţiune pe care o poate efectua un MC. Comanda deţine un nume şi operanzii de lucru, stîng şi drept. Există comenzi comenzi cu un singur operand sau fără nici un operand.

   Operandul stîng serveşte drept destinaţie a rezultatului operaţiei presupusă de comanda respectivă. În calitate de operand poate fi un registru de uz general (R), o constantă (k), oadresă a memoriei sau un registru periferic (P)
    Comanda ASM are urmatorul format:
<mnemonica> <operand stang>, <operand drept>
    • mnemonica - denumirea comenzii care este o prescurtare de la operaţia pe care o execută.
    • operand - adresa de aces la un resurs a Microcontrollerului sau o constantă pentru operaţiile cu constante.

marți, 18 septembrie 2012

Peripheral modules. GPIO Peripheral Module - Input/Output Generic Port


Peripheral Modules

A microcontroller seen as a whole represents a structure composed of a microprocessor and a set of peripheral modules. A microcontroller can have a range of peripheral modules depending on its series. A peripheral module as a rule is a specialized circuit that can perform a certain function. See above some function examples: 

• GPIO allows setting or collection of logic level on a microcontroller pin
• EEPROM allows long-term data storage.
• ADC allows conversion of analog signals
• Serial UART allows serial communication

• TIMER offers the possibility to generate time intervals, measure the time between two events, generate digital signals of certain forms.



In a certain case we could say that peripherals represent the microcontroller interface with the external world.

The microprocessor (CPU) interaction with peripherals is performed via peripheral registers. The AVR architecture has 64 registers, called peripheral registers or I/O registers.

             Each peripheral module has in the peripheral registers space a reserved set of registers. For example, each GPIO module has reserved a set of 3 registers called PORT, PIN and DDR. By use of these registers the microprocessor is able to work with the pin logic levels. We could do a classification of peripheral registers depending on their destination as follows:

• Status Registers - offer the possibility to visualize the current state of a peripheral (flags). As a rule status registers are available only for reading.
• Control Registers - allow configuration of the peripheral module. They are available for both writing and reading.
• Data Registers - enable data exchange between  the peripheral module and kernel.
• Selection Registers - allow selection of resources / configurations in a peripheral module.    

In many cases peripheral registers don't have a well-defined purpose, ie a register might contain a collection of status bits, selection control. The purpose of peripheral registers can be recognized by their name and often by their termination: _SR, _CR, _DR, _AR, however it's not a rule.
The microprocessor (CPU) doesn't distinguish the peripheral modules. It "sees" the peripheral modules through peripheral registers and the the work with the peripherals occurs through data transfer with an address from the peripheral registers space. The access to peripheral registers will be performed by a special set of commands.
Transfer commands. These commands involve transfers on 8 bits:

    in  R, P - content transfer of a peripheral register to a general purpose register.
    out P, R  - content transfer of a general purpose register to a peripheral register.

The commands of setting/resetting of unique bits in a  peripheral register will affect only the bit specified by n, the remaining bits will keep the value they had before the operation:

    cbi P, n  - assigning logical "0", resetting the bit n of port P  n = 0 .. 7 
    sbi P, n  assigning logical "1", setting the bit n of port P   n = 0 .. 7 
     
Verification / testing commands of status bits within a peripheral register, ignoring the next operation if the check is true:

    sbic P, n - ignoring the following command in case the bit in the port P is "0", reset.
    sbis P, n - ignoring the following command in case the bit in the port P is "1", set.

        Before you start working with a specific peripheral in most cases will be required its configuration / initialization. The process of initialization will be  performed through setting the configuration to certain control peripheral registers. In some cases the configuration after reset can be ok, but it is recommended each peripheral module to be initialized on the entire set of configuration bits. The configurations of each peripheral module can be found in the technical documentation of the corresponding peripheral module.

In conclusion we can affirm that peripheral modules represent electrical devices attached to processor with  access to peripheral registers.

In order to understand better what a peripheral module is, we could imagine a complex panel, for example inside an airplane. What is seen on this panel are many light bulbs, switches, indicators, etc. Each bulb can be associated with a status bit, a switch with a configuration bit, an alphanumeric indicator - output data etc.

Peripheral modules can generate interrupts in case of appearance of special situations detected by peripheral, such as changing peripheral logic level on the pin, completion of data transfer, conversion completion etc. But we will talk about interruptions later on.


GPIO Peripheral Module - Generic I/O Port.

        Any microcontroller has a set of pins, most of which can be configured as generic input/output (GPIO) pins so that it will be possible to assign a logical value to   the terminal pin or this logical value to be read from it in case the pin is configured to the input.

         The input/output pins are grouped into ports by 8. The work with the port is executed as a whole, therefore, the single transfer operation to port can change at once the configuration of all 8 pins. In result, the transfer operations to port will affect all its pins.

Since GPIO module is a peripheral module of the microcontroller it has reserved a set of registers in the address space of peripheral registers.


Each GPIO module of a microcontroller of AVR architecture has 3 peripheral registers: PINx, DDRx and PORTx.

The explanation of how does the GPIO module work is presented by the following figure:


• PINx - used for reading the logical value of the physical terminal. This register has read only access. The write operation to this register will not affect the physical value of the physical terminal.
• PORTx - register for setting a value of port when it is set to output and activating the pull-up resistance in case when it is set to input.
• DDRx - register for setting the direction of port, input or output.

In case the location of DDRx contains "1", the corresponding pin will be set to output and the logical value from PORTx from the same location will be transfered towards physical terminal. In this case the value of PORTx register will conect the pull-up resistance with logical value "1", the logical value "0" will set the pin in the high impedance state - "HiZ".

It is recommended to avoid the HiZ on terminal, fact that occurs when the terminal isn't conected to a signal source in order to avoid the noise appearance inside the microcontroller chip that in critical cases can result in circuit failure.

In this way the recommendation for the unused pins is to set their configuration as input with activated pull-up resistance - action which will avoid accidental short circuits when it is set as output and avoid noise appearance on unwanted inputs when it is set to input.

Module Periferice. Modulul periferic GPIO - Port Generic de Intrare/Ieşire.


      Module Periferice

    Un microcontroller, privit în ansamblu, reprezintă o structură formată dintr-un microprocesor şi un set de module periferice.  Un microcontroller poate avea o combinaţie variată de module periferice în dependenţă de serie. Un modul periferic ca regulă este un circuit specializat care poate îndeplini o anumită funcţie. De exemplu:

  • GPIO permite setarea sau colectarea nivelului logic de pe un pin al Microcontrollerului
  • EEPROM permite stocare pe timp îndelungat a datelor
  • ADC permite conversia semnalelor analogice
  • UART permite comunicarea serială
  • TIMER ne dă posibilitatea sa generm intervale de timp, să masurăm timpul între două evenimente, să generăm semnale digitale de o anumită formă.

     Într-o măsură oarecare am putea spune ca perifericile reprezintă interfaţa Microcontrollerului cu lumea externă.
Interacţiunea microprocesorului (CPU) cu perifericile are loc prin intermediul regiştrilor periferici. În arhitectura AVR există 64 asemenea regiştri, numiţi regiştri periferici sau I/O regiştri.

luni, 17 septembrie 2012

Microcontroller Architecture. Memory organization

На русском     În română

Before proceeding to analysis of microcontroller architecture the followng definitions will be given:

Microprocessor - a circuit that can perform any function depending on the program that it has to execute.
Program - a set of instructions executed one after another.
Instruction - an action that can be executable by the microcontroller core.
Core - main part of microcontroller that executes instructions in the program.
Microcontroller - a minicomputer that includes the following components: microprocessor core, program memory and peripheral modules. In general the MCU (microcontroller) takes the information from input interfaces, processes it and applies the result to the output interface.


            Microcontroller Core Architecture
             The AVR microcontroller has an 8-bit RISC architecture built on the Haward architecture principle. Such architecture allows most instructions to be executed in a single clock of the clock signal. Therefore, at a frequency of 1 MHz clock signal, can be executed up to one million operations per second - 1MIPS / MHz. Schematic AVR architecture can be represented by the figure below:



  • PM - Programming memory. Here are stored the instructions that have to be executed by the MCU.
  • PC - Program counter based on a register counter that selects the instructions from program memory. Normally, the PC itself performs the automatic modification through increments ensuring the successive implementation of  instructions from the PM. However, it may load a value of CDC, which will perform jumps to conditional and unconditional jump instructions.
  • CDC - command decoder. The CDC configures the core to execute the selected instruction taking into account the flag values of SREG status register.
  • ALU - arithmetic logic unit. It executes arithmetical and logical instructions. The data to ALU is supplied by general purpose registers, each operand being selected by the CDC according to the current instruction, as well as data storage.
  • GPR - general purpose registers, working registers of the MCU. Data processing for the AVR architecture is restricted to the use of at least one general purpose register, ie any transfer instruction or data processing occurs involving a general-purpose register.
  •    RAM - random access memory, working memory for big data.
  • SREG - state register of MCU. Any data processing operation involving their processing by ALU, reports to the SREG register with changing its flags, presenting additional data about the operation result. It is placed in the address space of peripheral registers. The content of this register is accessible for writing and reading.
              • C - carry bit. It will be set to 1 in case the result representation will exceed the limit                       of 8 bits.
              • Z - Zero. It will be set to 1 in case the result will be equal to 0.
              • N - Negative. It will be set to 1 in case the 7th bit of the result will be 1.
              • V - carry in signed operations.
              • S - sign. It will be set to 1 if the result will be smaller than 0.
              • H - mid-carry. It will be set to 1 in case of occurence of carry at 4th bit (in the                               middle of  the outcome/result).
              • T - temporarily bit. Used for storing 1 bit information of a general purpose register.                       It can be written / read by BST / BLD.
              • I - interrupt permission bit. If it is set to 0 the interrupts are disabled, if 1 - allowed.                       It can be set or reset by CLI / SEI commands or when the interrupt processing                        subroutine is automatically called/returned by RETI.
              • I / O Reg - access registers of peripheral modules such as GPIO, Ext INT,                                  TIMER, etc.


The instruction execution inside the microcontroller core

The instruction itself is selected from PM (program memory) by using the PC (program counter) and is sent to the command decoder (CDC). The CDC decodes the command and generates the core configuration signals.

If the instruction is a processing, arithmetical or logical one to ALU will be generated a function selection signal and  addresses for the left LA and right RA operand. The address for storing the result coincides with the address of the left  operand LA. Once the result evaluation is performed will be generated the processor status bits that are stored in the SREG status register.

In case the instruction is a conditional jump instruction, if is satisfied the assumed condition of instruction by checking the state bits in SREG, will be generated a jump address to be registered in the program counter (PC). Similarly is generated the jump address for unconditional jump instructions, the only difference is that here are taken into consideration the SREG status bits.

For carry operations are generated the addresses to memory modules from which the carry is performed. It is important here to emphasize that for the AVR architecture are possible carry operations in which is strictly involved a general-purpose register (GPR).


Memory organization in AVR architecture
In the AVR architecture are present the following memory types:

• PM - Programming Memory
• GPR - General Purpose Registers
• I / O - Peripheral Access Registers
• RAM - Random Access Memory
• EEPROM - Permanent Memory



Memory blocks are grouped into three address spaces: program memory, data memory and EEPROM, representing three segments: code segment, data segment and EEPROM segment.

Unlike the program segment and the EEPROM segment, the data segment is made up of three memory types (as can be seen from the above figure). Therefore, general purpose registers, peripheral registers and RAM memory are placed in the same address space.

          The operand registers in the most arithmetical logical and carry operations are placed starting with general purpose address 0x000, are found in t0 and occupy 32 locations.

The peripheral registers are placed immediately after the general purpose registers, starting with the address 32 (0x0020) and are used for access to the microcontroller peripheries.

Immediately after I/O registers is situated the address space for RAM memory that can be divided into two parts: Internal memory that is a component of the microcontroller chip and External memory that can be attached to it through certain pins.

Arhitectura Microcontrolerului. Organizarea memoriei


     Înainte de a trece la analiza arhitecturii Microcontrollerului vom da câteva definiţii:
    Microprocesor - un circuit care poate îndeplini oricare funcţie în dependenă de programul care îl are spre execuţie.
    Program - un set de instrucţiuni care se execută una după alta.
    Instrucţiune - o acţiune pe care o poate executa nucleul Microcontrollerului.
    Nucleu - Partea principală a Microcontrollerului care execută instrucţiunile din program.
    Microcontroller - minicalculator care cuprinde în sine un nucleu de microprocesor, memorie de program şi    Module periferice. În general MCU preia informaţia de la interfeţele de intrare, o prelucrează  şi aplică rezultatul către interfaţa de ieşire.