My first program

Operation ››
Parent Previous Next

The minimal main program source code is:


PROGRAM MyProgram;
 BEGIN
  END.

 

What does this big program?

Well, not much things.

Assuming that the oscillator has been properly setup, and WDT disabled in the project's options:

  1. The processor is initialized with some defaults,
  2. The main loop is entered and executes forever.

Note that the main BEGIN END block is implicitly encapsulated in a "loop forever": you cannot "exit" or "terminate" a PIC program!


Now a (more) useful program: Let's say you have connected some LED to RA0 pin, here is the classical "blink a LED" program:


PROGRAM MyLedBlinking;

VAR
 LED: boolean @ PORTA.RA0; // Define where is the LED

BEGIN
 TRISA := NOT [LED]; // Define LED pin only as output, others stay inputs

  LOOP             // Infinite loop
   LED := true;   // Switch on
   DELAY_MS(200); // wait 200 ms
   LED := false;  // Switch off
   DELAY_MS(200); // wait 200 ms
  END;

END.

(assuming that the oscillator mode and frequency has been setup and the WDT disabled)

 

Now a (more) complicated program, using a library: Let's say you have connected your PIC TX pin through some interface to your PC, then a simple "hello world" program is:


PROGRAM MyProgram;

USES
 SERIAL; // We need the serial port library

BEGIN

  BAUD(19200, TX); // Initialize baud rate and pins for TX only
 ASSIGN(OUTPUT, SerialPort_Output); // Assign the serial port output routine to the standard output

  LOOP             // Infinite loop
   WRITELN('Hello World! This is my first PMP program!');
   DELAY_MS(500); // wait 500 ms
  END;

END.

(assuming that the oscillator mode and frequency has been setup, and the WDT disabled)

 

Note that the BAUD() procedure initializes the TRIS register according to the needs.