• Pic® Basic


    by LittlePIC

    PART1 Preprocessor Directives


    - Where listed (see page number) is reference to the Proton Manual
    - This listing is in a note format just giving you some direction on converting C to Basic
    - This note form is trying to give you a beginners crash course in C
    - Do some ‘Google’ for more explanation.
    - I have tried to show the simplest from of C in some examples so not to confuse.
    - The notes are to encourage users to convert some C routines into equivalent Basic
    C Language has 2 basic types of files:

    C source code files – have .c extension – this has the C source code.
    Header code files – have .h extension – these are ‘support’ files for the C source.

    Preprocessor Directives in C are processes carried out before compiling takes place. For example #DEFINE works the same as SYMBOL in Proton. (See Page 56)

    Here is an example:

    In C: #define CLOCK_FREQ (20000000)

    In Basic: SYMBOL CLOCK_FREQ = 20000000

    It simply replaces any mention of CLOCK_FREQ with the value of 20000000

    Here is another example:

    Written in C:

    #define PORTA_RA4 PORTAbits.RA4
    #define PORTA_RA5 PORTAbits.RA5
    #define TRISA_RA2 TRISAbits.TRISA2
    #define TRISA_RA5 TRISAbits.TRISA5

    Written is Basic:

    SYMBOL PORTA_RA4 = PortA.4
    SYMBOL PORTA_RA5 = PortA.5

    If you go and download some C from from one of the source files you will see plenty of mention of #DEFINE. As you can see – it’s quite easy to convert much of C’s Header files(have a .h as the extension) to Basic.

    #DEFINES are also commonly listed in the C source code files (have a .c extension)

    You can open the C files using any Text Editor or even Proton IDE by selecting ‘Any File’ from the Extension dropdown box.


    The other common Preprocessor Directive used in C is the #Include“(Filename)” – also written as #Include stating that the file can be found in the default directory.

    The C #include works the same way as INCLUDE in Proton (See page 242)
    It replaces the location of the Include call with the content’s of the file being requested.

    Be aware that header files (.h extension) using < and > like:


    are typically not found in the source code folder – these are supplied by the C compiler and contain information about dealing with C syntax. So has the subroutines for dealing with strings.We don’t need to worry about these types of header files (,string.) because Proton deals with this for us….see how lucky we are?

    You would probably notice the # sign in front of both of these C calls. Actually this is how C knows to process them first before compiling the source code and not confuse them for another request. So any call or directive you see with the # as a prefix means ‘process first before compile’.

    Have a look through some of the C source code and you’ll see syntax like:

    #if defined(HI_TECH_C)
    #if defined(_MPC_)
    #define HITECH_C18
    #error "Unknown part is selected."

    We know that the # mean’s process first before compile.


    #if defined(HI_TECH_C) is saying ‘IF using HI TECH C Compiler…
    #if definded(_MPC_) and using MPC…
    #define HITECH_C18 then use HITECH_C18 calls…
    #else else…
    #error “Unknown part is selected.” Print warning message “Unknown part is selected.”
    #end if complete IF statement…

    Most of this doesn’t apply to us – we only use one ‘complier’ (actually Basic isn’t a compiler but an Interpreter). These statements are added to allow multiple C compilers to use the same source code files but just use syntax relevant.

    You should be able to see through that most of the syntax can be ‘read’ like in BASIC.

    If TRUE and
    If TRUE then
    Do something
    Do something


    Here is another typical line:

    #if !defined (_WIN32)

    It is similar to above but uses ‘!’, this mean’s NOT
    So this line is saying “If NOT using _WIN32 do next line else jump line”

    Other Operators (see page 79) are commonly found.

    Also (see page 57) are common C formatters you should be aware of…

    When you see ‘- -‘ this means decrement (see DEC page 159)
    When you see’+ +’ this means increment (see INC page 244)
    Arrays are written using the same type of format:

    AppConfig.MyIPAddr.v[0] = MY_DEFAULT_IP_ADDR_BYTE1;
    AppConfig.MyIPAddr.v[1] = MY_DEFAULT_IP_ADDR_BYTE2;
    AppConfig.MyIPAddr.v[2] = MY_DEFAULT_IP_ADDR_BYTE3;
    AppConfig.MyIPAddr.v[3] = MY_DEFAULT_IP_ADDR_BYTE4;


    (Quick Primer on Functions – To Be further explained in Part3)

    C as a language is basically made up of things called Functions (I’ll refer to them as subroutines from here on as this is what we use in Basic), some are inbuilt like:

    PRINTF (“information”, [other comments]) Like PRINT in standard BASIC

    and others are written by the programmer (to perform a set task) like:


    All these subroutines get called as required to perform the required task. For example

    InitAppConfig ();

    Is calling the subroutine InitAppConfig but passing no values (nothing in the brackets)

    InitAppConfig(variable ,variable);

    Is calling the subroutine InitAppConfig and passing 2 variables to it

    Value = InitAppConfig(variable ,variable);

    Is calling the subroutine InitAppConfig and passing 2 variables to it and wanting a returned value( shown as Value)

    The subroutine that is getting called has a format like:

    (return type) InitAppConfig(variable1,variable2)

    Return(return type)

    (return type) states what format the returned variable has e.g BYTE WORD CHAR(means character)VOID(means no returned value)

    (variable1,variable2) are the values being passed to the subroutine

    InitAppConfig is the name of the subroutine

    These subroutines can be converted to BASIC using the GOSUB (see page 203-204) call:

    GOSUB Label [variable, variable, etc], Return Variable

    So our above example

    Value = InitAppConfig(variable ,variable);

    Can be written as:

    GOSUB InitAppConfig [variable,variable] Value

    With the called routine written as:



    Return Value

    There is more involved with this type of call – namely the Stack but the basics can been seen…


    You will see also WHILE and FOR type loops – step through them one part at a time to convert into BASIC.

    Here is a quick primer:
    The C syntax:

    BYTE c;

    while ( !USARTIsGetReady() );

    c = USARTGet();

    First line: is first declaring a byte variable called ‘c’

    Second Line: WHILE loop checking a value of the function ‘USARTIsGetReady()’ but has the ! (NOT) value in front so ‘WHILE function USARTIsGetReady() returns a NOT value goto next line else jump out of the loop…”

    Third Line: Call function USARTGet() but send no values(nothing in brackets) but the return value is called c (the byte declared variable in line one…)

    (More on these next note – Part2)


    IF THEN types are also are easy enough to convert providing you understand the BASIC equivalent.(Also to be covered in Part2)

    I hope this helps some of the Proton Users who are not familiar with C see the similar approach used.


    Little PIC