A PIC microcontroller is programmed by converting the required task into an algorithm, writing it as source code, compiling it into machine code, and uploading it to the PIC as firmware. The PIC then repeatedly reads inputs, processes data, and controls outputs according to those instructions.
The Programming Process
-
Define the task and system requirements. Identify the required inputs, processing decisions, and outputs. For example, a temperature sensor provides an input, the PIC compares the reading with a threshold, and a fan is switched on as the output.
-
Develop an algorithm. Represent the control sequence using a flowchart or pseudocode. The algorithm should include sequencing, selection such as
IF...THEN, and iteration such as a continuous loop. -
Configure the hardware. Assign PIC pins as inputs or outputs. Analogue sensor signals may require the PIC's analogue-to-digital converter (ADC), while actuators may need driver circuits because a PIC pin cannot safely supply large currents.
-
Write the source code. Code is created in an integrated development environment (IDE) using a language such as C or assembly language. A simplified control structure is:
initialize input and output pins
repeat forever
read sensor
if sensor value exceeds threshold
switch fan on
else
switch fan off
end repeat
-
Compile and upload. A compiler translates the source code into machine code. A programmer or suitable programming interface transfers this code into the PIC's non-volatile program memory.
-
Test and refine. The designer checks the system under normal, boundary, and unexpected conditions. Errors may require debugging the code, recalibrating thresholds, or modifying the electronic circuit.
A common misconception is that the PIC understands C code directly. It executes only machine-code instructions produced by the compiler.
IB Exam Technique
For B3.4 HL questions, structure answers around the input, process, output model. Name the algorithm, source code, compiler, firmware upload, and testing stages, and apply each stage to the specific electronic system in the question.
