Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need general guidelines for using 8051 controller

Status
Not open for further replies.

ITP

Advanced Member level 4
Joined
Jul 11, 2001
Messages
116
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
1,136
Hi all 8051 users,

I request all experienced 8051 users kindly post
their experiences with this controller and also common pitfalls
that a novice may face.

Thank you.
Itp
 

8051 bcd 24 bits conversion

; Dear 8051 users,

; This is a simple algorithm and its 8051 microcontroller implimentation
; to convert binary to BCD .
; Modifications to my code is highly welcomed. If you make changes to this code
; please post here.

; This alorithm I got from following forum

; ------- Date: 19970630
; ------- From: Brian Trial
; ------- To: Mot-68HC11-Apps@freeware.mcu.motsps.com
; ------- Subject: Re: Simpler method to output 24bits in decimal


; 1. Add 3 to every BCD nibble that's >=5
; 2. Shift left
; Repeat until binary portion is all 0000
;
; Example: Convert $F9 to BCD
;
; 0000 0000 0000 : 1111 1001
; 0000 0000 0001 : 1111 0010 (shift left)
; 0000 0000 0011 : 1110 0100 (shift left)
; 0000 0000 0111 : 1100 1000 (shift left)
; 0000 0000 1010 : 1100 1000 (add 3)
; 0000 0001 0101 : 1001 0000 (shift left)
; 0000 0001 1000 : 1001 0000 (add 3)
; 0000 0011 0001 : 0010 0000 (shift left)
; 0000 0110 0010 : 0100 0000 (shift left)
; 0000 1001 0010 : 0100 0000 (add 3)
; 0001 0010 0100 : 1000 0000 (shift left)
; 0010 0100 1001 : 0000 0000 (shift left)
;
; And the left now holds BCD 249.
; MUCH easier than messing with IDIV and FDIV!!!



; -------------------------------------------------

; 8051 microcontroller implimentation by
; itp ( an elektroda eboard member)


org data_segment ; inernal data

bin_val: ds 2 ; 16 bit bin value to be converted
bcd_val: ds 3 ; converted 5 digit packed bcd


org code_segment ; code starts here

; 16 bit binary to bcd conversion.

bin2bcd:
push acc
push psw
mov psw,#08h ; select register bank 1
mov r0,#bin_val
mov r1,#bcd_val+2
clr a
mov @r1,a
dec r1
mov @r1,a
dec r1
mov @r1,a
mov r3,#16 ; for 16 bit
again1:
call adj_bcd
mov r0,#bin_val
mov r1,#bcd_val
clr c ; shift the bytes
mov a,@r0
rlc a
mov @r0,a
inc r0
mov a,@r0
rlc a
mov @r0,a
mov a,@r1
rlc a
mov @r1,a
inc r1
mov a,@r1
rlc a
mov @r1,a
inc r1
mov a,@r1
rlc a
mov @r1,a
inc r1
mov a,@r1
rlc a
mov @r1,a
inc r1
mov a,@r1
rlc a
mov @r1,a
skipx:
djnz r3,again1
exit:
pop psw
pop acc
ret
adj_bcd:
mov r1,#bcd_val
mov b,#3
again:
mov a,@r1
mov r7,a
anl a,#0fh
cjne a,#5,ok ; a >= 5 ?
ok:
jc skip ; if yes add 3
add a,#3
skip:
mov @r1,a
mov a,r7
anl a,#0f0h
swap a
cjne a,#5,ok1
ok1:
jc skip1
add a,#3
skip1:
swap a
add a,@r1
mov @r1,a
inc r1
djnz b,again
ret
-------------------------------------


/* C example for binary to bcd conversion*/
/* This uses different algoritham */

unsigned int bintobcd(unsigned int binvalue)
{
unsigned char j=0;
unsigned int h=0;
do
{
h += ((binvalue%10)<<j);
binvalue = binvalue/10;
j += 4;
}
while(binvalue >= 10);
h += ((binvalue) << j);
return h;
}
 

hex to bcd 8051

ITP said:
Hi all 8051 users,

I request all experienced 8051 users kindly post
their experiences with this controller and also common pitfalls
that a novice may face.

Thank you.
Itp

Mmm... I've use 8051 many times in the pass. Your question is too general and is hard to answer, but I will try my best.

1. 8051 has a decent set of assembler language. It is not the best but good enough for most case. It is relatively easy to learn and use. You can get LOTS of free assembler and linker for this little micro-controller.

2. 8051 has lots of good C compiler out there. Almost any of them has pros and cons. But consider 8051 has been around for ages, the tools usually is pretty good and robust.

3. There is a few things I don't like about 8051. The small interal RAM. I think 256bytes for 8052 and 128 for 8051. (I am going by my memory here, which may not be accurate.) The internal RAM is banked, which make software design somewhat complicated. (Read the data sheet and you will see what I mean.) The separate data/program space can be either a blissing or curse. If your application is embed application without reprogramming of program, then the uC will almost give you 128K of space. (64K data space and 64K program space.) You can store some constant into program space and load them into data space during run time. I prefer having one unify data/program space. It is a little easy to use, but then, I am just being lazy.

4. This is where your application comes. What you want to do will determine whether 8051 is a good fit or not. It is a good little uC in many way but sometimes it can be the worst choice.

The above is just my own 2 cents. You asked a general question. Here is a general answer. Hope that helps :)

Gunship
 

8051 store character in data pointer

ITP said:
; This is a simple algorithm and its 8051 microcontroller implimentation
; to convert binary to BCD .
; Modifications to my code is highly welcomed. If you make changes to this code

; This alorithm I got from following forum

; ------- Date: 19970630
; ------- From: Brian Trial
; ------- To: Mot-68HC11-Apps@freeware.mcu.motsps.com
; ------- Subject: Re: Simpler method to output 24bits in decimal


; 1. Add 3 to every BCD nibble that's >=5
; 2. Shift left
; Repeat until binary portion is all 0000

The 8051 has the "DA A" (Decimal-adjust Accumulator for Addition) instruction, you could use it to substitute for your 'adj_bcd'.
And code will be more fast and small such like as:
Code:
; 16 bits Binary: L,H store in R6,R7
; 24 bits BCD: L,M,H out to R3,R4,R5
;BIN2BCD:
        CLR     A
        MOV     R3,A
        MOV     R4,A
        MOV     R5,A
        MOV     R2,#16
nB2Bn   MOV     A,R6
        RLC     A
        MOV     R6,A
        MOV     A,R7
        RLC     A
        MOV     R7,A
        MOV     A,R3
        ADDC    A,A
        DA      A
        MOV     R3,A
        MOV     A,R4
        ADDC    A,A
        DA      A
        MOV     R4,A
        MOV     A,R5
        ADDC    A,A
        DA      A
        MOV     R5,A
        DJNZ    R2,nB2Bn
 

binary to bcd in 8051

8051 have excellent instruction set for bit manipulation. If you need some control flags you can use bit variables instead byte variables and save uC memory.

You can also make some efficient conversion using bit instructions. Next routine is efficient 8051 toupper() function.

; Convert string to Upper case

TO_UPPER:
MOV R1,#STRBUF ; set R1 pointer to STRBUF
UPPER_LOOP:
INC R1 ;
MOV A,@R1 ; store character in accumulator
CJNE A, #96,U_NEQ1 ; if character is not a..z skip
U_NEQ1: JC UPPER_CONT ;
CJNE A, #123,U_NEQ2 ;
U_NEQ2: JNC UPPER_CONT ;
CLR ACC.5 ; convert char to upper case
MOV @R1,A ; store result to STRBUF
UPPER_CONT:
JNZ UPPER_LOOP ; repeat until character==NULL
RET

For char conversion from lower to upper case you only need one cycle instruction CLR ACC.5 !!!

Some newer 8051 architectures have some architectural improvements as additional data pointer. Additional data pointer enables efficient manipulations with 16-bit addresses.
 

8051 routine hex to bcd 16 bits

Hi Gunship,yager and Hero,

Thank you for replies.

I appreciate "yager" for your compact code for bin2bcd.


Thank you
Itp
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top