PIC Microcontoller Radix Time Math Method

Unpacked BCD to 16 bit binary conversion

Drew Vassallo says:
There's been some discussion about using RTCs lately. Typically, they send/receive in packed BCD. Except for the year, which is 0-99, all of the other variables are within 0-64 BCD.

After briefly searching the PICLIST archives, I couldn't really find any snippets that weren't overkill for this purpose, so I made my own. These are more specific to RTC time conversion and have reduced instruction counts over the "standard" code fragments. If you convert your hours to 12-hour format instead of the 24-hour output format of the RTC, you can use the obvious:

; BCD conversion is limited to "Hours" values less than 16
                movf    Hours, 0
                addlw   0x06
                skpdc
                addlw   -0x06
                movwf   Hours

This can also be used for month and day of the week.

For minutes, seconds, and date, you can use:

; Hex to BCD conversion - limited to 0-99 decimal (0x00-0x63 hex)
; Call with: number to be converted in W
; Returns with: converted BCD number in W
; Registers: Temp, Temp+1 used during conversion
Hex2BCD
		movwf	Temp
		clrw
		btfsc	Temp, 4
		addlw	0x16
		btfsc	Temp, 5
		addlw	0x32
		addlw	0x06
		skpdc
		addlw	-0x06
		btfsc	Temp, 6
		addlw	0x64
		addlw	0x06
		skpdc
		addlw	-0x06
		movwf	Temp+1
		movf	Temp, 0
		andlw	0x0F
		addwf	Temp+1, 0
		skpndc
		addlw	0x06
		addlw	0x06
		skpdc
		addlw	-0x06
		return

See:

Comments:

See also: