Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Ring buffer for serial port (Oshonsoft Basic).

Status
Not open for further replies.

DogFlu66

Member
On this occasion I leave a buffer for serial port, it is of the ring buffer type.

Video:

Code:
'Tests with ring buffer and time bases.
'By COS, 02/2023, OshonSoft Pic Basic IDE v8.38
'*************************************************************
Define CONFIG = 0x2f70
Define CONFIG2 = 0x3ffc
Define CLOCK_FREQUENCY = 8  'Oscilador a 8Mhz
Define SIMULATION_WAITMS_VALUE = 1  'Activate for simulation.
Include "_FuncionesPic16F88.bas"
Include "_FuncionesTmrBase.bas"
Include "_FuncionesUartRingBuffer.bas"
'--------------------------------------------------------------
AllDigital
ConfigPin PORTA = Output
ConfigPin PORTB = Output
'Configuration lcd ports. -------------------------------------
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:
    'Pin config
    ConfigPin RB0 = Output
    ConfigPin RA7 = Output
    'Assign names to the Leds
    Symbol LedGreen = RB0
    Symbol LedYellow = RA7
    'Port config
    Hseropen 9600
    Lcdinit  'Lcd withou cursor
    'Config Tmr1
    Call _setup_timer1(_tmr1_internal, _tmr1_div1)
    Call _set_timer1(0xf831)
    'Configures the wait if there is no data in buffer in mSec.
    Call _setuprbf(10)
    'Activates interrupts of the internal modules used.
    Call _enable_interrupts(_int_timer1)
    Call _enable_interrupts(_int_rda)
    Call _enable_interrupts(_global)

    Lcdout "Ready:"
    Hserout "Ready:", CrLf
  
    Call _bt0call(_btsetup, 0)    'In mSec 32bit
    Call _bt1call(_btsetup, 0)    'In mSec 16bit
    Call _bt2call(_btsetup, 0)    'In mSec 16bit
    Call _bt3call(_btsetup, 0)    'In mSec 16bit

    Call _pause(10)  'Delay affected little by interruptions (mSec).
  
    'Infinite loop
    While True
  
        'Flashing led
        If _bt1if Then  'Control Bt1
            Call _bt1call(_btsetup, 10)  'Reload Bt1
            Toggle LedYellow
        Endif
        'If there is data in the buffer, extracts and sends to the serial port.
        If _buffer > 0 Then
            Call _receive()
            Hserout _receive
        Endif
        'Displays the number of characters stored in the buffer.
        Lcdcmdout LcdLine2Home
        Lcdout "Buffer=", #_buffer, "  "

    Wend
End                                             
'*******************************************************************
On Interrupt
Save System
    'Control time bases
    If _tmr1if = 1 Then
        Call _set_timer1(0xf831)  'Reload TMR1 registers to count 1mSec.
        Call _bt0call(_btupdated, _null)  'Update the BT0 (32bit counter), _Bt0If.
        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
    'Serial buffer control
    If _usartif = 1 Then
        Call _ringbuffer()  'Capture character in buffer.
    Endif
Resume

To download the functions (Include "_FuncionesPic16F88.bas" and Include "_FuncionesTmrBase.bas"):

 

Attachments

  • _FuncionesUartRingBuffer.bas
    3.6 KB · Views: 270
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top