02-01-2013, 10:37 AM
AN INTRODUCTION TO PROGRAMMING THE MICROCHIP PIC IN CCS C
Introduction
Why use C?
The C language was development at Bell Labs in the early 1970’s by Dennis
Ritchie and Brian Kernighan. One of the first platforms for implementation was
the PDP-11 running under a UNIX environment.
Since its introduction, it has evolved and been standardized throughout the
computing industry as an established development language. The PC has
become a cost effective development platform using C++ or other favored
versions of the ANSI standard.
C is a portable language intended to have minimal modification when
transferring programs from one computer to another. This is fine when working
with PC’s and mainframes, but Microcontrollers and Microprocessors are
different breed. The main program flow will basically remain unchanged, while
the various setup and port/peripheral control will be micro specific. An
example of this is the port direction registers on a PICmicro®MCU are set
1=Input 0=Output, whereas the H8 is 0=Input and 1=Output.
The use of C in Microcontroller applications has been brought about by
manufacturers providing larger program and RAM memory areas in addition
to faster operating speeds.
An example quoted to me – as a non believer – was: to create a stopclock
function would take 2/3 days in C or 2 weeks in assembler.
‘Ah’ I hear you say as you rush to buy a C compiler – why do we bother to
write in assembler? It comes down to code efficiency – a program written in
assembler is typically 80% the size of a C version. Fine on the larger program
memory sized devices but not so efficient on smaller devices. You pay the
money and take you PIC!!
PC Based vs. PICmicro®MCU Based Program
Development
Engineers starting development on PC based products have the luxury of
basic hardware pre-wired (i.e., keyboard, processor, memory, I/O, printer and
visual display (screen)). The product development then comes down to writing
the software and debugging the errors.
Those embarking on a PIC based design have to create all the interfaces to
the outside world in the form of input and output hardware.
A PC programmer could write the message “Hello World” and after compiling,
have the message displayed on the screen. The PIC programmer would have
to build an RS232 interface, set up the comm. port within the PIC, and attach
the development board to a comm. Port on a PC to enable the message to
be viewed.
‘Why bother’ I hear you say (and so did I). It comes down to portability of the
end product. If we could get the whole of a PC in a 40 pin DIL package
(including monitor and keyboard) we would use it; today’s miniaturization does
not reach these limits.
Product Development
Product development is a combination of luck and experience. Some of the
simplest tasks can take a long time to develop and to perfect in proportion to
the overall product – so be warned where tight timescales are involved.
To design a product one needs: time – peace and quiet – a logical mind and
most important of all a full understanding of the requirements.
I find the easiest way to begin any development is to start with a clean sheet
of paper together with the specification or idea.
Start by drawing out a number of possible solutions and examine each to try to
find the simplest and most reliable option. Do not discard the other ideas at
this stage as there are possibly some good thoughts there.
Microcontrollers
The PICmicro®MCU, on the other hand, is a Microcontroller and has all the
CPU, memory, oscillator, watchdog and I/O incorporated within the same
chip. This saves space, design time and external peripheral timing and
compatibility problems, but in some circumstances can limit the design to a set
memory size and I/O capabilities.
The PIC family of microcontrollers offers a wide range of I/O, memory and
special functions to meet most requirements of the development engineer.
You will find many general books on library shelves exploring the design of
microcontrollers, microprocessors and computers, so the subject will not be
expanded or duplicated here other than to explain the basic differences.
Why use the PIC
Code Efficiency The PIC is an 8 bit Microcontroller based on the Harvard
architecture – which means there are separate internal busses for memory and
data. The throughput rate is therefore increased due to simultaneous access
to both data and program memory. Conventional microcontrollers tend to
have one internal bus handling both data and program. This slows operation
down by at least a factor of 2 when compared to the PICmicro®MCU.
Safety All the instructions fit into a 12 or 14 bit program memory word.
There is no likelihood of the software jumping onto the DATA section of a
program and trying to execute DATA as instructions. This can occur in a non
Harvard architecture microcontroller using 8-bit busses.
Instruction Set There are 33 instructions you have to learn in order to write
software for the 16C5x family and 14 bits wide for the 16Cxx family.
Each instruction, with the exception of CALL, GOTO or bit testing instructions
(BTFSS, INCFSZ), executes in one cycle.
Trying and Testing Code
Getting to grips with C can be a daunting task and the initial outlay for a C
compiler, In Circuit Emulator and necessary hardware for the PIC can be
prohibitive at the evaluation stage of a project. The C compiler supplied on
this disk was obtained from the Internet and is included as a test bed for code
learning. Basic code examples and functions can be tried, tested and viewed
before delving into PIC specific C compilers which handle I/O etc.
C Coding Standards
Program writing is like building a house – if the foundations are firm, the rest of
the code will stack up. If the foundations are weak, the code will fall over at
some point or other. The following recommendations were taken from a C++
Standards document and have been adapted for the PIC.
Basics
All computer programs have a start. The start point in Microcontrollers is the
reset vector. The 14 bit core (PIC16Cxx family) reset at 00h, the 12 bit core
(PIC16C5x and 12C50x) reset at the highest point in memory – 1FFh, 3FFh, 7FFh.
The finish point would be where the program stops if run only once e.g. a
routine to set up a baud rate for communications. Other programs will loop
back towards the start point such as traffic light control. One of the most
widely used first programming examples in high level languages like Basic or C
is printing ‘Hello World’ on the computer screen.
Using C and a PC is straightforward as the screen, keyboard and processor are
all interconnected. The basic hooks need to be placed in the program to link
the program to the peripherals. When developing a program for the PICmicro®
MCU or any microprocessor / microcontroller system, you need not only the
software hooks but also the physical hardware to connect the micro to the
outside world. Such a system is shown below.