C sample code for PIC micros and Hi-Tech C( PART-2)

Sample Hi-Tech C projects by Mike Pearce (set B)

These sample Hi-Tech C projects were supplied by Mike Pearce while he was working at the University of Canterbury, New Zealand. He is an excellent programmer for PIC micros, and manages to make them do things that I barely thought were possible. These projects partially cover the 8+ years of work he performed in the Chemistry Department and as a hobby. They give a window into the way he does his code, and perhaps you will learn a few tricks from him :) Enjoy!

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 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

PIC12C509 logic gate replacement for nitrogen filler by Mike Pearce

From readme.txt: This project was a really quick replacement for an old logic gate that had faulted and could not be fixed. It is a simple program that uses nested if{}else{} statements to do a BETTER job than the original logic did, by the addition of some error trapping and a timeout delay for safety reasons (and to save money because Liquid Nitrogen is not cheap).
^ Go to top

Demo Board by Mike Pearce


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.
  • Schematic for demo board. View.
  • Entire archive. Download (180KB).
^ Go to top

Heater Project by Mike Pearce


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

Phase Controller for 2kW heater by Mike Pearce

From readme.txt: One of the projects I did need accurate control of a 2KW heater element ... with quite a bit of thought and some help with some math to work out "Even Power" steps for a sign wave, I sat down and wrote this simple, soft timed program that controls a single output and is set but 4 input pins. Uses 12C508. Note: this is an assembly language project, not a Hi-Tech C based project.
^ Go to top

Dear Shane Tolmie,

This site is great. It helped me a lot to start with c development on PIC.
Thank you for this great work.

Regards,
Parag Panchal
India

Disco Light Controller by Mike Pearce

From readme.txt: I wanted to make up a simple disco light controller that went through a pre-programmed sequence, and I had not had HT-PIC C for long, so it was a good opportunity to have a play. Included are a couple of DOS programs that I used to generate the patterns and convert to an array ready to be pasted into the PIC's C file. I took this software one step further and had it output to the PC Parallel port so with a patch cable I could simulate the PIC's output on the actual circuit board I used, hence I was able to preview the lights outputs. Included is an image of the Lighting Box I built up.
^ Go to top

Dallas DS1821 three-pin digital thermostat by Mike Pearce


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).

Archive includes Hi-Tech C source code, schematics, and more.


^ Go to top

Gym Timer by Mike Pearce

From readme.txt: For PIC12C509, this was a very simple timer and buzzer that I made for a client that wanted timer for doing gym circuits in his home gym. I made this one up on a piece of vera board, and it injects a signal into an audio line that was fed into an amplifier from his CD Player. This cut costs dramatically due to no extra amplifier/speakers needed. The time is set by the state of GP0,1,2,3 and is calculated by the binary value * 5 + 30 seconds. This allows the interval to be set from 30 seconds to 105 seconds in 5 second increments. This is only approximate!!GP5 was fed through a resistor and capacitor type set up into one channel of the audio and just does a short Buzz at the end of each interval.
^ Go to top

High Speed Pulse Generator (HSPG) by Mike Pearce


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

LCD and Keypad project by Mike Pearce

From readme.txt: This project was for the first version of the High Speed Pulse Generator, but was scrapped due to technology restrictions of the time. But the board has been used for testing character LCDs and keypads, and does make a reasonable general purpose display and keypad type controller with a number of control lines available.
Hello Shane,

Congratulations on a truly excellent learning resource for PIC-C! I have learnt from your examples and refer many of our PIC customers to your site for additional design tips.
Please encourage contributions and grow the resource !
-Andrew Metcalfe
Field Applications Engineer,
Arrow Electronics
^ Go to top

Sample code from Mike Pearce


Miscellaneous code for reference, the projects above are a more complete and updated version of this archive.
  • Battery Charger for 8 Ni-Cad battery packs
  • Disco Light Driver For the PIC12C50x
  • Repeating Timer For Gym Circuits, switchable from 30 sec to 60 sec in 5 sec intervals
  • Program for using a PIC16C64 to control the heating/cooling for the PVC Reactor
  • Software to replace logic driving the Liquid Nitrogen Filler system, uses PIC12C508
  • Motor control software to drive UOC-MOTORCTRL-001 using PWM.
  • A program for the PIC12C50x to drive 4 solid state relays, uses RS232 and Dallas 1-wire 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.
The entire archive is available. Download (213KB).
^ Go to top

Sample code from Ed Waugh (set C)


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.
EEPROM source in CCS C
^ 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.



MMC source in CCS C
^ Go to top

C code samples for dsPIC by Mike Pearce (set D)

"Hello World" for dsPIC30F3011 on a KPIC-2840P prototyping board

From readme.txt: All my samples will be written for the dsPIC30F3011 40 pin DIP package that can work (with minor mods) on a KPIC-2840P prototyping board (available from www.kiwacandirect.com). Compiled with the C30 evaluation version of the C compiler from www.microchip.com.
This project is a simple "Hello World" sent out the serial port. Shows quick setup of the UART for TX only.
C source for Microchip C30 compiler for Hello World
^ Go to top

RC Pulse for dsPIC30F3011 on a KPIC-2840P prototyping board

From readme.txt: All my samples will be written for the dsPIC30F3011 40 pin DIP package that can work (with minor mods) on a 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.
C source for Microchip C30 compiler for RC Pulse

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.
Original hardware obtained from Mike Pearce from www.kiwacandirect.com.

Click to enlarge
1. Create a new project.

2. Select PIC24 or dsPIC processor.

3. Select MPLAB-C30 compiler.

4. Add the following files and select "copy to project folder".

glcd.c
glcd.h
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\)
5. Once setup has completed open the glcd.h file for editing.
Change the I/O LAT, PORT, TRIS settings to match your design.
Change frequecy to match your design.
Change display resolution to match your LCD.
Save your changes.
6. Open main.c for editing (This is just a basic framework).

Fill in the gaps with the rest of your program.
Make sure any analog input ports that have been used for the LCD are set up as digital I/O.
7. Compile and hopefully it works.
C project for Microchip C30 compiler for Samsung KS0713 Graphical LCD Driver.
^ Go to top

C code samples for PIC18F4550 from John Leung (set E)

PIC18LF4550 with LCD and Temperature Sensor

"Attached is an example project to display real-time ambient temperature and relative humidity from SHT10 digital sensor on a 65k 1.8" color display. The microcontroller was PIC18LF4550. The result is shown in the jpeg file as well. All code with heavy comment."
Compiler: C18 compiler v2.40
Comaptible with SHT10 digital temperature and humidity sensor.
Compatible with CM50012ACT6 65k color Graphical LCD Display Driver (HYVIX HDC1600 controller).
Original hardware obtained from www.techtoys.com.hk.
Temperature sensor with LCD for PIC18LF4550.


^ 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."

Original hardware obtained from www.techtoys.com.hk.
PIC sample project with JoyStick and I/O handling. Note: zip file includes documentation in .pdf format.




^ Go to top

MiniBasic Source Code in C (set F)

MiniBasic example peripheral source code in C

MniBasic is a full basic interpreteter for the PIC18, PIC24, and PIC32. It is written in C. By examining the source code, you can work out how to use almost any peripheral on the PIC18, PIC24 or PIC32. Example peripheral source code includes, but is not limited to, RS232, PWM, timers, LCD, port control, SPI, I2C, ethernet (under development), etc.
See www.pic32.org/minibasic/.
^ Go to top

Source Code from Regulus Berdin (set G)

Interrupt Driven Serial Routines with Circular FIFO for PIC16x Micro

This is another routine that I want to share. This is the interrupt driven serial routine with circular FIFO for any PIC16x micro with a hardware UART. Compiled with Hi-Tech C. Since this is unbuffered, so I added a software FIFO. The FIFO management is quite lean and fast but required the size of the buffer to be power of 2 (2^x). This is because FIFO roll-over uses bit-wise AND of (buffer_size - 1). This reduces code size due to that no more conditionals to test pointers overflowing the buffer.
Interrupt driven serial routine with circular FIFO for PIC micro.

Tiny Threads - Tiny Multitasking Threads for Microcontrollers

For the efficiency and performance conscious C programmers, here is my tiny cooperative multitasking engine.

by Regulus Berdin
Limitations:
  • Maximum 254 lines per thread.
  • Thread context switching will not work within a switch block.
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

Source code from Shane Tolmie (set H)

InnoSetupAV v1.00, a sample installer script for any Windows based support programs for any embedded work.

UVa Tutorial

You can easily rank your programming aptitude compared to others - over 80,000 others, to be precise.
Read more on the UVa Online Judge tutorial.

The Dot Factory: An LCD Font and Image Generator

From www.pavius.net: The Dot Factory is a small, GPL, open source tool intended to generate the required C language information to store many fonts and images, as efficiently as possible, on a microcontroller. These fonts are then uploaded via the LCD driver (see the Drivers and Modules page for a few) to the actual dot matrix LCD. Compatible with numerous PIC micros, more micros can easily be added by writing a simple driver.
Read more.

No comments:

Post a Comment

Labels

PROJECTS 8086 PIN CONFIGURATION 80X86 PROCESSORS TRANSDUCERS 8086 – ARCHITECTURE Hall-Effect Transducers INTEL 8085 OPTICAL MATERIALS BIPOLAR TRANSISTORS INTEL 8255 Optoelectronic Devices Thermistors thevenin's theorem MAXIMUM MODE CONFIGURATION OF 8086 SYSTEM ASSEMBLY LANGUAGE PROGRAMME OF 80X86 PROCESSORS POWER PLANT ENGINEERING PRIME MOVERS 8279 with 8085 MINIMUM MODE CONFIGURATION OF 8086 SYSTEM MISCELLANEOUS DEVICES MODERN ENGINEERING MATERIALS 8085 Processor- Q and A-1 BASIC CONCEPTS OF FLUID MECHANICS OSCILLATORS 8085 Processor- Q and A-2 Features of 8086 PUMPS AND TURBINES 8031/8051 MICROCONTROLLER Chemfet Transducers DIODES FIRST LAW OF THERMODYNAMICS METHOD OF STATEMENTS 8279 with 8086 HIGH VOLTAGE ENGINEERING OVERVOLATGES AND INSULATION COORDINATION Thermocouples 8251A to 8086 ARCHITECTURE OF 8031/8051 Angle-Beam Transducers DATA TRANSFER INSTRUCTIONS IN 8051/8031 INSTRUCTION SET FOR 8051/8031 INTEL 8279 KEYBOARD AND DISPLAY INTERFACES USING 8279 LOGICAL INSTRUCTIONS FOR 8051/8031 Photonic Transducers TECHNOLOGICAL TIPS THREE POINT STARTER 8257 with 8085 ARITHMETIC INSTRUCTIONS IN 8051/8031 LIGHTNING PHENOMENA Photoelectric Detectors Physical Strain Gage Transducers 8259 PROCESSOR APPLICATIONS OF HALL EFFECT BRANCHING INSTRUCTIONS FOR 8051/8031 CPU OF 8031/8051 Capacitive Transducers DECODER Electromagnetic Transducer Hall voltage INTEL 8051 MICROCONTROLLER INTEL 8251A Insulation Resistance Test PINS AND SIGNALS OF 8031/8051 Physical Transducers Resistive Transducer STARTERS Thermocouple Vacuum Gages USART-INTEL 8251A APPLICATIONs OF 8085 MICROPROCESSOR CAPACITANCE Data Transfer Instructions In 8086 Processors EARTH FAULT RELAY ELECTRIC MOTORS ELECTRICAL AND ELECTRONIC INSTRUMENTS ELECTRICAL BREAKDOWN IN GASES FIELD EFFECT TRANSISTOR (FET) INTEL 8257 IONIZATION AND DECAY PROCESSES Inductive Transducers Microprocessor and Microcontroller OVER CURRENT RELAY OVER CURRENT RELAY TESTING METHODS PhotoConductive Detectors PhotoVoltaic Detectors Registers Of 8051/8031 Microcontroller Testing Methods ADC INTERFACE AMPLIFIERS APPLICATIONS OF 8259 EARTH ELECTRODE RESISTANCE MEASUREMENT TESTING METHODS EARTH FAULT RELAY TESTING METHODS Electricity Ferrodynamic Wattmeter Fiber-Optic Transducers IC TESTER IC TESTER part-2 INTERRUPTS Intravascular imaging transducer LIGHTNING ARRESTERS MEASUREMENT SYSTEM Mechanical imaging transducers Mesh Current-2 Millman's Theorem NEGATIVE FEEDBACK Norton's Polarity Test Potentiometric transducers Ratio Test SERIAL DATA COMMUNICATION SFR OF 8051/8031 SOLIDS AND LIQUIDS Speed Control System 8085 Stepper Motor Control System Winding Resistance Test 20 MVA 6-digits 6-digits 7-segment LEDs 7-segment A-to-D A/D ADC ADVANTAGES OF CORONA ALTERNATOR BY POTIER & ASA METHOD ANALOG TO DIGITAL CONVERTER AUXILIARY TRANSFORMER AUXILIARY TRANSFORMER TESTING AUXILIARY TRANSFORMER TESTING METHODS Analog Devices A–D BERNOULLI’S PRINCIPLE BUS BAR BUS BAR TESTING Basic measuring circuits Bernoulli's Equation Bit Manipulation Instruction Buchholz relay test CORONA POWER LOSS CURRENT TRANSFORMER CURRENT TRANSFORMER TESTING Contact resistance test Current to voltage converter DAC INTERFACE DESCRIBE MULTIPLY-EXCITED Digital Storage Oscilloscope Display Driver Circuit E PROMER ELPLUS NT-111 EPROM AND STATIC RAM EXCITED MAGNETIC FIELD Electrical Machines II- Exp NO.1 Energy Meters FACTORS AFFECTING CORONA FLIP FLOPS Fluid Dynamics and Bernoulli's Equation Fluorescence Chemical Transducers Foil Strain Gages HALL EFFECT HIGH VOLTAGE ENGG HV test HYSTERESIS MOTOR Hall co-efficient Hall voltage and Hall Co-efficient High Voltage Insulator Coating Hot-wire anemometer How to Read a Capacitor? IC TESTER part-1 INSTRUMENT TRANSFORMERS Importance of Hall Effect Insulation resistance check Insulator Coating Knee point Test LEDs LEDs Display Driver LEDs Display Driver Circuit LM35 LOGIC CONTROLLER LPT LPT PORT LPT PORT EXPANDER LPT PORT LPT PORT EXTENDER Life Gone? MAGNETIC FIELD MAGNETIC FIELD SYSTEMS METHOD OF STATEMENT FOR TRANSFORMER STABILITY TEST METHODS OF REDUCING CORONA EFFECT MULTIPLY-EXCITED MULTIPLY-EXCITED MAGNETIC FIELD SYSTEMS Mesh Current Mesh Current-1 Moving Iron Instruments Multiplexing Network Theorems Node Voltage Method On-No Load And On Load Condition PLC PORT EXTENDER POTIER & ASA METHOD POWER TRANSFORMER POWER TRANSFORMER TESTING POWER TRANSFORMER TESTING METHODS PROGRAMMABLE LOGIC PROGRAMMABLE LOGIC CONTROLLER Parallel Port EXPANDER Paschen's law Piezoelectric Wave-Propagation Transducers Potential Transformer RADIO INTERFERENCE RECTIFIERS REGULATION OF ALTERNATOR REGULATION OF THREE PHASE ALTERNATOR Read a Capacitor SINGLY-EXCITED SOLIDS AND LIQUIDS Classical gas laws Secondary effects Semiconductor strain gages Speaker Driver Strain Gages Streamer theory Superposition Superposition theorem Swinburne’s Test TMOD TRANSFORMER TESTING METHODS Tape Recorder Three-Phase Wattmeter Transformer Tap Changer Transformer Testing Vector group test Virus Activity Voltage Insulator Coating Voltage To Frequency Converter Voltage to current converter What is analog-to-digital conversion Windows work for Nokia capacitor labels excitation current test magnetic balance voltage to frequency converter wiki electronic frequency converter testing voltage with a multimeter 50 hz voltages voltmeter

Search More Posts

Followers