Using INCLUDES, FUNCTIONS PROCEDURES and SUBROUTINES in Oshonsoft

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Also remember that the default string length for Oshonsoft is 16 characters, including CR and LF. If your MSG1 is not declared larger and str_cnt is 3 digits, you will overflow that size in msg1.
Hi S,
Ok, I can adjust MSG1 if needed , but if you look at #17, you will see that MSG1 isn't the problem.
C
 

sagor

Joined Mar 10, 2019
1,050
I'll give it a try with some code extracted from my Oshonsoft. I just filled in the boxes where it prompts when I selected "code" insert. Maybe you have some pop-up blocker on? Mine works with a very old Firefox...

Example of code insert:
Define CLOCK_FREQUENCY = 4
'Define SIMULATION_WAITMS_VALUE = 0
AllDigital
ConfigPin PORTC = Output
ConfigPin PORTA = Input
ConfigPin PORTA.4 = Output  'LED
OPTION_REG = 0  'Disable Port A pullups
WPUA = 0  'Disable all pullups

Dim i As Byte
Dim j As Byte
Dim k As Byte
Dim pattern(16) As Byte
Dim AUXenable As Bit
Dim AUXflag As Bit
Dim Cnt As Byte  'steady state counter
Dim state As Byte  'last read state of input for compare

etc....
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
I'll give it a try with some code extracted from my Oshonsoft. I just filled in the boxes where it prompts when I selected "code" insert. Maybe you have some pop-up blocker on? Mine works with a very old Firefox...

Example of code insert:
Define CLOCK_FREQUENCY = 4
'Define SIMULATION_WAITMS_VALUE = 0
AllDigital
ConfigPin PORTC = Output
ConfigPin PORTA = Input
ConfigPin PORTA.4 = Output  'LED
OPTION_REG = 0  'Disable Port A pullups
WPUA = 0  'Disable all pullups

Dim i As Byte
Dim j As Byte
Dim k As Byte
Dim pattern(16) As Byte
Dim AUXenable As Bit
Dim AUXflag As Bit
Dim Cnt As Byte  'steady state counter
Dim state As Byte  'last read state of input for compare

etc....
Hi S,
This test was carried out earlier, but try adding the problem LINE from #17
C
 

sagor

Joined Mar 10, 2019
1,050
Test10.txt code does not match your error message, the code is different. Also, we do not know what kind of variables those two are. Is s2m a byte variable or already a "character" variable? Your code seems to be all "characters"
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Test10.txt code does not match your error message, the code is different. Also, we do not know what kind of variables those two are. Is s2m a byte variable or already a "character" variable? Your code seems to be all "characters"
Hi S,
+Xchr(s2m)
Try to remove the X in the above text, then reply
s2m is a BYTE, it means SLAVE to MASTER
C
 

sagor

Joined Mar 10, 2019
1,050
I had to guess what types of variables you declared. Your code works just fine
I had to comment out the SPI and Hserout commands to make the code run. Now I'm having trouble posting the code... Seems like this site does not like people posting anything with "CHR(" with the last bracket in it.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Hi,
In Oshonsoft, I replace the [ CHR ] with [ & ] and I could post this, which is my MAIN test INCLUDE file.
If someone with Oshonsoft could open it, then REPLACE all (In this case 1x) & with CHR, to see if this works.
C

Code:
'18F46K20 16mHz INT SPI PCB7 150421 1200
Include "setup.bas"
Include "subroutines.bas"

Define SIMULATION_WAITMS_VALUE = 1  'Comment in for SIM out for PIC

MAIN:

'---------------------------------------------------------
str_cnt = #cnt  'M2S
msg1 = "$COMPDEGM" + "," + str_cnt + "," + "W"  '+ Cr + Lf
'---------------------------------------------------------

'send msg1
msg1len = Len(msg1)  'find the length of msg1
For x = 1 To msg1len  'set the number of iterations to be done
m2s = MidStr(msg1, x, 1)  'get the character to be sent to the slave
SSPBUF = m2s  'put the character into the SSPBUFF (spi buffer) - send it?
Call spi_wr_rd()  'read via spi?
s2m = SSPBUF  '? data read back from the spi bus buffer? already done by the subroutine [[IS THIS DUPLICATE?]]??????

s2ms2m = s2ms2m + &(s2m)  'add the character to the message data string
Next x
Hserout "From Slave=", s2ms2m, CrLf  'show received data
Toggle rled  'tell the world something has happened
s2ms2m = ""  'clear received data
WaitMs 100

cnt = cnt + 1

If cnt > 180 Then
cnt = 0
Endif

Goto MAIN

End
 

sagor

Joined Mar 10, 2019
1,050
Try again, removed [ chr ] like you did and replaced with "&". This code runs ok, as does the string concatenation.. I don't have your "includes", so declared variables as they seemed to be used...

Code:
'18F46K20 16mHz INT SPI PCB7 150421 1200
'Include "setup.bas"
'Include "subroutines.bas"

Dim str_CNT As String
Dim MSG1 As String
Dim MSG1LEN As Byte
Dim M2S As String
Dim S2M As Byte
Dim X As Byte
Dim CNT As Byte
Dim S2MS2M As String


Define SIMULATION_WAITMS_VALUE = 1  'Comment in for SIM out for PIC

MAIN:

'---------------------------------------------------------
str_CNT = #CNT  'M2S
MSG1 = "$COMPDEGM" + "," + str_CNT + "," + "W"  '+ Cr + Lf
'---------------------------------------------------------

'send msg1
MSG1LEN = Len(MSG1)  'find the length of msg1
For X = 1 To MSG1LEN  'set the number of iterations to be done
M2S = MidStr(MSG1, X, 1)  'get the character to be sent to the slave
SSPBUF = M2S  'put the character into the SSPBUFF (spi buffer) - send it?
'Call spi_wr_rd()  'read via spi?
S2M = SSPBUF  '? data read back from the spi bus buffer? already done by the subroutine [[IS THIS DUPLICATE?]]

S2MS2M = S2MS2M + &(S2M)  'add the character to the message data string
Next X
Break
'Hserout "From Slave=", s2ms2m, CrLf  'show received data
'Toggle rled  'tell the world something has happened
S2MS2M = ""  'clear received data
WaitMs 100

CNT = CNT + 1

If CNT > 180 Then
CNT = 0
Endif

Goto MAIN
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Code:
'18F46K20 16mHz INT SPI PCB7 150421 1200
Proc spi_wr_rd()

pic4431_cs = 0
WaitUs 10  'wait  10us
While Not SSPSTAT.BF  'If buffer empty do nothing
Wend
s2m = SSPBUF  'if buffer full then get the character [[IS THIS DUPLICATE?]]???????????
WaitUs 2
pic4431_cs = 1

End Proc                                         

Proc spi_init()  'looks like straight from the datasheet

'4620
PORTA.4 = 0  'CS Sets SS PIN on 4431
TRISC.3 = 0  'SCK to Slave
TRISC.4 = 1  'MISO
TRISC.5 = 0  'MOSI

'MODE 0,0
pic4431_cs = 1
SSPSTAT.SMP = 0  'sample a mid data
SSPSTAT.CKE = 1
SSPSTAT.5 = 0  'I2C only
SSPSTAT.4 = 0  'I2C only
SSPSTAT.3 = 0  'I2C only
SSPSTAT.2 = 0  'I2C only
SSPSTAT.1 = 0  'I2C only
SSPSTAT.BF = 0  'If SSPBUF=1=Full

SSPCON1.WCOL = 0  'Collision detect
SSPCON1.SSPOV = 0  'Overflow
SSPCON1.SSPEN = 1  'Configure SCK,SD0,SDI,/SS
SSPCON1.CKP = 0  'Clock Idle Low, Active High
SSPCON1.SSPM3 = 0  '0010 = SPI Master mode, clock = FOSC/64
SSPCON1.SSPM2 = 0
SSPCON1.SSPM1 = 1
SSPCON1.SSPM0 = 0

End Proc
 

sagor

Joined Mar 10, 2019
1,050
I don't see any:
"Define STRING_MAX_LENGTH = "
declaration anywhere. Default for Oshonsoft Basic is 16 characters. You can define maximum length anywhere from 8 to 100 as default string lengths.
What is the maximum string lengths you are working with? Is 16 bytes long enough?
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
I don't see any:
"Define STRING_MAX_LENGTH = "
declaration anywhere. Default for Oshonsoft Basic is 16 characters. You can define maximum length anywhere from 8 to 100 as default string lengths.
What is the maximum string lengths you are working with? Is 16 bytes long enough?
Hi S,
I must have missed it when copying.
The actual STRING_MAX_LENGTH hasn't settled yet, but I think the [ 20 ] is working at the moment?
C
Code:
Include "fontsml.bas"  'This 'FONT' file must be in the same folder as this program code

'------------- TARGET SETUP  ------------------
'Const PR2set = 125  'use this for the live board: gets 5ms per interrupt
Const PR2set = 2  'use this for fast timing to speed up simulator
Define SIMULATION_WAITMS_VALUE = 1  'Comment in for SIM out for PIC

'----------- SYSTEM CONFIG  --------------------
'PIC Config
Define CONFIG1L = 0x00
Define CONFIG1H = 0x02  '0x02 =EXT 8MHz
Define CONFIG2L = 0x1e
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x81  'Set for HVP
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
Define CLOCK_FREQUENCY = 8

'OSH config
Define SINGLE_DECIMAL_PLACES = 2
Define STRING_MAX_LENGTH = 20  'long enough?
 

sagor

Joined Mar 10, 2019
1,050
I tried to compile, but do not have compiler extensions, so things like Hserout fail to compile. That said, the general code does compile without other errors when I comment out the Hserout statement.
Since I cannot compile fully, I can't help much more... I only have the IDE version, not the "Basic Compiler" versions of Oshonsoft.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
I tried to compile, but do not have compiler extensions, so things like Hserout fail to compile. That said, the general code does compile without other errors when I comment out the Hserout statement.
Since I cannot compile fully, I can't help much more... I only have the IDE version, not the "Basic Compiler" versions of Oshonsoft.
Hi S,
That's fine, thanks.
I'm in a slight tangle at the moment, but may be able to figure it out.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Hi,
A friend of mine is going to help me with all this. (Note change of title!)

He understands how to lay out a programs, much better than I do, so we are going to work it out between us.
At the moment he is only dealing with the shape of the programs, where I know better how the programs work. (there are 4x programs. 2xPCBS 2xPICs on each)

I know the programs pretty well and what I want them to do. Is there anything I can help him with, to save him effort.

Something he mentioned is GLOBAL. I'm not too sure what this means, but I was told that PROCEDURES can use the same VARIABLE names as in the MAIN program. I'm pretty sure this doesn't work in Oshonsoft, is this correct? (I think same name VARIABLES would confuse me!)
C
 

ericgibbs

Joined Jan 29, 2010
21,448
Something he mentioned is GLOBAL. I'm not too sure what this means, but I was told that PROCEDURES can use the same VARIABLE names as in the MAIN program. I'm pretty sure this doesn't work in Oshonsoft, is this correct? (I think same name VARIABLES would confuse me!)
hi C,
In OSH a Global variable is declared in the Program Code Header, it can be accessed by any routine within the Main program Code, including Proc, Func or Subr.

A Local variable which is declared within a Proc, Func or Subr can only be changed/accessed by the Code in that Proc, Func.

Write a simple piece of test Code and try it.
E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
hi C,
In OSH a Global variable is declared in the Program Code Header, it can be accessed by any routine within the Main program Code, including Proc, Func or Subr.

A Local variable which is declared within a Proc, Func or Subr can only be changed/accessed by the Code in that Proc, Func.

Write a simple piece of test Code and try it.
E
Hi E,
I found a FUNCTION in my latest program, and changed one of the VARIABLES to a the name of a VARIABLE in the MAIN program header, and it compiled. This is dangerous for me, as they are both sharing one name.

In this FUNCTION, it includes it's own DIMs, so I see how it works, but as mentioned I need to always use different VARIABLE names for ALL VARIABLES, to save confusion.
Thanks,
C.
 

djsfantasi

Joined Apr 11, 2010
9,237
Hi E,
I found a FUNCTION in my latest program, and changed one of the VARIABLES to a the name of a VARIABLE in the MAIN program header, and it compiled. This is dangerous for me, as they are both sharing one name.

In this FUNCTION, it includes it's own DIMs, so I see how it works, but as mentioned I need to always use different VARIABLE names for ALL VARIABLES, to save confusion.
Thanks,
C.
But sometimes, using different variables IS MORE confusing. There are times when ONE variable is the proper choice.

I’m going to describe an example. Let’s say your code is dealing with another device. Maybe an external GPS device. Your code has two or more functions that access the device. You would create a GLOBAL variable for the device id or address. Then, you would use the same name in all functions and Main to access the external device.

Otherwise, you’d see the separate name and wonder what it’s value is. You’d have to initialize the device address in Main and each function. At least three places. I don’t know but can guarantee you’ll have a problem because you’ll forget to change the value in one of the functions
 
Top