PDA

View Full Version : Starting & Stopping a New Cog, clearing the stack ...



JMLStamp2p
01-23-2008, 09:33 PM
Good Morning,
I want to start a new cog to run the Recieve Method in my program and need help understanding a few things. I want the new Cog to monitor Pin "1"·in a constant loop where I have a tranciever in-putting Serial data. I understand that I need to be concerned about Stack space but un-clear how to code. Once I recieve a byte of data and compare that byte to "$01" in my PUB Recieve Method I want to call a·Transmit Method to output some data.

Other Questions:

1) Does rxbyte "located in FullDuplexSerials PUB rx Method " hold the Hex·· byte recieved and pass that byte back to my calling routine so that It can be compared ?

NOTE: See Recieve·Method below.

2) Once I call the Stop Method does this clear the Stack space that the "Recieve" Cog uses ?

.................................................. ......................
My Code Below:
.................................................. ......................

CON
·_CLKMODE = XTAL1 + PLL1X
·_CLKFREQ = 5_000_000·········
.................................................. .......................
OBJ
·DELAY: "Clock"
·DATA: "FullDuplexSerial"
.................................................. ........................
VAR
·long Stack [9]
·byte rxbyte
.................................................. .........................
PUB MAIN

·dira[0]~~
·dira[17]~~
·dira[1]~

·data.start(1,0,2,9600)

·Stop
·Success := (Cog := cognew(Recieve(Pin), @Stack) + 1)

·Recieve
.................................................. ..........................

PUB Stop
··if Cog
·· cogstop(Cog~ - 1)
·Main
.................................................. ..........................

PUB Recieve
·data.rx············

·· If rxbyte :=··$01
·····Transmit
·· Recieve
.................................................. ..............................................
PUB Transmit

·data.tx ($22)
·!outa[17]

·Stop····················· 'Calls the Stop Method to stop the "Recieve" Cog.
.................................................. ...............................................

Any suggestions on how to condense this code would be appreciated.
Regards.
John.






·

JMLStamp2p
01-23-2008, 09:40 PM
Please ignore the "Main" command in the Stop Method. I have re-written it as listed below.
Realized it would have caused an unwanted loop.


PUB Stop
if Cog
cogstop(Cog~ - 1)

Mike Green
01-24-2008, 02:10 AM
If I understand what you want, you don't need a separate cog in your program since FullDuplexSerial already has a buffered receive and transmit routine running in its own cog. When a character is received on pin 1, it is put into the receive buffer. There are calls (methods) in FullDuplexSerial that will wait indefinitely for a character to be received, then return it (rx), wait up to a specified amount of time in milliseconds, then return either the received character or a -1 indicating that none were received (rxtime), or return immediately with a character if available or a -1 if there are none (rx???? - you'll have to look in FullDuplexSerial for the name).

1) There is no way for your program to refer to the variables in FullDuplexSerial. You can reference constants declared in FullDuplexSerial and you can call methods in FullDuplexSerial. You could, if you needed this, add your own method to FullDuplexSerial that simply returns the address of a variable and then access that variable by using the address.

2) When you do a COGSTOP on a cog that you've started, that cog gets completely reset. It's not executing instructions. The counters are not running. The video generator is turned off. It's drawing minimal power. The space that it may have been using for a stack is no longer associated with the cog and that space contains the data that was in use when the cog was stopped.

JMLStamp2p
01-24-2008, 03:42 AM
Ok Mike, I have tried to make this as simple as I know how but still need a little help. Below I have "Main" calling two Methods. The Start Method to set up Serial Communications through FullDuplexSerial and my Recieve Method to start monitoring line "1". In FullDuplexSerial the following Method is listed:

.................................................. ..

PUB rx : rxbyte

'' Receive byte (may wait for byte)
'' returns $00..$FF

· repeat while (rxbyte := rxcheck) < 0

.................................................. ...

Does rxbyte not hold the byte that was recieved, I guess I'm confused about how we referance the value recieved. Could you please give me a sample code to accomplish this. If you don't mind, change the code below in my Recieve Method the way that it should be.

Thank You !

........................................

My Code Below:

.........................................

{{ Run Program.Spin }}

.........................................

CON
·_CLKMODE = XTAL1 + PLL16X
·_CLKFREQ = 5_000_000

OBJ
·DELAY: "Clock"
·DATA: "FullDuplexSerial"
·GO: "Sloss Timer Program"

PUB Main

·GO.Start
·GO.Recieve

...............................................

{{ Sloss Timer Program.Spin }}

...............................................


OBJ
·DELAY: "Clock"
·DATA: "FullDuplexSerial"

..................................
PUB Start

·dira[0]~~
·dira[1]~
·dira[17]~~

·data.start(1,0,2,9600)

.................................................. ..........................................

Note: When I try and compile this program it Highlights the opening parenthesis before rxbyte and tell me that an "end of line is expected" Could you please explain this.

.................................................. ...........................................

PUB Recieve

·· data.rx(rxbyte)············

·· If rxbyte :=· $01··········
···· Transmit

......................

PUB Transmit
·data.tx($05)

.....................

Mike Green
01-24-2008, 04:25 AM
data.rx is a parameterless method call that returns the next character from the buffer and waits until one is present. Maybe you want to write "rxbyte := data.rx".

JMLStamp2p
01-24-2008, 05:01 AM
Thank You Mike, I sure appreciate your help.
John.

JMLStamp2p
01-25-2008, 06:48 AM
Mike,
When you say that it returns the next char. from the buffer, how do I access it from within my program ? Could you give me a sample bit of code that would accomplish this.
Thanks,
John.

Mike Green
01-25-2008, 07:29 AM
You access the value of the character by storing it in a variable as I showed. If you have a variable "char", you write "char := data.rx" and "char" now has the value of the character and you can do whatever you want with it. Each time you call "data.rx", you get a new character value and "data.rx" will not return until one is available. There are other calls as mentioned that will return -1 if no character is available.

JMLStamp2p
01-26-2008, 11:22 PM
Mike,
Thank You so much for your help. Everything is working fine now, I will post the working code this afternoon as I would like to get your opinion on the way the rest code is structured.
Thanks again,
JMLStamp2p