; ; *************************************************************************** ; *** Bubble Software Parallax to PIC Source Converter. Copyright 1999. *** ; *** http://www.picnpoke.com email: sales@picnpoke.com *** ; *************************************************************************** ; ; ASC_BIN numeric string ; This program converts a counted string of ASCII characters ; received by Serin into a 16-bit value in the variable n1. P = pic16c55 #include <16c55.inc> ; processor assembler definitions _CONFIG _xt_osc & _wdt_off & _protect_off reset start buffer equ d'31' ; String buffer at end of memory ASC_0 equ '0'; ASCII chars for nos. are 30h thru 39h. org 8 dec_no Res d'1' ; Decade (1,10,100...) to work on. n1H Res d'1' ; MSB of converted number. n1L Res d'1' ; LSB of converted number. n2H Res d'1' ; MSB of 16-bit temp variable. n2L Res d'1' ; LSB of 16-bit temp variable. temp Res d'1' ; Temporary storage ; Device data and reset vector org 0 decade ADDWF pcl RETLW d'39' RETLW d'16' RETLW d'3' RETLW d'232' RETLW d'0' RETLW d'100' RETLW d'0' RETLW d'10' RETLW d'0' RETLW d'1' start MOVLW d'0' ; Set ports to output. TRIS 6h MOVLW d'0' TRIS 7h MOVLW d'5' ; String count. MOVWF buffer MOVLW 0x36 ; Numeric text: "65535." MOVWF buffer-1 MOVLW 0x35 ; To view the results of the MOVWF buffer-2 MOVLW 0x35 ; conversion, run this program MOVWF buffer-3 MOVLW 0x33 ; on the PSIM simulator, or merge MOVWF buffer-4 MOVLW 0x35 ; it with Serin. MOVWF buffer-5 CALL ASC_BIN MOVF n1L,w ; Show result on LEDs. MOVWF 6h MOVF n1H,w MOVWF 7h GOTO $ ; Endless loop. ; ASC_BIN gets the value of each digit (0-9) of a numeric string. Depending ; on the number of digits in the string, it looks up the multiplier (10000, 1000,100,10, or 1) and adds that multiplier to n1 digit number of times. ASC_BIN CLRF n1H ; n1 = 0 CLRF n1L MOVLW buffer-1 ; If the string is empty, MOVWF fsr MOVF buffer ; return 0 in 16-bit answer BTFSS status,z ; and error code (1) in GOTO ASC_BIN_loop RETLW d'1' ; w. Else, do the conversion. ASC_BIN_loop MOVLW ASC_0 ; w = 30h (ASCII start point). subwf indirect,0 ; w = character_value-30h. BTFSC status,z GOTO ASC_BIN_next MOVWF temp ; temp = w MOVLW d'5' MOVWF dec_no MOVF buffer,w ; dec_no = 5-string_length SUBWF dec_no BCF status,c RLF dec_no ; dec_no = dec_no*2 MOVF dec_no,w CALL decade ; Look up MSB of multiplier. MOVWF n2H ; Store it in MSB of n2. INCF dec_no,w ; Look up the LSB of multiplier. CALL decade MOVWF n2L ; Store it in LSB of n2. ASC_BIN_add_it CALL Add16 ; n1 = n1 + n2. DECFSZ temp ; Repeat 1 to 9 times for each digit. GOTO ASC_BIN_add_it ASC_BIN_next DECF fsr ; Get the next byte of the string. DECF buffer ; Adjust string counter. BTFSS status,z ; Any bytes left? Yes: loop. GOTO ASC_BIN_loop RETLW 0h ; No: return. Add16 MOVF n2L,w ; Add the LSBs. If carry, ADDWF n1L BTFSC status,c ; increment MSB of sum. INCF n1H MOVF n2H,w ; Add the MSBs. ADDWF n1H RETLW 0h end