constructive criticism please
Good evening all,
I m looking for advice on how to improve my code, and learn new tricks.· If any of you would like to share how you would do the same thing I am doing in a different way I would appreciate it.· Also a big thank you to Tracy Allen and others who have helped me get this far.
P.S.· This is just the beginning of a bigger project and I will try to post pics of the final result.
I m looking for advice on how to improve my code, and learn new tricks.· If any of you would like to share how you would do the same thing I am doing in a different way I would appreciate it.· Also a big thank you to Tracy Allen and others who have helped me get this far.
P.S.· This is just the beginning of a bigger project and I will try to post pics of the final result.
' File:AD592.bsp ' Author:JJ ' Date:2-29-08 ' {$STAMP BS2p} ' {$PBASIC 2.5} '----Program Description------------------------------------------------------- 'Display temperature in degrees celsius utilizing an AD592 sensor and LTC1298 'ADC '----PIN Declaration----------------------------------------------------------- CS PIN 0 CLK PIN 1 DIO_n PIN 2 Temp PIN 3 '----Constants----------------------------------------------------------------- Slope CON $0138 Offset CON 273 Config CON %1011 '----Variables----------------------------------------------------------------- rawvalue VAR Word celsius VAR Word celsius1 VAR Word celsius2 VAR Word celsius3 VAR Word average VAR Nib '----EEPROM DATA--------------------------------------------------------------- '----Initialization------------------------------------------------------------ DEBUG CLS HIGH CS HIGH DIO_n '----Program Code--------------------------------------------------------------- Main: IF Temp = 1 THEN GOSUB Get_Temp ELSE DEBUG "Temperature not selected" DEBUG HOME ENDIF GOTO Main '----Subroutines--------------------------------------------------------------- Get_Temp: DEBUG CLS DO FOR average = 1 TO 3 LOW CS SHIFTOUT DIO_n,CLK,LSBFIRST,[noparse][[/noparse]config\4] SHIFTIN DIO_n,CLK,MSBPOST,[noparse][[/noparse]rawvalue\12] HIGH CS IF average = 1 THEN celsius1 = ((rawvalue */ Slope) - 273) ELSEIF average = 2 THEN celsius2 = ((rawvalue */ Slope) - 273) ELSEIF average = 3 THEN celsius3 = ((rawvalue */ Slope) - 273) ENDIF NEXT celsius = (celsius1 + celsius2 + celsius3) DEBUG "Celsius = ",REP "-"\celsius.BIT15,DEC ABS celsius/3,".",DEC1 ABS celsius," Degrees",CR PAUSE 500 DEBUG HOME LOOP UNTIL Temp = 0 RETURN
Comments
One thing I would certainly do is to add some comments in the program! A program with no documentation is difficult to understand once you've been away from it for a while, and it's easier to debug and troubleshoot a program with documentation included in the program code, than one with out.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Programming can't be all that difficult, it's nothing but 1's and 0's
I will definitely add thos comments.
To save some code space, you can refer to celsius1, celsius2 and celsius3 as celsius(1), celsius(2) and celsius(3) respectively, so long as you declare your variables in the order you have. That allows:
Get_Temp:
DEBUG CLS
DO
FOR average = 1 TO 3
LOW CS
SHIFTOUT DIO_n,CLK,LSBFIRST,[noparse][[/noparse]config\4]
SHIFTIN DIO_n,CLK,MSBPOST,[noparse][[/noparse]rawvalue\12]
HIGH CS
celsius(1) = ((rawvalue */ Slope) - 273)
celsius(2) = ((rawvalue */ Slope) - 273)
celsius(3) = ((rawvalue */ Slope) - 273)
NEXT
celsius = (celsius(1) + celsius(2) + celsius(3))
DEBUG "Celsius = ",REP "-"\celsius.BIT15,DEC ABS celsius/3,".",DEC1 ABS celsius," Degrees",CR
PAUSE 500
DEBUG HOME
LOOP UNTIL Temp = 0
RETURN
If you don't need to retain the individual samples that are used to calculate the average, you can also save some variable space by accumulating them into one variable as follows:
Get_Temp:
DEBUG CLS
DO
celsius = 0
FOR average = 1 TO 3
LOW CS
SHIFTOUT DIO_n,CLK,LSBFIRST,[noparse][[/noparse]config\4]
SHIFTIN DIO_n,CLK,MSBPOST,[noparse][[/noparse]rawvalue\12]
HIGH CS
celsius = celsius + ((rawvalue */ Slope) - 273)
NEXT
DEBUG "Celsius = ",REP "-"\celsius.BIT15,DEC ABS celsius/3,".",DEC1 ABS celsius," Degrees",CR
PAUSE 500
DEBUG HOME
LOOP UNTIL Temp = 0
RETURN
Above code not tested but should be close and may point in helpful direction.
DH