HSERIN - receive PC keystrokes into Picaxe?

GreenLeader

Senior Member
I decided to put this up under its own thread this time:
(Since earlier posts, I've exhausted the advice given so far:(. I need to use HSERIN/HSEROUT because I need the higher baud rates that it offers).

I am using the "hardware serial port" and HSEROUT to upload data from my 40X1 to the PE serial terminal. I have a cable connecting pins 25/26 to the PC serial port, similar to the recommended "download" circuit shown in the manual. This works fine for sending from 40X1 to PC.

I'd also like to have the 40X1 receive keystrokes from the connected PC using HSERIN - just a single number between 1 and 6.

I am using the code below to test this (works fine on the PE simulator). However, when I test on hardware I find that I get a scrambled response:

key | decimal response (in b2)
-----------------------------
1 | 103
2 | 51
3 | 102
4 | 25
5 | 101
6 | 50
7 | 100


I could probably just write code to map 103 to 1, 51 to 2 etc, but I'd rather understand what the issue is and do it correctly.

Is there any reason that my hardware could send ok, but not receive?
Should I be looking at the hardware, or I am missing something in the code?


Code:
#PICAXE 40X1
' Test keyboard input from PC into hardware serial port 

'set up the hardware
 hsersetup B38400_4, %10
 
' clear 20x4 lcd 
pause 1000
serout 6,T2400,(254,1) 

b2 = 0    'initialise variable to receive keystroke
  
Start:
  serout 6,T2400,(254,1)        'clear  LCD

  ' ask user to enter number (between 1 and 6)
  hserout 0,("Hit a number ",CR,LF)
  hserin 4,1  'receive 1 byte to scratchpad loc'n 4 

  ptr = 4     'set scratchpad pointer 
  b2 = @ptr  ' copy the byte to b2
  
 'display on LCD Line 1
  serout 6,T2400,(254,128,"Number was ",#b2)   

goto Start
 

Technical

Technical Support
Staff member
A computer keyboard does not send a single number out when a key is hit.
It sends a 'sequence' of numbers on each of these events:
keydown
keyup
keyheld down (repeated signal after short delay)

Your codes for 2,4,6 seem correct 'down' codes. The others look like sequence prefixes or release codes.
 

GreenLeader

Senior Member
A computer keyboard does not send a single number out when a key is hit.
It sends a 'sequence' of numbers .....
But the keyboard is not connected directly to the Picaxe - keystrokes are tranmitted via the PE terminal, so wouldn't the character associated with the key be sent and nothing else?
 

westaust55

Moderator
Keyboard scan codes

here is a table of keyboard scan codes. This table shows the differnet codes for key make (down/pressed) and key break (up/released) signalling.

Whenever a key is pressed, that key's make code is sent to the computer.
And likelwise when the key is released that key's break code is sent to the computer.

A make or break code only represents a key on a keyboard--it does not represent the character printed on that key. There is no defined relationship between a make or break code and an ASCII code. It's up to the host software to translate scan codes to characters or commands.
 

Attachments

Last edited:

tarzan

Senior Member
From the Manual:

The hardware serial input polarity is always true, it cannot be inverted. This is a limitation of the internal microcontroller structure. Therefore a MAX232 type invertor is required for computer connections.

 

GreenLeader

Senior Member
From the Manual:

The hardware serial input polarity is always true, it cannot be inverted. This is a limitation of the internal microcontroller structure. Therefore a MAX232 type invertor is required for computer connections.

Thanks - I did see that, but I figured that since I am able to transmit from picaxe to PC, I should also be able to go the other way - after all, the manuals are often not accurate :)

Now that I read it a bit more carefully it does specifically say that the serial input needs a max232 invertor. I don't want to go down that path so I might think about another way.....
 

GreenLeader

Senior Member
.....It's up to the host software to translate scan codes to characters or commands.
Thanks for that - but as I interpret it, the host software is the PE terminal, not the software (ie my program) on the Picaxe....

The PE terminal software should be sending serial RS232 data to the Picaxe, ie PE terminal should be translating then sending the desired characters...

I am beginning to suspect that its the hardware, as tarzan is suggesting...

Can Technical comment on whether a MAX232 is essential for HSERIN to work with a PC connection?
 
Last edited:

moxhamj

New Member
No, a max232 is not essential, but you have to get the polarity right.

The picaxe itself manages to receive real RS232 data with the 10k/22k network, and it manages to send the data back from pin0 (on an 08 etc) with no max232.

But there are some tricks.

It helps to think of the resting state of the line (coz I get hopelessly confused with RS232 and I'm actually in the middle of building a radio/picaxe/Z80 interface and I'm still confused *grin*)

The resting state of a true RS232 line is -3 to -12V.
The resting state of the same line after it goes through a max232 is +5V (the max232 is an inverter)

Thus, if you want to 'wire OR' a couple of RS232 signals after they have gone through a max232, you use an AND gate. Confused yet?!

The serout and serin protocols use T for true, and true is the signal that comes out of a max232, ie resting = high. If you do use True on an output, make sure you put the line high as the first line in the code, otherwise you will lose the first character.

Pick a protocol and stick with it. I've gone for 'resting = high' because it works with max232s and is the T protocol for picaxes.

If you talk to a PC and use a max232 then the T protocol works fine. The PC rests at minus 9V, the max232 thus rests at +5V and the picaxe then outputs resting at 5V and hence through a max232 sends a -9V to the PC.

But if you want to use the poor mans version, leave out the max232. Now the -9V from the PC comes directly into the picaxe. -9V is clamped at 0V with the internal diodes. +9V goes to +5V again with clamped diodes. The 'resting' state is thus 0V and so you use the N rather than T. Similarly with outputs from the picaxe to the PC, you use N because the picaxe rests at 0V, which the PC interprets as -9V (this exploits a fudge in the RS232 protocol, because in the real protocol, values from -3 to +3V are declared invalid - neither high nor low).

In simple terms, if you don't use a max232, use N protocol, and replicate the 22k/10k circuit on another picaxe input pin, and use N for picaxe to PC and just wire it straight through.
 
Last edited:

Technical

Technical Support
Staff member
Can Technical comment on whether a MAX232 is essential for HSERIN to work with a PC connection?
For HSERIN, yes on the 28X1, the polarity is fixed inside the PICs internal serial module and cannot be changed. So you do need a MAX232 or some other kind of inverter.

FOR SERIN you can use either polarity, 'N' without MAX232, 'T' with.
 

GreenLeader

Senior Member
...... Confused yet?!
Yes, afraid most of that went over my head - far to advanced for me, but thanks for the clues! Maybe I'll revisit it when I feel a bit cleverer :)

I conclude that HSERIN needs more work than I am willing to put in at the moment :(

So in the meantime, I am using this workaround:

Although I can't figure out what key has been pressed, I do know that a key has been pressed. So I've written some rather ugly code to monitor and count key presses. This is OK since I only need the user to indicate a number between 1 and 6.
 
Top