This is our first program. It will simply light the LED on the bottom bit of port a. It has two statements:
-
Set the data direction register for port a (TRISA) to make the bottom bit of port a an output (remember that the PICmicro will use a bit as an output if the control bit is 0).
-
Set the bottom bit of port a high to make the LED come on.
This is a very simple program. You can find out more about the various parts by following the links below:
to find out what the word main is for, click here
to find out what a statement is, click here
to find out about ports in the PIC, click here
to find out about hexadecimal values (0xff for example) and how they are written in C programs, click here
If the code is not visible double-click on the C file in the project workspace to open it up for editing.
Don't worry about the '#pragma
' bits in the exercise visable when you open it. These are used to configure the chip, for details have a look here.
When we run this program it simply enables the output pin and then turns it on. If you try this on the VPIC you should see that the bottom bit of port a lights up. Note that our program is not perfect, in that it sits at the very end of the main function. In real life the PIC program would continue down memory doing whatever statements it finds.
/* Light an LED */
void main ( void )
{
/* bit 0 of PORTA for output */
TRISA = 0x1e ;
/* now set the LED bit */
PORTA = 1 ;
}
[Exercise 1.1]
Make sure that you configure the PICmicro development board for the RC clock and set the speed switch to SLOW. When you run the program (b y pressing the RESET button on the PIC board) you should find that the LED lights a fraction of a second after the reset. This is because of the time it takes for the program to reach the instruction which turns the light on.
When you compile this program you will notice that the compiler will give warnings that some of the functions that you are using have been superseded. This is true, but I really like using these functions and they make using the PIC much easier, so don't worry about this.