Programming PIC Microcontrollers Presented by Robert Dyball
Oct 26, 2014
Programming PIC Microcontrollers
Presented by Robert Dyball
Programming PIC Microcontrollers
Microcontrollers? And why PIC?
Programming PICsElectronics & Interfacing PICs
Putting it all together, where next
What are the alternatives?
What are Microcontrollers, what can I use them for?
Microcontrollers may be thought of as a complete computer on a single chip.
What are Microcontrollers, what can I use them for?
Microcontrollers may be thought of as a complete computer on a single chip.
Without a CRT or LCD display (usually)
Why PIC Microcontrollers?
Why PIC Microcontrollers? Low cost Few other “glue” components required Low active power use + sleep modes to save power. Many options across hundreds of versions; some
have USB or TCP/IP, differing amounts of RAM, ROM and I/O, flash or one time programmable.
Product families are often code and/or pin compatible
Programmers are very inexpensive Programmable in Assembler or C (free) and a
number of other programming languages including Basic.
How do I program PIC Microcontrollers
To program a PIC microcontroller:
PIC
In Circuit /ICSP Stand-Alone Manufactured
ProgrammerElectrically Connected to Microcontroller
IDE Compiled / Assembled Program
Usually transferred in “Hex”
ICSP – In Circuit Serial Programming
Usually Parallel, RS232 or USB interface
Ideally with ZIF socket(s)
Stand-Alone Programmers
ICSP – In Circuit Serial Programming
PIC Architecture – RISC / Harvard RISC (as opposed to CISC)
typically 35, no more than 80 assembler instructions.
Harvard Architecture (not von-Neumann) = separate8 data memory versus 12/14/16 bit program memory.
PIC Architecture – Clocking / Pipelining Operations execute in one clock cycle (except
branches)
How do I program PICs - languages Internally PIC executes machine code
You need to compile your code to the correct PIC micro(or near equivalent)
Many different languages are available. Microchip provide with MPLAB IDE their MPASM (assembler), as well as Microchip C, Microchip HiTech C is also available.
Many other 3rd party options including CCS C, CC5X C, GCBasic, Proton Basic, and PIC Basic Pro.
Programming PICs – Assembler MPLABX for Windows, Mac OS X and Linux
usersor
MPLAB for Windows users
Start simple, use a simpler, low-end PIC
Spacing is important
Programming PICs – C Use MPLAB or MPLABX
Choose a version of C to suit your target PIC
Sensors – getting info from the outside Many I/O Port options:
Digital I/O Ports, D to A ports, + specialised ports including:
LIN I2C CAN SPI USB PWM and more …
I/O ports – the basics Can be configured for input, output or both = “Tristate”
Input ports can detect a digital (on/off) device’s statussuch as a switch.
Output ports can “source” or “sink power”(watch current limitations, check the datasheet)
Some I/O ports have Analog to Digital converter, A to Dcheck datasheet for no. of ports + no. of bits resolution
LIN – Local Interconnect Network Bus Developed by Freescale along with a number of
automotive companies.
Uses 1 active wire = very simple and inexpensive.
Data is half-duplex (one way at a time).
Speed up to 19.2kbit/s at 40 meter length bus.
Commonly used in motor vehicles.
I2C – Inter-Integrated Circuit Developed by Philips, uses 2 active wires:
serial data + serial clock= simple and inexpensive.
Data is half-duplex (one way at a time)
10kbit/s, 100kbit/sec, 400kbit/sec, 1Mbit/sec, 3.4Mbit/sechigh speed transfer lowers range to a meters, but using lower speeds, or with buffering can reach 200-1000m
Commonly used in embedded devices, such as mobile phones, to connect the displays, buttons and lights.
SPI – Serial Peripheral Interface Bus
Developed by Motorola, it’s a “4-wire” bus, comprising serial clock, serial data out, serial data in, chip select.
Full duplex (data can go both ways at once)
Speed can be from 1Mbit/sec to 70Mbit/sec
Used in motherboards, cell phones, embedded devices.
CAN – Controller Area Network
Started as a message based Vehicle Bus standard for automotive applications.
Very high noise immunity
40 metres at 1Mbits/sec or 500m at 125kbits/sec
Now also used for industrial automation and medical equipment , as well as some trucks, tractors, aircraft etc.
PWM – Pulse Width Modulation
Commonly used for DC motor control, aircraft servos,can also control brightness of lights
USB – Universal Serial Bus
What Electronics knowledge do I need?
As little or as much as you want
vs
Basic PIC circuit – RC clock
Basic PIC circuit – ceramic resonator
Basic PIC circuit – ceramic resonator
Interfacing PICs - A to D inputs
Interfacing PICs to sensors, switches … PIC can see a single push of a switch as
multiple presses this is known as “key bounce”, may need to de-bounce
Interfacing PICs to PCs, Phones … Many ways to interface to PC
- USB is one of the easiest- RS232 relatively easy to use, but voltages can be an issue
Phones often have RS232
Putting it all together - project ideas, tips
Is your project idea is possible?
Try the same principles as we do in Agile software development:
- break it down into small do-able pieces- do a research “Spike”, pick a small task- try to create a “Sashimi slice” – a small vertical layer
Putting it all together - project ideas, tips Breadboard
Putting it all together - project ideas, tips
Putting it all together - project ideas, tips Packaging: DIP, DIL, SMD, SOIC …
Putting it all together - project ideas, tips Packaging: DIP, DIL, SMD, SOIC …
Putting it all together - project ideas, tips Ready-made Development Boards:
Putting it all together - project ideas, tips Breadboard + Development Board:
Putting it all together - project ideas, tips Development Boards with Breadboard
Putting it all together - project ideas, tips
Look at projects people have already done, it may not be as much fun, but is it simpler to buy something close?
Putting it all together - project ideas, tips Vehicle OBD-II diagnostics
OBD-II connectors on all new cars, these communicate using CAN protocol …
Vehicle OBD-II diagnostics
See: http://sourceforge.net/projects/priidash/
Vehicle OBD-II diagnostics
See: http://sourceforge.net/projects/priidash/
Tired of phones not doing enough?
See: http://wiki.openmoko.org/wiki/I2C_Pressure_Sensor
Add a sensor that reads temperature and pressure
So … Make your phone work harder!
See: http://wiki.openmoko.org/wiki/I2C_Pressure_Sensor
Add code with some maths + air pressure readings, and get an altimeter as well …
DIY Protection:An Ultrasonic Nerf-Missile launcher.
See: http://www.steffenschuette.de/steffenschuette/elektronik/us2bRadar/
One PIC18F4550, an ultrasonic ranging module and a recycled stepper motor…
DIY Protection:An Ultrasonic Nerf-Missile launcher.
See: http://www.steffenschuette.de/steffenschuette/elektronik/us2bRadar/
DIY CNC Router
See: http://erik-pic-microcontroller.blogspot.com/2010/02/cnc-router-project.html
A PIC microcontroller, open source design, home built hardware
Security – Key Logger
There are numerous projects that might be possible, thoughthe use of them might best beconsidered very carefully!
eg. A key logger with just 3 chipsthat can secretly log keys andthen later retrieve them witha special combination of keys.
Where next
Breadboard + a few components + programmer
or
Pre-made development board
What are the alternatives?
More Information
Go to
http://www.coastnerds.info
for links to this presentation
Join the Coastnerds Facebook group.