I've been working with a one wire serial LCD that likes variable values in ASCII format. Does GCBasic have a math modulo operation or equivalent? Have got some ugly code that does the job, but looking for a more elegant solution.
A random thought along this line would be to have a function that would send a string value as applied to a variable, such as:
I've made a couple of minor changes to GCBASIC, and now % functions as a modulo operator. The update is in the usual place (http://gcbasic.sourceforge.net/newfiles/update.zip).
There isn't an easy way to convert between decimal and ASCII in GCBASIC at present, but it should be possible to adapt the LCDInt routine.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is the routine I use to do Byte to ASCII conversion. Like Hugh suggest is taken directly from LCDINT Routine.
Greetings
Stefano Bonomi
STRInt (Forserial, HUN, TENS, UNITS) ; Forserial is the byte to convert in ASCII
; the function return ASCII of Hundreds, Tens, units
If CEN <>0 Then ; Number is between 100 and 255
TransmitSerial (HUN)
TransmitSerial (TENS)
TransmitSerial (UNITS)
Goto TXCOMPLETE
End if
If DEC <>0 Then ; Number is between 10 and 99
TransmitSerial (TENS)
TransmitSerial (UNITS)
GOTO TXCOMPLETE
End if
TransmitSerial (UNITS) ; Number is between 0 and 9
TXCOMPLETE:
Goto TXCOMPLETE
sub STRInt(LCDValue,SERCEN,SERDEC,SERUN)
SERCEN = 0
SERDEC = 0
SERUN = 0
LCDValueTemp = 0
IF LCDValue >= 100 then
LCDValueTemp = LCDValue / 100
SERCEN = LCDValueTemp + 48
LCDValue = LCDValue - LCDValueTemp * 100
end if
IF LCDValueTemp > 0 or LCDValue >= 10 then
LCDValueTemp = LCDValue / 10
SERDEC = LCDValueTemp + 48
LCDValue = LCDValue - LCDValueTemp * 10
end if
SERUN = LCDValue + 48
end sub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hugh: will give the % function a try thanks! Per Stefano, will have to look at the STRInt sub as being the answer here.
Stefano:
"Do you mean a function to tranform a byte in ASCII form, or do you mean a function to do a math modulo operation? (floating point to integer)"
Well, in a roundabout way both. The math operation, if Thousands = Data/1000 +Mod 100, then Hundreds = Mod100/100 + Mod10 and so forth (looks like Hugh has sprung an operator for that now). And the ASCII conversion per the STRInt sub you dug up. Both of the code examples are much better than what I could come up with. Thanks.
Regards
Kent
P.S. It's funny that I started off along the lines of STRInt code, then had to make it way more complicated. An example of UGLY code is below (I knew calling a sub within a sub is not good practice):
Sub ascii(Decimal)
Hundreds = Decimal/100
If Hundreds <> 0 Then Hndrd
Tens = Decimal/10
If Tens <> 0 Then Ten
Ones = Decimal
If Ones <> 0 Then TransmitSerial(Ones+48)
If Decimal = 0 Then TransmitSerial(48)
Sub Hndrd
TransmitSerial(Hundreds+48)
Tens = (Decimal-Hundreds*100)/10
If Tens = 0 Then TransmitSerial(48)
If Tens <> 0 Then TransmitSerial(Tens+48)
Ones = Decimal-(Hundreds*100+Tens*10)
If Ones = 0 Then TransmitSerial(48)
If Ones <> 0 Then TransmitSerial(Ones+48)
goto Place
end sub
Sub Ten
TransmitSerial(Tens+48)
Ones = Decimal-Tens*10
If Ones = 0 Then TransmitSerial(48)
If Ones <> 0 Then TransmitSerial(Ones+48)
goto Place
end sub
end sub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Can't seem to get the modula operator "%" going. Have tried many different ways using something like: Hundreds = Decimal/100 + %100 or Hundreds = Decimal/100 %100.
The assembler is giving back incorrect syntax error. Any example on how the proper syntax goes? Thanks.
Regards
Kent
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've been working with a one wire serial LCD that likes variable values in ASCII format. Does GCBasic have a math modulo operation or equivalent? Have got some ugly code that does the job, but looking for a more elegant solution.
A random thought along this line would be to have a function that would send a string value as applied to a variable, such as:
TransmitSerial("Variable") ???
-or-
SerSend(1,"Variable") ???
Going back to the modulo operation, this would be handy for seven segment displays too.
Regards,
Kent
Do you mean a function to tranform a byte in ASCII form, or do you mean a function to do a math modulo operation? (floating point to integer)
Regards
Stefano Bonomi
I've made a couple of minor changes to GCBASIC, and now % functions as a modulo operator. The update is in the usual place (http://gcbasic.sourceforge.net/newfiles/update.zip).
There isn't an easy way to convert between decimal and ASCII in GCBASIC at present, but it should be possible to adapt the LCDInt routine.
This is the routine I use to do Byte to ASCII conversion. Like Hugh suggest is taken directly from LCDINT Routine.
Greetings
Stefano Bonomi
STRInt (Forserial, HUN, TENS, UNITS) ; Forserial is the byte to convert in ASCII
; the function return ASCII of Hundreds, Tens, units
If CEN <>0 Then ; Number is between 100 and 255
TransmitSerial (HUN)
TransmitSerial (TENS)
TransmitSerial (UNITS)
Goto TXCOMPLETE
End if
If DEC <>0 Then ; Number is between 10 and 99
TransmitSerial (TENS)
TransmitSerial (UNITS)
GOTO TXCOMPLETE
End if
TransmitSerial (UNITS) ; Number is between 0 and 9
TXCOMPLETE:
Goto TXCOMPLETE
sub STRInt(LCDValue,SERCEN,SERDEC,SERUN)
SERCEN = 0
SERDEC = 0
SERUN = 0
LCDValueTemp = 0
IF LCDValue >= 100 then
LCDValueTemp = LCDValue / 100
SERCEN = LCDValueTemp + 48
LCDValue = LCDValue - LCDValueTemp * 100
end if
IF LCDValueTemp > 0 or LCDValue >= 10 then
LCDValueTemp = LCDValue / 10
SERDEC = LCDValueTemp + 48
LCDValue = LCDValue - LCDValueTemp * 10
end if
SERUN = LCDValue + 48
end sub
Thanks Hugh and Stefano,
Hugh: will give the % function a try thanks! Per Stefano, will have to look at the STRInt sub as being the answer here.
Stefano:
"Do you mean a function to tranform a byte in ASCII form, or do you mean a function to do a math modulo operation? (floating point to integer)"
Well, in a roundabout way both. The math operation, if Thousands = Data/1000 +Mod 100, then Hundreds = Mod100/100 + Mod10 and so forth (looks like Hugh has sprung an operator for that now). And the ASCII conversion per the STRInt sub you dug up. Both of the code examples are much better than what I could come up with. Thanks.
Regards
Kent
P.S. It's funny that I started off along the lines of STRInt code, then had to make it way more complicated. An example of UGLY code is below (I knew calling a sub within a sub is not good practice):
Sub ascii(Decimal)
Hundreds = Decimal/100
If Hundreds <> 0 Then Hndrd
Tens = Decimal/10
If Tens <> 0 Then Ten
Ones = Decimal
If Ones <> 0 Then TransmitSerial(Ones+48)
If Decimal = 0 Then TransmitSerial(48)
Sub Hndrd
TransmitSerial(Hundreds+48)
Tens = (Decimal-Hundreds*100)/10
If Tens = 0 Then TransmitSerial(48)
If Tens <> 0 Then TransmitSerial(Tens+48)
Ones = Decimal-(Hundreds*100+Tens*10)
If Ones = 0 Then TransmitSerial(48)
If Ones <> 0 Then TransmitSerial(Ones+48)
goto Place
end sub
Sub Ten
TransmitSerial(Tens+48)
Ones = Decimal-Tens*10
If Ones = 0 Then TransmitSerial(48)
If Ones <> 0 Then TransmitSerial(Ones+48)
goto Place
end sub
end sub
Can't seem to get the modula operator "%" going. Have tried many different ways using something like: Hundreds = Decimal/100 + %100 or Hundreds = Decimal/100 %100.
The assembler is giving back incorrect syntax error. Any example on how the proper syntax goes? Thanks.
Regards
Kent