MPLAB Code Source
MPLAB Code Source
Search
Expand All | Collapse All C sample code for PIC micros and HiTech C
Home Sample projects for the Microchip PIC micro series of microcontrollers, including the PIC12x, PIC16x, PIC18x, PIC24x, and dsPICx
microcontrollers.
+ PIC Code in C (55)
Code is targeted at the Hi‐Tech C compiler, from www.htsoft.com, the C18 or C30 compiler from www.microchip.com, or CCS
Index C.
+ PIC16Fx code in C (2)
+ PIC18Fx code in C (2) We want to publish your embedded source code for the benefit on the PIC community. Send it to [email protected]
+ dsPIC30Fx code in C (2) and I will post it on the site, together with recognition of your name and website.
USB code in C
CRC. Phase Controller for 2kW heater.
CRC routines in C
USB serial port for PIC18F4550. Dallas DS1821 threepin digital thermostat.
EEPROM MMC card. Gym Timer.
I2C Delay routines in C for PIC16Fx core. LCD and keypad project.
A/D Delay routines in C for PIC18Fx core. Heater Project involves 1wire routines, serial
D/A UART for PIC16F87x and PIC18Fx. routines, a P.I.D (Proportional, Integral, Derivative)
LCD Bootloader PIC16F876. calculation, ADC, and a interrupt driven burst mode
Bootloader PIC18F1320. heater control.
MMC Card
Bootloader PIC18Fx52. dsPIC30Fx "Hello World" example.
Pulse Monitor Project Bootloader PIC17C4x. dsPIC30Fx "RC Pulse" example.
Triac Controller Project Bootloader dsPIC (all variants). C driver code project for Samsung KS0713 and PIC
PIC12C509 logic gate EEPROM. micros.
Phase Controller Project A/D. PIC18LF4550 with LCD and temperature sensor.
D/A. MiniBasic example peripheral code in C, for
Dallas DS1821 Thermostat
SPI. PIC18, PIC24, PIC32. Examine the C source code to
Heater Project LCD. work out how to use any peripheral on a PIC18,
Multitasking PIC12C509 logic replacement nitrogen filler. PIC24 or PIC32. Very useful.
Samsung KS0713 driver code I2C. Interrupt driven serial with circular FIFO for PIC16x
... and 40 other example Multitasking and RTOS. micro.
17C4x bootloader. Tiny threads example 1 byte per thread.
projects
16F84 based pulse monitor. The Dot Factory: An LCD Font and Image Generator
+ PIC C FAQ (68) TRIAC controller Embedded PIC Programmer
Dallas DS1821 thermometer. Portable LCD driver.
+ PIC bootloaders (5) Decimal routines. SimpleRTOS. A tiny, portable multitasking OS.
PIC16F84 pulse mon date/time RS232 serial Improve your programming with the UVa tutorial.
+ PIC Articles (3) port ... and much, much more.
PIC16F84 TRIAC / IGBT 50/60Hz control.
+ PIC tips (7)
+ Reviews (7)
Sample C Code (set A)
+ UVa tutorial (2)
Types for HiTech C Enyx FPB1
Advertise
Platform
Data Types for Hi‐Tech C, from Eduardo Rosso. Reasonably well known code, makes handling of Highend PCIe
Stratix V FPGA
data easier.
board 10G/40G
Ethernet,
Data types for Hi‐Tech C. DDR3, QDR2+
^ Go to top
Unlike the routines available from the Hi‐Tech C site, these are written in in‐line assembler and thus give precise
timing. Remember to match the actual microprocessor clock speed to the setting for PIC_CLK.
^ Go to top
Unlike the routines available from the Hi‐Tech C site, these are written in in‐line assembler and thus give precise
timing. Remember to match the actual microprocessor clock speed to the setting for PIC_CLK.
Note: these routines could cause problems if Hi‐Tech C banks the assembly variables in the incorrect bank; the problem
would only surface on a large project with high RAM usage. See file "Warning_deprecated_routines.txt" within the archive
http://www.microchipc.com/sourcecode/#mmc 1/11
3/28/2015 Microchip PIC micros and C source and sample code
for an alternative solution.
^ Go to top
Serial port example. How to communicate with RS232 to your PC via a serial port, with Hi‐Tech C code, hardware
schematics, and Visual Basic 6 sample project.
^ Go to top
USB example for PIC18F4550. Plug your PIC18F4550 into your PC using a USB cable. A virtual RS232 COM port is
created, allowing your application to print RS232 messages into HyperTerminal. Allows porting of legacy code that used
RS232 comms.
This project is a modified form of the CDC (Communications Class Driver) available from www.microchip.com. The
www.htsoft.com forum has some discussion on this code.
Note: ensure that you read "readme (including install notes).txt" after download.
Schematics: this code runs on the PICDEM‐FS board from Microchip. To make your own schematic, place a PIC18F4550 on a
breadboard, ensure that you are using a 20Mhz crystal, place a 470nF capacitor on port Vusb, and hook the USB directly to
the chip. It should start to work straight away. Download the PICDEM‐FS datasheet for the exact schematic to use with
this code.
Result: start up HyperTerminal or putty, find the new COMx port that has been enumerated. Set it to 115200,N,8,1. The
actual COM port speed doesn't matter, as its a virtual comm port. You will see "[alive]" being continuously generated
on virtual com port X over USB. You can find the exact number of the virtual COM port by going into "Control Panel",
then "System", then selecting the "Hardware" then "Device Manager", there will be a new COM port under "Ports (COM &
LPT). You can change the number of the virtual comm port by going into properties, and selecting the new COM port.
11th Nov 2007 ‐ tried it on Hi‐Tech C v9.62, it worked. Had reports that it does not work on Hi‐Tech C v9.51.
2nd Feb 2009 ‐ customer got it working with Hi‐Tech C v9.51, it worked after some tweaks. Added instructions to v1.10
of the download.
^ Go to top
PIC18F1320 bootloader. This is a port of the PIC16F87x bootloader to the PIC18F1320. Project
includes Microsoft Visual C++ source for the Windows downloader, and assembly source for the PIC
core. For more background documentaiton about bootloaders in general, see the PIC16F87x
bootloader page.
Download (570KB).
dsPIC bootloader from Ingenia. For any dsPIC device. Browse to section.
^ Go to top
Open source firmware code, written in assembler. Can be adapted for generic for all dsPIC devices.
A Windows based Graphical User Interface. Comes with user's guide, algorithm flow, etc.
http://www.microchipc.com/sourcecode/#mmc 2/11
3/28/2015 Microchip PIC micros and C source and sample code
The main features of the firmware are:
AutoBaud rate detection. This allows one to use any speed of crystal.
Ability to read/write program memory.
Ability to read/write EEPROM Memory
Ability to read/write configuration registers.
Easily add new dsPIC devices. By default comes only with dsPIC30F4011 and dsPIC30F3011. Define and
protect memory zones through a XML configuration file.
Forum devoted to the Ingenia bootloader. See feedback from other users of this bootloader.
Or browse directly to the Ingenia bootloader homepage or the Ingenia company homepage.
Download
If you wish to contribute configuration files for this bootloader, for different dsPIC devices, email [email protected].
^ Go to top
Example project in MPLab for PIC16F876 and Hi‐Tech C 7.86pl2, showing how to output decimals using putch().
Download.
^ Go to top
I2C
Example Hi‐Tech C code for I2C, interfacing to Microchip 24LC01B non‐volatile EEPROM and Dallas Temperature sensors
DS1775 and DS1721.
Download (4KB).
By Mike Pearce. From readme.txt: "A complete set of bit banged, software driven I2C routines I created for any PIC
device ‐ and they work!! These functions are single master only functions, and are ideal for communicating with things
like EEPROMs, LCD Drivers, ADC Converters etc.". Intel enquired about using this code in one of their products.
Download (51KB).
Example code for I2C, routines for PIC16F877 to write to the 24LC01B EEPROM, using the PICDEM 2 demo board
from Microchip. By Michael Alon.
Download (5KB).
^ Go to top
CRC
^ Go to top
http://www.microchipc.com/sourcecode/#mmc 3/11
3/28/2015 Microchip PIC micros and C source and sample code
For a PIC16F87x, how to initialize the built‐in 256 bytes of EEPROM at compile
time. Download.
How to use the 93XXX EEPROM chip utilizing built‐in hardware SPI for 16X and 17X series micros. Download.
A/D chip, the MAX186 12 bit 8‐channel micro‐wire and D/A chip, the LTC1446 12 bit 2‐channel micro‐wire.
Download.
Example Hi‐Tech C code for RS232, Dallas DS1821 temperature probe, I2C, A/D, LCD, keypad, see sample projects below
by Mike Pearce.
^ Go to top
Download the complete design, including sample PIC code in C (for PIC16F876 or pIC16F877), Visual Basic
code, Protel 99 .pcb files, plans and photos.
Download (1080KB).
^ Go to top
Example of a simple but powerful multitasking system. Have multiple tasks, each with their own priority, running in the
backgrounds. Step‐by‐step tutorial and sample code.
^ Go to top
See the following link for Hi‐Tech C source code for interfacing a 16F876 to a PC using a Philips PDIUSBD11 chip. Many thanks to
Craig Peacock. http://www.beyondlogic.org/usbnutshell/usb7.htm#PIC16F876Example.
^ Go to top
From Rickard Gunée in Norway, the design for a rotor with a row of LEDs on the end. As it spins, it forms pictures. The photo to
the right shows a game of tetris implemented on the spinning led rotor! See http://www.rickard.gunee.com/projects/
^ Go to top
Other
Sample projects that come with Hi‐Tech C compiler. Look in directory c:\ht‐pic\samples for a/d routines, delay, i2c,
interrupt, interrupt serial, software serial, LCD, SCI, timer0 and more.
Sample routines that come with the Hi‐Tech C compiler. Look in directory c:\ht‐pic\sources for many math, string, and
variable routines.
^ Go to top
PIC16F84 based pulse monitor with date/time output to an RS232 serial port by Mike
Pearce
Here's the scenario: at University of Canterbury, New Zealand, they operate a hugely expensive X‐ray crystallography machine. It
was water cooled, and each run would take 24 hours and cost the client a lot. Unfortunately, as the story goes, there was a
problem with the water supply controller, and it would periodically drop pressure and the machine would shut down. This means
http://www.microchipc.com/sourcecode/#mmc 4/11
3/28/2015 Microchip PIC micros and C source and sample code
the run would have to be started again.
So, Mike Pearce designed a water‐monitor. It would measure the flow, based on pulses from a flow meter. If the pulses dropped
below a pre‐determined threshold, it would log the date and time out to a serial port. They set up a PC with a serial program,
and came back the next day to see exactly when the flow had dropped.
Mike tracked down the problem and had the system adapted to counter for the glitches, due to this nice piece of design work.
^ Go to top
PIC16F84 based TRIAC / IGBT phase controller for 50/60Hz brightness control by Mike
Pearce
From readme.txt: A single channel phase controller for a PIC16x84 @ 10Mhz. RB0/INT is used for the zero cross detection. RA0 is
an active LOW output to an opto isolator to drive a TRIAC or IGBT circuit. Phase control is a simple even time‐slice method, with
255 levels of output. This has samples of how to use the interrupt routine for TMR0 and INT. Timing for 50Hz and 60Hz has been
included ‐ selectable by commenting out the unwanted option. Also TRIAC or IGBT output is available by commenting out the
unwanted option.There is more info in the .c file pertaining to TRIAC/IGBTS and EMI standards.
^ Go to top
^ Go to top
This code was used to drive a demo board that I made for a University of
Canterbury demonstration day.
From readme.txt: The demo board consisted of a 16F84, five 240V 3A solid state
relays, an RS‐232 port, a Dallas 1‐wire interface port (using a RJ‐11/12
connector), and a number of LEDs ...
The archive contains the Hi‐Tech C source code, Protel 99 schematic and pcb
files, and .jpg schematic.
^ Go to top
From readme.txt: The Heater 3 project is used to control waterbath heaters and other specialist temperature controlled
equipment within the Chemistry Department.
This project combines the use of the 1‐wire routines, serial routines, a P.I.D (Proportional, Integral, Derivative) calculation,
ADC, and a interrupt driven burst mode heater control.
^ Go to top
^ Go to top
http://www.microchipc.com/sourcecode/#mmc 5/11
3/28/2015 Microchip PIC micros and C source and sample code
Entire archive. Download (200KB). Regards,
Parag Panchal
India
^ Go to top
From readme.txt: The DALLAS DS1821 is a 3 pin digital thermostat that can have its trigger points programmed using the Dallas
1‐wire interface. The need arose for this programmer when we had to replace the bi‐metal thermostat controls from some
fridges in the department for safety reasons. The DS1821 was the ideal replacement, the combination of small size, low
operation voltage (3‐5 volts) and operates to pre programmed settings that couldn't be changed unless you had a special
programmer. Hence the birth of the DS1821 Programmer project. This project uses the 1‐wire and I2C routines.The Turn On,
Turn Off and Polarity values can be programmed using this code. There are 3 buttons, 'Menu', '+', and '‐' to allow adjustments.
The Data is displayed on a 4 digit I2C LCD controlled by a PCF8577 (Philips).
^ Go to top
^ Go to top
From readme.txt: I have included this code as an Example of using a 20x2 character LCD, I2C EEPROM and basic I/O Control, it
also has a ascii to unsigned long function that I created to convert entered text into a useable number.The HSPG project is a
specialized piece of hardware for accurately generating pulses on a rather large piece of equipment (it takes up a large room).
This unit has a resolution of 20ns and a 24 bit counter (0 to approx. 0.335 sec in 20ns steps) and all the pulse outputs are fibre
optic for noise reduction, earth loop elimination and speed. This is the only piece of equipment like it in the world that we know
of, and hence it is restricted, so circuit diagrams will not be supplied. However, if you want to buy a complete unit (with
updated firmware) ... not a problem if you have a spare US$10,000 ... but you still won't get the diagrams!!!
^ Go to top
^ Go to top
Hello Shane,
Sample code from Mike Pearce
Congratulations on a truly
excellent learning
Miscellaneous code for reference, the projects above are a more complete and updated version resource for PIC‐C! I have
of this archive. learnt from your examples
and refer many of our PIC
Battery Charger for 8 NiCad battery packs customers to your site for
additional design tips.
Disco Light Driver For the PIC12C50x
Repeating Timer For Gym Circuits, switchable from 30 sec to 60 sec in 5 sec Please encourage
intervals contributions and grow
Program for using a PIC16C64 to control the heating/cooling for the PVC the resource !
Reactor
‐Andrew Metcalfe
Software to replace logic driving the Liquid Nitrogen Filler system, uses
Field Applications
PIC12C508 Engineer,
Motor control software to drive UOCMOTORCTRL001 using PWM. Arrow Electronics
A program for the PIC12C50x to drive 4 solid state relays, uses RS232 and
Dallas 1wire system.
Program is a serial communication between Dallas Temp Probes PC and Phase Controller.
Basic code that turns the 16F84 into a EEPROM to get things up and running.
^ Go to top
http://www.microchipc.com/sourcecode/#mmc 6/11
3/28/2015 Microchip PIC micros and C source and sample code
EEPROM
Sometimes it is useful to have access to a larger amount of non‐volatile storage than is available onboard the PIC. A serial
accessed eeprom can be the best way to do this but they are usually only available in sizes up to 512Kbits. The 24LC256 is a
256Kbit eeprom avaialble from microchip and is what these functions are designed to work with although they should work with
any size and speed of eeprom with suitable modification of the loops. This is just the functions you need to get some code
working and then drop these in.
The code has been tested on both a 16F876 using software and hardware I2C interfaces. Setting the pins to the hardware I2C
port int the #use line at the top of the code enables the hardware I2C.
Basic read and write byte functions are provided as well as a set of functions for a contiguous read (by Tom Cook) that allows
faster read out of a stream of data. There is also a function to write a string of any length to the eeprom. They should all be
fairly self explanatory.
^ Go to top
MMC card
For applications where more storage is required I would definately reccomend MultiMediaCards (some times called SanDisk (SD)
flash). The built in SPI interface only requires four data lines and they are available in sizes of up to 256MByte (at the moment).
There are a few little quirks in making the cards work with a PIC but these are hopefully all sorted out within the code. The
code is set to use the hardware SPI pins on the 16F876 and I haven't tried it any other way.
The MMC card has quite a large block size for writing, I think at the moment it is at least 512Bytes although I think I saw
something about a new version of the specifcation that is more flexible. But at the moment this means that you need to buffer
512Bytes and write them all in one go, the eeprom code (above) is ideal for this. The write_block function just writes the lowest
512Bytes from the eeprom to the MMC. The same is true when reading the card and in my current application I only need to read
the data to a PC so the read_block function just outputs every byte to RS232 although it wouldn't be hard to insert some
processing or pass each byte to whatever function you wanted. This is just the functions you need to get some code working and
then drop these in.
^ Go to top
This project is a simple "Hello World" sent out the serial port. Shows quick setup of the UART for TX only.
^ Go to top
http://www.microchipc.com/sourcecode/#mmc 7/11
3/28/2015 Microchip PIC micros and C source and sample code
KPIC‐2840P prototyping board (available from www.kiwacandirect.com). Compiled with the C30 evaluation version of the C
compiler from www.microchip.com.
RCPulse generates the 1 to 2ms pulse required for remote control servo control. Demonstrates the use of Timer and interrupts.
Currently writing a multichannel version for a project. Compiled with the C30 evaluation version of the C compiler from
www.microchip.com.
Samsung KS0713 Graphical LCD Driver for Microchip PIC24 and dsPIC33 Microcontrolers
From readme.txt: Make sure you have the latest MPLAB and MPLAB C30 programs from www.microchip.com before proceeding.
glcd.c
glcd.h Click to enlarge
glcd‐bitmaps.c
glcd‐bitmaps.h
glcdtest.c (for demo code ‐ otherwise your main c file )
pXXxxxx.gld (The gld file specifically for your micro‐
controller)
(Found in c:\Program files\Microchip\Mplab
C30\Support\Gld\)
C project for Microchip C30 compiler for Samsung KS0713 Graphical LCD Driver.
^ Go to top
^ Go to top
Joystick Demo
"Attached please find another application for using joystick commonly found in game pad, mobile phones, etc. It is nothing more
than individual tactile keys. There is a .pdf explaining the source code as well. Working on a dev board for PIC24 series. It will
come with a 2.2" TFT color LCD. Designed for the C18 compiler."
PIC sample project with JoyStick and I/O handling. Note: zip file includes documentation in .pdf format.
http://www.microchipc.com/sourcecode/#mmc 8/11
3/28/2015 Microchip PIC micros and C source and sample code
^ Go to top
See www.pic32.org/minibasic/.
^ Go to top
Interrupt driven serial routine with circular FIFO for PIC micro.
by Regulus Berdin
Limitations:
tthread.h. Header file for Tiny Threads. This is the only file needed to use Tiny Threads.
tt_sample.zip. Sample application using tthread. Compiles with PICC compiler from www.htsoft.com.
^ Go to top
UVa Tutorial
You can easily rank your programming aptitude compared to others ‐ over 80,000 others, to be precise.
^ Go to top
Read more.
^ Go to top
If you feel it is relevant for microchipc, please feel free to link to the project page:
http://www.pavius.net/2011/06/lpicp‐the‐embedded‐linux‐pic‐programmer/.
Read more.
http://www.microchipc.com/sourcecode/#mmc 9/11
3/28/2015 Microchip PIC micros and C source and sample code
^ Go to top
Following link contains code for Character LCD interface Driver for 16x2 Size (8bit/4bit configurable), this is fully portable code
for any MCU.
http://pallav‐aggarwal.blogspot.in/2012/06/generic‐character‐lcd‐driver‐16x2.html
Requesting you to please add my code for the benefit of all engineers and students.
If you feel it is relevant for microchipc, please feel free to link to the project page:
Read more.
SimpleRTOS
Published in response to an email sent the admin at MicrochipC. This website also has a good range of other sample code, its
worth checking out.
Dear All,
I just published a new version of my "toy" RTOS in my personal pages at PICList. Just it is not a "toy" anymore, it is really useful
and I'm using it already in new projects.
For now the ports for dsPIC/PIC24 and PIC32MX are already working, but I published only the PIC32MX port because it is already
tested.
The link is
<http://www.piclist.com/Techref/member/IMB‐yahoo‐J86/SimpleRTOS2.htm>
My main page is
<http://www.piclist.com/Techref/member/IMB‐yahoo‐J86/index.htm>
I plan to port it also to ARM Cortex‐M3 and M4 because I use Atmel's SAM3 and SAM4 devices, but those ports may take some time
to be finished.
My main motivation to rewrite it for bigger MCUs was that FreeRTOS uses too much memory and it doesn't fit even in some
PIC32MX devices's (PIC32MX210F016B) RAM or FLASH. My RTOS uses just a fraction of the memory needed by FreeRTOS and I
expect it to be somewhat faster.
The sample application which has six tasks, each one with a 256‐bytes stack, when compiled with optimization level ‐O3 uses
just 3685 instruction words and 2956 bytes of RAM. It fits easily in a PIC32MX210F016B.
I know that it cannot compete with FreeRTOS in general, but for some simple applications or very constrained devices it may fit
while FreeRTOS won't.
Best regards,
Isaac
Xilinx Fpga trainings 8Channel LED Dimmer USB SPI Programming Cable Free Calculator Toolbar
mvdfpga.com abeltronics.co.uk/dimmers embeddedcomputers.net calculator.utilitychest.com
Approved trainings: Fpga, VHDL, EDK 010V DIN Rail Dimmer for LED Program thousands of devices fast! Calculators: Different Calculators
SoC, Virtex, MicroBlaze, RocketIO strip and tape, UK factory prices Includes free software and shipping For Every Situation Free!
http://www.microchipc.com/sourcecode/#mmc 10/11
3/28/2015 Microchip PIC micros and C source and sample code
We welcome any suggesions or comments! Send them to Shane Tolmie on [email protected]. This site is a completely separate site to www.microchip.com, and
is maintained independently of Microchip Ltd., manufacturers of the PIC micro. All code on this site is free for non‐commercial use, unless stated otherwise.
Commercial use normally free, however, it is prohibited without contacting [email protected] for permission. All content on this site created by Shane Tolmie is
copyrighted by Shane Tolmie 1999‐2009. Click to advertise on this website ‐ $29.90 for a banner ad which will reach 55,000 user sessions per month. One months free
trial!
http://www.microchipc.com/sourcecode/#mmc 11/11