Shorter code?

Tony P

Member
Below is a section of code that I have within a Program.
I am reading an RTC and outputting day, date, time etc etc.
This code works fine but I was wondering if it can be made shorter in any way using something like 'Lookup' or 'Select case'?

Code:
    IF DAY=$1 THEN SEROUT LCD,N2400,(254,128,"Su"):END IF
    IF DAY=$2 THEN SEROUT LCD,N2400,(254,128,"Mo"):END IF
    IF DAY=$3 THEN SEROUT LCD,N2400,(254,128,"Tu"):END IF
    IF DAY=$4 THEN SEROUT LCD,N2400,(254,128,"We"):END IF
    IF DAY=$5 THEN SEROUT LCD,N2400,(254,128,"Th"):END IF
    IF DAY=$6 THEN SEROUT LCD,N2400,(254,128,"Fr"):END IF
    IF DAY=$7 THEN SEROUT LCD,N2400,(254,128,"Sa"):END IF
 

hippy

Technical Support
Staff member
You are on the right track with LOOKUP ...
Code:
LookUp day, ( "-SMTWTFS" ), day1st
LookUp day, ( "-uouehra" ), day2nd
SerOut LCD, N2400, ( 254,128, day1st, day2nd )
 

Tony P

Member
Thank you hippy.
I have just realised that I have run out of variables so I have to stick to my original code as your's uses two :(
 

Tony P

Member
Could the following code be reduced in any way?

Code:
    IF MINS=$0 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$5 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$10 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$15 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$20 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$25 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$30 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$35 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$40 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$45 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$50 AND SECONDS=$1 THEN GOTO MAIN
    IF MINS=$55 AND SECONDS=$1 THEN GOTO MAIN
 

hippy

Technical Support
Staff member
Could the following code be reduced in any way?
Code:
bTemp = MINS & $0F // 5
If bTemp = 0 And SECONDS = $01 Then Goto Main
Though if you don't have any spare variables there's only the option of moving the 'AND SECONDS = $1" out of each IF, compacting multiple IF's as OR's as below.
Code:
If SECONDS = $1 Then 
  If MINS = $00 Or MINS = $05 Then Goto Main
  If MINS = $10 Or MINS = $15 Then Goto Main
  etc
End If
Many code optimisations will involve the use of some temporary variables, which is why it's always a good idea to keep w0, b0, b1 and bit0-bit15 free for such temporary use.

It might be worth posting your full code so it can be assessed to see if there are variables you could free up.
 

Tony P

Member
Thank you again hippy.

I have allocated S_W1 to S_W6 as temporary variables and can now use your LOOKUP code and also your shorter version of the mins/seconds code.

I must now look into the 'maths' side of programming to learn more about the & $0F // 5 :)
 

hippy

Technical Support
Staff member
I must now look into the 'maths' side of programming to learn more about the & $0F // 5
The "& $0F" keeps only the least significant digit of the BCD value of MINS, turns $00-$59 intoto $00-$09, then the "// 5" determines the remainder if that digit is divided by 5; that will deliver a 0-4 value, 0 only when the original number is $n0 or $n5.
 

hippy

Technical Support
Staff member
Where in the manual is a reference to the $0F ?
Page 7, PICAXE Manual 2 - "Basic Commands", is where numeric constants are described. For the PICAXE, hexadecimal numbers are prefixed by a "$". That "$0F" would be 15 in decimal, but more importantly here, %00001111 in binary.

Numbers coming back from the RTC are in what is called BCD format which is a digit in each nibble of a byte, eg, decimal 49 would be $49, or %01001001 (%0100=4, %1001=9) when BCD encoded.

Anding with $0F clears the most significant digit leaving only the least -

%01001001
%00001111
======== &
%00001001
 

RNovember

Well-known member
It might be worth posting your full code so it can be assessed to see if there are variables you could free up.
You can free up some of your variables by writing certain ones to the EEPROM, and then using them for something else, and then setting them back to the value stored in EEPROM.

I use this approach a lot.
 

hippy

Technical Support
Staff member
You can free up some of your variables by writing certain ones to the EEPROM, and then using them for something else, and then setting them back to the value stored in EEPROM.

I use this approach a lot.
I note your "certain ones", but not such a good idea if writing data frequently as it wears out the EEPROM.

RAM however has an infinite lifetime and using those instead can often be as simple as swapping READ/WRITE for PEEK/POKE, remembering that variables share the start of RAM so one has to be careful to not accidentally overwrite them !

"POKE 28, 123" will put the 123 value to where 'b28' would have existed.

"PEEK 28, b0" will load that into 'b0'.
 
Top