i need to send out a 2 digit hex number based of a decimal number such as:
if num= 1 then Xmit_rs232 (h'01')
if num = 10 then Xmit_rs232 (h'0A')
if num= 45 then Xmit_rs232 (h'2D')
but by doing it this way I have about 255 if statements, and that causes the program to not run.
I need somthing like:
xmit_rs232 (h'num') , but that does not work
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Each character of the RS232 communication constitutes a byte, with the ability of only sending one byte at a time. the Hex is a base 16 operation, so only 16 IF or Case statements required. Hex includes decimal and alpha characters so best to send out string equivalents. Try this out:
Main:
....
....
Dec2HexString = num / 16
Select Case Dec2HexString
Dec2HexString = num % 16 'Modulo or remainder
Select Case Dec2HexString
....
...
goto Main
Select Dec2HexString
Case 0
Xmit_Print ('0') 'send out string equivalent of hex
....
....
End Select
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
HERE IS WHAT I DID:
SUB NUMOUT
dec2hexstring = num / 16
select case dec2hexstring
dec2hexstring = num % 16
select case dec2hexstring
END SUB
select dec2hexstring
case 0
xmit_print ('0')
case 1
xmit_print ('1')
case 2
xmit_print ('2')
case 3
xmit_print ('3')
case 4
xmit_print ('4')
case 5
xmit_print ('5')
case 6
xmit_print ('6')
case 7
xmit_print ('7')
case 8
xmit_print ('8')
case 9
xmit_print ('9')
case 10
xmit_print ('A')
case 11
xmit_print ('B')
case 12
xmit_print ('C')
case 13
xmit_print ('D')
case 14
xmit_print ('E')
case 15
xmit_print ('F')
end select
but I get errors on it when i compile it
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
1) You got a select case with nothing in it, and/or there is no END case there also. You just have end sub.
2) Your last select is missing the CASE statement (select dec2hexstring) need a case in there (select CASE dec2hexstring)
3) I also thought we had to use quotes " " in the xmit_print("") not ' ', but never tried anything but quotes. So that looks off also.
Hope that helps out some.
Mz
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, as noted, there a couple of important omissions in my previous code. That's what happens when I don't test run the code first. So I fired up a board and the below code works fine on the 1/26/2009 update zip code. Haven't had time to see what's up with the latest update zip, but not presently not working at the moment.
'Sortware Serial with Decimal to Hex converter
'works under 1/29/2009 updatezip
#chip 16f648a,20
Ser_Init
Main:
For test = 1 to 255
Dec2HexString = test / 16
convert2Hex
Dec2HexString = test % 16 'Modulo or remainder
convert2Hex
wait 1 sec
next
goto Main
sub convert2Hex
Select case Dec2HexString
case 0
xmit_print "0"
case 1
xmit_print "1"
case 2
xmit_print "2"
case 3
xmit_print "3"
case 4
xmit_print "4"
case 5
xmit_print "5"
case 6
xmit_print "6"
case 7
xmit_print "7"
case 8
xmit_print "8"
case 9
xmit_print "9"
case 10
xmit_print "A"
case 11
xmit_print "B"
case 12
xmit_print "C"
case 13
xmit_print "D"
case 14
xmit_print "E"
case 15
xmit_print "F"
end select
end sub
Sub Ser_Init
;slight adjustment was required for 9600bps delay value
#define baud 103
#define halfbaud 52 ;place Read of SerRx in middle of bit
#define SerTxHigh Set PortB.0 On
#define SerTxLow Set PortB.0 Off
'#define SerRx PortC.7
'dir PortC.7 in ;Rx
dir PortB.0 out ;Tx
SerTxHigh ;Initial RS232 idle state
end sub
sub XMIT_PRINT (PrintData$)
'sub XMIT_PRINT (PrintData As String)
PrintLen = PrintData(0)
if PrintLen = 0 then exit sub
'Write Data
for SysPrintTemp = 1 to PrintLen
XMIT_RS232(PrintData(SysPrintTemp))
next
end sub
Sub XMIT_RS232(Xmit_Byte)#NR
SerTxLow
wait baud us
For cntr = 1 to 8
Rotate Xmit_Byte Right
If Status.C ON Then SerTxHigh
If Status.C Off Then SerTxLow
wait baud us
Next
SerTxHigh
wait baud us
end sub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
using what you have above what i get
reading the serial out using a port monitor:
30 30 instead of 00 and so on 35 36 instead of 56 it sends it as two seperate pairs with a leading 3 on each instead of a single pair of hex? I m guessing this is from the xmit_print command?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The presumption was that one would be displaying ascii characters on a terminal program (like Brays terminal http://braypp.googlepages.com/terminal ), or serial lcd, or ???. If not, then not sure what is required, Be sure that the terminal is in ascii receive mode, not hex.
For two hex numbers/characters, then two bytes are required, plain and simple (e.g. ascii "0", "A" would be 30, 41 in hex, or 48, 65 dec ).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
the units I m sending to recieve hex only and i use the xmit_rs232 (h'0A') method in most of my program as it sends a nice two digit hex number, I need the same end result from this routine, the program I use is called port monitor and it listens in hex for serial input to check the pics output. so i need a way to combine the first digit and second digit of the calculation into one 2 digit transmission such as number/16 = 5, and number % 16= A then output would equivilent to xmit_rs232 (h'5A'). I hope that clarifies my needs
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
As one would imagine, looking on the web for port monitor gets nothing. Not familiar with it. It just seems odd to use hex,when there are terminal programs out there,( like the previously mentioned Bray's++) that can listen in ascii. Try it!
With the ascii conversion, the 256 IF statements have been reduced to 16 Select Case statements. So sorry, I do not fully understand your needs.
All the best.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi...
Now i understand some cuestions previously posted in the forum: what you need is sending a string, not a value... isn't it?
I think one way can be separating the byte value in two nibbles, each nibble is a hex digit, then with the 16 "select case" convert the nibble to a string caracter, and then join the hex caracters:
low_nible = value and 0x0F
high_nible= value and 0xF0
SWAPF high_nible,F
'now convert low_nible and high_nible to hex string
nible = low_nible
convert2Hex1
result(1) = hex_str
end sub
sub convert2Hex1
Select case nible
case 0
hex_str = "0"
case 1
hex_str = "1"
case 2
hex_str = "2"
case 3
hex_str = "3"
case 4
hex_str = "4"
case 5
hex_str = "5"
case 6
hex_str = "6"
case 7
hex_str = "7"
case 8
hex_str = "8"
case 9
hex_str = "9"
case 10
hex_str = "A"
case 11
hex_str = "B"
case 12
hex_str = "C"
case 13
hex_str = "D"
case 14
hex_str = "E"
case 15
hex_str = "F"
end select
end sub
But it does not compile
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
But... reading carefully the previous posts and havin a look to Xmit_rs232 () routine...
Anonymous said:
"using what you have above what i get
reading the serial out using a port monitor:
30 30 instead of 00 and so on 35 36 instead of 56 it sends it as two seperate pairs with a leading 3 on each instead of a single pair of hex? I m guessing this is from the xmit_print command? "
then you are reading just bytes not ascii code
Then i think you don't need to do anything but:
Xmit_rs232 (num)
Where "num" is the number you want to send.
you can write that number in decimal, hex or binary, but the number is the same.
if you want to send a value from 0-255 (a byte) for example fron a calculation, just send that value.
For example all these are the same thing:
Xmit_rs232 (65)
Xmit_rs232 (0x41)
Xmit_rs232 (h'41')
Xmit_rs232 (b'00110001')
And you will receive the same value... depending how your monitor or terminal print the data in the screen you will see diferent things...
If your terminal show ascii characters then you will see "A"
if it show in decimal format you will see "65"
in hex: "0x41"
in binary: "00110001"
If we talk about what really happens then the data is: 00110001, because the real comunication are just bits.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think we are getting close:
SUB CAMOUT
low_nible = cam and 0x0F
high_nible = cam and 0xF0
SWAPF high_nible,F
'convert now
result(3) = nible2hex(high_nible)
result(4) = nible2hex(low_nible)
xmit_rs232 (result)
end sub
function nible2hex(nible)
if nible < 10 then
nible2hex = nible + 48
else
nible2hex = nible + 55
end if
end function
when I compile it says that line "xmit_rs232 (result)" has duplicate, conflicting definition for result
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
i think XMIT_PRINT (result) will not work, because this is not really a function but a subroutine, the same for XMIT_RS232 (anything)... you should use this way: XMIT_RS232 anything.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I do only need to send one byte total, but I need to be able to change each half of that byte based on my number ex: number = 1 then set high nible to 0 and low nible to 1 then send h'01' , number = 128 thenset high nible to 8 and low nible to 0 then send h'80' . I need to send hex out and only one byte, with each nible being set by the number conversion . here is what I'm trying to replace:
if cam = 0 then Xmit_rs232 (h'00')
if cam = 1 then Xmit_rs232 (h'01')
if cam = 2 then Xmit_rs232 (h'02')
if cam = 3 then Xmit_rs232 (h'03')
if cam = 4 then Xmit_rs232 (h'04')
if cam = 5 then Xmit_rs232 (h'05')
if cam = 6 then Xmit_rs232 (h'06')
if cam = 7 then Xmit_rs232 (h'07')
if cam = 8 then Xmit_rs232 (h'08')
if cam = 9 then Xmit_rs232 (h'09')
if cam = 10 then Xmit_rs232 (h'0A')
if cam = 11 then Xmit_rs232 (h'0B')
if cam = 12 then Xmit_rs232 (h'0C')
if cam = 13 then Xmit_rs232 (h'0D')
if cam = 14 then Xmit_rs232 (h'0E')
if cam = 15 then Xmit_rs232 (h'0F')
if cam = 16 then Xmit_rs232 (h'10')
..........
if cam = 255 then Xmit_rs232 (h'FF')
so that it will be much shorter and run better
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
i need to send out a 2 digit hex number based of a decimal number such as:
if num= 1 then Xmit_rs232 (h'01')
if num = 10 then Xmit_rs232 (h'0A')
if num= 45 then Xmit_rs232 (h'2D')
but by doing it this way I have about 255 if statements, and that causes the program to not run.
I need somthing like:
xmit_rs232 (h'num') , but that does not work
Each character of the RS232 communication constitutes a byte, with the ability of only sending one byte at a time. the Hex is a base 16 operation, so only 16 IF or Case statements required. Hex includes decimal and alpha characters so best to send out string equivalents. Try this out:
Main:
....
....
Dec2HexString = num / 16
Select Case Dec2HexString
Dec2HexString = num % 16 'Modulo or remainder
Select Case Dec2HexString
....
...
goto Main
Select Dec2HexString
Case 0
Xmit_Print ('0') 'send out string equivalent of hex
....
....
End Select
HERE IS WHAT I DID:
SUB NUMOUT
dec2hexstring = num / 16
select case dec2hexstring
dec2hexstring = num % 16
select case dec2hexstring
END SUB
select dec2hexstring
case 0
xmit_print ('0')
case 1
xmit_print ('1')
case 2
xmit_print ('2')
case 3
xmit_print ('3')
case 4
xmit_print ('4')
case 5
xmit_print ('5')
case 6
xmit_print ('6')
case 7
xmit_print ('7')
case 8
xmit_print ('8')
case 9
xmit_print ('9')
case 10
xmit_print ('A')
case 11
xmit_print ('B')
case 12
xmit_print ('C')
case 13
xmit_print ('D')
case 14
xmit_print ('E')
case 15
xmit_print ('F')
end select
but I get errors on it when i compile it
If this is what you are using (above code) then?
1) You got a select case with nothing in it, and/or there is no END case there also. You just have end sub.
2) Your last select is missing the CASE statement (select dec2hexstring) need a case in there (select CASE dec2hexstring)
3) I also thought we had to use quotes " " in the xmit_print("") not ' ', but never tried anything but quotes. So that looks off also.
Hope that helps out some.
Mz
Well, as noted, there a couple of important omissions in my previous code. That's what happens when I don't test run the code first. So I fired up a board and the below code works fine on the 1/26/2009 update zip code. Haven't had time to see what's up with the latest update zip, but not presently not working at the moment.
'Sortware Serial with Decimal to Hex converter
'works under 1/29/2009 updatezip
#chip 16f648a,20
Ser_Init
Main:
For test = 1 to 255
Dec2HexString = test / 16
convert2Hex
Dec2HexString = test % 16 'Modulo or remainder
convert2Hex
wait 1 sec
next
goto Main
sub convert2Hex
Select case Dec2HexString
case 0
xmit_print "0"
case 1
xmit_print "1"
case 2
xmit_print "2"
case 3
xmit_print "3"
case 4
xmit_print "4"
case 5
xmit_print "5"
case 6
xmit_print "6"
case 7
xmit_print "7"
case 8
xmit_print "8"
case 9
xmit_print "9"
case 10
xmit_print "A"
case 11
xmit_print "B"
case 12
xmit_print "C"
case 13
xmit_print "D"
case 14
xmit_print "E"
case 15
xmit_print "F"
end select
end sub
Sub Ser_Init
;slight adjustment was required for 9600bps delay value
#define baud 103
#define halfbaud 52 ;place Read of SerRx in middle of bit
#define SerTxHigh Set PortB.0 On
#define SerTxLow Set PortB.0 Off
'#define SerRx PortC.7
'dir PortC.7 in ;Rx
dir PortB.0 out ;Tx
SerTxHigh ;Initial RS232 idle state
end sub
sub XMIT_PRINT (PrintData$)
'sub XMIT_PRINT (PrintData As String)
PrintLen = PrintData(0)
if PrintLen = 0 then exit sub
'Write Data
for SysPrintTemp = 1 to PrintLen
XMIT_RS232(PrintData(SysPrintTemp))
next
end sub
Sub XMIT_RS232(Xmit_Byte)#NR
SerTxLow
wait baud us
For cntr = 1 to 8
Rotate Xmit_Byte Right
If Status.C ON Then SerTxHigh
If Status.C Off Then SerTxLow
wait baud us
Next
SerTxHigh
wait baud us
end sub
using what you have above what i get
reading the serial out using a port monitor:
30 30 instead of 00 and so on 35 36 instead of 56 it sends it as two seperate pairs with a leading 3 on each instead of a single pair of hex? I m guessing this is from the xmit_print command?
The presumption was that one would be displaying ascii characters on a terminal program (like Brays terminal http://braypp.googlepages.com/terminal ), or serial lcd, or ???. If not, then not sure what is required, Be sure that the terminal is in ascii receive mode, not hex.
For two hex numbers/characters, then two bytes are required, plain and simple (e.g. ascii "0", "A" would be 30, 41 in hex, or 48, 65 dec ).
the units I m sending to recieve hex only and i use the xmit_rs232 (h'0A') method in most of my program as it sends a nice two digit hex number, I need the same end result from this routine, the program I use is called port monitor and it listens in hex for serial input to check the pics output. so i need a way to combine the first digit and second digit of the calculation into one 2 digit transmission such as number/16 = 5, and number % 16= A then output would equivilent to xmit_rs232 (h'5A'). I hope that clarifies my needs
As one would imagine, looking on the web for port monitor gets nothing. Not familiar with it. It just seems odd to use hex,when there are terminal programs out there,( like the previously mentioned Bray's++) that can listen in ascii. Try it!
With the ascii conversion, the 256 IF statements have been reduced to 16 Select Case statements. So sorry, I do not fully understand your needs.
All the best.
Hi...
Now i understand some cuestions previously posted in the forum: what you need is sending a string, not a value... isn't it?
I think one way can be separating the byte value in two nibbles, each nibble is a hex digit, then with the 16 "select case" convert the nibble to a string caracter, and then join the hex caracters:
low_nible = value and 0x0F
high_nible= value and 0xF0
SWAPF high_nible,F
'now convert low_nible and high_nible to hex string
dim hex_str as string
dim result(2)
nible = high_nible
call convert2Hex
result(2) = hex_str
nible = low_nible
call convert2Hex
result(1) = hex_str
sub convert2Hex
Select case nible
case 0
hex_str = "0"
case 1
hex_str = "1"
case 2
..........
........
Now in "result" you have a two byte string with the caracters yu want to send.
I think it could work...
So this is what I got:
SUB CAMOUT
dim hex_str as string
dim result(2)
Dec2Hex = cam / 16
high_nible= dec2hex and 0xF0
Dec2Hex = cam % 16 'Modulo or remainder
low_nible = dec2hex and 0x0F
SWAPF high_nible,F
'now convert low_nible and high_nible to hex string
nible = high_nible
convert2Hex1
result(2) = hex_str
nible = low_nible
convert2Hex1
result(1) = hex_str
end sub
sub convert2Hex1
Select case nible
case 0
hex_str = "0"
case 1
hex_str = "1"
case 2
hex_str = "2"
case 3
hex_str = "3"
case 4
hex_str = "4"
case 5
hex_str = "5"
case 6
hex_str = "6"
case 7
hex_str = "7"
case 8
hex_str = "8"
case 9
hex_str = "9"
case 10
hex_str = "A"
case 11
hex_str = "B"
case 12
hex_str = "C"
case 13
hex_str = "D"
case 14
hex_str = "E"
case 15
hex_str = "F"
end select
end sub
But it does not compile
Hi Nobody.. the problem to compile is just this line:
dim hex_str as string
should be:
dim hex_str(2)
And i think this:
Dec2Hex = cam / 16
Dec2Hex = cam % 16 'Modulo or remainder
Is the same thing as this:
high_nible= dec2hex and 0xF0
low_nible = dec2hex and 0x0F
SWAPF high_nible,F
Then you should use one of the methods, but not both, the second one generate a shorter asm code and does the same work.
But there is a simpler method to do what you want.
Lets suppose that you want to send this string to a device:
h(XX)
Where XX is a hex "value".
this code is tested and works:
________________________________________________________________________
#include <usart.h>
#define baudrate 9600
InitUSART
dim nible as byte
dim result(5)
dim cam as byte
result() = "h(00)"
for cam = 0 to 255
call CAMOUT
HserPrint result
next
SUB CAMOUT
low_nible = cam and 0x0F
high_nible= cam and 0xF0
SWAPF high_nible,F
'now convert low_nible and high_nible to hex string
result(3) = nible2Hex(high_nible)
result(4) = nible2Hex(low_nible)
end sub
function nible2Hex(nible)
If nible < 10 then
nible2Hex = nible + 48
else
nible2Hex = nible + 55
End if
end function
_____________________________________________________________________
Received in a serial terminal:
h(00)h(01)h(02)h(03)h(04)h(05)h(06)h(07)h(08)h(09)h(0A)h(0B)h(0C)h(0D)h(0E)h(0F)h(10)
h(11)h(12)h(13)h(14)h(15)h(16)h(17)h(18)h(19)h(1A)h(1B)h(1C)......................................
....................
....................
h(F0)h(F1)h(F2)h(F3)h(F4)h(F5)h(F6)h(F7)h(F8)h(F9)h(FA)h(FB)h(FC)h(FD)h(FE)h(FF)
But... reading carefully the previous posts and havin a look to Xmit_rs232 () routine...
Anonymous said:
"using what you have above what i get
reading the serial out using a port monitor:
30 30 instead of 00 and so on 35 36 instead of 56 it sends it as two seperate pairs with a leading 3 on each instead of a single pair of hex? I m guessing this is from the xmit_print command? "
then you are reading just bytes not ascii code
Then i think you don't need to do anything but:
Xmit_rs232 (num)
Where "num" is the number you want to send.
you can write that number in decimal, hex or binary, but the number is the same.
if you want to send a value from 0-255 (a byte) for example fron a calculation, just send that value.
For example all these are the same thing:
Xmit_rs232 (65)
Xmit_rs232 (0x41)
Xmit_rs232 (h'41')
Xmit_rs232 (b'00110001')
And you will receive the same value... depending how your monitor or terminal print the data in the screen you will see diferent things...
If your terminal show ascii characters then you will see "A"
if it show in decimal format you will see "65"
in hex: "0x41"
in binary: "00110001"
If we talk about what really happens then the data is: 00110001, because the real comunication are just bits.
I think we are getting close:
SUB CAMOUT
low_nible = cam and 0x0F
high_nible = cam and 0xF0
SWAPF high_nible,F
'convert now
result(3) = nible2hex(high_nible)
result(4) = nible2hex(low_nible)
xmit_rs232 (result)
end sub
function nible2hex(nible)
if nible < 10 then
nible2hex = nible + 48
else
nible2hex = nible + 55
end if
end function
when I compile it says that line "xmit_rs232 (result)" has duplicate, conflicting definition for result
Yes... you are using a function (xmit_rs232) to send one byte, the code i posted is to send an string.
I supose you are using these functions: https://sourceforge.net/forum/forum.php?thread_id=2155758&forum_id=629990 , But not sure.
If you are using that functions you should use:
XMIT_PRINT result
wich is the function to send a string.
i think XMIT_PRINT (result) will not work, because this is not really a function but a subroutine, the same for XMIT_RS232 (anything)... you should use this way: XMIT_RS232 anything.
I do only need to send one byte total, but I need to be able to change each half of that byte based on my number ex: number = 1 then set high nible to 0 and low nible to 1 then send h'01' , number = 128 thenset high nible to 8 and low nible to 0 then send h'80' . I need to send hex out and only one byte, with each nible being set by the number conversion . here is what I'm trying to replace:
if cam = 0 then Xmit_rs232 (h'00')
if cam = 1 then Xmit_rs232 (h'01')
if cam = 2 then Xmit_rs232 (h'02')
if cam = 3 then Xmit_rs232 (h'03')
if cam = 4 then Xmit_rs232 (h'04')
if cam = 5 then Xmit_rs232 (h'05')
if cam = 6 then Xmit_rs232 (h'06')
if cam = 7 then Xmit_rs232 (h'07')
if cam = 8 then Xmit_rs232 (h'08')
if cam = 9 then Xmit_rs232 (h'09')
if cam = 10 then Xmit_rs232 (h'0A')
if cam = 11 then Xmit_rs232 (h'0B')
if cam = 12 then Xmit_rs232 (h'0C')
if cam = 13 then Xmit_rs232 (h'0D')
if cam = 14 then Xmit_rs232 (h'0E')
if cam = 15 then Xmit_rs232 (h'0F')
if cam = 16 then Xmit_rs232 (h'10')
..........
if cam = 255 then Xmit_rs232 (h'FF')
so that it will be much shorter and run better
It is what i was thinking...
you just have to do this:
Xmit_rs232 cam
because you just want to send a byte, 255 **** IS THE SAME THAN **** h'FF' , you don't need to convert anything.
I think Xmit_rs232 (cam) does not work... but try it if you want.
That did it!
Thanks so much for all the help.
Ryan
well !!... finally it was the simplest way... :)