PIC16 Basic Compiler Library Support


The library support is a new way for the basic statements implementation.
This is indeed an advanced feature, however that is a way that can be used by both the author and the users to extend the compiler language from the external library files.
The information is loaded by the compiler from the external textual .lib files stored in the OshonSoft application data folder.
OshonSoft .lib files are well documented with comments covering all the currently available features of the library support compiler engine.
Library files can be edited by Notepad, or any other plain text editor.
 
 

Library files selection:


 
 
 

oshonsoftpic16.lib file:
 
//the concept is that one library group contains the implementation of one new language functionality - one or more related statements or functions
//library items contain different implementations of the same functionality for different groups of microcontrollers
//#lib_item_begin must be followed by #processor
//#statement_begin, #statement_type, #argument sequence must be fulfilled for proper library load
 
//#processor comma-separated list of processors, x can be used as a wild card character
//#processor can be used in multiple lines to quote all devices if needed
//#parameter is used to implement #define parameters needed for the statement implementation
//#parameter const, parameter_name, allowed_range, default_value
//#parameter symbol, parameter_name, type (pin, bit, byte, address of), system_bit or system_register
//'pin' type is used for the bits in the PORT registers
//'address of' type will implement a constant parameter
//#variable is used to declare global system variables
//#variable variable_name, type (byte, word, long, single, string)
//#statement_begin statement_name [argument1_name[, argument2_name[, ...]]]
//#statement_type type (procedure; inline; function, f_type (byte, word, long, single, string))
//#argument argument_name, type (const xx, byte, byte system xx, word, word system xx, long, long system xx, single, string), passing_type
//the default type of the system variables can not be changed with #argument
//passing_type (byval, byval allowed_constant_range, byref, byrefout) for statement_type procedure and inline
//passing_type (byval, byval allowed_constant_range) for statement_type function
//allowed_constant_range can contain arithmetic expressions in brackets, like 0-[EEPROM_Num-1]
 
//used to define parameters and statements that are not available or not applicable for the current item devices
//#parameter n/a, parameter_name
//#statement n/a, statement_name
 
//used to define alternative names for the parameter and statement names defined in the library group
//#alias_for <library_defined_element_name>, <new_alternative_name>
//used to define code variations among processors when only one or more register names in the code should be replaced with their alternatives
//#alternate_reg_name <register_name_used_in_code>, <alternate_register_name>
 
//code section can contain both inline assembler and basic language lines of code
//symbol and const parameters can be used directly in the assembler lines of code
//calculate[] or calc[] macro is available for double precision calculations
//calculate[] must be used to enter the parameter value in the basic code, will be replaced with the value of the parameter
//calculate[] can be used to perform one arithmetic operation, will be replaced with the value of the result
//calc[] arithmetic operators: +, -, *, /, % (modulus or remainder operator), \ (division returning integer result)
//calc[] macros can be nested
//reg_addr[] macro will be replaced with the register address; if not found, -1 is returned
//Clock_Freq in MHz is available as an argument
//Flash_Num and EEPROM_Num (total number of memory locations) are available as arguments
//int_val() and abs_val() functions are available for getting integer or absolute values of the argument
//int_val() and abs_val() can be used on one or both arguments in calc[] expression, and/or on the whole expression
 
//#if [], #endif macro is available
//#if comparison operators: ==,<>,<,>,<=,>=
//#if [] macro can be used without #endif when followed by one operation in the same line
//for example: #if [libtemp3 > 0.5] #math libtemp2 = calc[libtemp2 + 1]
//for example: #if [reg_addr[ANSELA] >= 0] ANSELA = 0x00
 
//'const' statement argument type can be used to load the numeric value into one of the library engine temp variables libtemp0-libtemp99
//for const arguments only 'byval' and 'byval allowed_constant_range' passing types are allowed
//#math directive is used to assign numeric value or the result of calc[] expression to one libtempxx variable
//for example: #math libtemp8 = calc[calc[Clock_Freq * 1000000] / calc[calc[libtemp2 + 1] * 4]] //exact baud rate achieved
//#if macro also works with libtemp0-libtemp99 variables
//desired info can be inserted as a comment in the generated assembler source with the #echo directive
//#echo is followed by a string that can contain temp variable names to be replaced with their numeric values
//for example: #echo "exact baud rate achieved = libtemp8; bit period = libtemp7s; baud rate error = libtemp6%"
 
//when calling 'procedure' type statements with one 'byval' byte argument declared, library engine will accept multiple comma-separated arguments
//for that purpose #statement_accept_multiple_arguments directive should be placed after #statement_type
//variables are passed byte by byte staring from the lowest byte; string constants can be used, along with symbolic constants Qt, CrLf, Cr, Lf
//decimal representation of a variable can be passed to the procedure by using the # prefix before the variable name
//#break_for_string_argument directive
//for string variables and decimal representations (argument in the form #variable_name)
//library engine will load FSR1 register with address of zero-terminated string prepared in memory
//and call the procedure at position of the #break_for_string_argument directive in the procedure code
//when calling 'procedure' type statements with one 'byrefout' byte argument declared, library engine will also accept multiple comma-separated arguments
//variables are filled in byte by byte staring from the lowest byte
 
//be careful that basic statements like WaitUs also make use of the system registers and can alter those values
//when using inline assembler code, be careful that basic statements expect to be called with BANK zero selected
//#banksel register_name_or_address
//#banksel directive should be used before any register access with assembler code, in order to use the compiler internal memory banking optimizations
 
//special function registers are declared in basic code as byte variables; if needed, this byte variable type can be changed with #redim directive
//#redim register_name new_type (byte, word, long, single)
 
//#pagesel directive should be used before all the GOTO and CALL instructions in the code
//#pagesel will be used by the compiler to setup the appropriate page bits in the PCLATH register for the next GOTO or CALL instruction
 
//list of available compiler system byte registers (bank 0): R0L, R0H, R1L, R1H, R2L, R2H, R3L, R3H, R4L, R4H, R5L, R5H
//list of available compiler system word registers (bank 0): R0HL, R1HL, R2HL, R3HL, R4HL, R5HL
//list of available compiler system long registers (bank 0): R1HL0HL, R3HL2HL, R5HL4HL


 
 
 

oshonsoftpic16-adc.lib file:
 
#lib_name OshonSoft ADC Module Library
 
 
//ADC_Read statement
#lib_group_begin
//for backward compatibility
#alias_for ADC_Sample_uS, ADC_SAMPLEUS
#alias_for ADC_Clk, ADC_CLOCK
#alias_for ADC_Read, Adcin
 
#lib_item_begin
//ADCON0, bank1,
// CHS4 CHS3 CHS2 CHS1 CHS0 GO/DONE ADON
//ADCON1, bank1,
//ADFM ADCS2 ADCS1 ADCS0 ADPREF1 ADPREF0
//ADFM ADCS2 ADCS1 ADCS0 ADNREF ADPREF1 ADPREF0 //v2
//ADRESL, bank1,
//ADRESH, bank1,
//PIR1.ADIF, bank0,
#processor 16f1512, 16f1513
#processor 16f1516, 16f1517, 16f1518, 16f1519
#processor 16f1526, 16f1527
#processor 16f1824, 16f1828 //v2
#processor 16f1825, 16f1829 //v2
#processor 16f1826, 16f1827 //v2
#processor 12f1822, 16f1823
#processor 12f1840
#processor 12lf1840t48a
#processor 16f1847 //v2
#processor 16f1933 //v2
#processor 16f1934, 16f1936, 16f1937 //v2
#processor 16f1938, 16f1939 //v2
#processor 16f1946, 16f1947 //v2
#parameter const, ADC_Sample_uS, 0-255, 20
#parameter const, ADC_Clk, 0-7, 3
        #statement_begin ADC_Read adc_channel, adc_result
        #statement_type procedure
        #argument adc_channel, byte system R0L, byval 0-31
        #argument adc_result, word system ADRESL, byrefout
        #code_begin
        //
        #redim ADRESL word
        #banksel ADCON1
        bsf ADCON1,ADFM
        movlw 0x8f
        andwf ADCON1,f
        movlw calculate[ADC_Clk * 16]
        iorwf ADCON1,f
        #banksel 0
        rlf R0L,f
        rlf R0L,f
        movlw 0x7c
        andwf R0L,w
        #banksel ADCON0
        movwf ADCON0
        bsf ADCON0,ADON
        //
        #banksel 0
        WaitUs calculate[ADC_Sample_uS]
        //
        #banksel ADCON0
        bsf ADCON0,GO
        btfsc ADCON0,GO
        bra $-1
        bcf ADCON0,ADON
        #banksel 0
        bcf PIR1,ADIF
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
//ADCON0, bank1,
// CHS4 CHS3 CHS2 CHS1 CHS0 GO/DONE ADON
//ADCON1, bank1,
//ADFM ADCS2 ADCS1 ADCS0 ADPREF1 ADPREF0
//ADCON2, bank1,
// TRIGSEL2 TRIGSEL1 TRIGSEL0
//TRIGSEL3 TRIGSEL2 TRIGSEL1 TRIGSEL0 //v2
//ADRESL, bank1,
//ADRESH, bank1,
//PIR1.ADIF, bank0,
#processor 16f1455, 16f1459
#processor 16f1503 //v2
#processor 16f1507 //v2
#processor 16f1508, 16f1509 //v2
#processor 16f1704, 16f1708 //v2
#processor 12f1501 //v2
#processor 12lf1552
#parameter const, ADC_Sample_uS, 0-255, 20
#parameter const, ADC_Clk, 0-7, 3
        #statement_begin ADC_Read adc_channel, adc_result
        #statement_type procedure
        #argument adc_channel, byte system R0L, byval 0-31
        #argument adc_result, word system ADRESL, byrefout
        #code_begin
        //
        #redim ADRESL word
        #banksel ADCON1
        bsf ADCON1,ADFM
        clrf ADCON2
        movlw 0x8f
        andwf ADCON1,f
        movlw calculate[ADC_Clk * 16]
        iorwf ADCON1,f
        #banksel 0
        rlf R0L,f
        rlf R0L,f
        movlw 0x7c
        andwf R0L,w
        #banksel ADCON0
        movwf ADCON0
        bsf ADCON0,ADON
        //
        #banksel 0
        WaitUs calculate[ADC_Sample_uS]
        //
        #banksel ADCON0
        bsf ADCON0,GO
        btfsc ADCON0,GO
        bra $-1
        bcf ADCON0,ADON
        #banksel 0
        bcf PIR1,ADIF
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
//ADCON0, bank1,
//ADRMD CHS4 CHS3 CHS2 CHS1 CHS0 GO/DONE ADON
//ADCON1, bank1,
//ADFM ADCS2 ADCS1 ADCS0 ADNREF ADPREF1 ADPREF0
//ADCON2, bank1,
//TRIGSEL3 TRIGSEL2 TRIGSEL1 TRIGSEL0 CHSN3 CHSN2 CHSN1 CHSN0
//ADRESL, bank1,
//ADRESH, bank1,
//PIR1.ADIF, bank0,
#processor 16f1782, 16f1783
#processor 16f1784, 16f1786, 16f1787
#processor 16f1788, 16f1789
#parameter const, ADC_Sample_uS, 0-255, 20
#parameter const, ADC_Clk, 0-7, 3
        #statement_begin ADC_Read adc_channel, adc_result
        #statement_type procedure
        #argument adc_channel, byte system R0L, byval 0-31
        #argument adc_result, word system ADRESL, byrefout
        #code_begin
        //
        #redim ADRESL word
        #banksel ADCON1
        bsf ADCON1,ADFM
        movlw 0x0f
        movwf ADCON2
        movlw 0x8f
        andwf ADCON1,f
        movlw calculate[ADC_Clk * 16]
        iorwf ADCON1,f
        #banksel 0
        rlf R0L,f
        rlf R0L,f
        movlw 0x7c
        andwf R0L,w
        #banksel ADCON0
        movwf ADCON0
        bsf ADCON0,ADON
        //
        #banksel 0
        WaitUs calculate[ADC_Sample_uS]
        //
        #banksel ADCON0
        bsf ADCON0,GO
        btfsc ADCON0,GO
        bra $-1
        bcf ADCON0,ADON
        #banksel 0
        bcf PIR1,ADIF
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
#processor 16f1454
#parameter n/a, ADC_Sample_uS
#parameter n/a, ADC_Clk
#statement n/a, ADC_Read
#lib_item_end
#lib_group_end
 
 
#lib_group_begin //All_Digital statement
//for backward compatibility
#alias_for All_Digital, AllDigital
 
#lib_item_begin
#processor 16f1512, 16f1513
#processor 16f1516, 16f1517, 16f1518, 16f1519
#processor 16f1526, 16f1527
#processor 16f1824, 16f1828
#processor 16f1825, 16f1829
#processor 16f1826, 16f1827
#processor 12f1822, 16f1823
#processor 12f1840
#processor 12lf1840t48a
#processor 16f1847
#processor 16f1933
#processor 16f1934, 16f1936, 16f1937
#processor 16f1938, 16f1939
#processor 16f1946, 16f1947
#processor 16f1455, 16f1459
#processor 16f1503
#processor 16f1507
#processor 16f1508, 16f1509
#processor 16f1704, 16f1708
#processor 12f1501
#processor 12lf1552
#processor 16f1782, 16f1783
#processor 16f1784, 16f1786, 16f1787
#processor 16f1788, 16f1789
        #statement_begin All_Digital
        #statement_type inline
        #code_begin
        //
        #if [reg_addr[ANSELA] >= 0] ANSELA = 0x00
        #if [reg_addr[ANSELB] >= 0] ANSELB = 0x00
        #if [reg_addr[ANSELC] >= 0] ANSELC = 0x00
        #if [reg_addr[ANSELD] >= 0] ANSELD = 0x00
        #if [reg_addr[ANSELE] >= 0] ANSELE = 0x00
        #if [reg_addr[ANSELF] >= 0] ANSELF = 0x00
        #if [reg_addr[ANSELG] >= 0] ANSELG = 0x00
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
#processor 16f1454
#statement n/a, All_Digital
#lib_item_end
#lib_group_end


 
 
 

oshonsoftpic16-uart.lib file:
 
#lib_name OshonSoft UART Library
 
 
//UART_Init, UART_Write, UART_Read, UART_Get statements
#lib_group_begin
#alias_for UART_Init, UART1_Init
#alias_for UART_Write, UART1_Write
#alias_for UART_Read, UART1_Read
#alias_for UART_Get, UART1_Get
//for backward compatibility
#alias_for UART_Init, Hseropen
#alias_for UART_Write, Hserout
#alias_for UART_Read, Hserin
#alias_for UART_Get, Hserget
 
#lib_item_begin
//TXREG, bank3,
//TXSTA, bank3,
//CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
//RCREG, bank3,
//RCSTA, bank3,
//SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
//BAUDCON, bank3,
//ABDOVF RCIDL SCKP BRG16 WUE ABDEN
//SPBRGL, bank3,
//SPBRGH, bank3,
//PIR1.TXIF, bank0,
//PIR1.RCIF, bank0,
#processor 16f1512, 16f1513
#processor 16f1516, 16f1517, 16f1518, 16f1519
#processor 16f1782, 16f1783
#processor 16f1784, 16f1786, 16f1787
#processor 16f1788, 16f1789
#processor 16f1933
#processor 16f1934, 16f1936, 16f1937
#processor 16f1938, 16f1939
#alternate_reg_name SPBRGL, SPBRG
#alternate_reg_name BAUDCON, BAUDCTR
#alternate_reg_name TXREG, TX1REG
#alternate_reg_name TXSTA, TX1STA
#alternate_reg_name RCREG, RC1REG
#alternate_reg_name RCSTA, RC1STA
#alternate_reg_name BAUDCON, BAUD1CON
#alternate_reg_name SPBRGL, SP1BRGL
#alternate_reg_name SPBRGH, SP1BRGH
        #statement_begin UART_Init baud_rate
        #statement_type inline
        #argument baud_rate, const libtemp0, byval 100-1000000
        #code_begin
        //
        #math libtemp10 = 1 //baud rate generator mode
        #math libtemp1 = calc[calc[calc[calc[Clock_Freq * 1000000] / libtemp0] / 4] - 1]
        #math libtemp2 = calc[int_val(libtemp1)] //SPBRG value
        #math libtemp3 = calc[abs_val(libtemp2 - libtemp1)]
        #if [libtemp3 > 0.5] #math libtemp2 = calc[libtemp2 + 1]
        //
        #math libtemp8 = calc[calc[Clock_Freq * 1000000] / calc[calc[libtemp2 + 1] * 4]] //exact baud rate achieved
        #math libtemp7 = calc[1000000 / libtemp8] //bit period
        #math libtemp6 = calc[calc[calc[abs_val(libtemp8 - libtemp0)] / libtemp0] * 100] //baud rate error
        #math libtemp6 = calc[int_val(calc[libtemp6 * 100]) / 100] //round to 2 decimal places
        #echo "exact baud rate achieved = libtemp8; bit period = libtemp7s; baud rate error = libtemp6%"
        //
        #banksel TRISC
        bsf TRISC,6
        bsf TRISC,7
        #banksel SPBRGL
        movlw calc[libtemp2 % 256]
        movwf SPBRGL
        movlw calc[libtemp2 \ 256]
        movwf SPBRGH
        clrf BAUDCON
        #if [libtemp10 == 1] bsf BAUDCON,BRG16
        #if [libtemp10 == 1] movlw 0x24 //TXEN, BRGH
        movwf TXSTA
        movlw 0x90 //SPEN, CREN
        movwf RCSTA
        #banksel 0
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
//TXREG, bank3,
//TXSTA, bank3,
//CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
//RCREG, bank3,
//RCSTA, bank3,
//SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
//BAUDCON, bank3,
//ABDOVF RCIDL SCKP BRG16 WUE ABDEN
//SPBRGL, bank3,
//SPBRGH, bank3,
//PIR1.TXIF, bank0,
//PIR1.RCIF, bank0,
#processor 16f1824
#processor 16f1825
#processor 16f1823
#processor 16f1454, 16f1455
#alternate_reg_name SPBRGL, SPBRG
        #statement_begin UART_Init baud_rate
        #statement_type inline
        #argument baud_rate, const libtemp0, byval 100-1000000
        #code_begin
        //
        #math libtemp10 = 1 //baud rate generator mode
        #math libtemp1 = calc[calc[calc[calc[Clock_Freq * 1000000] / libtemp0] / 4] - 1]
        #math libtemp2 = calc[int_val(libtemp1)] //SPBRG value
        #math libtemp3 = calc[abs_val(libtemp2 - libtemp1)]
        #if [libtemp3 > 0.5] #math libtemp2 = calc[libtemp2 + 1]
        //
        #math libtemp8 = calc[calc[Clock_Freq * 1000000] / calc[calc[libtemp2 + 1] * 4]] //exact baud rate achieved
        #math libtemp7 = calc[1000000 / libtemp8] //bit period
        #math libtemp6 = calc[calc[calc[abs_val(libtemp8 - libtemp0)] / libtemp0] * 100] //baud rate error
        #math libtemp6 = calc[int_val(calc[libtemp6 * 100]) / 100] //round to 2 decimal places
        #echo "exact baud rate achieved = libtemp8; bit period = libtemp7s; baud rate error = libtemp6%"
        //
        #banksel TRISC
        bsf TRISC,4
        bsf TRISC,5
        #banksel SPBRGL
        movlw calc[libtemp2 % 256]
        movwf SPBRGL
        movlw calc[libtemp2 \ 256]
        movwf SPBRGH
        clrf BAUDCON
        #if [libtemp10 == 1] bsf BAUDCON,BRG16
        #if [libtemp10 == 1] movlw 0x24 //TXEN, BRGH
        movwf TXSTA
        movlw 0x90 //SPEN, CREN
        movwf RCSTA
        #banksel 0
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
//TXREG, bank3,
//TXSTA, bank3,
//CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
//RCREG, bank3,
//RCSTA, bank3,
//SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
//BAUDCON, bank3,
//ABDOVF RCIDL SCKP BRG16 WUE ABDEN
//SPBRGL, bank3,
//SPBRGH, bank3,
//PIR1.TXIF, bank0,
//PIR1.RCIF, bank0,
#processor 16f1828
#processor 16f1829
#processor 16f1459
#processor 16f1508, 16f1509
#alternate_reg_name SPBRGL, SPBRG
        #statement_begin UART_Init baud_rate
        #statement_type inline
        #argument baud_rate, const libtemp0, byval 100-1000000
        #code_begin
        //
        #math libtemp10 = 1 //baud rate generator mode
        #math libtemp1 = calc[calc[calc[calc[Clock_Freq * 1000000] / libtemp0] / 4] - 1]
        #math libtemp2 = calc[int_val(libtemp1)] //SPBRG value
        #math libtemp3 = calc[abs_val(libtemp2 - libtemp1)]
        #if [libtemp3 > 0.5] #math libtemp2 = calc[libtemp2 + 1]
        //
        #math libtemp8 = calc[calc[Clock_Freq * 1000000] / calc[calc[libtemp2 + 1] * 4]] //exact baud rate achieved
        #math libtemp7 = calc[1000000 / libtemp8] //bit period
        #math libtemp6 = calc[calc[calc[abs_val(libtemp8 - libtemp0)] / libtemp0] * 100] //baud rate error
        #math libtemp6 = calc[int_val(calc[libtemp6 * 100]) / 100] //round to 2 decimal places
        #echo "exact baud rate achieved = libtemp8; bit period = libtemp7s; baud rate error = libtemp6%"
        //
        #banksel TRISB
        bsf TRISB,5
        bsf TRISB,7
        #banksel SPBRGL
        movlw calc[libtemp2 % 256]
        movwf SPBRGL
        movlw calc[libtemp2 \ 256]
        movwf SPBRGH
        clrf BAUDCON
        #if [libtemp10 == 1] bsf BAUDCON,BRG16
        #if [libtemp10 == 1] movlw 0x24 //TXEN, BRGH
        movwf TXSTA
        movlw 0x90 //SPEN, CREN
        movwf RCSTA
        #banksel 0
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
//TXREG, bank3,
//TXSTA, bank3,
//CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
//RCREG, bank3,
//RCSTA, bank3,
//SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
//BAUDCON, bank3,
//ABDOVF RCIDL SCKP BRG16 WUE ABDEN
//SPBRGL, bank3,
//SPBRGH, bank3,
//PIR1.TXIF, bank0,
//PIR1.RCIF, bank0,
#processor 16f1826, 16f1827
#processor 16f1847
#alternate_reg_name SPBRGL, SPBRG
        #statement_begin UART_Init baud_rate
        #statement_type inline
        #argument baud_rate, const libtemp0, byval 100-1000000
        #code_begin
        //
        #math libtemp10 = 1 //baud rate generator mode
        #math libtemp1 = calc[calc[calc[calc[Clock_Freq * 1000000] / libtemp0] / 4] - 1]
        #math libtemp2 = calc[int_val(libtemp1)] //SPBRG value
        #math libtemp3 = calc[abs_val(libtemp2 - libtemp1)]
        #if [libtemp3 > 0.5] #math libtemp2 = calc[libtemp2 + 1]
        //
        #math libtemp8 = calc[calc[Clock_Freq * 1000000] / calc[calc[libtemp2 + 1] * 4]] //exact baud rate achieved
        #math libtemp7 = calc[1000000 / libtemp8] //bit period
        #math libtemp6 = calc[calc[calc[abs_val(libtemp8 - libtemp0)] / libtemp0] * 100] //baud rate error
        #math libtemp6 = calc[int_val(calc[libtemp6 * 100]) / 100] //round to 2 decimal places
        #echo "exact baud rate achieved = libtemp8; bit period = libtemp7s; baud rate error = libtemp6%"
        //
        #banksel TRISB
        bsf TRISB,1
        bsf TRISB,2
        #banksel SPBRGL
        movlw calc[libtemp2 % 256]
        movwf SPBRGL
        movlw calc[libtemp2 \ 256]
        movwf SPBRGH
        clrf BAUDCON
        #if [libtemp10 == 1] bsf BAUDCON,BRG16
        #if [libtemp10 == 1] movlw 0x24 //TXEN, BRGH
        movwf TXSTA
        movlw 0x90 //SPEN, CREN
        movwf RCSTA
        #banksel 0
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
//TXREG, bank3,
//TXSTA, bank3,
//CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
//RCREG, bank3,
//RCSTA, bank3,
//SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
//BAUDCON, bank3,
//ABDOVF RCIDL SCKP BRG16 WUE ABDEN
//SPBRGL, bank3,
//SPBRGH, bank3,
//PIR1.TXIF, bank0,
//PIR1.RCIF, bank0,
#processor 12f1822
#processor 12f1840
#processor 12lf1840t48a
#alternate_reg_name SPBRGL, SPBRG
        #statement_begin UART_Init baud_rate
        #statement_type inline
        #argument baud_rate, const libtemp0, byval 100-1000000
        #code_begin
        //
        #math libtemp10 = 1 //baud rate generator mode
        #math libtemp1 = calc[calc[calc[calc[Clock_Freq * 1000000] / libtemp0] / 4] - 1]
        #math libtemp2 = calc[int_val(libtemp1)] //SPBRG value
        #math libtemp3 = calc[abs_val(libtemp2 - libtemp1)]
        #if [libtemp3 > 0.5] #math libtemp2 = calc[libtemp2 + 1]
        //
        #math libtemp8 = calc[calc[Clock_Freq * 1000000] / calc[calc[libtemp2 + 1] * 4]] //exact baud rate achieved
        #math libtemp7 = calc[1000000 / libtemp8] //bit period
        #math libtemp6 = calc[calc[calc[abs_val(libtemp8 - libtemp0)] / libtemp0] * 100] //baud rate error
        #math libtemp6 = calc[int_val(calc[libtemp6 * 100]) / 100] //round to 2 decimal places
        #echo "exact baud rate achieved = libtemp8; bit period = libtemp7s; baud rate error = libtemp6%"
        //
        #banksel TRISA
        bsf TRISA,0
        bsf TRISA,1
        #banksel SPBRGL
        movlw calc[libtemp2 % 256]
        movwf SPBRGL
        movlw calc[libtemp2 \ 256]
        movwf SPBRGH
        clrf BAUDCON
        #if [libtemp10 == 1] bsf BAUDCON,BRG16
        #if [libtemp10 == 1] movlw 0x24 //TXEN, BRGH
        movwf TXSTA
        movlw 0x90 //SPEN, CREN
        movwf RCSTA
        #banksel 0
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
//TX1REG, bank3,
//TX1STA, bank3,
//CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
//RC1REG, bank3,
//RC1STA, bank3,
//SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
//BAUD1CON, bank3,
//ABDOVF RCIDL SCKP BRG16 WUE ABDEN
//SP1BRGL, bank3,
//SP1BRGH, bank3,
//PIR1.TXIF, bank0,
//PIR1.RCIF, bank0,
#processor 16f1704
        #statement_begin UART_Init baud_rate
        #statement_type inline
        #argument baud_rate, const libtemp0, byval 100-1000000
        #code_begin
        //
        #math libtemp10 = 1 //baud rate generator mode
        #math libtemp1 = calc[calc[calc[calc[Clock_Freq * 1000000] / libtemp0] / 4] - 1]
        #math libtemp2 = calc[int_val(libtemp1)] //SPBRG value
        #math libtemp3 = calc[abs_val(libtemp2 - libtemp1)]
        #if [libtemp3 > 0.5] #math libtemp2 = calc[libtemp2 + 1]
        //
        #math libtemp8 = calc[calc[Clock_Freq * 1000000] / calc[calc[libtemp2 + 1] * 4]] //exact baud rate achieved
        #math libtemp7 = calc[1000000 / libtemp8] //bit period
        #math libtemp6 = calc[calc[calc[abs_val(libtemp8 - libtemp0)] / libtemp0] * 100] //baud rate error
        #math libtemp6 = calc[int_val(calc[libtemp6 * 100]) / 100] //round to 2 decimal places
        #echo "exact baud rate achieved = libtemp8; bit period = libtemp7s; baud rate error = libtemp6%"
        //
        #banksel RC4PPS
        movlw 0x14
        movwf RC4PPS //TX on RC4
        #banksel RXPPS
        movlw 0x15
        movwf RXPPS //RX on RC5
        #banksel TRISC
        bsf TRISC,4
        bsf TRISC,5
        #banksel SP1BRGL
        movlw calc[libtemp2 % 256]
        movwf SP1BRGL
        movlw calc[libtemp2 \ 256]
        movwf SP1BRGH
        clrf BAUD1CON
        #if [libtemp10 == 1] bsf BAUD1CON,BRG16
        #if [libtemp10 == 1] movlw 0x24 //TXEN, BRGH
        movwf TX1STA
        movlw 0x90 //SPEN, CREN
        movwf RC1STA
        #banksel 0
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
//TX1REG, bank3,
//TX1STA, bank3,
//CSRC TX9 TXEN SYNC SENDB BRGH TRMT TX9D
//RC1REG, bank3,
//RC1STA, bank3,
//SPEN RX9 SREN CREN ADDEN FERR OERR RX9D
//BAUD1CON, bank3,
//ABDOVF RCIDL SCKP BRG16 WUE ABDEN
//SP1BRGL, bank3,
//SP1BRGH, bank3,
//PIR1.TXIF, bank0,
//PIR1.RCIF, bank0,
#processor 16f1708
        #statement_begin UART_Init baud_rate
        #statement_type inline
        #argument baud_rate, const libtemp0, byval 100-1000000
        #code_begin
        //
        #math libtemp10 = 1 //baud rate generator mode
        #math libtemp1 = calc[calc[calc[calc[Clock_Freq * 1000000] / libtemp0] / 4] - 1]
        #math libtemp2 = calc[int_val(libtemp1)] //SPBRG value
        #math libtemp3 = calc[abs_val(libtemp2 - libtemp1)]
        #if [libtemp3 > 0.5] #math libtemp2 = calc[libtemp2 + 1]
        //
        #math libtemp8 = calc[calc[Clock_Freq * 1000000] / calc[calc[libtemp2 + 1] * 4]] //exact baud rate achieved
        #math libtemp7 = calc[1000000 / libtemp8] //bit period
        #math libtemp6 = calc[calc[calc[abs_val(libtemp8 - libtemp0)] / libtemp0] * 100] //baud rate error
        #math libtemp6 = calc[int_val(calc[libtemp6 * 100]) / 100] //round to 2 decimal places
        #echo "exact baud rate achieved = libtemp8; bit period = libtemp7s; baud rate error = libtemp6%"
        //
        #banksel RB7PPS
        movlw 0x14
        movwf RB7PPS //TX on RB7
        #banksel RXPPS
        movlw 0x0d
        movwf RXPPS //RX on RB5
        #banksel TRISB
        bsf TRISB,5
        bsf TRISB,7
        #banksel SP1BRGL
        movlw calc[libtemp2 % 256]
        movwf SP1BRGL
        movlw calc[libtemp2 \ 256]
        movwf SP1BRGH
        clrf BAUD1CON
        #if [libtemp10 == 1] bsf BAUD1CON,BRG16
        #if [libtemp10 == 1] movlw 0x24 //TXEN, BRGH
        movwf TX1STA
        movlw 0x90 //SPEN, CREN
        movwf RC1STA
        #banksel 0
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
#processor 16f1512, 16f1513
#processor 16f1516, 16f1517, 16f1518, 16f1519
#processor 16f1782, 16f1783
#processor 16f1784, 16f1786, 16f1787
#processor 16f1788, 16f1789
#processor 16f1824
#processor 16f1825
#processor 16f1828
#processor 16f1829
#processor 16f1826, 16f1827
#processor 16f1847
#processor 12f1822
#processor 16f1823
#processor 12f1840
#processor 12lf1840t48a
#processor 16f1933
#processor 16f1934, 16f1936, 16f1937
#processor 16f1938, 16f1939
#processor 16f1454, 16f1455
#processor 16f1459
#processor 16f1508, 16f1509
#processor 16f1704
#processor 16f1708
#alternate_reg_name TXREG, TX1REG
#alternate_reg_name RCREG, RC1REG
        #statement_begin UART_Write tx_data
        #statement_type procedure
        #statement_accept_multiple_arguments
        #argument tx_data, byte system WREG, byval
        #code_begin
        //
        l1:
        btfsc PIR1,TXIF
        bra l2
        bra l1
        l2:
        #banksel TXREG
        movwf TXREG
        #banksel 0
        //
        #break_for_string_argument
        l3:
        moviw FSR1++
        btfsc STATUS,Z
        return
        #pagesel
        call l1
        bra l3
        //
        #code_end
        #statement_end
 
        #statement_begin UART_Read rx_data
        #statement_type procedure
        #statement_accept_multiple_arguments
        #argument rx_data, byte system WREG, byrefout
        #code_begin
        //
        l1:
        btfsc PIR1,RCIF
        bra l2
        bra l1
        l2:
        #banksel RCREG
        movf RCREG,w
        #banksel 0
        //
        #code_end
        #statement_end
 
        #statement_begin UART_Get rx_data
        #statement_type procedure
        #argument rx_data, byte system WREG, byrefout
        #variable UART_Data_Ready, byte
        #code_begin
        //
        UART_Data_Ready = 0
        clrw
        btfss PIR1,RCIF
        bra l1
        UART_Data_Ready = 1
        #banksel RCREG
        movf RCREG,w
        #banksel 0
        l1:
        //
        #code_end
        #statement_end
#lib_item_end
 
#lib_item_begin
#processor 12f1501
#processor 16f1503
#processor 16f1507
#processor 12lf1552
#statement n/a, UART_Init
#statement n/a, UART_Write
#statement n/a, UART_Read
#statement n/a, UART_Get
#lib_item_end
#lib_group_end