Announcement
Collapse
No announcement yet.
Articles
Collapse
microEngineering Labs Forums

BCD (Binary Coded Decimal) Number System
 Created by: Charles Leo
 Published: 3 weeks ago
BCD (Binary Coded Decimal) Number System
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 ... 
Using Registers (SFRs) and reading the Microchip Data Sheet
 Created by: SteveC
 Published: 4 weeks ago
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...Last edited by SteveC; 3 weeks ago. 
 0 comments
Welcome
Welcome to the melabs Support Forums.
We hope that these forums will provide many answers to questions you may have about melabs products including PicBasic Pro, programmers, developement and protoboards.
Continue to forums #mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sansserif; width:400px;} /* Add your own MailChimp form style overrides in your site stylesheet or in this style block. We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */ Subscribe to our mailing list Email Address First Name Last Name ...
There are no articles in this category.
Please log in to your account to view your subscribed posts.
Article Tags
Collapse
 1Wire (5)
 16F877A (54)
 16F887 (38)
 18F's (6)
 18F452 (4)
 Analog (33)
 ASM Interrupt (8)
 Enhanced Core (9)
 External EEPROM (11)
 HSER (15)
 i2c (23)
 Internal EEPROM (7)
 LABX1 (92)
 LABX2 (14)
 LABX3 (12)
 LABXUSB (23)
 lcd (86)
 Matrix Keypad (6)
 ON INTERRUPT (10)
 RTC (7)
 Serial (31)
 spi (14)
 Timer0 (7)
 timer1 (4)
 timers (4)
Latest Articles
Collapse

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 ...3 weeks ago

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...4 weeks ago 
We've classified PBP 3.1 as a major upgrade due to additional assemblylibraries 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 8bit 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,
04262017, 08:27 AM 
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 apimswincrtruntime110.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/enus/...untimeinwind...03292017, 05:41 AM

It is highly recommended that you update to PBP 3.0.10. This update is free to users of 3.0.9, but it isn't compatible with Windows XP. This post is intended as a warning and workaround for those that can't bring themselves to upgrade to a newer version of Windows. Individual LONGs are not vulnerable to this issue, only arrays. This is a rare issue that is most likely to be encountered in PIC18 programs that use most of the available ram and declare large LONG array variables. In 3.0.9, PBP may place LONG arrays at addresses that result in individual elements ...02252017, 05:57 AM

PBP 3.0.10 and later requires Windows 7/8/10. Windows XP is no longer supported.
 Fixed bankboundary vulnerability for LONG array variables
 Fixed SPBRG register names on 16F1824 family
 Fixed READ/WRITE issues for 16(L)F183xx
 Changed default config for 18F87K22 for better results
 Added DAC1CON0/DAC1CON1 SFRs for 12F1571/72
 Added support for 16(L)F18326, 16(L)F18346
02252017, 04:52 AM