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.

RS232 Simple linear Buffer (OshonSoft Basic).

DogFlu66

Member
Hello; on this occasion I leave a simple linear buffer.
Specially applied for the RS232 port.
To avoid losing data, what is done is to add another software buffer to the hardware buffer of the pic (2 bytes). The length of the latter will depend on the user's needs and the amount of memory available. This is for those who are interested in learning how a simple buffer works and how it is complemented by the asynchronous pic module, in this case the usat module.
The input data blocking check has been implemented, since apparently those who start working with this type of port are not clear that if the pic buffer is overloaded it is blocked and stops working, and to activate it again you have to reset the control bits of the internal buffer of the pic.
If anyone sees that improvements can be made or another better algorithm can be presented, they can share it.

I leave the main program and the functions necessary for it to work as attachments.

Main program:

'Example of buffer for RS232 port
'Pic12F1840, 08/12/2023
Define CONFIG1 = 0x0f84
Define CONFIG2 = 0x1dff
Include "_FuncionesPic12F1840.bas"
Include "_Funciones_Buffer_RS232.Bas"
Define CLOCK_FREQUENCY = 32 '32Mhz system oscillator
'constants (ASCII control codes).
Const FF = 0x0c 'FF, page advance
Const Bell = 0x07 'Bell, sound signal
Const Sp = 0x20 'Sp, space
Const Bksp = 0x08 'Bksp, go back
'Const Cr = 0x0d 'Cr, car return
'Const Lf = 0x0a 'Lf, line break
Const Ctrlz = 0x1a 'Ctrl - Z
Const Esc = 0x1b 'Escape
main:
'Call _setup_oscillator_mode(_oscillator_by_fosc) 'Select by configuration word.
Call _setup_oscillator(_osc_32mhz_hf) 'Interno 8Mhz x 4(PLL) = 32Mhz
AllDigital
TRISA = 0xff

UART_Init 9600 'Initialize port pin (default: A0 = Tx, RA1 = Rx)
Call _uart_pin_tx_ra4(True) 'Pin Tx = RA4
Call _uart_pin_rx_ra5(True) 'Pin Rx = RA5
WaitMs 10

Call _setup_buffer_rs232() 'Initialize buffer by software
Call _enable_interrupts(_int_rda) 'Enables uart interrupts
Call _enable_interrupts(_global) 'Enables peripheral and global interrupts.

Dim data As Byte

UART_Write "Ready", CrLf

While True

If _buffer() = True Then 'If there is data in the buffer.
'data = _read_buffer_rs232()'Reads byte fron buffer and assigns it to a variable.
'UART1_Write data'Write the byte to the serial port.
UART_Write _read_buffer_rs232() 'Reads byte fron buffer and write to the serial port.
Endif
Wend
End
On Interrupt
Save System
If _uartif = True Then 'If there is data in the uart hardware buffer.
Call _get_rs232() 'Moves a byte from the hardware buffer to the software buffer.
Endif
Resume
 

Attachments

  • _FuncionesPic12F1840.bas
    31.6 KB · Views: 93
  • _Funciones_Buffer_RS232.Bas
    3.5 KB · Views: 76
Last edited:
just some of the more obvious ones...

In the file _Funciones_Buffer_RS232.Bas you have
Code:
Dim Long_Buffer As Byte
You can get rid of Long_Buffer entirely... it's a const value based on the buffer size, so it'll never change.
You can define it and the buffer size definition in 'dim Rx_Data(20)' as const.

In the routines _Setup_Buffer_RS232() and _Get_RS232() you assign '_buffer = 0'.
Does that "variable" even exist? _buffer is the return value of the Function _buffer(), so there's no reason to try and set it outside the function.

The variable 'Tx_Buffer' is poorly named. It's the index used to read buffer elements and has nothing to do with transmitting, which is what the name implies.

If the buffer ever fills data is just discarded with no way for the user to know that's happened.
Same with OERR.
 
just some of the more obvious ones...

In the file _Funciones_Buffer_RS232.Bas you have
Code:
Dim Long_Buffer As Byte
You can get rid of Long_Buffer entirely... it's a const value based on the buffer size, so it'll never change.
You can define it and the buffer size definition in 'dim Rx_Data(20)' as const.

In the routines _Setup_Buffer_RS232() and _Get_RS232() you assign '_buffer = 0'.
Does that "variable" even exist? _buffer is the return value of the Function _buffer(), so there's no reason to try and set it outside the function.

The variable 'Tx_Buffer' is poorly named. It's the index used to read buffer elements and has nothing to do with transmitting, which is what the name implies.

If the buffer ever fills data is just discarded with no way for the user to know that's happened.
Same with OERR.
My intention was to declare the length of the buffer in the following way as I do in C:
Const Long_Buffer = 20 -1
Dim _Buffer(Long_Buffer + 1)
But these two statements are the only ones that do not allow it, they only allow numerical argument without mathematical operations.
I have already reported it, I hope that they include it in the next version, because it is more comfortable to work with arrays.

You're right making _buffer = 0 is redundant, I've already removed it.

On the other hand, _buffer is declared, because every function name is automatically declared as a global variable, it is a form more or less equivalent to C "return _buffer".

I use Tx_buffer because it refers to the output buffer, I don't know what other more appropriate name I could call it.

This is a form of simple buffer, I did not put a full buffer indicator, it is indicated with a simple overflow bit.

If there are no more suggestions I will add all the modifications.
 
So leave the variables related to the buffer functions.

'**********************************************************
Const Long_Buffer = 19 'Buffer elements -1
Dim _Buffer_Data(20) As Byte '0 al 19 store bytes Buffer
Dim _Buffer_Hardware_Index As Byte 'Input data index
Dim _Buffer_Software_Index As Byte 'Output data index
Dim _Buffer_Overflow_Bit As Bit 'Overflow bit
'**********************************************************
Added the buffer overflow bit, if activated the user will be responsible for clearing it.
 
Last edited:
A linear buffer is a suboptimal choice. What you want is a circular buffer. They work marvelously for both TX and RX.
 
A linear buffer is a suboptimal choice. What you want is a circular buffer. They work marvelously for both TX and RX.
I have been using a ring buffer for years, in this link I leave the Basic version that I translated from C (CCS) that I usually use.

 

Latest threads

New Articles From Microcontroller Tips

Back
Top