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.

[SOLVED] Convert BCD to ASCII using Assembly

Status
Not open for further replies.

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,936
Dear All
My RTC project is success some extend
now I need put RTC result to LCD
I know that RTC result is BCD, so I have to convert BCD to ASCII before I send to LCD
Can any one advice me, how BCD to ASCII convert using assembly
my RTC is DS1307
pic 16f877a
Thanks in advance
 

Enter with the BCD number in 'temp'.

1. swapf temp,w ;put the first digit into the lower bits of w
2. andlw 0x0F ;get rid of the high bits
3. addlw '0' ;add ascii zero
4. movwf FirstDigit
5. movf temp,w ;recover the original BCD number
6. andlw 0x0F ;get rid of the high bits
7. addlw '0' ;add ascii zero
8. movwf SecondDigit

The BCD number is now split into two ASCII characters, the most significant is returned in FirstDigit, the least significant in SecondDigit - and all in 8 instructions.

Brian.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating

Code - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
bcdtoascii:
Mov b,a
swap a
anl a,#0fh
orl a,#30h
acall data_display
mov a,b
anl a,#0fh
orl a,#30h
acall data_display
ret
 
 
data_display:
Acall ready 
mov p1,a 
setb p2.0 
clr p2.1 
setb p2.2 
acall delay 
clr p2.2 
ret
ready: Setb p1.7 
clr p2.0 
setb p2.1 
back: Clr p2.2 
acall delay 
setb p2.2 
jb p1.7,back 
ret

 

Dear Brian
Thanks for reply and help, I did it as you said, now LCD display only 00 it does not update the time
can you please advice on this . please see image
Please see coff file and Proteus file in the attachment

Thanks In advance
rtc.png
 

Attachments

  • New folder.rar
    21.9 KB · Views: 70
Last edited:

I do not have Proteus but the asm file suggests you simply copied my code example at the end of the program. I gave you an example which showed how to do the conversion, I didn't intend you to use it literally. You must take the BCD number and process it in the way I showed, split it into a top 4-bit half and a bottom 4-bit half, then add 0x30 (or character zero) to the results of both before passing them to the LCD.

You can use the method papunblg showed which is the same but directly writes each character to the LCD rather than saving them as individual digits first. Their code isn't for a PIC though.

Brian.
 

Dear Brian,
Thank you so much for advice, I change program the way you guide me, So problem remaining the same
Code:
                                swapf        sec,w 			
				andlw 	0x0F 			;get rid of the high bits
				addlw 	0x30 			;add ascii zero
				movwf 	FD
				call	data_wrt
				movf 	sec,w 			;recover the original BCD number
				andlw 	0x0F 			;get rid of the high bits
				addlw 	0x30			;add ascii zero
				movwf 	SD
				call	data_wrt
				return

Can you explain following code I can not understand. what is this .13 is it related to ds1307 or any

Code:
                         BANKSEL SSPSTAT                                        ; ### point to correct BANK for SSPSTAT
    		         bsf     SSPSTAT,SMP             ; ### Set for Standard speed slew rate
			
			movlw	.13
			movwf	SSPADD


can you please advice me more
Thanks in advance
 
Last edited:

SSPADD is the I2C address register. You load it with the address of the I2C device you are accessing. The address is 8 bits long with the LSB being 0 if you are reading the device and 1 if you are writing to it. According to the data sheet for the DS1307, it's slave address is 1101000x where 'x' is the read or write bit so to read it you should use address 0xD0 (.208) and to write to it 0xD1 (.209), perhaps this is where the problem lies.

Brian.
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Brian,
Thanks for reply
As I know, We should insert slave address to SSPADD in slave mode and in master mode we should load to SSPADD baud rate. I have little bit confuse on this. can you please explain it. in my program I set in master mode but still it does not read the value from RTC. If you can point out where the fault is, it is really really helpful me
Thanks in advance







SSPADD is the I2C address register. You load it with the address of the I2C device you are accessing. The address is 8 bits long with the LSB being 0 if you are reading the device and 1 if you are writing to it. According to the data sheet for the DS1307, it's slave address is 1101000x where 'x' is the read or write bit so to read it you should use address 0xD0 (.208) and to write to it 0xD1 (.209), perhaps this is where the problem lies.

Brian.
 

Dear All
I solved the problem, thanks for kind help
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top