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.

Database simulation (OshonSoft Basic).

DogFlu66

Member
Hello; I have been converting an old door opener program by telephone touch (phone call) to this language.

Not having a function in the language that allows me to associate data, I have written a library to simulate the use of registers and fields.

I thought it would be very complex to do, but it is actually very simple and is done with a couple of very simple functions and with hardly any code in them.

I leave the example for those who want to play with them for a while in the environment simulator:

'***********************************************************************
'Experimental functions for simulate registers with fields.
'Pic18F26k22, OshonSoft Pic18 Basic Compiler v5.33
'By COS, 2024/02/24
'***********************************************************************
'Phone list:
'Fields: Name, Phone and Date.
'Length of fields equal to 10 characters.
'***********************************************************************
#define CLOCK_FREQUENCY = 64 'Clock 64Mhz
#define STRING_MAX_LENGTH = 12
'#define SIMULATION_WAITMS_VALUE = 1
'***********************************************************************
'***********************************************************************
main:
UART1_Init 115200
'UART2_Init 4800
'WaitMs 1

Dim _String[10] As String 'Auxiliary variable for data exchange.
Dim x As Byte

'Initializes the list vector.
Call _Pointer0(P_List)
Format_List(P_List)

'Write field (write registers)
Write_List("Peter", Name, 0, P_List) 'Reg (0,0)
Write_List("600000001", Phone, 0, P_List) 'Reg (0,1)
Write_List("2024/02/25", Date, 0, P_List) 'Reg (0,2)

Write_List("John", Name, 1, P_List) 'Reg (1,0)
Write_List("600000002", Phone, 1, P_List) 'Reg (1,1)
Write_List("2024/02/26", Date, 1, P_List) 'Reg (1,2)

Write_List("George", Name, 2, P_List) 'Reg (2,0)
Write_List("600000003", Phone, 2, P_List) 'Reg (2,1)
Write_List("2024/02/27", Date, 2, P_List) 'Reg (2,2)

UART_Write "Registers with three fields", CrLf
UART_Write "---------------------------", CrLf, CrLf

'Extract field (read registers).
For x = 0 To 9 'Registers
UART_Write "Reg: ",#x, CrLf

_String = Read_List(Name, x, P_List)
UART_Write "Field 0 (Name): ", _String, CrLf

_String = Read_List(Phone, x, P_List)
UART_Write "Field 1 (Phone): ", _String, CrLf

_String = Read_List(Date, x, P_List)
UART_Write "Field 2 (Date): ", _String, CrLf

UART_Write "---------------------------", CrLf
Next x
UART_Write "End", CrLf

End
'From here, export the code as a library.

'*************************************************************************
'* Functions To simplify working with lists or small database simulation *
'* By COS, 20240225, OshonSoft Pic18 v5.33 *
'*************************************************************************

'*****************************************************************************************
'For two fields per register.
'*****************************************************************************************
'Declares the elements necessary to form the list [Name, Phone].
//Const nFields = 2 'Number of fields per register.
//Const FieldLength = 10 'Field length
//Const nRegisters = 20 'Number of registers = nReg * nField
//Dim List(200) As Byte 'Vector length equals nReg * FieldLength
'Dim _String[10] As String 'Auxiliary variable for data exchange.
//Const Name = 0 'To select the Name field.
//Const Phone = 1 'To select the Phone field.
'*****************************************************************************************

'*****************************************************************************************
'For three fields per register.
'*****************************************************************************************
'Declares the elements necessary to form the list [Name, Phone, Date].
Const nFields = 3 'Number of fields per register.
Const FieldLength = 10 'Field length
Const nRegisters = 30 'Number of registers = nReg * nField
Dim List(300) As Byte 'Vector length equals nReg * FieldLength
'Dim _String[10] As String 'Auxiliary variable for data exchange.
Const Name = 0 'To select the Name field.
Const Phone = 1 'To select the Phone field.
Const Date = 2 'To select the Date field.
'*****************************************************************************************
'Pointer
Dim P_List As Word 'It will contain the address of then firs element of the list vector.
Symbol _p0 = List 'Indicates the vector to use as a list.

'******************************************************************************************************
'In the Main: to initialize the list.
'Call _Pointer0(P_List) 'Call from main to assign the address of the vector before using the functions.
'Format_List(P_List) 'Call function to start the list once you know the address.
'******************************************************************************************************

'Pointer assignment for Pic18 series.
Proc _Pointer0(ByRef _p As Word)
Symbol _Return_HB = _p.HB
Symbol _Return_LB = _p.LB
ASM: LFSR 2, _p0
ASM: MOVFW FSR2H
ASM: MOVWF _Return_HB
ASM: MOVFW FSR2L
ASM: MOVWF _Return_LB
End Proc
'*************************************************
'Function to "Write" a field in a register
'*************************************************
'field: field on which work will be done.
'register: register on which work will be done.
'string_input: field to add to the list.
'p_array = Address of the first element of the list vector.
'Writes a copy of the input string to the requested field of the indicated register.
Function Write_List(string_input[10] As String, field As Word, register As Word, p_array As Word) As Word
Dim index As Word
Dim flag As Bit
flag = 0
'Calculates the address of the first element of the field.
p_array = field + (register * (FieldLength * nFields)) + (field * (FieldLength -1)) + p_array
For index = 0 To (FieldLength -1) 'Traverse the field using the index "x".
If string_input(x) = 0 Then flag = 1 'End of field mark
If flag = 0 Then
Pointer(index + p_array) = string_input(index) 'Write a copy in the indicated register field.
Else
Pointer(index + p_array) = 0 'Remove any unused characters from the field.
Endif
Next index
End Function
'*************************************************
'Function to "Read" a field in a register
'*************************************************
'field = field on which work will be done.
'register = register on which work will be done.
'p_array = Address of the first element of the list vector.
'The requested field is returned according to the input parameters.
Function Read_List(field As Word, register As Word, p_array As Word) As String
Symbol Return_String = Read_List 'Returns requested field.
Dim index As Word
'Calculates the address of the first element of the field.
p_array = field + (register * (FieldLength * nFields)) + (field * (FieldLength -1)) + p_array
For index = 0 To (FieldLength -1) 'Traverse the field using the index.
Return_String(index) = Pointer(index + p_array) 'makes a copy of the requested field.
Next index
End Function

DataBase_Output.jpg
 

Attachments

  • DataBase.bas
    7 KB · Views: 89
Last edited:
Back
Top