PDA

View Full Version : PicBasic Pro Math Problem??



Glen65
- 2nd March 2006, 16:22
Hello All....


It's been a while since I have done any coding, and I'm stumped as to the problem, I'm sure it's a math problem, or an array problem.

The following code is where I'm having the problem:

@ DEVICE pic16F648A, HS_OSC
'@ DEVICE pic16F648A, INTRC_OSC_NOCLKOUT
' System Clock Options
@ DEVICE pic16F648A, WDT_ON
' Watchdog Timer
@ DEVICE pic16F648A, PWRT_ON
' Power-On Timer
@ DEVICE pic16F648A, MCLR_OFF
' Master Clear Options (Internal)
@ DEVICE pic16F648A, BOD_ON
' Brown-Out Detect
'@ DEVICE pic16F648A, LVP_ON
' Low-Voltage Programming
@ DEVICE pic16F648A, CPD_OFF
' Data Memory Code Protect
@ DEVICE pic16F648A, PROTECT_OFF
' Program Code Protection

DEFINE OSC 20
DEFINE LCD_DREG PORTA
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTA
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50

DEFINE DEBUG_REG PORTB
DEFINE DEBUG_BIT 2
DEFINE DEBUG_BAUD 9600
DEFINE DEBUG_MODE 1
DEFINE DEBUGIN_REG PORTB
DEFINE DEBUGIN_BIT 1
DEFINE DEBUGIN_BAUD 9600
DEFINE DEBUGIN_MODE 1

CMCON = 7 'set PORT A to Digital
TRISA = %00000000
TRISB = %00000000
clear
Pause 2000 'Wait for LCD to startup
Lcdout $FE,1 'Clear Display
high PORTB.0 'Turn LCD backlight OFF
myVar VAR byte[10] 'Array for inbound serial data
X VAR BYTE 'Holds "I" start position
FL VAR word
ID VAR word

Begin:
debugin [STR myVar\10] 'I010010101001010 Data string

FOR X = 0 TO 9
IF myVar[X] = "I" THEN Sort ' Look for "I"
NEXT X
GOTO BEGIN

Sort:
low PORTB.0 'Turn LCD backlight ON

lcdout $FE,1,"String=",myvar[2]
lcdout $FE,$C0,myvar[3],myvar[4],myvar[5],myvar[6],myvar[7],myvar[8],myvar[9]
'The above line outputs the correct string
Pause 2000

FL = 0
fl = myVar[5]
fl = FL + (myVar[4]*10)
fl = fl + (myVar[3]*100)
FL = fl + (myVar[2]*1000)
lcdout $FE,1,"FL=",dec4 fl 'Output to LCD
'The above line output is wrong, it should be 0100
ID = 0
ID = myVar[9]
ID = ID + myVar[8]*10
ID = ID + myVar[7]*100
ID = ID + myVar[6]*1000
Lcdout $FE,$C0,"ID=",dec4 ID 'Output to LCD
'The above line output is wrong, it should be 1010
pause 5000
High PORTB.0 'Turn LCD backlight ON
Lcdout $FE,1 'Clear the LCD
GOTO Begin

END

Any help would be muchly appreciated.

Thanks in advance.

Darrel Taylor
- 2nd March 2006, 21:38
Hi Glen,

It might help for us to know what the symptoms are, and what it's supposed to be doing.
<br>

BigWumpus
- 2nd March 2006, 22:19
The solution should be easy:

You want to convert an ASCII-string to a decimal number.

You forgot, a ASCII-number has not the value from 0 to 9, but from $30 to $39. So you must truncate some bits !

Try FL=...(myvar[...] & $F) * 10... !!!!

Glen65
- 3rd March 2006, 17:08
Thanks BigWumus...

The code is working perfect, I missed the ASCII part, good thing for the forums.

Sorry Darrel Taylor, I should have been more explicit with my post. (Boss breathing down my neck).

Thanks again...

Glen65

Darrel Taylor
- 5th March 2006, 04:36
My fault Glen, I didn't see you had the symptoms intermixed with the code.

I tend to not look at code that isn't within [CODE] tags, cause it's so hard to read, all scrunched to the left.

Thanks to BigWumpus, my lazyness was overcome.

Carry On,
&nbsp;&nbsp;&nbsp;Darrel