Programming
Encoding a BCD Number
Definition
BCD represents each of the digits of an unsigned decimal
as the 4-bit binary equivalents.
Unpacked BCD
Unpacked BCD representation contains only one decimal
digit per byte. The digit is stored in the least significant 4 bits; the most
significant 4 bits are not relevant to the value of the represented number.
Packed BCD
Packed BCD representation packs two decimal digits into a
single byte.
Invalid BCD Numbers
These binary numbers are not allowed in the BCD
code: 1010, 1011, 1100, 1101, 1110, 1111
Packing a Two-Byte BCD
To pack a two-byte unpacked BCD number into a single byte
creating a packed BCD number, shift the upper byte left four times,
then OR the results with the lower byte.
For example,
Converting between Decimal and BCD
From Decimal to Unpacked BCD: To convert
a decimal number into an unpacked BCD number, assign each decimal digit its
8-bit binary equivalent.
For example,
From Decimal to Packed BCD: To convert a
decimal number into a packed BCD number, assign each digit of the decimal to
its 4-bit equivalent, padding the upper nybble with zeroes if necessary.
For example,
From BCD to Decimal: To convert a BCD
into a decimal number, just reverse the appropriate process from above;
beginning with the LSB, group the binary digits by either 4 or 8 bits for
packed and unpacked, respectively, then convert each set into its decimal
equivalent.
Converting between Binary and BCD
From Binary to Unpacked BCD: To convert a
binary number into an unpacked BCD, divide the binary number by decimal 10
and place the quotient in the most significant byte and the remainder in the
least significant byte.
For example,
From Two-Byte Unpacked BCD to Binary: To
convert from a two-byte unpacked BCD to a binary number, multiply the most
significant byte of the BCD by decimal ten, then add the product to the least
significant byte.
For example,
00001001 00000010(unpacked BCD) = 01011100(base 2)
BCD Addition
Either packed or unpacked BCD numbers can be summed. BCD
addition follows the same rules as binary addition. However, if the
addition produces a carry and/or creates an invalid BCD number, an adjustment
is required to correct the sum. The correction method is to add 6 to the sum
in any digit position that has caused an error.
For example,
BCD Subtraction
Either packed or unpacked BCD numbers can be subtracted.
BCD subtraction follows the same rules as binary subtraction. However,
if the subtraction causes a borrow and/or creates an invalid BCD number, an
adjustment is required to correct the answer. The correction method is to
subtract 6 from the difference in any digit position that has caused an
error.
For example,
Unpacked BCD Multiplication
Multiplication cannot be performed on packed BCD; the 4
most significant bits must be zeroed for the adjustment to work. Multiply the
two unpacked BCD numbers using the rules for binary multiplication. To
adjust the product divide it by decimal 10, then place the quotient in the
most significant byte and the remainder in the least significant byte (convert
the binary answer to unpacked BCD).
For example,
Unpacked BCD Division
BCD division also cannot be performed on packed numbers.
Before dividing an unpacked BCD number, the division adjustment is made
by converting the BCD numbers to binary. Adjust the two-byte BCD number
by multiplying the upper byte by decimal 10 and adding the product to the
lower byte. After the adjustment, divide the two binary numbers using the
rules of binary arithmetic. Finally, convert the binary quotient
into an unpacked BCD number if necessary.
For example,
Other Decimal Codes
ASCII Representation of Digits
ASCII digits are examples of unpacked binary coded
decimals. A digit is represented as its 4-bit binary equivalent, which is
stored in the lower nybble. The upper nybble contains 011 and has
no bearing on the value of the represented number.
EBCDIC Representation of Digits
EBCDIC digits are similiar to ASCII digits. The
lower nybble still contains the 4-bit binary equivalent, but the upper nybble
is padded with 1111 instead of 011.
Excess-3 (XS3)
In excess-3 representation, each decimal digit is the
4-bit binary equivalent with 3 (0011) added.
4221
For 4221 coded-decimals, each digit also is represented as
a 4-bit group. Instead of each bit representing a successive power of two (8,
4, 2, and 1, from the left) as in binary, the bits represent 4, 2, 2, and 1.
|
Saturday, July 7, 2012
BCD Conversion
Thursday, June 7, 2012
8051 stepper motor interface
Connecting Unipolar stepper using L293D
There are actually many ways you can interface a stepper motor to your controller, out of them the most usedinterfaces are:
Interface using L293D - H-Bridge Motor Driver
Interface using ULN2003/2004 - Darlington Arrays
We will dicuss both connection techniques one by one. The above mentioned methods need 4 controller pins for interface.
As you see in the circuit above the four pins "Controller pin 1",2,3 and 4 will control the motion and direction of thestepper motor according to the step sequece programmed in the controller.
Connecting Unipolar stepper using ULN2003/2004
As already discussed in case of L293D, Here in this circuit too the four pins "Controller pin 1",2,3 and 4 will control themotion and direction of the stepper motor according to the step sequece sent by the controller.
code
/*Program to interface stepper motor*/
#include //include at89c51 microcontoller header file
#include"lcd.h"
sbit sw1=P1^0;
sbit sw2=P1^1;
sbit sw3=P1^2;
//connect p0.0 to switch
#define motor P0 //connect lower nibble of port0 to stepper motor
void delay_ms(unsigned int);
unsigned char dir;
int i=0;
void main(void)
{
init_lcd();
display_lcd("STEPPER MOTOR");
cmd_lcd(0xc0);
display_lcd("CONTROLLING");
delay_ms(2000);
while(1) //infinite loop
{
if(sw1==0)
{
cmd_lcd(0x01);
display_lcd("FORWARD");
dir=0;
}
if(sw2==0)
{
dir=1;
cmd_lcd(0x01);
display_lcd("REVERSE");
}
if(sw3==0)
{
dir=2;
cmd_lcd(0x01);
display_lcd("STOP");
P0=0x00;
}
if(dir==0)
{
for(i=0;i<12;i++)
{
motor=0xc0; //for CCW direction "C639"
delay_ms(12);
motor=0x60;
delay_ms(12);
motor=0x30;
delay_ms(12);
motor=0x90;
delay_ms(12);
}
}
if(dir==1)
{
for(i=0;i<12;i++)
{
motor=0x90; //for CCW direction "C639"
delay_ms(12);
motor=0x30;
delay_ms(12);
motor=0x60;
delay_ms(12);
motor=0xc0;
delay_ms(12);
}
}
}
}
Subscribe to:
Posts (Atom)