PDA

View Full Version : Array -- something wrong?



Michael
- 25th March 2010, 18:30
For the life of me, I can't figure out what is wrong with this code. This is just the start of a program where I check all the bytes in the serial port (DATAIN) --
if the total = 0 then it just loops and an LED is off.

If any of the bytes contain data (a 1 or greater) then the LED is on. Then, eventually on to do other stuff.

Maybe it's the way I set up the array as I've never used one in picbasic before.

The pause is in there so I can see the LED. Thanks for any help.


DATAIN VAR BYTE[13]
ADDUP VAR BYTE
TOTAL VAR BYTE

QUAL1 VAR BYTE
QUAL2 VAR BYTE

DATAIN= 0
TOTAL = 0
ADDUP = 0

QUAL1 = 170
QUAL2 = 255

START:


SerIn PORTA.0,N1200,[QUAL1,QUAL2],DATAIN

FOR TOTAL = 1 TO 13
ADDUP = ADDUP + DATAIN[TOTAL]
NEXT TOTAL

IF ADDUP = 0 THEN
LOW PORTA.1 'DATA RECEIVED LED OFF
GOTO START
ENDIF

IF ADDUP >= 1 THEN
HIGH PORTA.1 'DATA RECEIVED LED ON
ENDIF

PAUSE 3000

ADDUP = 0
TOTAL = 0
DATAIN = 0

GOTO START

keithdoxey
- 25th March 2010, 18:37
For a 13 element array it should be 0 to 12

Michael
- 25th March 2010, 20:00
right after I posted I went to lay down -- and realized I put a one in there for some odd reason.

thanks.

Michael
- 25th March 2010, 20:15
oh well -- thought that was it myself but still no go -- yet I know my data is there on the serial port.

on the PC end (using VBnet), I have my 2 qualifiers 170 and 255 then 13 bytes being sent. everything fine N1200, port etc.

changed the PIC to --

DATAIN[13]

and (FOR 1 TO 12).

still nothing?

keithdoxey
- 25th March 2010, 20:19
What chip are you using ?

does it have analogue ports ?

Michael
- 25th March 2010, 21:58
16c72 (old timer)

but I did ADCON1 = 7

that changes porta to digital, right?

Michael
- 25th March 2010, 21:59
I see I did it again in my reply (for 1 to 12)

trust me -- it's for 0 to 12

jeeez

Chris Barron
- 26th March 2010, 05:24
Michael, I'm no expert but some of your references to DATAIN include brackets and an array element identifier after them, others don't.
In the serin command does picbasic know to put the received data byte into sequential bytes of the datain array, or should yo be putting that command in a loop and incrementing the array pointer with every pass through ?

Michael
- 26th March 2010, 13:19
Chris --

I've never used array either in picbasic anyway.

You're probably right -- there has to be more to the serin code than what I have shown. ? But all I see with SERIN in the books is how I have it set up.

I do get some action if I use separate byte variables rather than an array --

like [QUAL1,QUAL2],b0,b1,b2,b3,b4 etc

but I really want to use an array -- and to learn how as well.

Everything is setup fine on the breadboard -- I see the data with my scope etc.

Maybe someone can point to the problem, in the mean time I need to study some more on it.

mackrackit
- 26th March 2010, 13:24
Look at SERIN2.

Chris Barron
- 26th March 2010, 13:58
Chris --

You're probably right -- there has to be more to the serin code than what I have shown. ? But all I see with SERIN in the books is how I have it set up.

.

Working with the condition that you know you will receive 13 bytes of data in advance

For Bytes = 0 to 12
SerIn PORTA.0,N1200,[QUAL1,QUAL2],DATAIN[Bytes]
Next

Does the SerIn command also have a timeout which is similar to how I remember Hserin working, IE if no data is received for a predefined period the command makes a jump to another label in order to escape the loop.

Michael
- 26th March 2010, 14:14
Ok -- I'll try that -- I would have thought it would automatically load all the bytes you declare in the array.

Whatever -- I'll try it. thanks.

Michael
- 26th March 2010, 15:22
still no luck -- 2 days on this.

I think "STR" needs to be in there somewhere but as always the PBP manual is extremely vague and can't google much on it.

mackrackit
- 26th March 2010, 15:26
Did you see post #10 ?

Michael
- 26th March 2010, 16:18
yeah but?

I looked at SERIN2 in the PBP manual, don't know what you're getting at.

I may just forget the array idea -- went back to 13 bytes in a row and now that isn't even cooking.

Trying to fiddle with the timeout to see if that's it -- but once again, reading the PBP manual on certain subjects is fruitless.

For example, they say there is an optional timeout and say it's in milliseconds -- AND THAT'S IT.

No examples -- no info about "default timeouts" -- I've said it before, it's the worst manual I've ever had.

By the way, my other SERIN projects work great (even ones I've interfaced to the PC with VBnet).

mackrackit
- 26th March 2010, 16:23
From the manual... SERIN2 part


STR followed by a byte array variable, count and optional ending character will receive a string of characters. The string length is determined by the count or when the optional character is encountered in the input.

Michael
- 26th March 2010, 17:02
I'll try it out.

Went back to 13 bytes b1 thru b13

serin porta.0,n1200 [qual1,qual2],b1,b2, -- b13

which can be done, right?

even added a long timeout.

n1200,1000,start [qual1 etc

but having problems with it too.

I'm just testing each byte for the right number 1,2,4,8,16 etc

even tried t1200 and pullups and pulldowns -- 22k and no resistor.

I'll get it.

aratti
- 26th March 2010, 17:11
Dave, we must agree with Michael, the PBP manual is not realy a masterpiece.

So I think we should give him some help:


START:
SerIn2 PORTA.0,17197,500, START,[WAIT(QUAL1,QUAL2),STR DATAIN\13]

The above is the line code you must use with serin2 command.

17197 comes from 813 for 1200 bauds, plus 16384 (bit 14 set because inverted)

500 are the milliseconds serin2 will wait for data before to timeout to START

WAIT() will wait for the qualifier sequence before acquiring data

Str array_name \ 13 is the command to collect 13 bytes in your 13 byte array

Try it should work.

Tray to read again,(more carefully) the manual (pag 132-136) very likely you will find it more clear now.

Al.

mackrackit
- 26th March 2010, 17:32
Dave, we must agree with Michael, the PBP manual is not realy a masterpiece.

So I think we should give him some help:

You are correct.
Sorry Michael.

Archangel
- 26th March 2010, 18:02
START:
SerIn2 PORTA.0,17197,500, START,[WAIT(QUAL1,QUAL2),STR DATAIN\13]

The above is the line code you must use with serin2 command.

17197 comes from 813 for 1200 bauds, plus 16384 (bit 14 set because inverted)

500 are the milliseconds serin2 will wait for data before to timeout to START

WAIT() will wait for the qualifier sequence before acquiring data

Str array_name \ 13 is the command to collect 13 bytes in your 13 byte array

Try it should work.

Try to read again, (more carefully) the manual (pages 132-136) very likely you will find it more clear now.

Al.
Short and sweet, very elegant explanation Al, thanks !

Michael
- 26th March 2010, 18:21
Great -- I always hate to end up getting code written for me (and I usually don't) but the manual does have problems with being vague.

More code examples would be nice.

Oh well, at least this is added to forum "array" search.

Crystal clear now.

At first, I just saw SERIN2 as "basic stamp" friendly and ignored it -- after all, I've never even touched a stamp so why should I care?

Yet the manual sells it that way right up front.

Thanks kindly.

mackrackit
- 26th March 2010, 18:25
At first, I just saw SERIN2 as "basic stamp" friendly and ignored it
SERIN = Basic Stamp 1
SERIN2 = Basic Stamp 2

So you should have ignored both. :p

Michael
- 30th March 2010, 19:40
been ill all week and haven't tried this out yet before I burn a chip, do I also need to add DEFINE SER2_BITS 8 ?

Or does it default to that?

Good thing all those mode codes are on melabs site ot that would have been my next question.

mackrackit
- 30th March 2010, 20:45
Default is
8 N 1