Alternative titles:
First Program: PIC18F67J60
First Program: Manipulating Inputs and Outputs on PIC18F67J60
First Program: Manipulating Inputs and Outputs on PICs using C18
Following my last post about the Olimex PIC-P67J60(http://www.smacula.co.uk/2012/10/getting-started-olimex-pic-p67j60.html), this is a post talking about the basic programming of the PICs, and manipulating inputs and outputs on the PIC. The Olimex has an input button and an output led connected to pins RB3 and RB4 respectively. These will be used in our program. Pressing the button will turn the LED off, and turning it off will turn the LED on. I assume that the reader has a basic knowledge of C/C++/Java to understand programming.
All PIC programs have to start off with the #include directive. While this directive will be used to import various libraries that allow different features to be performed with the PIC. The one directive that will always be required in each program will be the file that includes the library for the specified PIC. Since this PIC is is the PIC18F67J60, it’s library will be imported to the code. So the first step is to include that.
#include
Next step is to set the configuration bits for the PIC. This includes two basic setups for all programs. One will disable the extended instruction set, mainly because we are not using it, and setting the Oscillator option to external high speed crystal. The extended instruction set is a feature of the C18 library, and if that is not being used then this instruction may not be required. The configuration bits are set using the #pragma directive. This is followed by the ‘config’, and then using the XINST = OFF to turn off the extended instruction set, as well as FOSC = HS to set the oscillator to high speed crystal. This is the next step.
#pragma config XINST = OFF // Extended Instruction Set (Disabled)
#pragma config FOSC = HS // Choose external crystal as oscillator
Once this is achieved, the main method is written up. This method will include all the code that the PIC will run.
void main()
In the main method, the first thing is to define the outputs and inputs of the PIC. As mentioned earlier, RB3 is used as an input, and RB4 is used as an output on the Olimex board. To set them as outputs/inputs, we use the TRISXbits.RXn directive. The X defines the interface port which can be anything from A to G on this particular chip. The number ‘n’ is any number between 0 to 7, and defines the bit on that particular port. Using a ‘0’ sets that particular pin as an output, and a ‘1’ sets that pin as an input.
TRISBbits.RB4 = 0; // Set RB4 as an output (LED)
TRISBbits.RB3 = 1; // Set RB3 as an input (Button)
To set or reset an output, the PORTXbits.RXn directive is used. Setting is used by using a ‘1’, while a reset is imposed by using a ‘0’.
PORTBbits.RB4 = 0; // Set RB4 as low to turn LED on (connects LED to ground)
Once that is set, we use a while(1) to make sure that program keeps looping forever so that the program constantly monitors its inputs and sets outputs accordingly.
For inputs, the same PORTXbits.RXn directive is used to give a TRUE or a FALSE, or a 1 or 0. This will be used in the if/else directive to turn the LED on or off.
while(1){
if (PORTBbits.RB3)PORTBbits.RB4 = 1;
else PORTBbits.RB4 = 0;
}
This sums up our first program for PIC18F67J60 (or the Olimex PIC-P67J60). The complete code is as follows:
#include
#pragma config XINST = OFF // Extended Instruction Set (Disabled)
#pragma config FOSC = HS // Choose external crystal as oscillator
void main()
{
TRISBbits.RB4 = 0; // Set RB4 as an output (LED)
TRISBbits.RB3 = 1; // Set RB3 as an input (Button)
PORTBbits.RB4 = 0; // Set RB4 as low to turn LED on (connects LED to ground)
while(1){
if (PORTBbits.RB3)PORTBbits.RB4 = 1;
else PORTBbits.RB4 = 0;
}
}
Once the code is written, it needs to be compiled and put into the Olimex board.
To check if the code is valid, the code must be compiled. If you have entered the code properly, the program shall compile without any problems. If it does, it should be good to go in the PIC. The following button can be pressed to compile the program.
If the code is correct, now it can be used to program the device. The following button on the toolbar is pressed to Make and Program Device:
Once this is done, the status bar will connect to the programmer, and verify that the correct programmer and device are connected. Once the verification is done, the programmer will program the PIC. Once it is completed, the status window will show the magic words ‘Programmed and Verified’. This means that the unit is running.
This should be verified by the LED on the board being lit now. Pressing the button should turn it off. If this works as expected, congratulations, you have just written your first program.
In the next few posts I will focus on Serial communication with the PIC (using USART) as well as SPI communication with an 25AA1024 EEPROM.
If you have any questions, feel free to drop a comment!
Hi. I have same board, but when i connect an usb/serial converter to the 9 pin connector, there is an overload of current in the circuit and serial doensn’t work. How can solve this problem?
Have you checked your SERIAL/USB converter? Does it work with other boards?
Also, do some diagnostics, see if there is a short or something on the 9W…
I have the same problem with 2 cables. what is the 9W? I’m a newbie…
Thanks
The 9W (9W stands for 9 – way connector, it is a convention used often in electronics) is the serial connector. It seems like there is something definitely wrong in your setup.
I changed the power supply and it seems there are no more shorts. But uart doesn’t work. Have i damaged the board? Other functions (eg telnet, webserver) work well.
This indicates that the problem might be a bad USART chip on your board. You’ll have to see if you get any data going in or out of the board.
How can i check this? Have i to check the 3232 chip on the board? How? Please help me, I’m a really newbie with circuits. thanks
Not sure if I can provide you with an exact method. Read the datasheet, and it still depends on the kind of equipment you have. OScilloscope? If not, you might struggle! See if you’re getting power to the right pins. See if anything at all appears on the data in and out pins if you are using a meter. You might see changes in voltages, but obviously no signals. Any life in and then look for any life going out.
I think my board is damaged. I measure 2.73V between V+ (of my st3232) and GND jumper (proto section of the board). But I measure -0.59V between V+ and GND of st3232. Is it damaged?
Either that or the power supply. Like I said,read up the datasheet and see if you can find some information on forums. Forums work best for these kind of questions. Alternatively you could also get in touch with Olimex. Sadly my own Olimex board had fulfilled it’s purpose so haven’t touched it in ages!