storage of words in the Scratchpad

guyba

Member
Hi

I noticed that the manual states that -

"...the ‘@ptr’ is a byte size variable, not a word size variable."

I was wondering if there is any way of storing words in the Scratchpad. As my application will recieve around 50 numbers (words) from an extenal device and I will need to store them some how beofre processing them.

Any suggestions ?

Cheers
Guy
 

BCJKiwi

Senior Member
Word stored to a variable consists of 2 bytes.
e.g. w0 == b0 and b1.
When writing to the scratchpad multiple bytes can be written at once, they just go into consecutive scratchpad addresses.

So if a number received into w0 then the next into w1, b0,b1,b2,b3 can be written in one instruction. The data will be written to wherever the pointer is in scratchpad, and when finished, the pointer will be left pointing at the next location.

If you check out the Put and Get instructions you will see you can also put and get word variables directly by specifying Word e.g. get word w1

check the manual a bit more and test with some code snippets. The simulator will show you what is going on if you turn on the scratchpad display in the simulator.
 

guyba

Member
Thanks for that BCJKiwi. I used the simulator and see what you mean.

Just one more quick question - reading the manual I could see that the only way to write a word to the scrachpad is using the put command.

But if I recieve 50 numbers (words) from serrxd (external input) I cant really use the @ptrinc variable to accept them and input them to the scratchpad because @ptrinc will always be a byte. Is there anyway around this ?

Thanks again for your valuable help !!
Guy
 

BCJKiwi

Senior Member
I think there is an issue with that number of words. There are not enough variables (max 14 for the 28X1) to store all the incoming data in one session.

I trust others with more experience in handling serial input will throw some light on this for you.

Once you have a block of data in consecutive variables they can be written out to the scratchpad sequentially.
 

inglewoodpete

Senior Member
If you are receiving serial data (8-bit 'characters') using HSerIn, then the incoming data will be written to the scratchpad automatically. Note that the format of the data that appears in the scratchpad depends on what data/characters/8-bit codes are sent as serial from the other end. The data is then read from the scratchpad and assembled into the required format by the PICAXE software that you write.

The following is a test programme that I wrote to familiarise myself with HSerIn & HSerOut on the 40X1 (28X1 code is the same). For the test, I used the Pregramming Editor's serial terminal.

Code:
'v1.5	22-May-2008		106 bytes	B/ground receive variable number of bytes then retransmit up to CR
'
#PICAXE 40X1
'
	'Output pin is PortC 6 (Leg 25) (Fixed)
	'Input  pin is PortC 7 (Leg 26) (Fixed)
	'
	Symbol SendNoBreak = 0
	Symbol HalfSec = 1000	'PICAXE is running at 8MHz so 0.5 seconds = 1000
'
Initialise: SetFreq m8
            HSerSetup B9600_8, %01         '%ab where a=0 No invert a=1 Invert transmit;
            '                                    and b=0 Foreground b=1 Background Receive
Main:       HSerOut SendNoBreak, ("Waiting...", CR, LF)
            HSerPtr = 0                    'Reset (background) write pointer
            HSerInFlag = 0                 'Reset reception indicator
            Ptr = 0                        'Reset read pointer
            @Ptr = 0                       'Clear the first scratchpad location
            '
            Do Until HSerInFlag = 1        'Wait for incoming character (string)
            Loop
            '
            Pause HalfSec                  'Ensure string has fully arrived
            '
            HSerOut SendNoBreak, ("Echo: ", 34) '34 is a double quote
            Do Until @Ptr = CR
               If Ptr = hserptr Then NoCR       'Don't echo beyond received chars buffer
               If @Ptr = CR Then NoCR           'Have found <nuls>
               HSerOut SendNoBreak, (@PtrInc)
            Loop
            HSerOut SendNoBreak, (34, CR, LF)   'Add closing quote
            Goto Main
            '
NoCR:       HSerOut SendNoBreak, ("(No CR)", CR, LF)
            Goto Main
 
Last edited:

guyba

Member
Ingelwoodpete
Thanks for your replay
reading the manual I understood that you cant use the HSerIn command with the programming cable. and that something that I think i will have to do.

It seems to me that there is no way around it. Would an externall eeprom help ? can I right numbers strait to an eeprom chip ?
Guy
 

BCJKiwi

Senior Member
Using the standard serial cable and sertxd/serrxd with the Programming Editor is a special case and uses the special programming ports.

serin/out use standard in/out ports but don't handle lockups/timeouts without a lot of extra work.

On the 28X1 hserin/out are also available and do have timeout, and, as IP has indicated get around the issue with buffering/writing incoming data direct to scratchpad. They also have timeout and do use special ports on the 28X1.

There is nothing to stop these (serin/out & hserin/out) being used on the same PC port (and even the same cable if it is connected to the right ports) and using hyperterminal or any other terminal program or serial data comms to send/receive this data.

The programming editor / sertxd can be used simulataneously with serin/out or hserin/out on other ports and other com ports onthe PC. A seceond PC serial port and cable, or, USB port and USB to serial cable are required. i.e. there can be two parallel simultaneous serial connections between PC and PICAXE if required.

Serin/out use any of the in/out ports but Hserin/out use special dedicated ports on the PICAXE.

For simple debugging back to the programming editor sertxd is the usual choice
 

guyba

Member
Thanka BCJKiwi. I understand that now.

It seems to me now that the best way for me to go is to take Ingelwoodpete's advice. I tried to figure our the HSerIn command. Yet I cant figure out how to write word (numbers) to the SP.

I ran the manual code but couldnt figure out how ro modify it to make it happen -

hsersetup B19200_16, %00
main:
hserin [1000,main],0,4
ptr = 0
hserout 0,(@ptrinc,@ptrinc,@ptrinc,@ptr)
goto main

I tried to play with the hserin attributes but this didnt help. any help here will be much appriciated...

Cheers
Guy
 

Technical

Technical Support
Staff member
Yet I cant figure out how to write word (numbers) to the SP.
A word is simply two bytes back to back - you can't serially send a word value, it must be two consequative bytes if raw data (or 5 bytes if in ascii). Therefore you probably don't even need to worry about the word/byte issue and are getting yourself overly confused!

You don't say if the third party sends out ascii or raw data. Presuming it is 'raw' data then the 'word' you are expecting is simply two bytes back to back anyway. So save the serial data into the scratchpad, just using two scratchpad locations for each word you expect, e.g to receive the first word simply receive two bytes into scratchpad addresses 0 and 1.

Then use two byte 'gets' for each word expected
get 0,b0
get 1,b1

as w0 is also made up of b0 and b1, w0 will therefore now contain the word value you expect (you may need to swap around b0 and b1 if the third party device sends low/high byte of the word first).
 
Top