PDA

View Full Version : Please Help How Simplifies my code



aa222k
- 18th February 2011, 14:39
HI EVERYONE
CODE IS WORKING BUT.
I AM CONFUSED THAT CODE TAKE LONG SPACE FOR SIMPLIFI WHAT IS AN OTHER WAY?
MY CODE IS


@ DEVICE PIC16F688, HS_OSC, WDT_OFF, PWRT_OFF, MCLR_OFF, BOD_OFF,PROTECT_OFF
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_SPBRG 9 ' 115200 Baud @ 18.432MHz, 0.0%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

TRISA =%11111111
TRISC =%11110111
ANSEL = 0 'ALL DIGITAL
INTCON = 0 'Disable interrupts
DATAIN VAR byte[7]
X VAR BYTE
LED VAR PORTC.3
MAIN:
HSERIN [WAIT($07,$06), str DATAIN\7]
GOSUB A
GOSUB B
GOSUB C
GOSUB CK
GOTO MAIN
A:
IF DATAIN[2]=$2D AND DATAIN[3]=$A1 AND DATAIN[4]=$00 AND DATAIN[5]=$CD AND DATAIN[6]=$C2 THEN
X[0]=1
ELSE
X[0]=0
ENDIF
RETURN
B:
IF DATAIN[2]=$2B AND DATAIN[3]=$FA AND DATAIN[4]=$3B AND DATAIN[5]=$65 AND DATAIN[6]=$0C THEN
X[1]=1
ELSE
X[1]=0
ENDIF
RETURN
C:
IF DATAIN[2]=$2E AND DATAIN[3]=$69 AND DATAIN[4]=$06 AND DATAIN[5]=$A6 AND DATAIN[6]=$21 THEN
X[2]=1
ELSE
X[2]=0
ENDIF
RETURN
CK:
IF X[0]=0 AND X[1]=0 AND X[2]=0 THEN
LOW LED
ELSE
HIGH LED
ENDIF
RETURN
GOTO MAIN
END

HenrikOlsson
- 18th February 2011, 15:22
Hi,
Try splitting up the long logical expressions, like:


A:
IF DATAIN[2]=$2D THEN
IF DATAIN[3]=$A1 THEN
IF DATAIN[4]=$00 THEN
IF DATAIN[5]=$CD THEN
IF DATAIN[6]=$C2 THEN
X[0]=1
ENDIF
ENDIF
ENDIF
ENDIF
ELSE
X[0]=0
ENDIF
RETURN


/Henrik.

aa222k
- 18th February 2011, 18:24
MANY THANKS MY BROTHER HenrikOlsson FOR REPLY
WHAT YOU THINK ABOUT
SELECT CASE
HOW CAN I USE ?

HenrikOlsson
- 18th February 2011, 18:48
You're welcome, but there's no need to SHOUT all the time ;-)

SELECT CASE might give you "neater" code but I've found it usually produces larger code than IF-THEN constructs and in this particular case I don't think Select Case is of any use to you.

Another thing that might be worth a try is to do the extract the values from the array into temporary variables first, then run your comparisons. Something like:

TempA = DataIn[2]
TempB = DataIn[3]
TempC = DataIn[4]
TempD = DataIn[5]
TempC = DataIn[6]
GOSUB A
GOSUB B
'And so on

A:
If TempA = xx THEN
If TempB = xx THEN
'and so on


I'm sure there are more you can do depending on exactly what it is you're trying to achieve.

/Henrik.

cncmachineguy
- 18th February 2011, 18:53
Are you sure this is not the reason for the slow code?


HSERIN [WAIT($07,$06), str DATAIN\7]


After all, no matter how you code the checks, the pic will not take a noticable time to process that. If it compiled to 1000 lines of code, @4Mhz clock it would only take 1mSec

What clock speed?

aa222k
- 18th February 2011, 19:30
Hi,
Try splitting up the long logical expressions, like:


A:
IF DATAIN[2]=$2D THEN
IF DATAIN[3]=$A1 THEN
IF DATAIN[4]=$00 THEN
IF DATAIN[5]=$CD THEN
IF DATAIN[6]=$C2 THEN
X[0]=1
ENDIF
ENDIF
ENDIF
ENDIF
ELSE
X[0]=0
ENDIF
RETURN


/Henrik.

ABOVE CODE IS NOT WORKING
THIS IS A GOOD FORUM FOR BEGINNAR LIKE ME AND I HOPE HELP SO,
MY PROBLEM IS NOT slow code CLOCK IS 18.432MHz
100,S STRING I WANT COMPARE

HenrikOlsson
- 19th February 2011, 09:08
Hi,
Of course it doesn't, sorry about that. Try this instead:

A:
X[0]=0
IF DATAIN[2]=$2D THEN
IF DATAIN[3]=$A1 THEN
IF DATAIN[4]=$00 THEN
IF DATAIN[5]=$CD THEN
IF DATAIN[6]=$C2 THEN
X[0]=1
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
RETURN

And please stop SHOUTING all the time, it's hard to read!