• No se han encontrado resultados

4. RESULTADOS

4.3. ELABORAR UN PROTOTIPO QUE LE PERMITA EVALUAR LA

4.3.2. ANALIZAR LAS TAREAS Y NECESIDADES DE LOS USUARIOS

The datasheets for microcontrollers are generally several times that of other devices, often running into the hundreds if not thousands of pages. This is due in part to the number of devices, called peripherals, that are internal to the microcontroller, but also due to the required listings of all the special function registers (SFRs).

SFRs are specific locations in the memory map that have predefined tasks. The memory map is the linking of a memory address, which is the value used to determine where the desired information is, to the physical location of that data. For the majority of memory addresses, these locations are within a block of RAM (random-access memory). For SFRs, however, these addresses point to buffers, latches or other devices in the hardware related to each peripheral, not to the RAM. Each SFR is connected to one of the various peripherals in the microcontroller (ADC, timer, input/output etc.) or to the processor itself. The SFRs serve as the connection between the software and hardware. The majority of these registers are for settings and status information, with every bit field meaning something different. A bit field is one or more bits that relate to the same very specific operation, such as a 2-bit field choosing one of four modes of operation. Each register can contain several bit fields relating to different options on the peripheral. The datasheets must therefore list not only each register, but each bit of each register and explain what they do.

2.2.1

SFRs in Datasheets

Although specifics change from manufacturer to manufacturer, the sections of the datasheets relating to SFRs are always similar. For each peripheral in the microcontroller, a list of related SFRs is given. Each SFR entry has a listing of each bit, and what they do, including whether the can be read or written. Elsewhere there will also be a large table of all the SFRs and their memory ad- dresses, which may also include the bit names, though without the descriptions. Figure 2.4 presents an example from the ADC of Atmel’s ATmega644p.

This is one of the control and status registers for the ADC. The first line gives the name of the register as used in the libraries and elsewhere in the datasheet, followed by a more human-readable name. Note that all the register and bit names are some form of abbreviation of their full name. The small table below the register name is a listing of the bits. Each bit has a unique name. The second to last row of the chart indicates whether the bit can be read and/or written. An ’R’ indicates that the bit can be read, and a ’W’ for written. All the bits in this register can be both read and written. The final row gives the default value of the bit when the microcontroller is first powered on. In most cases this is 0, but not always.

Following the table is a listing of each bit: its bit number, its code name, its full name and a description. Often times the description is just text, however on occasion there is a table as well. Tables most often occur when there is a bit field of more than one bit, such as bits 0 through 2 in this register. Wider bit

Figure 2.4: ADCSRA Register Excerpt from ATmega644p datasheet.[11]

fields can be recognized because their names are identical except for a number at the end. When referring to the bit field as a whole either the number can be left off (ADPS) or the size of it can be specified by giving a range of bits (ADPS2:0).

Figure 2.5: ADPS2:0 Bit field from ADCSRA Register from ATmega644p datasheet.[11]

2.2.2

Addressing SFRs

As SFRs exist in the memory map, each one has a unique memory address. While they can appear anywhere in data memory, they are generally either at the bottom (near address 0x00) or near the top of the memory map. There may also be a number of general purpose registers (GPRs) which are used as temporary

storage and do not relate to the peripherals. Both PIC microcontrollers and Atmel’s AVR microcontrollers place the SFRs at the bottom of the data memory map, along with a number of GPRs.

Within the code, there are a few methods of accessing the contents of the SFRs, depending on how the code libraries are set up. PIC microcontrollers, for example, use prebuilt data structures called structs to access the individual bit fields of the SFRs, while AVR microcontrollers use bitmasking on predefined variables. As this text focuses on the ATMega644p microcontroller from Atmel’s AVR line of microcontrollers, bitmasking is the method that will be examined here. For additional information on using structs, both in general and for accessing SFRs, refer to chapter 6.

In and amongst the numerous files included in WinAVR (the development kit for AVR microcontrollers for Windows users), is a file called iomxx4.h. This is the file that maps all the SFR register addresses to names for the developer to use for the family of microcontrollers the ATmega644p belongs to. In it, for every SFR on the microcontroller, is a line similar to this:

# d e f i n e A D C S R A _ S F R _ M E M 8 (0 x68 )

This statement tells the compiler that any time the name ADCSRA appears in the code, to replace it with SFR MEM8(0x68). This function is defined elsewhere in the assorted libraries and accesses the given location in the memory map, in this case 0x68. After each register definition is a listing of each bit in the register, defining each name to be the bit’s location in the byte.

# d e f i n e A D I E 3

The above line of code causes any appearance of ADIE to be replaced with 3, as ADIE is the fourth bit in the ADCSRA register. Bits are zero indexed in a byte, which means that the first bit is called bit 0.

With all the registers and individual bits named, it is possible to read and write to the SFRs. Technically its possible to not use the names, however it becomes much more difficult both on the initial programmer and on anyone who needs to look at the code later to understand what is going on. To this end, there are several methods of reading and writing to the registers.

Reading an SFR

Retrieving data from an SFR can have several purposes. It can be used to retrieve data, inquire about the specific status of the peripheral, or be used as part of writing a specific bit (see below). The simplest case is when the contents of the entire SFR need to be read, such as when accessing the results of an analog to digital conversion:

c h a r A D C R e s u l t = A D C H ;

This reads the entire contents of the ADCH register and saves it to the single byte variable ADCResult. If only specific bits are desired, a bitmasking process must be used.

c h a r A D C E n a b l e d = A D C S R A & (1 < < A D E N );

This retrieves the value of only the ADEN (ADC Enable) bit of the register using a bitwise AND operation. A bitwise and compares each bit of the left argument (ADCSRA) with the corresponding bit of the right hand argument (1 <<ADEN) and saves the result of each of these comparisons in the corresponding bit in ADCEnabled. If the ADC were enabled, the result would be 0b10000000 else it would be 0, as the ADEN bit is bit 7. Another option is to replace (1 <<ADEN) with BV(ADEN) which accomplishes the same purpose. The function BV() is defined in the libraries supplied with the compilers to have the same result as the left shifting operation performed in the code example above. Writing to an SFR

While writing a specific value to an SFR is easy, it is not commonly done. Far more often only a single bit or two need to be changed, which becomes more complicated as then entire byte must always be written simultaneously. To avoid changing the other bits, they must first be read and then masked to prevent them from changing. To set a specific bit, use a bitwise OR operation such as: A D C S R A = A D C S R A | (1 < < A D E N );

After this executes, the ADC will be enabled whether or not it was before. In order to clear a bit however, the register must be bitwise ANDed with a bitwise NOT of the left-shifted bit.

A D C S R A = A D C S R A & ~(1 < < A D E N );

After being executed, this line will ensure that the ADC is disabled without changing any other bit. This works because any bit that was enabled, excepting the ADEN bit, will result in an enabled bit in the final result as it is being ANDed with another high bit. The ADEN bit is the only bit in the right hand argument that is not high after the bitwise not.

While the concept of SFRs, their names and the logic required to access them may seem daunting at the moment, they will become second nature through use and the logic will become a matter of recognizing a pattern rather than working out the steps every time.

Documento similar