Embedded Systems/PIC Programming
This module assumes
- you, the reader, know a little about programming using C and assembly.
- you have a MPLAB ICD 2 and a PICDEM 2 Plus demo board.
- you have selected a PICmicro microcontroller and want to program it. (Examples in this book will be focus on PIC18F452 microcontroller since it came with the demo board. The same process works with most other PICmicros).
Before getting started,
- install latest MPLAB IDE from Microchip Technology Inc and MPLAB C18 compiler. When this book was written, the latest MPLAB version is 7.40.
Getting Started with Simple I/O
editWe will start with a simple input output program using PORTA and PORTB. Let’s light some LEDs on the board. We should assign value 0x0A to PORTB. This will light up LEDs at RB3 and RB1. Everytime the switch button at RA4 is pressed, the value will be negated; thus the LEDS will switch between RB3/RB1 and RB2/RB0.
First, start your MPLAB IDE. In the menu bar, select Project → Project Wizard. Hit next, and select PIC18F452 as your device. Select next again. Under Active Toolsuite, select Microchip C18 Toolsuite and MPLAB C18 C Compiler (mcc18.exe) under Toolsuite Contents. Now, we have to give a name to our project and select a directory for it. Since we going to do a simple I/O, let us name it Simple_IO at Project Name. Then, select a directory. In this case, I would use c:\example\simpleIO. Hit the next button. We do not have any files to add to the project. So, hit the next button again. Now, we are finished with the Project Wizard. Hit the finish button.
Once the project is setup, we should create a C file called SimpleIO.c. At the menu bar, select File → New. Once the new editor pops, select File → Save As. At the new window, save the newly created file as SimpleIO.c. On the Project Window (where Simple_IO.mcv is the title) right click on Source Files. Select Add Files... from the menu. Select SimpleIO.c from where you save just now. On the same window, select Linker Scripts and select Add Files... from the menu. Under Microchip C18 C compiler directory (default is C:\MCC18), go to c:\installed directory/lkr and type 18f452.lkr at the File name. Type the following code into the editor.
#include <p18f452.h>
// Configurations
#pragma config OSC = HSPLL, OSCS = ON, PWRT = ON, BOR = OFF, WDT = OFF, LVP = OFF
// Main body
void main() {
// Initializing ports
PORTA = 0;
PORTB = 0;
// Set RA4 as input and RB3-RB0 as output
TRISA |= 0x10;
TRISB &= 0xF0;
// Set value 0x0A to PORTB
PORTB = 0x0A;
// If button is pressed, toggle PORTB
while(1) {
if(PORTAbits.RA4 != 0)
PORTB = ~PORTB;
}
}
Hit the save button on the toolbar or from the menu bar, select File → Save. Next, click on Project → Build All or Build All button at the toolbar at the top. Make sure you have no error or warning at the Output window. Hook up your MPLAB ICD 2 to your computer and connect PICDEM 2 Plus Demo board to the ICD (Please refer to the manual provided). Now, select Debugger → Select Tool → MPLAB ICD 2. Make sure there is no error at the output window. Next, click on Debugger → Program to program the microcontroller. Once you have successful programed the microcontroller, click on Debugger → Run. You can also hit the Run button at the toolbar. On the demo board, LEDs at RB3 and RB1 should light up.
Now, pressed the switch at RA4 on the demo board a few times. Noticed something wrong? The LEDs do flick, but do not switch between RB3/RB1 and RB2/RB0 all the time. And if you just pressed the switch without releasing it, all the LEDs do light up. This is not something that we want. Let us look at the code again to find out what is the problem. Noticed that in the while loop, as long as RA4 is read 0, value at PORTB will be inverted. With the microcontroller running at 16 MHz, a flash push at the switch, which may consume up to 500us, may cause the value in PORTB to invert more than 1000 times. That is why the LEDs appear to be light up in random whenever the switch is pushed. The code has to be modified to detect a push, and a release at the switch. LED Blinker Materials Circuit from “Introduction to the PIC16F877A” 100 Ohm resistor LED
Circuit 1. Pin RB7 to resistor 2. Resistor to LED 3. LED to ground
Code //all these # below set up the PIC
- include <16F877A.h>
- device adc=8
- FUSES NOWDT //No Watch Dog Timer
- FUSES HS //Highspeed Osc > 4mhz
- FUSES PUT //Power Up Timer
- FUSES NOPROTECT //Code not protected from reading
- FUSES NODEBUG //No Debug mode for ICD
- FUSES NOBROWNOUT //No brownout reset
- FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
- FUSES NOCPD //No EE protection
- use delay(clock=20000000) // Sets crystal oscillator at 20 megahertz
- use rs232(baud=9600, xmit=PIN_C6, invert) //Sets up serial port output pin & baud rate
//main program starts here void main() {
//Infinite program loop starts. LED blinks forever. while(true){ output_high(PIN_B7); //send a “1” to pin RB7, making RB7 “High” at 5v
//this will turn on the LED hooked to RB7
delay_ms(500); //wait half a second, delays for 500ms output_low(PIN_B7); //send a “0” to pin RB7, making RB7 “Low” at 0v delay_ms(500); //wait half a second, delays for 500ms }
}
Notes You can easily add more LEDs and make them flash in different patterns. For more readable code, use
- define RED_LED PIN_B7
- define GREEN_LED PIN_B6
… … output_high(RED_LED); output_high(GREEN_LED);
Compilers, Assemblers
edit- Flowcharts drawn with a "visual flowcharting tool" in the w:PICAXE system are automatically converted into PICAXE BASIC.
- PICAXE BASIC in the w:PICAXE system.
- PROTON IDE (BASIC Languange for all PIC device) Tutorial For PROTON IDE .
- mikroBasic by mikroElectronika—a Basic Compiler that supports most PICs.
- PBASIC Programming—a wikibook about PBASIC, which runs on a PICmicro or Scenix in a Parallax BasicStamp
- mikroC by mikroelektronika—Very simple C Compiler www.mikroe.com
- MPC by Byte Craft Limited—Optimizing C compiler; demonstration version creates size-limited executables.
- Microchip makes "MPLAB" available for free download from their web site. It includes a free assembly language compiler and emulator; also a limited C compiler for some chips (PIC18, PIC24, and PIC32—but alas, not PIC16).
- the C4SX environment and the CC1B compiler is a C compiler for the SX28 and the SX48 devices.
- SDCC - Small Device C Compiler—is a C compiler that has some support for both PIC16 and PIC18 chips [1].
- "Programming the PIC16f628a with SDCC"
- "Using the Code::Blocks IDE with SDCC on PIC MCUs" (on the Code::Blocks wiki)
- SDCC questions at Stack Overflow
- SDCC tutorials
- BoostC and BoostC++—a C compiler and a C++ compiler that has some support for both PIC16 and PIC18 chips
- Pyastra: python assembler translator
- Compilers for many languages—some of them free for downloading—are listed at http://massmind.org/techref/microchip/languages.htm .
- FlashForth runs on most PIC18F and dsPIC30F microcontrollers.
- Optimal PICmicro Code Solutions has a free online tool to scan your assembly code for banking and paging errors.
- JAL (Just Another Language) created by Wouter van Ooijen in 2003.
- w:OOPic: The OOPic ("Object Oriented Programmable Integrated Circuit") is a software development system and programming language for Microchip PIC hardware.
further reading:
Further reading
edit- A Guide To PIC Microcontroller Documentation goes into more detail.
- Massmind: PICList
- PICmicro wiki
- Down as of 1/19/2011 WardXmodem (discuss • contribs) 06:27, 19 January 2011 (UTC)
- PICmicros and C programming language
- The free AmQRP PIC Elmer tutorial has a very detailed "click-here, type this" tutorial on the Microchip PIC. Lessons 1 through 9 discuss setting up setting up MPLAB, assembly language programming, and running the simulator—you don't need any hardware for these lessons.
- "PIC Languages I find cool" by Byron A. Jeff
- PIC microcontroller tutorials
- "Compilers for the PIC"