PDA

View Full Version : Why (or how to) make a port array for OW



Roy___
- 21st February 2009, 21:19
Hi all !
This is my first post but this forum has been my extended manual or knowledge base for a couple of years.

I'v tried to do a array and a for next loop to addres a port(pin) where I have a DS18B20 on each pin.

Why I want a loop is to not need multiple lines of reads and calculations but a repeated read with changed port adressing.

I dont get it working with port(pin) arrays UNLESS I do a Tris first and since i do that i direct the port to be either output or input but for OWin an OWout there will be bidirectional communications.

Pbp 2.50 EasyPic 2 and 5 Microcode studio 3.0.0.5
Pic F877A
porta 0-7 all DS18b20.
Portc is leds for monitoring the states of port.
The supplied code is NOT working unless i un-rem the Tris statement.
The loop counter is working on the LCD.
This is just a test code for adressing problem.

adcon1=7 ' digital port on port A

portnr var word[8]


Deg CON 223 ' Data to display Deg ° symbol
CLR CON 1 ' CLR LCD command
LINE1 CON 128 ' LCD line #1
LINE2 CON 192 ' LCD line #2
LINE3 CON 148 ' LCD line #3
LINE4 CON 212 ' LCD line #4
INS CON 254 ' LCD command mode parameter
Sign VAR BYTE ' +/- sign for temp display
Dummy VAR BYTE ' Dummy for Div32

'Portc = %00000000 'Initiate all port c pins to low
'Trisc = %00000000 'Setup port c as all outputs


start:

lcdout ins,clr
lcdout INS,LINE1, " ++ Startup ++ "
Pause 500

for portnr = 0 to 7
pause 200
lcdout INS,LINE2, dec portnr
portc.0[portnr] = 1
pause 200
next portnr

pause 1000
goto start



Brgds Roy

Bruce
- 21st February 2009, 22:40
The supplied code is NOT working unless i un-rem the Tris statement.
Then un-rem the TRIS statement.

Port pins are inputs at power-up since TRIS regs are all 1's. If you're using HIGH & LOW commands for port pin control, it clears TRIS bits for you. With PORTx = ? it doesn't. You need to clear TRIS bits for whatever pin you want to be an output.

Roy___
- 21st February 2009, 23:06
Tnx for reply Bruce !
I'v tried what You just said, but no success.
the leds dont light up when i change it to "high portc.0[portnr]" instead of "portc.0 = 1"
With or without a Tris statement.


The 18B20 code is Yours from start (part 3 Ds1820&4 lines LCD) and what im try to do is a loop where i adress the port so i dont need 8 identical read parts.

I have it working direct adressing on the read parts and then a common calculate part but it's not a effective coding i guess and im curius why it does'nt work.

This work:
Start_Convert:
OWOUT porta.0, 1, [$CC, $44]' Skip ROM search & do temp conversion

This does not:
for portnr = 0 to 7
Start_Convert:
OWOUT porta.0[portnr], 1, [$CC, $44]' Skip ROM search & do temp conversion
( I can read the portnr on the LCD but not light leds or OW commands)


Brgds Roy

Bruce
- 22nd February 2009, 01:22
Hi Roy,

Unfortunately, not all commands work with a variable index pointer to the port pin.

For bit-twiddling - you can do something like this;



Index VAR BYTE

TRISC = 0

Main:
FOR Index = 0 TO 7
PORTC.0[Index] = 1
PAUSE 100
PORTC.0[Index] = 0
PAUSE 100
NEXT
GOTO Main
END
If you search here for port & pin indexing you'll find a TON of various ways to index pins.

Here's one http://www.picbasic.co.uk/forum/showthread.php?t=3753

And here's another that gets really interesting when Darrel steps in with his VirtualPort thing-a-ma-bob.
http://www.picbasic.co.uk/forum/showthread.php?t=4074

P.S. Welcome to the forum....;o}

Roy___
- 22nd February 2009, 15:34
Thanks Bruce !

I'v tested your example and it works like a charm.
But when I change it to Low and High instead of =1 it doesent work, neither
does Toggle so its proven that it is as you said that it NOT given that diffrent statements really will work with port-pin arrays.
Is it any list of what commands that is inoperative together ?

It's a pity that its features (bugs?) like this so we all need to try out before we know if its working and then try workarounds to get the wanted result.

Ok the whole thing with pics and basic is nice that its really IS possible in such a simple way to get such things running / results.
It's very interesting to read the forum and see how you gurus tweak and find solutions to all the undocumented possibilities in PbP.


Brgds Roy

Bruce
- 22nd February 2009, 16:52
Hi Roy,

Commands that require a pin # already know the pin is asociated with a port.

So you just need to include the pin# for the command to use.

From the manual for the HIGH command;

Pin may be a constant, 0-15, or a variable that contains a number 0-15 (e.g.B0) or a pin
name (e.g. PORTA.0).

So you can still do what you want, but not with variable bit indexing.

Just use the actual pin # in your loops Index variable.



Index VAR BYTE

Main:
FOR Index = 0 TO 15 ' PORTB.0 to PORTB.7 = 0-7. PORTC.0 to PORTC.7 = 8-15
TOGGLE Index
PAUSEUS 100
TOGGLE Index
PAUSEUS 100
NEXT
GOTO Main
END
This same approach should work with any PBP command with a pin# in the argument list.

If you're using all of PORTC on the 877A for OWOUT, then your loop Index variable would
be from 8 to 15.

On a different PIC, this may change since there may only be PORTA and PORTB. Look in the
16F877A.BAS file for how the port pins are listed like this;

PORTL VAR PORTB ' <-- 0-7
PORTH VAR PORTC ' <-- 8-15

In 16F84A.BAS

PORTL VAR PORTB ' <-- 0-7
PORTH VAR PORTA ' <-- 8-12

Now the same code example above would operate a bit different. Starting on PORTB, and
finishing on PORTA, with 0 TO 12 as the loop Index count.

Roy___
- 22nd February 2009, 18:06
Thanks Bruce !
That was informative and explaind to me why i did'nt get it working the way i tried.

I will now rewrite my working code and see how much smaller it will be by doing it by loops instead of repeatative sections.

I had a feeling of that pins was a historic basicstamp commands that was inefffective compared to Portb.x and needed to have include files.


Brgds Roy

Roy___
- 22nd February 2009, 20:16
I'v tested it now with one loop instaed of multiple lines of almost identical code and it now half the size.
Success !

Tnx Bruce !

Brgds Roy

Bruce
- 22nd February 2009, 23:30
Excellent...;o}