Distribution of time with Time Bases (OshonSoft Basic).

Status
Not open for further replies.

DogFlu66

Member
Time distribution in the code with “Times Bases” (Oschonsoft Basic),

Hello again; this time I leave you a set of functions that use interrupts and a timer. They are used to distribute execution time between codes. This set of functions allows you to decide the number of times a certain code is executed per second. They call these types of functions "Time Bases".

I will leave some examples so that you can appreciate the importance of time bases in controlling the flow of our program.

Note: I have updated the example because it was only for simulation.
I have set the clock to work on a card.
I have updated the Timer Base functions to the latest version and the comments are translated.

Note II: "TmrBaseFunctions"; I uploaded the wrong file, so I upload the correct one. It only added 1mSec. more than the assigned delay (2023/03/07).


Code:
'**************************************************************************
'**************************************************************************
'Example_01
'Example of the use of time bases with timer1.
'By COS, 03/2023
'Pic Basic Compiler v8.42 (PSI), Pic16F88
'**************************************************************************
'--------------------------------------------------------------------------
'Clock internal 8Mhz
Define CONF_WORD = 0x2f50
Define CONF_WORD_2 = 0x3ffc
Define CLOCK_FREQUENCY = 8  '8Mhz Oscillator
'Define SIMULATION_WAITMS_VALUE = 1  'Activate for simulation
Include "_FuncionesPic16F88.bas"
Include "_FuncionesTmrBase.bas"
Include "_FuncionesDutyCycle.bas"
'-------------------------------------------------------------------
'Pin configuration
AllDigital
ConfigPin PORTA = Output
ConfigPin PORTB = Output
'LCD Port ----------------------------------------------------------
Define LCD_BITS = 4  'Data bus length to 4bits
Define LCD_DREG = PORTA  'Data bus: puerto A
Define LCD_DBIT = 0  'Least significant bit of the data bus.
Define LCD_RSREG = PORTB  'Rs control bit on port B
Define LCD_RSBIT = 7  'RS bit on RB7 pin.
Define LCD_EREG = PORTB  'E control bit on port B.
Define LCD_EBIT = 6  'E pin on RB6 pin
Define LCD_COMMANDUS = 2000  'Wait after each command in uSec.
Define LCD_DATAUS = 100  'Wait after sending a data to the LCD in uSec.
Define LCD_INITMS = 50  'Wait during display initialization in mSec.
'---------------------------------------------------------------------
main:
    'Clock settings
    Call _setup_oscillator_mode_select_bit(_oscillator_mode_defined_by_fosc)
    Call _setup_oscillator(_osc_8mhz)  'Internal clock at 8 MHz
    'Initialize Timer1
    Call _setup_timer1(_tmr1_internal, _tmr1_div1)
    Call _set_timer1(0xf831)  'Interruption every 1mSec.
    Call _timer1(_on)
    Call _enable_interrupts(_int_timer1)
    Call _enable_interrupts(_global)
    '-----------------------------------------
    Lcdinit  'LCD without cursor
    Lcdout "Example_01"
    '-----------------------------------------
    'Initializes time bases in mSec.
    Call _bt1call(_btsetup, 0)  '16Bit, initialize _BT1
    Call _bt2call(_btsetup, 0)  '16Bit, initialize _BT2
    Call _bt3call(_btsetup, 50)  '16bit, initialize _BT3 And reload with 50msec.
    'Assign names to the Leds
    Symbol LedGreen = RB0
    Symbol LedYellow = RA7
    'Initialize Clock
    Call Clock(23, 59, 00, 1)
 
    Call _pause(2000)
 
    'Infinite Loop
    While True
 
        'Blink Led green
        If _bt1if = 1 Then
            Toggle LedGreen  'Changes the state of the selected pin o bit.
            Call _bt1call(_btsetup, 100)  '100) 'Reloads time base en mSec.
        Endif

     
        'Blink Led yellow: _Bt2DutyMs(on,off) use time base 2.
        LedYellow = _bt2dutyms(100, 500)  '500) 'Led flashing (ON, OFF) in mSec.
     
        'Clock
        If _bt3if = 1 Then
            Call _bt3call(_btsetup, 1000)  '1000) 'Reloads Time base in mSec.
            Call Clock(0, 0, 0, 0)
        Endif

    Wend
End                                            
'****************************
'Clock function
Proc Clock(_Hours As Byte, _Minutes As Byte, _Seconds As Byte, _Init As Bit)
    Dim Hours As Byte
    Dim Minutes As Byte
    Dim Seconds As Byte
    Dim Blink As Bit
 
    'Initialize
    If _Init = 1 Then
        Hours = _Hours
        Minutes = _Minutes
        Seconds = _Seconds
        Exit
    Endif

    Toggle Blink  'Blink ":"

    'Clock
    Seconds++
    If Seconds > 59 Then
        Seconds = 0
        Minutes++
        If Minutes > 59 Then
            Minutes = 0
            Hours++
            If Hours > 23 Then
                Hours = 0
            Endif
        Endif
    Endif
 
    'Display
    Lcdcmdout LcdLine2Pos(1)

    If Hours < 10 Then Lcdout "0"
    Lcdout #Hours

    If Blink = 0 Then
        Lcdout ":"
    Else
        Lcdout " "
    Endif

    If Minutes < 10 Then Lcdout "0"
    Lcdout #Minutes

    If Blink = 0 Then
        Lcdout ":"
    Else
        Lcdout " "
    Endif

    If Seconds < 10 Then Lcdout "0"
    Lcdout #Seconds
End Proc                                       
'****************************
On Interrupt
    Save System
    If PIR1.TMR1IF = 1 Then
        Call _set_timer1(0xf831)  'Reload TMR1 registers to count 1mSec.
        Call _bt1call(_btupdated, _null)  'Update the time base 1 and flag _Bt1If.
        Call _bt2call(_btupdated, _null)  'Update the time base 2 and flag. _Bt2If.
        Call _bt3call(_btupdated, _null)  'Update the time base 3 and flag. _Bt3If.
        Call _btpausecall()  'Update the time base for _Pause(delay in mSec.)
    Endif
    Resume
'******************************

Video:
Example_01 (Time Bases).

 

Attachments

  • _FuncionesDutyCycle.bas
    2.5 KB · Views: 161
  • Example_01.bas
    4.3 KB · Views: 175
  • _FuncionesTmrBase.bas
    8.5 KB · Views: 151
  • _FuncionesPic16F88.bas
    26.9 KB · Views: 155
Last edited:
Hi d,
As with your other thread, I'm interested, and will join in later.
C
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…