PDA

View Full Version : Is it allowed to change pin reference within a subroutine?



Alberto
- 31st January 2017, 02:20
I am using three temperature sensors wired to three different pins (portB.5 - portB.6 and portB.7)

Now I am using three different sub-routine to read the sensors value, but since all the routine have the same code, I would like to have one single routine for all the three sensors, just changing the alias reference within the sub-routine. In other words, something like this:

Read_Sensors:
If index = 0 then return
If index > 3 then return
If index = 1 then use_pin = portB.5
If index = 2 then use_pin = portB.6
If index = 3 then use_pin = portB.7

Read sensor using "use_pin"

Load variable[index]

Return

The above code could it work?

If the answer is yes, how do I declare the alias "use_pin"? (Byte or Word)

Alberto

richard
- 31st January 2017, 02:40
bit indexing

inx var byte [in pbp a bit index cannot exceed 255]


PORTB.0[inx]

if inx = 5 then it references portb.5
if inx = 7 then it references portb.7

HenrikOlsson
- 31st January 2017, 07:08
If index = 1 then use_pin = portB.5
Won't do what you want. use_pin will be be assigned the value of PortB.5 (1 or 0), you can't change aliases at runtime.

Do it the way Richard shows.

/Henrik.

Alberto
- 31st January 2017, 08:33
Thank you Richard & Henrik for the tips!

I did try the code below without success. It display 00.00 for all sensors. Did I mistake something?



Dindex var byte
CIndex var byte


if CIndex = 1 Then Dindex = 5
if CIndex = 2 Then Dindex = 6
if CIndex = 3 Then Dindex = 7

owout PortB.0[Dindex],1,[$CC,$44]
owout PortB.0[Dindex],1,[$CC,$BE]
owin PortB.0[Dindex],0,[STR Dta\9]


Alberto

richard
- 31st January 2017, 10:01
owout PortB.0[Dindex],1,[$CC,$44]
owout PortB.0[Dindex],1,[$CC,$BE]
owin PortB.0[Dindex],0,[STR Dta\9]

won't fly





OWOUT Pin, Mode,[Item...]{,Label}


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).



pin mapping is a bit vague in tne book



pin 0 is maybe porta.0 pin 15 might be portb.7 I think it can vary from chip to chip too
there is reference to it somewhere in the basic_stamp compatability notes , but I can no longer find it
so
mypin=13

owin mypin,[STR Dta\9] is a legal construct















from the book


7.6.5 Applying Offsets to Bits within a Variable or Register

The array mechanism in PBP offers a solution. An offset enclosed in brackets may

be written after the bit number:

PORTB.0[x] = 1 ' Set RBx high
The actual effect of this is to add the value of x to the numeric bit number.

x = 2
PORTB.4[x] = 1 ' Set bit(4+x) high. In this case, bit-6 is set.



NOTE THAT

PBP COMMANDS WON'T ACCEPT THIS SYNTAX for command parameters. This method can only be used in expressions and with direct register

access. You CANNOT write:

HIGH PORTB.0[x] ' COMPILE ERROR

COUNT PORTB.0[x], 100, y ' COMPILE ERROR

richard
- 31st January 2017, 10:12
found the pin map in my old pbp2.60c notes'




8 pin chips 0-7 gpio 8-15 gpio
14/20 pin chips 0-7 porta 8-15 portc
18 pin chips 0-7 portb 8-15 porta
28 pin chips 0-7 portb 8-15 portc
40 pin chips 0-7 portb 8-15 portc

Alberto
- 31st January 2017, 10:15
PBP manual states that in 28 pins mcu portB = 0-7!

I did try:

Use_pin = 5

Owin use_pin,0,[strDta\9]

But it doesn't work.

How can I verify If the numbers (5/6/7) I am using are correct or no?

Many Thanks for the help.

Alberto

richard
- 31st January 2017, 10:29
small steps like this is how I would try it

what chip ?


Owin portB.5 ,0,[strDta\9]


does this work?

Owin 5 ,0,[strDta\9]

does this work?

mypin var byte
mypin=5

Owin mypin ,0,[strDta\9]

Alberto
- 31st January 2017, 10:42
pic 18F2620

Owin portB.5 ,0,[strDta\9] (works!)

Owin 5 ,0,[strDta\9] (works!)

mypin var byte
mypin=5
Owin mypin ,0,[strDta\9] (doesn't work!)


Not very elegant but this way it works!




One_W1 var PortB.5
One_W2 var PortB.6
One_W3 var PortB.7

if CIndex = 1 Then
owout One_W1,1,[$CC,$44]
owout One_W1,1,[$CC,$BE]
owin One_W1,0,[STR Dta\9]
endif

if CIndex = 2 Then
owout One_W2,1,[$CC,$44]
owout One_W2,1,[$CC,$BE]
owin One_W2,0,[STR Dta\9]
endif

if CIndex = 3 Then
owout One_W3,1,[$CC,$44]
owout One_W3,1,[$CC,$BE]
owin One_W3,0,[STR Dta\9]
endif


Alberto

richard
- 31st January 2017, 11:12
well that should have worked. i don't have one of those chips handy to try any thing.
maybe the manual is incorrect and pin must be a constant not a var.
but it says everywhere (or a variable that contains a number 0 15 ) not just that command


you can always stick them all on the one wire and use the
MATCH ROM [55h] command rather than skip rom [CCh]

richard
- 31st January 2017, 11:19
if CIndex = 1 Then
owout One_W1,1,[$CC,$44]
you realise of course the ow device takes 750mS or so to complete the conversion
before the read scratch pad can occur or you can poll the busy state

owout One_W1,1,[$CC,$BE]
owin One_W1,0,[STR Dta\9]

Dave
- 31st January 2017, 12:40
In following this thread I have to ask, Why are you not using 1 wire? Why are all of the devices on separate pins? It would make your code so much easier.

Alberto
- 31st January 2017, 14:30
you realise of course the ow device takes 750mS or so to complete the conversion
before the read scratch pad can occur or you can poll the busy state


No! I didn't realise the convertion was such a slow process!

Well I need to modify the code to cope with this necessary delay.

Thank you Richard to have brought to my attention this problem.

Alberto

Edited: Dave, the reason for not using a single pin for all 3 devices using match rom comand is simple: I did try but I failed! But now that I am aware of this long delay necessary for the convertion time, I am happy to have choosen the three separate pins, becouse in this way I can split the delay of 750 milliseconds and get all three values in 750. - 800 milliseconds.

Dave
- 31st January 2017, 17:19
Alberto, If you operate the sensors off local VDD instead of Parasitic power you can start each conversion and then check for each to be finished while the others are doing there conversions. That is the problem with Parasitic power in that you can only communicate with 1 device on the bus.

Alberto
- 1st February 2017, 08:08
Dave, I am not using "parasitic power" and I understand the convenience to use a single bus for all three devices. But the failure in using "search rom" and "match rom" command, along with my strong desire to see the project working and so many unused pins on my mcu brought me to the conclusion to choose the simpler way and get the project working.

Now that everything seems working as per my need, I could try to modify the way of reading the DS18B20, but need some help. I need a code snippet that shows How to use properly the search rom and the match rom comand.

So If anybody can help me in understanding, How to use these two commands I will be gratefull, since I got sick for the number of Times I have read the device data sheet.

So again thank you for any help given.

Alberto

richard
- 1st February 2017, 08:48
http://www.picbasic.co.uk/forum/content.php?r=374-How-to-read-a-one-wire-DS18B20-temperature-sensor-or-nine-of-them

Alberto
- 1st February 2017, 10:42
WHOA! What a great gift I have received! Thank you Richard for the link, the code is over any expectation of mine!

I should learn how to search the forum properly, because the link didn't show up during my search.

Alberto

Dave
- 1st February 2017, 12:59
Alberto, Here is a routine I wrote some years ago. I hope it helps.

' PROGRAM: 1_WIRE_SEARCH.bas
'
' WRITTEN BY DAVID PUROLA 9/18/2003
' WRITTEN FOR PIC18F452-I/L MICROPROCESSOR
'
' ************************************************** ******************
' Declare Variables
' ************************************************** ******************
SCRATCH VAR WORD 'SCRATCH VARIABLE
SCRATCH1 VAR BYTE 'SCRATCH VARIABLE

CRCData VAR Byte 'CRC CUMULATIVE DATA BYTE
CRC VAR Byte 'CRC ENDING BYTE
ONEWIRE VAR BYTE 'ONE WIRE RECEIVED DATA
ID_BIT VAR BIT 'ID BIT STATE
CMP_ID_BIT VAR BIT 'ID BIT COMPLEMENT
ID_BIT_NO VAR BYTE 'ID BIT COUNTER
LASTDEVFLG VAR BIT 'LAST DEVICE HISTORY BIT
LASTDESC VAR BYTE 'LAST DEVICE DESCRIPTION
LASTFAMDESC VAR BYTE 'LAST DEVICE FAMILY DESCRIPTION
LAST_ZERO VAR BYTE 'LAST ZERO COUNTER
ROM_NO VAR BYTE(8) 'STORAGE AREA FOR CURRENT ROM SERIAL NUMBER
SEARCHDIR VAR BIT 'SEARCH DIRECTION BIT
OK VAR BIT 'VALID DATA FLAG
DALLAS_NO VAR BYTE 'DEVICE FOUND NUMBER
DEVICES VAR BYTE(80) 'STORAGE LOCATIONS FOR 10 DEVICE SERIAL NUMBERS

CLEAR
'************************************************* ********************
'MAIN LOOP FOR PROGRAM
'************************************************* ********************
MODE1: 'DO ROM SEARCH FOR ANY DEVICES
FOR SCRATCH = 0 TO 7 'clear rom number for first search
ROM_NO[SCRATCH] = 0
NEXT
DALLAS_NO = 255
LASTDEVFLG= 0
OK = 1
WHILE LASTDEVFLG = 0
GOSUB ONEWIRE_SEARCH
IF OK = 1 THEN
DALLAS_NO = DALLAS_NO + 1
LCDOUT $fe,$80,"TOTAL ROM NUMBERS FOUND= ",DEC2 DALLAS_NO + 1
FOR SCRATCH = 0 TO 7
DEVICES[(DALLAS_NO * 8) + SCRATCH] = ROM_NO[SCRATCH]
NEXT
ENDIF
WEND
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
' DISPLAY ANY DEVICES FOUND
FOR SCRATCH1 = 0 TO DALLAS_NO
LCDOUT $fe,$C0,"SEARCH= "
FOR SCRATCH = 7 TO 0 STEP -1
LCDOUT HEX2 DEVICES[(SCRATCH1 * 8) + SCRATCH]
NEXT
LCDOUT " ",DEC2 SCRATCH1 + 1
PAUSE 1000
NEXT

GOTO MODE1

END

'************************************************* ********************
' Subroutines
'************************************************* ********************
'************************************************* ********************
ONEWIRE_SEARCH:
'************************************************* ********************
OWOUT SDA,1,[$F0] 'SEND RESET & SEARCH ROM COMMAND
IF LASTDEVFLG = 1 THEN
GOTO TWO
ENDIF
ID_BIT_NO = 1
LAST_ZERO = 0
RESEARCH:
OWIN SDA,4,[ID_BIT]
OWIN SDA,4,[CMP_ID_BIT]
IF ID_BIT = 1 AND CMP_ID_BIT = 1 THEN
GOTO TWO
ELSE
IF ID_BIT = 0 AND CMP_ID_BIT = 0 THEN
IF ID_BIT_NO = LASTDESC THEN
SEARCHDIR = 1
ELSE
IF ID_BIT_NO > LASTDESC THEN
SEARCHDIR = 0
ELSE
SEARCHDIR = ROM_NO.0[ID_BIT_NO - 1]
ENDIF
ENDIF
IF SEARCHDIR = 0 THEN
LAST_ZERO = ID_BIT_NO
IF LAST_ZERO < 9 THEN
LASTFAMDESC = LAST_ZERO
ENDIF
ENDIF
ELSE
SEARCHDIR = ID_BIT
ENDIF
ENDIF
ROM_NO.0[ID_BIT_NO - 1] = SEARCHDIR
OWOUT SDA,4,[SEARCHDIR]
ID_BIT_NO = ID_BIT_NO + 1
IF ID_BIT_NO > 64 THEN
LASTDESC = LAST_ZERO
IF LASTDESC = 0 THEN
LASTDEVFLG= 1
ENDIF
GOTO THREE
ENDIF
GOTO RESEARCH
THREE:
CRC = 0
FOR SCRATCH = 0 TO 7
CRCData = ROM_NO[SCRATCH]
GOSUB CRC8LU
NEXT
IF CRC = 0 THEN 'GOOD ROM READBACK ADDRESS
OK = 1 'SET FLAG TO STORE ADDRESS
RETURN
ENDIF
TWO:
LASTDEVFLG = 0
LASTDESC = 0
LASTFAMDESC = 0
OK = 0
RETURN

'************************************************* ********************
' ********** Subroutine CRC8LU **********
' Expects CRC at some starting value with data to CRC
' passed in CRCData.
' Gives ending CRC value and destroys CRCData
' Uses CRCData-Byte, CRC-Byte
CRC8LU:
CRCData = CRCData ^ CRC
Lookup CRCData,[0,94,188,226,97,63,221,131,194,156,126,32,163,253, 31,65,_
157,195,33,127,252,162,64,30,95,1,227,189,62,96,13 0,220,_
35,125,159,193,66,28,154,160,225,191,93,3,128,222, 60,98,_
190,224,2,92,223,129,99,61,124,34,192,158,29,67,16 1,255,_
70,24,250,164,39,121,155,197,132,218,56,102,229,18 7,89,7,_
219,133,103,57,186,228,6,88,25,71,165,251,120,38,1 96,154,_
101,59,217,135,4,90,184,230,167,249,27,69,198,152, 122,36,_
248,166,68,26,153,199,37,123,58,100,134,216,91,5,2 31,185,_
140,210,48,110,237,179,81,15,78,16,242,172,47,113, 147,205,_
17,79,173,243,112,46,204,146,211,141,111,49,178,23 6,14,80,_
175,241,19,77,206,144,114,44,109,51,209,143,12,82, 176,238,_
50,108,142,208,83,13,239,177,140,174,76,18,145,207 ,45,115,_
202,148,118,40,171,245,23,73,8,86,180,234,105,55,2 13,139,_
87,9,235,181,54,104,138,212,149,203,41,119,244,170 ,72,22,_
233,183,85,11,136,214,52,106,43,117,151,201,74,20, 246,168,_
116,42,200,150,21,75,169,247,182,232,10,84,215,137 ,107], CRC
' PBP only handles 255 constants in the list (256 for 18Cxxx). Since table is
' indexed starting at zero, special case for $FF (256th element).

If CRCData = $FF Then
CRC = 53
EndIF
Return

Alberto
- 1st February 2017, 17:26
Thank you Dave for the code posted.

At the late, I should start playing with the code next Friday. I am sure I will came back with more question on this subject!

By now thank you again for your kindness.

Alberto