• PicŪ Basic

  • How a program should look

    This is an example of how a program should look and the basic format it should have. There are variations but this should be a place to start. This is not a complete setup, but should act as a guide only. Keep your code neat and organised, it makes it easy to work with. Look at other code examples.


    Device = 16F684 ' Define the Pic processor
    Xtal = 4 ' Define the clock speed so timing works out in the program
    Config_Start ' Define the pic fuses configuration here
    ' This tells the pic how it is configured to start up
    ' See the Fuse Configurator Plugin
    TRISA = 011011 ' Configure I/O on port A
    TRISB = 011111 ' Configure I/O on port B
    TRISC = 000000 ' Configure I/O on port C or however many ports there are
    ' All ports must be configured otherwise the default setting
    ' will apply and they may not be what you want them to be
    ' 0 = Output, and a 1 = Input. Define unused pins as Outputs
    CMCON = 000111 ' disable comparators if the pic have them and they are not used
    ' They are ENABLED by default
    ' Define peripheral and other registers here.
    ' If you are not going to use it, don't define it
    ADCON0 = 001101
    ' Alias the pins you are going to use on the ports.
    ' Anywhere in the code where you refer to Pot1 the pic will now know it is PortA.0
    Symbol Pot1 = PORTA.0 ' Pot 1 A/D Input
    Symbol Pot2 = PORTA.1 ' Pot 2 A/D Input
    Symbol NC01 = PORTA.2 ' Not Used Output
    'Define other aliasses for bits in registers you intend to use
    Symbol GIE = INTCON.7 ' Global Int En Bit
    Symbol Go_Done = ADCON0.1
    ' Define the variables you use in the program.
    ' These can be added as you develop a need for them during programming
    Dim Coun As Dword '
    Dim Revs As Word
    Dim Temp As Byte ' temp genl use reg
    Dim A As Bit ' A = flag to check
    'If interrupts are not used then omit the following -
    '************************************************* *********************
    '* Set up interrupts
    '************************************************* *********************
    On Interrupt GoTo Label
    Goto Start 'jump over the Interrupt subroutine to program start
    '************************************************* **************************
    '* Interrupt Handler When an interrupt occurs the code will jump to here
    '************************************************* **************************
    Label: ' This is a program label or marker
    ' Do NOT execute part of the program here
    ' The idea is only to see what interrupted you
    ' Set a flag ie A = 1 or BSF Temp.1 or BSF Temp.5
    ' Then handle the required action elsewhere ie in the main loop.
    ' Interrupt code is here
    '************************************************* *********************
    '* Set up Preprocessor
    '************************************************* *********************
    ' The following code will be placed in the code whenever you use Flash
    $define Flash '
    LED = 1 '
    Delayms 200 '
    LED = 0 '
    Delayms 300
    '************************************************* ********************
    '* Main Program Starts Here
    '************************************************* ********************
    Start: ' It is important to use labels so the code has references where to jump to
    LED = 1
    Gosub Timer1 ' Call a subroutine
    Led = 0 ' Subroutine will return to this line
    goto Start1 ' Code jumps to label Start1
    ' Fast Flash here
    Flash ' The pre-processor code is inseted here
    If A = 1 then LED = 1 : A = 0 ' Interrupt occurred, LED On, reset flag
    Goto Start ' Jump to label Start
    '************************************************* ********************
    '* Sub Routines are Here -
    '************************************************* ********************
    ' A Subroutine is code you use over and over by jumping to it and back from within the program
    ' Timer Delay
    Delayms 2000
    Return ' Return to the next line from where the subroutine was calledEnd ' End of program statement