Announcement
Collapse
No announcement yet.
Articles
Collapse
microEngineering Labs Forums

BCD (Binary Coded Decimal) Number System
 Created by: Charles Leo
 Published: 06282017, 08:34 AM
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: 06272017, 02:45 PM
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; 06282017, 01:52 PM. 
 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
There are no tags yet.
Latest Articles
Collapse

 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 CCPPPS defaults in devices so

Channel: PBP Version History
01022018, 06:08 AM 
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 ...

Channel: Welcome
06282017, 08:02 AM 

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...
Channel: Welcome
06272017, 02:42 PM 

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,

Channel: PBP Version History
04262017, 09: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...

Channel: PBP Known Issues
03292017, 06:41 AM 

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...
Channel: PBP Known Issues
02252017, 06:57 AM 