Using INCLUDES, FUNCTIONS PROCEDURES and SUBROUTINES in Oshonsoft

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi,
Post a sample of the Function call + args
and the Function itself.
I will edit it.
E
Hi E,
I got mixed up copying and pasting the correct sections into the CODE area, so I've posted the whole program, while I have another attempt.

As it's a fairly short program, which may be acceptable, but let me know if I should carry on copying and pasting as requested.

FUNCTION at the bottom!
C
 

Attachments

ericgibbs

Joined Jan 29, 2010
18,862
hi,
You where asking about CS select in Functions calls, procedures.

Just post a clip of a Function and its Calling args.
E

EDIT: If [ chip_sel = altmtr_cs ] is a ligitimate statement, then I could write a PROCEDURE to select [ chip_sel = altmtr_cs or chip_sel = cmpss_cs or chip_sel = 4431_cs ] and select each as the sequence procedes, and use chip_sel as one of the AGRs. Does this seem ok?
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi,
You where asking about CS select in Functions calls, procedures.

Just post a clip of a Function and its Calling args.
E

EDIT: If [ chip_sel = altmtr_cs ] is a ligitimate statement, then I could write a PROCEDURE to select [ chip_sel = altmtr_cs or chip_sel = cmpss_cs or chip_sel = 4431_cs ] and select each as the sequence procedes, and use chip_sel as one of the AGRs. Does this seem ok?
Hi E,
Yes CS is the question! Each one has a different PIC PIN.

Here is hopefully all you asked for. Watch out for errors.
C
 

Attachments

ericgibbs

Joined Jan 29, 2010
18,862
hi C,
Your understanding of the Function is way off track.:)

Note: these HEX values are for explaining the Function, use the actual HEX addr

periph_wr( 0x74 As Byte, 0x5f As Byte, altmtr_cs As Byte) ' you must the actual addr for altmtr_cs

Function periph_wr(arg1 As Byte, arg2 As Byte, arg3 As Byte) As Byte ' nothing returned
altmtr_cs = 0
chip_sel = 0
WaitUs 1
SSPBUF =arg1 ' write addr
While Not SSPSTAT.BF
Wend
WaitUs 1
SSPBUF = arg2 'wr_byte
While Not SSPSTAT.BF
Wend
altmtr_cs = 1
End Function
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi C,
Your understanding of the Function is way off track.:)

Note: these HEX values are for explaining the Function, use the actual HEX addr

periph_wr( 0x74 As Byte, 0x5f As Byte, altmtr_cs As Byte) ' you must the actual addr for altmtr_cs

Function periph_wr(arg1 As Byte, arg2 As Byte, arg3 As Byte) As Byte ' nothing returned
altmtr_cs = 0
chip_sel = 0
WaitUs 1
SSPBUF =arg1 ' write addr
While Not SSPSTAT.BF
Wend
WaitUs 1
SSPBUF = arg2 'wr_byte
While Not SSPSTAT.BF
Wend
altmtr_cs = 1
End Function
Hi E,
Yes, early days in my understanding of FUNCTIONS/PROCEDURES

In your FUNCTION eaxample #67 there is [altmtr_cs = 0] I also want to use the same FUNCTION for e,g, [ compass_cs ] plus others, my question is how can it be used for more than 1 [ -CS ]
C
 

ericgibbs

Joined Jan 29, 2010
18,862
hi,
Give a sample of the actual value of a CS, say for the compass,, which pin of the PIC is the CD output.
E
For this example post the addr of the compass
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi,
Give a sample of the actual value of a CS, say for the compass,, which pin of the PIC is the CD output.
E
For this example post the addr of the compass
Hi E,
The example I posted #66 second paragraph down, is where both the [ _CS ] are set (SYMBOLS)
C.
 

ericgibbs

Joined Jan 29, 2010
18,862
hi,
I will look it over later.

I am running some tests on this Function command, the results I get are not consistent.?

E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi,
I will look it over later.

I am running some tests on this Function command, the results I get are not consistent.?

E
Hi E,
Ok, thanks.

The READings in Chip sel #71 use LIVE peripherals, which need each of the CS to be selected. How are you able to run tests?
C.
 
Last edited:

ericgibbs

Joined Jan 29, 2010
18,862
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
 

Attachments

jjw

Joined Dec 24, 2013
823
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....
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,730
Hi E,
I
ve found errors in the
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,
In the full program there is a FUNCTION that return the result of a calculation.

It took me a long time to get FUNCTIONS and PROCESSES to work, and tried both in different sections, using a digital analyser to test the BITs, connected to the peripheral.

This problem only occured when I added the second peripheral.

I tried running your test in the simulator, but it stalls at [Hserout "Inside Square Function b = ", #b, CrLf]
C.
 

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
 

ericgibbs

Joined Jan 29, 2010
18,862
hi,
This is what J is pointing out.

Code:
loop:  ''' NOTE this a READ function
Select perf1
read_any_perf(addr0, addr1)  'say comp addr
deselect perf1
'do whatever

Select perf2
read_any_perf(addr0, addr1)  'say baro addr
deselect perf2
'do whatever


Select perf3
read_any_perf(addr0, addr1) 'say some other perf
deselect perf3
'do whatever

End                                             

'note this a Read Function
Function Read_any_perf(arg1 As Byte, arg2 As Byte) As Byte
'say read the SPI data for example
End Function
 

sagor

Joined Mar 10, 2019
912
A function has to have its name equated to the final value. ie:
Code:
Function square(arg1 As Word) As Word

b = arg1 * arg1
square = b

Hserout "Inside Square Function b = ", #b, CrLf

End Function
Will return the value 81 in the main routine. It is the name "square" that is the variable returned... SQUARE is a variable....

Always check the variable storage locations, you will also see which variables are global and which are local to the routine.
 
Last edited:
Top