Using INCLUDES, FUNCTIONS PROCEDURES and SUBROUTINES in Oshonsoft

ericgibbs

Joined Jan 29, 2010
18,865
hi S,
I agree this is way that Oshonsoft deals with the Return value, but I prefer the alternative method of returning the variable through an pre-assigned Global argument.

E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
You don't need a separate variable chip_sel.
Chip select can be done outside of the functions.
Also the values of the arguments can be written directly to the function.

For example:

wr_adr = 0x60 'WRITE 0xE0 %01100000 RESET ADDR [Is this needed?]
wr_byte = 0xb6 '%10110110 resets BMP280 = 0x00
chip_sel = altmtr_cs
Call periph_wr(wr_adr, wr_byte)

rd_adr = 0xd0 'address of ID =0x58 =88
rd_Byte = 0 'Dummy BYTE
data = periph_rd(rd_adr)

--->
altmtr_cs=0
Call periph_wr(0x60,0xb6)
Waitms 50
data = periph_rd(0xe0)
altmtr_cs=1
Hserout "READ RST 0 ", #data, Cr

etc....
Hi J,
You don't need a separate variable chip_sel. I'll need to read this a few times, but initially, so my first comment:

Do this section:
_________________________________________________________

altmtr_cs=0
Call periph_wr(0x60,0xb6)
Waitms 50
data = periph_rd(0xe0)
altmtr_cs=1
Hserout "READ RST 0 ", #data, Cr
'________________________________________________________
get repeated by this?:
'_________________________________________________________

compss_cs=0
Call periph_wr(0x60,0xb6)
Waitms 50
data = periph_rd(0xe0)
compss_cs=1
Hserout "READ RST 0 ", #data, Cr
'_______________________________________________________
If so, then I was trying to get both in one FUNCTION.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
Hi all,
I thought I was doing well when I got it to work and output 2x peripherals in #71, but it appears I'm not.
I'll have a re-look at it all :(
C.
 

jjw

Joined Dec 24, 2013
823
hi S,
I agree this is way that Oshonsoft deals with the Return value, but I prefer the alternative method of returning the variable through an pre-assigned Global argument.

E
I got #75 square function to work with a global variable.
Old PIC IDE v.7.85 and using watch variables.
Could the Hserout #b be the problem?
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi,
I am just dealing with basic code.
Usually a Function Returns a data value to the calling routine and a Proc does not Return any data.

In OSH it appears to be the wrong way around!
A Func does not return but a Proc does, yet the coding is shown as

Code line:
Function writeit(arg1 As Byte, arg2 As Byte, arg3 As Byte) As Word which indicates a WORD is returned.

Code line:
Proc proctest(arg1 As Byte, arg2 As Byte) ' no reference is given for the returned value!


Run this simple demo program to show this problem, I am not surprised you are having difficulty in getting the code right.
E
Hi E,
I got the #75 test program to work on my laptop, by commenting out ALL DIGITAL
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi, Ref post 85,
Are you sure that All Digital is the only change you have to that program.?

It does not work for me, the Functions return a '0'


Post a screen shot.
E
Hi E,
On my desktop, this doesn't work. V3.923 Laptop version V3.85

So was it a fluke that I got it working?
C.
 

sagor

Joined Mar 10, 2019
912
I got #75 square function to work with a global variable.
Old PIC IDE v.7.85 and using watch variables.
Could the Hserout #b be the problem?
The line before Hserout has "b = square(9)". Since "square" is a variable (when defined as Function), it is initialized as zero. And, since the function does not replace or set that value of "square" variable, it remains at zero. Global variables always work, that is not debatable. It is understanding that when a function is declared, it is defined as a variable and is allocated storage space. Unless one writes to that variable, it will remain as zero.
This method of using a "function" goes way back to my Fortran IV days, the function name/variable value has to be set in the function itself.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,163
When you use a global variable, it is equivalent to using two identical global variables. Wasteful. The variable you define and the variable defined by the function definition. It is no easier to use a separately defined global variable than using the global variable defined by the function name. Try using a global definition then replacing the variable name with he function name (& remove the global variable definition). It should still work.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi C,
Which PC did you use to make it work.?
If there is a All Digital bug we should let Vlad know.

E
Hi E,
I've now updated the laptop, so both have V3.946.
The laptop works
The desktop stalls at HSEROUT.

My question: How did my program work for me in #71?
C
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,730
When you use a global variable, it is equivalent to using two identical global variables. Wasteful. The variable you define and the variable defined by the function definition. It is no easier to use a separately defined global variable than using the global variable defined by the function name. Try using a global definition then replacing the variable name with he function name (& remove the global variable definition). It should still work.
Hi S and D,
Are you replying about 'E's example or mine?
I can see that you both have been programming for a while, so your answers can be a little 'over my head', but almost understandable, thanks.

EDIT: I think with one or the other of FUNC/PROC a Global VARIABLE 'say' [test] can be also set inside the FUNC/PROC? and used in a different way, as it's a different VARIABLE. Not sure?)
C
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi C,
Which PC did you use to make it work.?
If there is a All Digital bug we should let Vlad know.

E
Hi E,
Today I remember this happened previously. I think it is something to do with which PIC is selected in OSH, and which Vlad has worked on.
EDIT: With the PIC changed, it now works on both computers:)

C
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,730
Hi,
I appreciate all of the advice above, but it's really difficult to convert all of those wors and examples into my program. One line I do remember is 'Set the C/S outside of the FUNC/PROC.'

I've slid back to a previous program, with only one Peripheral, the ALTMTR. I moved all of the C/S from the FUNC/PROC and as each is being called, and it's working.

Sorry 'E' it's a whole small program I'm afraid. there may be errors, but I'll proof read it later.
C
 

Attachments

djsfantasi

Joined Apr 11, 2010
9,163
Hi S and D,
Are you replying about 'E's example or mine?
I can see that you both have been programming for a while, so your answers can be a little 'over my head', but almost understandable, thanks.

EDIT: I think with one or the other of FUNC/PROC a Global VARIABLE 'say' [test] can be also set inside the FUNC/PROC? and used in a different way, as it's a different VARIABLE. Not sure?)
C
Sorry for the confusion. I’m not referring to either example. I’m responding to.general comments that have said that a global variable is easier and always works. I disagree and my reply explains why.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
Sorry for the confusion. I’m not referring to either example. I’m responding to.general comments that have said that a global variable is easier and always works. I disagree and my reply explains why.
Hi D,
No probs! This is why I always reply to at least an initial, so I can connect the replies to the questions:)
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi, Ref post 85,
Are you sure that All Digital is the only change you have to that program.?

It does not work for me, the Functions return a '0'


Post a screen shot.
E
Hi E,
It's taking me ages to work through your example, and now I see you did get a result of 81, where it should be.

Here's my result:
Being more visual, I had to add the lines.
C
 

Attachments

Thread Starter

camerart

Joined Feb 25, 2013
3,730
Hi,
Full circle, we've gone back to INCLUDES.
I'm using an 18F4431 PIC, with an INCLUDE, but when compiling, the INCLUDE isn't included?
C
 
Top