PDA

View Full Version : HSEROUT and Commas, What? Help Me Understand



altech6983
- 20th July 2009, 04:17
Hello,

Okay so I am having a weird problem, that really isn't a problem as I can work around it. I just think that it shouldn't be working this way as I have seen no code online written that way.

Here is what I am doing, I have a remote control system that transmits the status of nine momentary switches to a receiver that then turns on the corresponding reed relay.

I had the hardest of time getting the serial to "work", I wrote a simple program to turn on a led once com was set to "1" and state to "O", which is received over the hardware port, with AX as the qualifier.

I played with it for a while and no matter what I did I couldn't get the led to turn on. I had a usb to serial cable laying around but I didn't have a converter, so I ended up just ripping the box open and soldering on to the usb to serial chip before the TTL serial to RS-232 Serial converter. After doing that I opened up microcode studio serial terminal and checked the transmitting string, I would receive this AX1O, "AX" qualifier, "1" port one, "O" on. So the transmitter was working, but not the receiver.

I then tried sending AX1O from the terminal but that didn't work either. Finally about six hours over two days later I thought, well lets try sending this, AX,1,O wow it worked :eek: . So then I programmed the transmitter to send that like this HSEROUT ["AX",",",DEC 1,",","O"] and sure enough it worked.

My question is, Why do I have to have the commas? :confused:

Below is the code for the transmitter and receiver, respectfully:

PS If you know a better way please tell me, thanks.


'Author: Albert Eardley
'Writen for 16F628A



DEFINE OSC 20
CMCON = %00000111
OPTION_REG = %11000000

HIGH PORTA.2
HIGH PORTA.3

PAUSE 300

DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 255 ' 1200 Baud @ 1.75%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

mytimer VAR BYTE
mytimer = 0

power VAR PORTA.2
switch0 VAR PORTB.0
switch1 VAR PORTA.1
switch2 VAR PORTA.5
switch3 VAR PORTB.3
switch4 VAR PORTB.4
switch5 VAR PORTB.5
switch6 VAR PORTB.6
switch7 VAR PORTB.7
switch8 VAR PORTA.0

INPUT switch0
INPUT switch1
INPUT switch2
INPUT switch3
INPUT switch4
INPUT switch5
INPUT switch6
INPUT switch7
INPUT switch8

INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
'INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _mytimercounter, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

T1CON = $31 ; Prescaler = 8, TMR1ON
@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts

MAIN:
iF switch0 = 0 then
WHILE switch0 = 0
mytimer = 0
HSEROUT ["AX",DEC 1,"O"]
PAUSE 500
WEND
HSEROUT ["AX",DEC 1,"I"]
ENDIF

IF switch1 = 0 THEN
WHILE switch1 = 0
mytimer = 0
HSEROUT ["AX",DEC 2,"O"]
PAUSE 500
WEND
HSEROUT ["AX",DEC 2,"I"]
ENDIF

IF switch2 = 0 THEN
WHILE switch2 = 0
mytimer = 0
HSEROUT ["AX",DEC 3,"O"]
PAUSE 500
WEND
HSEROUT ["AX",DEC 3,"I"]
ENDIF

IF switch3 = 0 THEN
WHILE switch3 = 0
mytimer = 0
HSEROUT ["AX",DEC 4,"O"]
PAUSE 500
WEND
HSEROUT ["AX",DEC 4,"I"]
ENDIF

IF switch4 = 0 THEN
WHILE switch4 = 0
mytimer = 0
HSEROUT ["AX",DEC 5,"O"]
PAUSE 500
WEND
HSEROUT ["AX",DEC 5,"I"]
ENDIF

IF switch5 = 0 THEN
WHILE switch5 = 0
mytimer = 0
HSEROUT ["AX",DEC 6,"O"]
PAUSE 500
WEND
HSEROUT ["AX",DEC 6,"I"]
ENDIF

IF switch6 = 0 THEN
WHILE switch6 = 0
mytimer = 0
HSEROUT ["AX",DEC 7,"O"]
PAUSE 500
WEND
HSEROUT ["AX",DEC 7,"I"]
ENDIF

IF switch7 = 0 THEN
WHILE switch7 = 0
mytimer = 0
HSEROUT ["AX",DEC 8,"O"]
PAUSE 500
WEND
HSEROUT ["AX",DEC 8,"I"]
ENDIF

IF switch8 = 0 THEN
WHILE switch8 = 0
mytimer = 0
HSEROUT ["AX",DEC 9,"O"]
PAUSE 500
WEND
HSEROUT ["AX",DEC 9,"I"]
ENDIF

GOTO MAIN

mytimercounter:
mytimer = mytimer + 1
IF mytimer >= 143 THEN
LOW power
ENDIF
@ INT_RETURN



'Author: Albert Eardley
'Writen for 16F628A

DEFINE OSC 20
CMCON = %00000111
OPTION_REG = %11000000

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
DEFINE HSER_SPBRG 255 ' 1200 Baud @ 1.75%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

switch0 VAR PORTB.0
switch1 VAR PORTB.3
switch2 VAR PORTB.4
switch3 VAR PORTB.5
switch4 VAR PORTB.6
switch5 VAR PORTB.7
switch6 VAR PORTA.0
switch7 VAR PORTA.1
switch8 VAR PORTA.2

com VAR BYTE
state VAR BYTE

MAIN:
LOW switch0
LOW switch1
LOW switch2
LOW switch3
LOW switch4
LOW switch5
LOW switch6
LOW switch7
LOW switch8

MAIN1:
HSERIN [WAIT("AX"), DEC com, state]

SELECT CASE com
CASE 1
WHILE state = "O"
HIGH switch0
HSERIN 2000,MAIN,[WAIT("AX"), DEC com, state]
IF com != 1 THEN MAIN
WEND
GOTO MAIN

CASE 2
WHILE state = "O"
HIGH switch1
HSERIN 2000,MAIN,[WAIT("AX"), DEC com, state]
IF com != 1 THEN MAIN
WEND
GOTO MAIN

CASE 3
WHILE state = "O"
HIGH switch2
HSERIN 2000,MAIN,[WAIT("AX"), DEC com, state]
IF com != 1 THEN MAIN
WEND
GOTO MAIN

CASE 4
WHILE state = "O"
HIGH switch3
HSERIN 2000,MAIN,[WAIT("AX"), DEC com, state]
IF com != 1 THEN MAIN
WEND
GOTO MAIN

CASE 5
WHILE state = "O"
HIGH switch4
HSERIN 2000,MAIN,[WAIT("AX"), DEC com, state]
IF com != 1 THEN MAIN
WEND
GOTO MAIN

CASE 6

WHILE state = "O"
HIGH switch5
HSERIN 2000,MAIN,[WAIT("AX"), DEC com, state]
IF com != 1 THEN MAIN
WEND
GOTO MAIN

CASE 7
WHILE state = "O"
HIGH switch6
HSERIN 2000,MAIN,[WAIT("AX"), DEC com, state]
IF com != 1 THEN MAIN
WEND
GOTO MAIN

CASE 8
WHILE state = "O"
HIGH switch7
HSERIN 2000,MAIN,[WAIT("AX"), DEC com, state]
IF com != 1 THEN MAIN
WEND
GOTO MAIN

CASE 9
WHILE state = "O"
HIGH switch8
HSERIN 2000,MAIN,[WAIT("AX"), DEC com, state]
IF com != 1 THEN MAIN
WEND
GOTO MAIN

CASE ELSE
GOTO MAIN

END SELECT

Jerson
- 20th July 2009, 08:58
The reason why - AX1O could as well be a name of a variable. The compiler is not human, and you need to tell it that you mean to send a string which contains AX1O.

You could send HSEROUT["AX1O"]. Much cleaner this way.

If you send as you do, the commas are needed because, each value in quotes is treated as a sequence of bytes (string). So, HSEROUT knows what to do with each.

altech6983
- 20th July 2009, 16:35
Not sure I understand, everybody's code I've seen always has something like this,

HSEROUT [B0, DEC 6] or how ever else you want to write it and the receiver is like this

HSERIN [B0var, DEC number]

I even saw one guy's command that looked like this:
HSEROUT [$FE,$47,0,2,"RPM = ", DEC5 RPM] (is sending to a serial lcd most likely but still)

and thus I was wondering why, in my case, does HSERIN need the commas to be transmitted (sorry if you've explained it, I just don't understand as I thought it was on the HSERIN side not the HSEROUT side)?

Jerson
- 20th July 2009, 17:33
I was wondering why, in my case, does HSERIN need the commas to be transmitted
You only need to transmit the commas if you expect HSERIN to read the commas. It is like sending a '0' character and expecting HSERIN to see a '0'. Think of it as a place holder or separator or as in C, a parameter separator. If you use it in that context, yes, you will need the comma to be transmitted as well as read.

Examples you quoted of HSEROUT [B0. Dec num] send out the value of variable B0 followed by the Decimal representation of num. Let us take B0 to be holding a value 123 and the value in num to be just 5. What HSEROUT sends down the line is
1235
Now, when HSERIN is trying to decipher this stream it would not know how much of the number 1235 belongs to B0. So, put a 'non-number' between the 2 numbers and HSERIN will now smart up and know which numbers go where.
let us see
HSEROUT [B0, "," , Dec Num] will now send this
123,5
When HSERIN [B0var, CommaVar, NumVar] tries to read this, it will capture the number preceding the comma into B0var and the Comma into CommaVar and Num into Numvar. That is the reason for the commas in the odd places you see.

Hope it is clear this time
cheers

altech6983
- 20th July 2009, 19:12
thanks that makes sense.