Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Subroutines in a seperate file

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Climber

Bascom Member



Joined: 05 Jun 2012
Posts: 15

PostPosted: Sat May 31, 2014 5:06 pm    Post subject: Subroutines in a seperate file Reply with quote

I have a collection of subroutines that I want to swap to a separate file.
How can I tell my main program where the subroutins are.
I experimented a little with $include "Sub.bas"in the main program but i got a lot of errors.

Example:

Main Program:

$include "SUB.bas"
Declare Sub Wait_taste

'Alias Definition
Center Alias Pinb.4 'Center Taste

Dim Testcount As Byte

Do
Call Wait_taste
Loop

End 'end program
------------------------------------------------------------

Program Sub.bas:

Sub Wait_taste
If Center = 0 Then
Incr Testcount
Lcd Testcount
End If
End Sub
-------------------------------------------------------------


When i compile the main program i get the error : Label not found [Wait_taste ]
I hope someone can tell me how to outsource correctly subroutines

Many greetings
Climber



(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6198
Location: Holland

blank.gif
PostPosted: Sat May 31, 2014 8:40 pm    Post subject: Reply with quote

Hi

the best is to use config submode=new and to include them first.
Code:
$regfile="m88def.dat"
$hwstack=32
... etc

config submode=new

$include "SUB.bas"


now just call the subs from here. you do not need declare statements this way, and you do not need goto to jump over code. only the used subs will be included in your code.

_________________
Mark
Back to top
View user's profile Visit poster's website
Visovian

Bascom Member



Joined: 31 Oct 2007
Posts: 584
Location: Czech

czechrepublic.gif
PostPosted: Sat May 31, 2014 8:52 pm    Post subject: Reply with quote

Code:
$include "SUB.bas"
Declare Sub Wait_taste

Sub Wait_taste must be declared before it is defined (coded) in 'sub.bas'.
You did it just vice versa.

Try either
Code:
Dim Testcount As Byte
Center Alias Pinb.4
Declare Sub Wait_taste

Goto Main
$include "sub.bas"

Main:

   Do

      Portb = 1
      Wait_taste
   Loop

End 'end program

Or
Code:

Dim Testcount As Byte
Center Alias Pinb.4
Declare Sub Wait_taste


   Do

      Portb = 1
      Wait_taste
   Loop

End 'end program

$include "sub.bas"
 
Back to top
View user's profile
Climber

Bascom Member



Joined: 05 Jun 2012
Posts: 15

PostPosted: Sun Jun 01, 2014 7:59 am    Post subject: Reply with quote

Hi,
thanks alot for your help.
The following test code with a Butterfly module works

The trick is to insert the $include between the "Goto Main" And the Main or to write the
$include after the main program

To do this not, was the error from me.
No i understand and i'm happy.



Code:

$regfile = "m169def.dat"
$crystal = 1000000                                          '1Mhz   Achtung:Fusebit 7 = 0 (Teiler durch 16 = enabled)
$hwstack = 100
$swstack = 80
$framesize = 100
$baud = 4800                                                'Baudrate zum PC: 4800 Baud

 'E/A Belegung Register B
 '        |--------PB7  Tief Taste
 '        ||-------PB6  Hoch Taste
 '        |||------PB5  Piezo Lautsprecher
 '        ||||-----PB4  Center Taste
 '        |||||----PB3  ( Miso )
 '        ||||||---PB2  ( MoSi )
 '        |||||||--PB1  ( SCK
 '        ||||||||-PB0  ("Chip Select" Data Flash Baustein )
 Ddrb = &B0010111                                           '0 = Eingang  1 = Ausgang
Portb = &B11011000                                          '1 = Pullup Widerstand eingeschaltet


'Für das Butterfly Display
$lib "lcd_butterfly.lbx"
Dim _butterfly_digit As Byte

Center Alias Pinb.4                                         'Center Taste  

Goto Main
 $include "SUB.bas"

Main:

Do
 Call Wait_taste
Loop

End                                                         'end program

Lcd_butterfly_data:
'Data & Fedcba9876543210
Data 0%                                                     ' space  chr(32)
Data &B0010000010000000%                                    ' !
Data &B0000000010010000% ' "
Data &B0000101001010001%                                    ' # -> Grad chr(35)
Data &B0011101111000001%                                    ' $
Data &B1100101101101000%                                    '  %
Data &B1101000110100001%                                    ' &
Data &B0000000010000000%                                    '  '
Data &B0001010001000001%                                    '  (  chr(40)
Data &B0001000100010001%                                    '  )
Data &B1100101000101000%                                    ' *
Data &B0010101010000000%                                    ' +
Data &B0100000000000000%                                    ' ,
Data &B0000101000000000%                                    ' -  chr(45)
Data &B0100000000000000%                                    ' .
Data &B0100000000001000%                                    '/
Data &H5559%                                                ' 0
Data &H0118%                                                ' 1
Data &H1E11%                                                ' 2  chr(50)
Data &H1B11%                                                ' 3
Data &H0B50%                                                ' 4
Data &H1B41%                                                ' 5
Data &H1F41%                                                ' 6
Data &H0111%                                                ' 7   chr(55)
Data &H1F51%                                                ' 8
Data &H1B51%                                                ' 9
Data 0%                                                     ': als Blank
Data 0%                                                     '; als Blank
Data &B1000000000001000%                                    '<   chr(60)
Data &B0001101000000000%                                    '=
Data &B0100000000100000%                                    '>
Data &B0010100001010001%                                    '?
Data &B0101011100010001%                                    '@
Data &H0F51%                                                ' A  naechste Zeilen BCDEFG.. usw. bis Z
Data &H3991% , &H1441% , &H3191% , &H1E41% , &H0E41% , &H1D41% , &H0F50% , &H2080% , &H1510% , &H8648% , &H1440% , &H0578%
Data &H8570% , &H1551% , &H0E51% , &H9551% , &H8E51% , &H1B41% , &H2081% , &H1550% , &H4448% , &HC550% , &HC028% , &H2028% , &H5009%
Data &B1100000010000000%                                    '  chr(91) frei definiert  Beispiel: umgedrehtes Y
Data &B0001101000000001%    


 


Code from SUB.bas

Code:


Sub Wait_taste
   If Center = 0 Then
       Incr Testcount
       Cls
       Lcd "TEST"
   End If
End Sub

End                                                         'end program

 
Smile Smile [/list]
Back to top
View user's profile
mmarlette

Bascom Member



Joined: 30 Nov 2007
Posts: 311
Location: Delano, MN

usa.gif
PostPosted: Thu Jan 08, 2015 10:24 pm    Post subject: Reply with quote

Mark,

Per your reply above on May 31st. I am using v2.0.7.8

The following code example will not compile. What is wrong. I just don't see it.
Error : 61 Line : 9 Label not found [SETDATE] , in File : C:\PROGRAM FILES\MCS ELECTRONICS\V2078\LIB\DATETIME.LBX
Error : 61 Line : 18 Label not found [SETTIME] , in File : C:\PROGRAM FILES\MCS ELECTRONICS\V2078\LIB\DATETIME.LBX


The SetDate and SetTime references are being derived from the date$ and time$ at lines 64 & 65.

Code Explorer knows where the labels are in DS1307_NewMode.inc

I have also tried removing the Sub DS1307_SetTime() to Sub SetTime() still errors.

Here are the two code pieces.

Main.bas

Code:

$regfile = "m2560def.dat"      ' specify the used micro
 $crystal = 16000000            ' used crystal frequency
 $baud = 115200                 ' use baud rate
 $hwstack = 200
 $swstack = 250
 $framesize = 1000
 $programmer=23                 ' Define programmer as ARDUINO V2 (using stk500v2 protocol)
 '$prog $fc,$c6,$df,$ff         ' Lock Bits, Fuse Bits Low, Fuse Bits High, Fuse Bits Extended
 '
 Config SubMode = new
 $include "DS1307_newmode.inc"

 $lib "i2c_twi.lbx"                       ' using hardware I2C/TWI

 Const False = 0
 Const True = 1
 'address of ds1307
 Const Ds1307w = &HD0                     ' Addresses of Ds1307 clock
 Const Ds1307r = &HD1

 'dim other needed variables
 Dim Weekday As Byte
 Dim lngTick as long                     ' one second tick timer counter
 Dim booRTCupdate as boolean             ' Flag to allow event to occur
 Dim strTime As String * 8
 '
 Config Date = mdy , Separator = /        ' configure the date format
 config clock = user                      ' define USER clock, it dims date / time vars
                                          ' The USER must have the calls of settime, setdate, getdatetime
                                          ' to talk to the USER's hardware
 '
 Config Sda = Portd.1                     ' define pin names, scl and sda pins
 Config Scl = Portd.0
 Config Twi = 400000                      ' CONFIG TWI will ENABLE the TWI master interface, 100KHZ or 400KHZ
 i2cinit                                  ' initialize the I2C bus

 '-----[ Output Timer ISR  for Oscilloscope Check on Porta.0 ]-----------------
 Config Porta.0 = Output                  '
 Porta.0 = 1                              ' LED off, active lo.
 TickBit Alias Porta.0
 '-----[ Setup Timer1 for Tick ISR off system XTAL]----------------------------
 ' Timer1 is used for Tick counter at 1Hz
 Config Timer1 = Timer , Prescale = 1024 , Compare A = Disconnect , Clear Timer = 1
 ' crytal   / prescale / x Hz    = Compare1a val needed
 ' 16000000 / 1024     / 1(Hz) = 15625
 Compare1a = 15625
 On Compare1a T1_TickCounter_ISR          ' Define ISR Entry Point
 Enable Timer1
 Enable Compare1a
 '-----[Serial Port(s)]--------------------------------------------------------
 Config Com1 = 115200, Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
 Open "com1:" For Binary As #1
 Waitms 5
 '
 Enable Interrupts                       ' Using a timer to generate 1 second ISR interval

 Print #1, "DS1307"
 '
 ' Clear Vars
 '
 lngTick = 0
 booRTCupdate = false
 date$ = "12/02/14"
 time$ = "20:03:00"
 '
 Do                                 ' Main Loop
    if booRTCupdate = true then
       booRTCupdate = false
       call GetDateTime()           ' read from RTC
       strTime = Time(_sec)         ' use system vars to create time string
       'strDate = Date(_day)         ' use system vars to create date string
       Print #1, "Date : "; _Month;"/";_day;"/20";_year; "  Time : "; strTime
    end if
 Loop

'------[ Timer ISR routine ]----------------------------------------------------
T1_TickCounter_ISR:
    Incr lngTick                            ' inc event counter
    Toggle TickBit                          ' allow visual on scope
    booRTCupdate = true                     ' every tick, allow DS1307 RTC to read date/time
    Return

 End
 


DS1307_NewMode.inc

Code:

sub DS1307_SetDate()
SetDate:                                     ' need to create a label for BASCOM funtions to hook in to
  _day = Makebcd(_day) : _month = Makebcd(_month) : _year = Makebcd(_year)
  I2cstart                                                  ' Generate start code
  I2cwbyte Ds1307w                                          ' send address
  I2cwbyte 4                                                ' starting address in 1307
  I2cwbyte _day                                             ' Send Data to SECONDS
  I2cwbyte _month                                           ' MINUTES
  I2cwbyte _year                                            ' Hours
  I2cstop
  Print #1, "SetDate completed"
'  return
end sub

sub DS1307_SetTime()
SetTime:                                     ' need to create a label for BASCOM funtions to hook in to
  _sec = Makebcd(_sec) : _min = Makebcd(_min) : _hour = Makebcd(_hour)
  I2cstart                                                  ' Generate start code
  I2cwbyte Ds1307w                                          ' send address
  I2cwbyte 0                                                ' starting address in 1307
  I2cwbyte _sec                                             ' Send Data to SECONDS
  I2cwbyte _min                                             ' MINUTES
  I2cwbyte _hour                                            ' Hours
  I2cstop
  Print #1, "SetTime completed"
end sub

sub GetDateTime()
  'set PORTA.0
  I2cstart                                                  ' Generate start code
  I2cwbyte Ds1307w                                          ' send address
  I2cwbyte 0                                                ' start address in 1307
  I2cstart                                                  ' Generate start code
  I2cwbyte Ds1307r                                          ' send address
  I2crbyte _sec , Ack
  I2crbyte _min , Ack                                       ' MINUTES
  I2crbyte _hour , Ack                                      ' Hours
  I2crbyte Weekday , Ack                                    ' Day of Week
  I2crbyte _day , Ack                                       ' Day of Month
  I2crbyte _month , Ack                                     ' Month of Year
  I2crbyte _year , Nack                                     ' Year
  I2cstop
  _sec = Makedec(_sec) : _min = Makedec(_min) : _hour = Makedec(_hour)
  _day = Makedec(_day) : _month = Makedec(_month) : _year = Makedec(_year)
  'reset PORTA.0
end sub
 


Please advise. TIA.

Regards,

Mark
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1136

poland.gif
PostPosted: Fri Jan 09, 2015 2:47 pm    Post subject: Reply with quote

This will compile

Code:
$regfile = "m2560def.dat"                                   ' specify the used micro
 $crystal = 16000000                                        ' used crystal frequency
 $baud = 115200                                             ' use baud rate
 $hwstack = 200
 $swstack = 250
 $framesize = 1000
 $programmer = 23                                           ' Define programmer as ARDUINO V2 (using stk500v2 protocol)
 '$prog $fc,$c6,$df,$ff         ' Lock Bits, Fuse Bits Low, Fuse Bits High, Fuse Bits Extended

 $lib "i2c_twi.lbx"                                         ' using hardware I2C/TWI

 Config Submode = New

  Config Clock = User                                       ' define USER clock, it dims date / time vars
  Config Date = Mdy , Separator = /                         ' configure the date format

 Const False = 0
 Const True = 1

'-----[Serial Port(s)]--------------------------------------------------------
 Config Com1 = 115200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
 Open "com1:" For Binary As #1

 'address of ds1307
 Const Ds1307w = &HD0                                       ' Addresses of Ds1307 clock
 Const Ds1307r = &HD1
 $include "DS1307_newmode.inc"
 'dim other needed variables
 Dim Weekday As Byte
 Dim Lngtick As Long                                        ' one second tick timer counter
 Dim Boortcupdate As Boolean                                ' Flag to allow event to occur
 Dim Strtime As String * 8
 '


                                          ' The USER must have the calls of settime, setdate, getdatetime
                                          ' to talk to the USER's hardware
 '
 Config Sda = Portd.1                                       ' define pin names, scl and sda pins
 Config Scl = Portd.0
 Config Twi = 400000                                        ' CONFIG TWI will ENABLE the TWI master interface, 100KHZ or 400KHZ
 I2cinit                                                    ' initialize the I2C bus

 '-----[ Output Timer ISR  for Oscilloscope Check on Porta.0 ]-----------------
 Config Porta.0 = Output                                    '
 Porta.0 = 1                                                ' LED off, active lo.
 Tickbit Alias Porta.0
 '-----[ Setup Timer1 for Tick ISR off system XTAL]----------------------------
 ' Timer1 is used for Tick counter at 1Hz
 Config Timer1 = Timer , Prescale = 1024 , Compare A = Disconnect , Clear Timer = 1
 ' crytal   / prescale / x Hz    = Compare1a val needed
 ' 16000000 / 1024     / 1(Hz) = 15625
 Compare1a = 15625
 On Compare1a T1_tickcounter_isr                            ' Define ISR Entry Point
 Enable Timer1
 Enable Compare1a

 Waitms 5
 '
 Enable Interrupts                                          ' Using a timer to generate 1 second ISR interval

 Print #1 , "DS1307"
 '
 ' Clear Vars
 '
 Lngtick = 0
 Boortcupdate = False
 Date$ = "12/02/14"
 Time$ = "20:03:00"
 '
 Do                                                         ' Main Loop
    If Boortcupdate = True Then
       Boortcupdate = False
       Call Getdatetime()                                   ' read from RTC
       Strtime = Time(_sec)                                 ' use system vars to create time string
       'strDate = Date(_day)         ' use system vars to create date string
       Print #1 , "Date : " ; _month ; "/" ; _day ; "/20" ; _year ; "  Time : " ; Strtime
    End If
 Loop
 End



'------[ Timer ISR routine ]----------------------------------------------------
T1_tickcounter_isr:
    Incr Lngtick                                            ' inc event counter
    Toggle Tickbit                                          ' allow visual on scope
    Boortcupdate = True                                     ' every tick, allow DS1307 RTC to read date/time
    Return
 


Code:
$nocompile


Setdate:                                                    ' need to create a label for BASCOM funtions to hook in to
  _day = Makebcd(_day) : _month = Makebcd(_month) : _year = Makebcd(_year)
  I2cstart                                                  ' Generate start code
  I2cwbyte Ds1307w                                          ' send address
  I2cwbyte 4                                                ' starting address in 1307
  I2cwbyte _day                                             ' Send Data to SECONDS
  I2cwbyte _month                                           ' MINUTES
  I2cwbyte _year                                            ' Hours
  I2cstop
  Print #1 , "SetDate completed"
Return



Settime:                                                    ' need to create a label for BASCOM funtions to hook in to
  _sec = Makebcd(_sec) : _min = Makebcd(_min) : _hour = Makebcd(_hour)
  I2cstart                                                  ' Generate start code
  I2cwbyte Ds1307w                                          ' send address
  I2cwbyte 0                                                ' starting address in 1307
  I2cwbyte _sec                                             ' Send Data to SECONDS
  I2cwbyte _min                                             ' MINUTES
  I2cwbyte _hour                                            ' Hours
  I2cstop
  Print #1 , "SetTime completed"
Return


Sub Getdatetime()
  'set PORTA.0
  I2cstart                                                  ' Generate start code
  I2cwbyte Ds1307w                                          ' send address
  I2cwbyte 0                                                ' start address in 1307
  I2cstart                                                  ' Generate start code
  I2cwbyte Ds1307r                                          ' send address
  I2crbyte _sec , Ack
  I2crbyte _min , Ack                                       ' MINUTES
  I2crbyte _hour , Ack                                      ' Hours
  I2crbyte Weekday , Ack                                    ' Day of Week
  I2crbyte _day , Ack                                       ' Day of Month
  I2crbyte _month , Ack                                     ' Month of Year
  I2crbyte _year , Nack                                     ' Year
  I2cstop
  _sec = Makedec(_sec) : _min = Makedec(_min) : _hour = Makedec(_hour)
  _day = Makedec(_day) : _month = Makedec(_month) : _year = Makedec(_year)
  'reset PORTA.0
End Sub
 


Some order must be saved. You , for example, cannot use Print #1 before You declare/describe Print #1 and first User Clock must be declared for create variables used in subs. ( I think ) Very Happy But now everything should work.



..and @Climber - You can move Lcd_butterfly_data: to separate file too but then include them under END statement because DATA must be placed there.
Back to top
View user's profile Visit poster's website
mmarlette

Bascom Member



Joined: 30 Nov 2007
Posts: 311
Location: Delano, MN

usa.gif
PostPosted: Wed Jan 14, 2015 12:04 am    Post subject: Reply with quote

EDC,

Thank you for the code and explanation. All makes sense on the order of the code, rearranged.

Sometimes one looks at stuff to long and forgets basic fundamentals.

Thanks again!!!!

Mark
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum