From ASCII to BIN (ASCII2BIN)

caganjan

New Member
Hi all.
After my first post at discussion, I would like to try my first blog.

I was trying to solve problem with converting number at ASCII (received via hserial on background into scratchpad) to BIN. I read manuals and discussions (I was not see all discussions thread, I just read some of the threads, eg about the NMEA GPS, etc.)

If i understood correctly all things about converting with # prefix, qualifiers and generally HSERIN(OUT) and SERIN(OUT), RevCoUk not prepared function for conversion at this situation.

Because I need HSER at background, i cannot use # prefix for conversion.
So I invented (do it yourself) ASCII2BIN. This idea can be generally usefull for people, which need convert 3 digit ascii number stored at scratchpad to 8 bit BIN. It is easy to extend it to 16 bit and more ...

I present this idea at sub-procedure in following program code:
Code:
;**************************************************
;AUTHOR: CAGI, DATE: 30.5.2012

;**************************************************
;PROGRAM DESCRIPTION:
#rem
Program receive string via HSER at background.
He is expecting this format "XxxxYyyy" where xxx is
number from 0 to 255, and yyy too. X and Y are
qualifiers. This string is immediately separated
and converted to BIN via interrupt. You can also
use a hardware button to send converted numbers to
PC (via interrupt INT0).
ps. numbers higher than 255 causes saturation
of register (265->255 ... and so on)
#endrem

;**************************************************
;PROCESSOR SETTING
	#PICAXE 40X2

;**************************************************
;CLOCK SETTING
;8MHz external oscilator
;(32MHz after PPL multiplying)
	setfreq em32	

;**************************************************
;IO SETTING
;bits order:       %76543210;1=out, 0=in
	let dirsA =  %00100000
	let dirsB =  %00000000
	pullup on
;**************************************************
;HARDWARE SERIAL PORT SETTING (PicAxeMANUAL2.pdf, page 94)
;bit0 - background receive serial data to the scratchpad
;bit1 - invert serial output data (0 = ‘T’, 1 = “N”)
;bit 2 - invert serial input data (0 = “T”, 1 = “N”)
;bit 3 - disable hserout (1 = hserout pin normal i/o)
;bit 4 - disable hserin (1 = hserin pin normal i/o)
;bits order:        %43210
	hsersetup B9600_32,%00001

;**************************************************
;DEFINITIONS
	symbol LED=A.5
	symbol PERIOD=w0 ;b0,b1
		 PERIOD=50
	symbol WORK=b2
	symbol RESULT=b3
	symbol POS_X=b4
	symbol POS_Y=b5
;**************************************************
;INTERRUPT SETTING (PicAxeManual2.pdf, page 219)
;flag0 hint0flag  X2 parts - interrupt on INT0 hintsetup
;flag1 hint1flag  X2 parts - interrupt on INT1 hintsetup
;flag2 hint2flag  X2 parts - interrupt on INT2 hintsetup
;flag3 hintflag   X2 parts - interrupt on any pin 0,1,2 hintsetup
;flag4 compflag   X2 parts - comparator flag compsetup
;flag5 hserflag   hserial background receive has occurred hsersetup
;flag6 hi2cflag   hi2c write has occurred (slave mode) hi2csetup
;flag7 toflag     timer overflow flag settimer
;bits order:    %76543210
	hintsetup %00000001
	setintflags OR %00100001,%00100001

;**************************************************
;MAIN LOOP

MainLoop:
	toggle LED
	pause PERIOD
goto MainLoop

;**************************************************
;INTERRUPT SUBRUTINE
interrupt:


hserflag=0
pause 10	;waiting for end of receiving, it is correct?
		;it works well done at this baudrate, i will
		;see after incerase... 
if hserflag=1 then { goto interrupt } else {
			PERIOD=4000

			ptr=0
			if @ptrinc="X" then {
						gosub ASCII2BIN
						POS_X=RESULT
			 			  }endif

			ptr=4
			if @ptrinc="Y" then {
						gosub ASCII2BIN
						POS_Y=RESULT
						  }endif
			ptr=0
			hserptr=0
			 } endif

if hint0flag=1 then {
				PERIOD=50
				pause 1000
				hserout 0,("X:",#POS_X,"Y:",#POS_Y)
				hint0flag=0
			} endif

setintflags OR %00100001,%00100001
return

;**************************************************
;ASCII2BIN (need registers RESULT and WORK)
ASCII2BIN:
RESULT=0
WORK=@ptrinc-48
if WORK>2 then {RESULT=255} else
{
	RESULT=100*WORK
	if WORK=2 then {
		;convert with conditions
		WORK=@ptrinc-48
		if WORK>=5 then {RESULT=RESULT+50} else
		{
			RESULT=10*WORK+RESULT
		}endif
		WORK=@ptrinc-48
		if WORK>=5 AND RESULT>=250 then {RESULT=RESULT+5} else
		{
			RESULT=WORK+RESULT
		}endif
	} else {
		;convert without conditions
		WORK=@ptrinc-48
		RESULT=10*WORK+RESULT
		WORK=@ptrinc-48
		RESULT=RESULT+WORK
	}endif
}endif
return
Program was physically tested and works very well. I am going to test it at higher baudrates.
I am open to corrections, new ideas, critic and so on. First of all i need size up efficiency of ASCI2BIN.
After correction from GURUs it can be useful example of double interrupt and using of HSER too.

Have a nice day :cool:
 

Attachments

Top