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


Sample projects for the Microchip PIC micro series of microcontrollers, including the PIC12x, PIC16x, PIC18x, PIC24x, and dsPICx microcontrollers.
Code is targeted at the Hi-Tech C compiler, from www.htsoft.com, the C18 or C30 compiler from www.microchip.com, or CCS C.
NOTE: If you have any high-quality sample projects send it to me, I will strongly consider posting it here.



Sample C Code (set A)


Types for Hi-Tech C

  • Data Types for Hi-Tech C, from Eduardo Rosso. Reasonably well known code, makes handling of data easier.

    Data types for Hi-Tech C.
^ Go to top

Delay and Timeout Routines for Hi-Tech C (PIC16Fx core)

  • Example project illustrating delay and timeout routines.
    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.
  • Delay routines v7.1 for Hi-Tech C and PIC16x core.
^ Go to top

Delay and Timeout Routines for Hi-Tech C (PIC18Fx core)

  • Example project illustrating delay and timeout routines.
    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 for an alternative solution.
  • Delay routines v2.2 for Hi-Tech C and PIC18x core.
^ Go to top

RS232 Serial port

^ Go to top

USB Serial port on PIC18F4550

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


    Download PIC18F4550 USB serial port example v1.02 (540KB).
    Download PIC18F4550 USB serial port example v1.10 (540KB).
^ Go to top

Bootloaders for PIC Microcontrollers

  • Bootloaders for various PIC microcontrollers.

    • PIC16F87x bootloader. Browse.

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

    • PIC17C4x bootloader (obsolete).
      Download (224KB).

    • PIC18x52 bootloader. Browse.

    • dsPIC bootloader from Ingenia. For any dsPIC device. Browse to section.

^ Go to top

dsPIC bootloader from Ingenia


This is the source code of the Ingenia dsPIC bootloader.

The bootloader consists of:
  • 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.
The main features of the firmware are:
  • Auto-Baud 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


Download firmware and Windows installer (3.8MB).
Download user manual (442KB).
If you wish to contribute configuration files for this bootloader, for different dsPIC devices, email  support@microchipc.com.
^ Go to top

Sample Projects in MPLab for Hi-Tech

  • Example project in MPLab for PIC16F876 and Hi-Tech C 7.86pl2, showing how to output decimals using putch().
    Download.
  • Extremely simple example of how to efficiently count bits in an integer.
    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).
    See Mike Pearces projects below for more.
  • 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

  • Excellent CRC code.
    // Update the CRC for transmitted and received data using
    // the CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1).
    unsigned char ser_data;
    static unsigned int crc;
    crc = (unsigned char)(crc >> 8) | (crc << 8);
    crc ^= ser_data;
    crc ^= (unsigned char)(crc & 0xff) >> 4;
    crc ^= (crc << 8) << 4;
    crc ^= ((crc & 0xff) << 4) << 1;
^ Go to top
    Hi Shane,

    I appreciate the time and effort you spent to make this page, well done !!

    I've been involved with PIC micros since 1996 and programming in
    C (CCS Compiler) for the last two years.

    I want to send you my best regards from Argentina.

    - Humberto

Interfacing to miscellaneous hardware with Hi-Tech C

  • PIC16F876 to PC comms via RS232 with sample Hi-Tech C code and VB 6 code. Link.
  • By Mike Pearce. From readme.txt: Dallas DS1820 Digital Temperature Probe routines for the PIC12Fx and PIC16Fx series. Includes CRC checking routines. Download (19KB). See Mike Pearces projects below for more.
  • PIC and SPI bus for a Linear Tech LTC2404 24 bit A/D module, from Scott Douglas, Project Engineer, Blodgett Combi. Heavily commented implementation. Download (6KB).
  • How to use EEPROM on 12CE673/4. Download (1KB).
  • For a PIC16F87x, how to initialize the built-in 256 bytes of EEPROM at compile time. Download.
  • How to use EEPROM under CCS C for 24LC256. Link.
  • How to use EEPROM under Hi-Tech C for 24LC01. Link.
  • Example A/D code for 12-bit Texas Instruments TLC2543.  Download.
  • Example D/A code for 12-bit Analogue Devices AD7390.  Download.
  • How to use the 93XXX EEPROM chip utilizing built-in hardware SPI for 16X and 17X series micros. Download.
  • Example A/D code for inbuilt ports on PIC16F876/7.  Download.
  • Example C code for SPI to access the following chips.
    • 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

Complete Design for Giant 8-foot LCD Counter

This is the complete design for a giant 8 foot money counter for student debt. It is still working beautifully to this day, counting up student debt in the main library at Canterbury University of New Zealand.
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).
Explanation of the time-sliced multitasking used in this project.
^ Go to top

Multitasking on a PIC in Hi-Tech C

^ Go to top

USB to IBM-PC communications using PIC16F876


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

Mechanically scanned display source code in Hi-Tech C


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

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