Announcement

Collapse
No announcement yet.

Using Registers (SFRs) and reading the Microchip Data Sheet

Collapse
X
Collapse

  • Using Registers (SFRs) and reading the Microchip Data Sheet

    The following excerpt from the ME Labs Trainer Board and Tutorial is provided to help users create a deeper understanding of SFRs and how to utilize information from the Microchip Data Sheet to write to a specific register. Here we are using an excerpt from the Microchip PIC16F1937 Data Sheet.


    Using registers (SFRs) and reading the Microchip Data Sheet



    The Microchip Data Sheet holds a lot of information. The intent is for it to hold all the information you will ever need
    when developing program code for the microcontroller.

    In reality, you only need a small part of this information at any given time. Most of the time, you will be using the Data
    Sheet to learn the function of bits in a specific register (SFR) and setting the register to achieve the desired effect.

    What follows are some clues to help you make sense of the register diagrams and notes that are found in the Data
    Sheet.

    Each register in the microcontroller is detailed with a diagram similar to the following. I've detailed the different parts
    of the diagram and its description below. The register shown below is called "ANSELB". As you can see, Microchip
    calls it the "PORTB Analog Select Register".

    Click image for larger version  Name:	register12-9.jpg Views:	1 Size:	98.4 KB ID:	6670

    (1) Bit information: This bar gives us clues about the nature of each individual bit in the register. The meanings
    of the codes are detailed in the legend (item-4). In this example, "U" means that the bit is unimplemented and
    "R/W" means that the bit is "read-write" (it can be both read and written). The numbers indicate the state of
    the bit after a reset or power-cycle. In this SFR, the unimplemented bits will always read zero. The other bits
    will be set to "1" after all types of reset.
    (2) Bit names: This bar lists the names of the individual bits. These names may be used elsewhere in the Data
    Sheet to refer to these bits.
    (3) Bit numbers: This bar shows how the bits are numbered. In this case, and in most cases, it only shows the
    leftmost bit-number (7) and the rightmost bit-number (0). The intent is for you to fill in the blanks. In reality,
    the bits are numbered | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |.
    (4) Legend: As mentioned previously, this block supplies the information needed to decode the information in the
    bit-information block (item-1).
    (5) Bit function description: This section lists all the bits and describes briefly what happens when the bits are set
    to "1" or "0". If a bit is a read-only indicator, the description denotes the meaning behind the state of the bit.
    (6) Notes: Lastly, Microchip provides notes that are important to know when using the register.
    Here are some practical examples showing different ways (there are many) to write to the ANSELB register. On our
    Trainer board, we need PORTB bits 4 and 5 (ANSB4 and ANSB5) to be analog inputs, and bits 0-3 to be digital I/O.
    The following examples will all accomplish this.

    ANSELB = %00110000 ' PortB.4 and B.5 analog inputs



    The example above writes values to all the bits in ANSELB. This works when you know what the all the bits need to
    be (sometimes you won't know). The percent sign signifies that you are writing a binary number. The order of the bits
    is the same as the order in the register diagram. Bit-7 is on the left and bit-0 is the on the right.

    ANSELB = $30 ' PortB.4 and B.5 analog inputs (hexadecimal)
    ANSELB = 48 ' PortB.4 and B.5 analog inputs (decimal)



    This example is functionally identical to the previous one, but I wrote the number in hexadecimal format ($30) and
    decimal format (48). Since the microcontroller only understands binary, the numbers in each case will be converted by
    the compiler. In reality, they are all the same number: %00110000 is equal to $30 which is equal to 48. Note that the
    percent sign (%) and the dollar sign ($) are specific to PICBASIC PRO. Other compilers may use different characters
    to specify the format of the number. It is your choice which format to use.

    ANSELB.0 = 0 ' PortB.0 digital I/O
    ANSELB.1 = 0 ' PortB.1 digital I/O
    ANSELB.2 = 0 ' PortB.2 digital I/O
    ANSELB.3 = 0 ' PortB.3 digital I/O



    The method above works because (as shown in the register diagram) all the implemented bits in ANSELB default to a
    value of "1" when power is applied. All we have to do is clear the bits that correspond to the pins that we want to be
    digital I/O. All other bits remain in their default states.
    Last edited by SteveC; 06-28-2017, 12:52 PM.
      Posting comments is disabled.

    Article Tags

    Collapse

    There are no tags yet.

    Latest Articles

    Collapse

    • Pbp 3.1.1
      Charles Leo
      • Added support for devices: PIC16(L)F19155, PIC16(L)F19156, PIC16(L)F19175, PIC16(L)F19176, PIC16(L)F19185, PIC16(L)F19186, PIC16(L)F19195, PIC16(L)F19196, PIC16(L)F19197, PIC18(L)F24K42, PIC18(L)F25K42, PIC18(L)F26K42, PIC18(L)F27K42, PIC18(L)F45K42, PIC18(L)F46K42, PIC18(L)F47K42, PIC18(L)F55K42, PIC18(L)F56K42, PIC18(L)F57K42, PIC18(L)F25K83, PIC18(L)F26K83
      • Changed default PPS pins for HSER2 commands to avoid accidental ICSP lockout
      • Added method to cancel CCP-PPS defaults in devices so
      ...
      01-02-2018, 05:08 AM
    • BCD (Binary Coded Decimal) Number System
      Charles Leo
      You should now be familiar with the Binary, Decimal and Hexadecimal Number System. If we view single digit values for hex, the numbers 0 - F, they represent the values 0 - 15 in decimal, and occupy a nibble. Often, we wish to use a binary equivalent of the decimal system. This system is called Binary Coded Decimal or BCD which also occupies a nibble. In BCD, the binary patterns 1010 through 1111 do not represent valid BCD numbers, and cannot be used. Conversion from Decimal to BCD is straightforward. You merely assign each digit of the decimal number to a byte and convert 0 through 9 to 00000000 through 00001001, but you cannot perform the repeated division by 2 as you did to convert decimal to binary. Let us see how this works. Determine the BCD value for the decimal number 5,319. Since there are four digits in our decimal number, there are four bytes in our BCD number. They are: Thousands Hundreds Tens Units 5 3 1 9 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 Since computer storage requires the minimum of 1 byte, you can see that the upper nibble of each BCD number is wasted storage. BCD is still a weighted position number system so you may perform mathematics, but we must use special techniques in order to obtain a correct answer. PACKED BCD Since storage in RAM is so valuable, we would like to eliminate this wasted storage. This may be accomplished by packing the BCD numbers. In a packed BCD number, each nibble has a weighted position starting from the decimal point. Therefore, instead of requiring 4 bytes to store the BCD number 5319, we would only require 2 bytes, half the storage. The upper nibble of the upper byte of our number would store the THOUSANDS value while the lower nibble of the upper byte would store the HUNDREDS value. Likewise, the lower byte would store the TENS value in the upper nibble and the UNITS digit in the lower nibble. Therefore, our previous example would be: Thousands - Hundreds Tens - Units 53 19 0 1 0 1 0 0 1 1 0 0 0 1 1 0 0 1 Here is some PBP code to pack 4 digits of BCD data into 2 bytes of memory.  The 4 digits of BCD are stored in the byte variables: BCD1000, BCD100, BCD10, BCD1.  Use the following to pack into the byte variables: PACKED_HIGH and PACKED_LOW. BCD1000 = $5 BCD100 = $3 BCD10 = $1 BCD1 = $9 PACKED_HIGH = (BCD1000<<4) + BCD100 PACKED_LOW = (BCD10<<4) + BCD1 Converting between Binary and BCD If you are doing calculations with data that is sent or received in BCD format, you will have to convert to binary in order to get the correct results.  The following example starts with the packed BCD from above (PACKED_HIGH = $53, PACKED_LOW = $19).  The value is converted to binary and stored in the word variable W1, divided by 2, then converted to 4 bytes and stored back in the original BCD variables above. Conversion with PICBASIC PRO W1 = ((PACKED_HIGH & $F0)>>4*1000) + ((PACKED_HIGH & $0F)*100)_     + ((PACKED_LOW & $F0)>>4*10) + (PACKED_LOW & $0F) W1 = W1 / 2 BCD1000 = W1 DIG 3 BCD100 = W1 DIG 2 BCD10 = W1 DIG 1 BCD1 = W1 DIG 0  ...
      06-28-2017, 07:02 AM
    • Using Registers (SFRs) and reading the Microchip Data Sheet
      SteveC
      The following excerpt from the ME Labs Trainer Board and Tutorial is provided to help users create a deeper understanding of SFRs and how to utilize information from the Microchip Data Sheet to write to a specific register. Here we are using an excerpt from the Microchip PIC16F1937 Data Sheet.


      Using registers (SFRs) and reading the Microchip Data Sheet



      The Microchip Data Sheet holds a lot of information. The intent is for it to hold all the information you...
      06-27-2017, 01:42 PM
    • Pbp 3.1.0
      Charles Leo
      We've classified PBP 3.1 as a major upgrade due to additional assembly-libraries necessary to support Microchip's changes in memory map on their latest devices. The new libraries allow us to continue adding support for the latest 8-bit microcontrollers as they are released.
      • Added support for devices: 10(L)F320, 10(L)F322, 16(L)F15313, 16(L)F15323, 16(L)F15324, 16(L)F15325, 16(L)F15344, 16(L)F15355, 16(L)F15356, 16(L)F15375, 16(L)F15376, 16(L)F15385, 16(L)F15386, 16(L)F1773, 16(L)F1776, 16(L)F1777,
      ...
      04-26-2017, 08:27 AM
    • PBP3 errors on Windows 7 related to api-ms-win-crt-runtime-|1-1-0.dll
      Charles Leo
      The latest versions of PBP (3.0.10 and later) are dependent on some Windows 10 files that Microsoft made available in Windows 7 with an update. If your Windows 7 system isn't up to date, you might get an error related to api-ms-win-crt-runtime-|1-1-0.dll. If this happens and you want to download only the required files without updating Windows in general, see this link and download the appropriate package: https://support.microsoft.com/en-us/...untime-in-wind...
      03-29-2017, 05:41 AM
    • PBP 3.0.9 LONG Array bank-boundary vulnerability
      Charles Leo
      PBP 3.0.9 is the last version of PBP that will run on Windows XP. It also compiles large programs significantly faster than the newer versions. This post is intended as a warning and workaround for those who have reason to use version 3.0.9. This is the only known issue that could cause problems without generating a compile error. This issue is fixed in 3.0.10 and later.

      Individual LONGs are not vulnerable to this issue, only arrays. This is a rare issue that is most likely to be...
      02-25-2017, 05:57 AM
    Working...
    X