Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:avr:registers [2010/09/24 14:49] allan.pettaien:avr:registers [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 16: Line 16:
   * Pause   * Pause
  
-Each button does something, but only when it is used correctly. For example, the stop button does nothing unless a tape is playing - only then will it do something apparent and stop the playback. Forward and rewind buttons, on the other hand, can be pressed at any time, because the tape can be wound in both directions, no matter if it is playing or stopped. The recording begins only when the play and record buttons are pressed down simultaneously. Some may have tried to depress several or all buttons at once - in this case, the tape player might have done something unexpected or break altogether.+Each button does something, but only when it is used correctly. For example, the stop button does nothing unless a tape is playing - only then will it do something visible and stop the playback. Forward and rewind buttons, on the other hand, can be pressed at any time, because the tape can be wound in both directions, no matter if it is playing or stopped. The recording begins only when the play and record buttons are pressed down simultaneously. Some may have tried to press down several or all buttons at once - in this case, the tape player might have done something unexpected or break altogether.
  
 Microcontroller registers behave like buttons on a tape player - each button does something, when used correctly. By pressing the wrong buttons, a microcontroller usually won't break, but it won't work either. In reality, there are no buttons in the registers, instead there are a whole lot of transistors, which turn the electricity on and off. Simpler microcontrollers have 8 transistor-based switches in a single register. A register can be thought of as an 8-bit number, where every bit is marked by the state of one of those switches. For example, a bit value of 1 can mean that the switch is on and 0 that the switch is off. Microcontroller registers behave like buttons on a tape player - each button does something, when used correctly. By pressing the wrong buttons, a microcontroller usually won't break, but it won't work either. In reality, there are no buttons in the registers, instead there are a whole lot of transistors, which turn the electricity on and off. Simpler microcontrollers have 8 transistor-based switches in a single register. A register can be thought of as an 8-bit number, where every bit is marked by the state of one of those switches. For example, a bit value of 1 can mean that the switch is on and 0 that the switch is off.
Line 22: Line 22:
 [{{  :images:logic:register_buttons_and_bits.png?240|Register's "buttons" and bit values}}] [{{  :images:logic:register_buttons_and_bits.png?240|Register's "buttons" and bit values}}]
  
-Since the state of a register's switches can easily be displayed as a number and vice versa, a register can be compared to a memory, which can hold data in the size of one number. By this comparison, we see that registers actually are memory slots. The difference between a register and a memory slot is that a memory slot only stores the information, but in a register this information actually controls something. For example, if a binary value of 01100001 is written to a register, then three imaginary buttons are pressed down and something happens.+Since the state of a register's switches can easily be displayed as a number, a register can be compared to a memory, which can hold data in the size of one number. By this comparison, we see that registers actually are memory slots. The difference between a register and a memory slot is that a memory slot only stores the information, but in a register this information actually controls something. For example, if a binary value of 01100001 is written to a register, then three imaginary buttons are pressed down and something happens.
  
 On a tape player it is possible to press each button separately, but in a register it is more difficult to change the value of one "switch" or bit. Typically it is necessary to change the entire content of the register. Before moving on to bit changing, one should know that there are a lot of registers in a microcontroller. Some parts of the microcontroller use tens of registers to control them. The variety of registers means that there has to be a way to distinguish between different registers and that is done by naming the registers. One register, for example, is called PORTB. Actually, these names are just to make things easier for the developer and each name corresponds to a numeric address. On a tape player it is possible to press each button separately, but in a register it is more difficult to change the value of one "switch" or bit. Typically it is necessary to change the entire content of the register. Before moving on to bit changing, one should know that there are a lot of registers in a microcontroller. Some parts of the microcontroller use tens of registers to control them. The variety of registers means that there has to be a way to distinguish between different registers and that is done by naming the registers. One register, for example, is called PORTB. Actually, these names are just to make things easier for the developer and each name corresponds to a numeric address.
Line 39: Line 39:
 [{{  :images:logic:logic_hexadecimal.png?209|Hexadecimal numbers}}] [{{  :images:logic:logic_hexadecimal.png?209|Hexadecimal numbers}}]
  
-In hexadecimal, the numbers are not only 0 and 1 as in binary or 0 to 9 as in decimal, but instead 0 to F. A hexadecimal number consists of four bits. The table on the right shows the binary numbers and their hexadecimal counterparts. Binary numbers are converted to hexadecimal by reading bits four at a time, starting from the lowest rank. Ranks are read from right to left and their numbers start from 0. For example, the lowest ranked (rank 0) bit is 0 and the highest (rank 3) is 1. In the previous example, the register's binary value is 01100001, which is 61 in hexadecimal and is written as 0x61 (leading zero) in C.+In hexadecimal, the numbers are not only 0 and 1 as in binaryor 0 to 9 as in decimal, but instead 0 to F. A hexadecimal number consists of four bits. The table on the right shows the binary numbers and their hexadecimal counterparts. Binary numbers are converted to hexadecimal by reading bits four at a time, starting from the lowest rank. Ranks are read from right to left and their numbers start from 0. For example, the lowest ranked (rank 0) bit is 0 and the highest (rank 3) is 1. In the previous example, the register's binary value is 01100001, which is 61 in hexadecimal and is written as 0x61 (leading zero) in C.
  
-To change single bits in a number (register, variable or anywhere else for that matter) it is necessary to use binary operations. Binary operation is an operation between two binary numbers, where each bit of the numbers is subject to its own logical operation. Typically a microcontroller supports four binary operations, each having several names. The following section describes the logical operation behind each of these four binary operations with a single bit or multpile bits.+To change single bits in a number (register, variable or anywhere else for that matter) it is necessary to use binary operations. Binary operation is an operation between two binary numbers, where each bit of the numbers is subject to its own logical operation. Typically a microcontroller supports four binary operations, each having several names. The following section describes the logical operation behind each of these four binary operations with a single bit or multiple bits.
  
 [{{  :images:logic:logic_all_4.png?550  |Negation, logical multiplication, logical addition and exclusive disjunction }}] [{{  :images:logic:logic_all_4.png?550  |Negation, logical multiplication, logical addition and exclusive disjunction }}]
  
-~~PB~~+<pagebreak>
  
   * **Negation / Inversion** \\ Negation changes the bit's value to its opposite, a 0 becomes a 1 and vice versa. In C, negation is marked with "~".   * **Negation / Inversion** \\ Negation changes the bit's value to its opposite, a 0 becomes a 1 and vice versa. In C, negation is marked with "~".
Line 56: Line 56:
 ~~CL~~ ~~CL~~
  
-==== Setting a single bit high ====+==== Setting a Single Bit High ====
  
 [{{  :images:logic:op_bit_set.png?230|Setting a single bit high}}] [{{  :images:logic:op_bit_set.png?230|Setting a single bit high}}]
Line 71: Line 71:
 </code> </code>
  
-==== Setting a single bit low ====+==== Setting a Single Bit Low ====
  
 [{{  :images:logic:op_bit_clear.png?229|Setting a single bit low}}] [{{  :images:logic:op_bit_clear.png?229|Setting a single bit low}}]
Line 86: Line 86:
 </code> </code>
  
-~~PB~~+<pagebreak>
  
-==== Inverting a single bit ====+==== Inverting a Single Bit ====
  
 [{{  :images:logic:op_bit_invert.png?229|Inverting a single bit}}] [{{  :images:logic:op_bit_invert.png?229|Inverting a single bit}}]
Line 103: Line 103:
 </code> </code>
  
-==== Inverting the whole register ====+==== Inverting the Whole Register ====
  
 [{{  :images:logic:op_reg_invert.png?229|Inverting all bits}}] [{{  :images:logic:op_reg_invert.png?229|Inverting all bits}}]
  
-To invert all bits in a register a negation operation is used. This operation is unary, which means it has only one operand. Below is the C code for the operation shown on the right:+To invert all bits in a registera negation operation is used. This operation is unary, which means it has only one operand. Below is the C code for the operation shown on the right:
  
 ~~CL~~ ~~CL~~
Line 117: Line 117:
 </code> </code>
  
-==== Reading the value of a single bit ====+==== Reading the Value of a Single Bit ====
  
 [{{  :images:logic:op_bit_get.png?229|Reading the value of a bit}}] [{{  :images:logic:op_bit_get.png?229|Reading the value of a bit}}]
Line 131: Line 131:
 </code> </code>
  
-~~PB~~+<pagebreak>
  
-==== Shifting a bit ====+==== Shifting a Bit ====
  
-Many programming languages actually have a few additional bitwise operations, which make it easier for the programmers. These are bit shifting operations that shift bits left or right in a binary number. The main value of shift operations in dealing with registers is their ability to convert bit ranks to bitmasks and vice versa.+Many programming languages actually have a few additional bitwise operations, which make it easier for programmers. These are bit shifting operations that shift bits left or right in a binary number. The main advantage of shift operations in dealing with registers is their ability to convert bit ranks to bitmasks and vice versa.
  
 [{{  :images:logic:op_bit_shift_left.png?241|Shift left}}] [{{  :images:logic:op_bit_shift_left.png?241|Shift left}}]
  
-The image on the right shows a shift left operation. Although bit shifting is not a logical operation and has no corresponding symbol, in C it is marked as "<<". Shift left is used to transform a bit rank to a bitmask. For example, to get the mask for the 6th bit (NB! rank 5), number 1 has to be shifted left 5 times. The example operation looks like this in C:+The image on the right shows a shift-left operation. Although bit shifting is not a logical operation and has no corresponding symbol, in C it is marked as "<<". Shift-left is used to transform a bit rank to a bitmask. For example, to get the mask for the 6th bit (NB! rank 5), number 1 has to be shifted left 5 times. The example operation looks like this in C:
  
 ~~CL~~ ~~CL~~
Line 150: Line 150:
 [{{  :images:logic:op_bit_shift_right.png?241|Shift right}}] [{{  :images:logic:op_bit_shift_right.png?241|Shift right}}]
  
-Shift right operation works similarly to shift left operation. It is marked as ">>" in C. Right shift is used to get the logical value of a bit from a bitmask. A leading example showed how to read the value of a single bit. Let's suppose the bit to read is not of the lowest rank, but for example of rank 5. In this case, the result would be either 0x20 or 0x00, but sometimes a result of 1 or 0 is needed and that is when the right shift comes to the rescue. The example operation on the right looks like this in C:+The shift-right operation works similarly to the shift-left operation. It is marked as ">>" in C. Right-shift is used to get the logical value of a bit from a bitmask. A previous example showed how to read the value of a single bit. Let's suppose the bit to be read is not of the lowest rank, but for example of rank 5. In this case, the result would be either 0x20 or 0x00, but sometimes a result of 1 or 0 is needed and that is when the right-shift comes to the rescue. The example operation on the right looks like this in C:
  
 ~~CL~~ ~~CL~~
Line 160: Line 160:
 </code> </code>
  
-If a bit is shifted right from the lowest rank or left from the highest rank by the bit shifting operation, it disappears. Some programming languages also have rotating bit shift operations, where the bit doesn't disappear, but moves from the lowest rank to the highest or vice versa. C doesn't have that kind of bit shift operations, but they can be written by the programmer if needed.+If a bit is shifted right from the lowest rank or left from the highest rank by the bit shifting operation, it disappears. Some programming languages also have rotating bit shift operations, where the bit doesn't disappear, but moves from the lowest rank to the highest or vice versa. C doesn't have that kind of bit-shift operations, but they can be written by the programmer if needed.
  
 All bit operations work with not only registers, but with variables and constants as well. The latter can of course only be used as operands and not the result. All bit operations work with not only registers, but with variables and constants as well. The latter can of course only be used as operands and not the result.
  
-===== AVR registers =====+===== AVR Registers =====
  
-To do anything actual with the microcontroller's registers, one needs to know how to use that particular microcontroller. Each microcontroller comes with one or several datasheets, which describe the whole structure and functionality or the microcontroller. The datasheet also describes the registers. The following will help understand the register descriptions in AVR datasheets.+To actually do anything with the microcontroller's registers, one needs to know how to use that particular microcontroller. Each microcontroller comes with one or several datasheets, which describe the whole structure and functionality of the microcontroller. The datasheet also describes the registers. The following will help understand the register descriptions in AVR datasheets.
  
 [{{  :images:logic:avr_example_register.png?580  |One of AVRs registers from its datasheet}}] [{{  :images:logic:avr_example_register.png?580  |One of AVRs registers from its datasheet}}]
Line 172: Line 172:
 The image shows ATmega128 microcontroller's UCSRnA register, which stands for "USART Control and Status Register A". This register is used to configure AVR's USART module and read its states. All AVR register names are written in capital letters, but as the reader might notice, the register name contains also a lower case n. A lower n is used to mark some module's index. Since ATmega128 has 2 almost identical USART modules, they are not described twice, but only once and the n must be read as 0 or 1 by the user. Therefore ATmega128 has registers UCSR0A and UCSR1A. The image shows ATmega128 microcontroller's UCSRnA register, which stands for "USART Control and Status Register A". This register is used to configure AVR's USART module and read its states. All AVR register names are written in capital letters, but as the reader might notice, the register name contains also a lower case n. A lower n is used to mark some module's index. Since ATmega128 has 2 almost identical USART modules, they are not described twice, but only once and the n must be read as 0 or 1 by the user. Therefore ATmega128 has registers UCSR0A and UCSR1A.
  
-The content of the register is marked by an 8-slot box with a bold line around it. Each slot marks one bit. Bit ranks are marked above the box - increasing from right to left. Since AVR is an 8-bit microcontroller, most of the registers are 8-bit as well. There are some exceptions, a few registers are 16-bit, but they actually consist of two 8-bit registers. Like each register has a name, each bit in the register has also a name - just like the buttons on a tape player. Each bit is described in the datasheet. Bit names are abbreviations as well and the lower n must be substituted with the module's index, just like with register names. Some registers don't use all 8 bits, in this case the bit's slot is marked with a hyphen.+The content of the register is marked by an 8-slot box with a bold line around it. Each slot marks one bit. Bit ranks are marked above the box - increasing from right to left. Since AVR is an 8-bit microcontroller, most of the registers are 8-bit as well. There are some exceptions, a few registers are 16-bit, but they actually consist of two 8-bit registers. Just as each register has a name, each bit in the register also has a name - just like the buttons on a tape player. Each bit is described in the datasheet. Bit names are abbreviations as well and the lower n must be substituted with the module's index, just like with register names. Some registers don't use all 8 bits, in this case the bit's slot is marked with a hyphen.
  
 Below the register's bits are two lines, which state whether the bit is readable (R), writable (W) or both (R/W). For example, the status bits can't be overwritten and even if it's attempted in the program, the bit will remain unchanged. If the bit is marked as writable, reading it will always result in one specific value stated in the datasheet. The second line specifies the default value of the bit, which it has after the reset of the microcontroller. Below the register's bits are two lines, which state whether the bit is readable (R), writable (W) or both (R/W). For example, the status bits can't be overwritten and even if it's attempted in the program, the bit will remain unchanged. If the bit is marked as writable, reading it will always result in one specific value stated in the datasheet. The second line specifies the default value of the bit, which it has after the reset of the microcontroller.
en/avr/registers.1285339780.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0