Programming PIC microcontrollers in C: 

Course summary: 


View module summary :

-1- -2- -3- -4- -5- -6- -7- -8- -9- -10- -11-

Module summary

Module1

Main concept: C program structure

This module examines the structure of a C program and shows you how to control the input and output ports of the microcontroller.

The first program is a simple one (flashing an LED) - it's deliberately kept simple so that it is easy to get going.  

Because it is simple it lets you concentrate on the interfaces needed to get it going.
C program structure.

Module2

Main concept: individual bit control

This module shows you one of the most important control methods i.e. how to control individual bits in a port.

This is demonstrated on the hardware by flashing the LED using individual bit control.

It also demonstrates using ports as inputs or outputs.  PORTA is used as an input port and PORTB is used as an output port.  Since LEDs are connected to PORTB and input buttons are connected to pins of PORTA you can immediately see the program operating.

Note: Port directions are controlled using registers TRISA and TRISB.

Also included in this module is the second video that shows how to compile using MikroC and in addition the reference manual is presented for download.
Controlling individual port bits.

Module3

Main concepts: variables and conditionals

This module discusses variables and how to choose them with reference to memory usage.

In addition it introduces conditionals which let you use the microcontroller to make decisions.

To demonstrate the conditionals an if-else construct and a variable are used to flash the LED.  This demonstrates using C to do the same task as the previous module but using a different method.

The second program uses the switch statementto change the update rate depending on the number of keys pressed i.e. it changes how fast the led flashes depending on which keys are pressed.

 It shows
use of the C  switch and if structures demonstrating their use on the hardware.
Variables types and Conditional statements.

Module4

Main concept: while loops and arrays

This module examines the 'while' loop which is used to develop the basic keypress detection Key debouncing is also done inside the keypress loop.

This module also discusses arrays which are then used to map a random number to the correct LED sequence for displaying the dice digits.  

The module shows how to map the internal representation to the PORTB LEDS.

These concepts are shown for real by running the code on the hardware.

The module also discusses the do-while loop and the break statement to exit from a loop.
Dice project using arrays and while loops.

Module5

Main concepts: functions, for loops, increment and decrement

This module introduces the concept of functions that let you split up complex programs into more manageable parts.

It also introduces the simple increment (add one) and decrement (subtract one) operators.

It then looks at 'for loops' which are used to simulate rolling the dice and demonstrates these concepts on the hardware.
Tidying up the dice project: Functions.

Module6

Main concept: RS232 port and strings

This module shows you how to set up an RS232 connection to communicate between the PIC and the PC.

First of all it covers methods of using string data and how you can format that data i.e. to convert a variable into its string representation (essential for outputting data from your PIC to the PC).

The first program uses the string formatting information to create  the "Hello World" program and output a number to the PC terminal showing how to send data to the PC.

The second program shows you how you can loopback the RS232 port so that anything typed at the PC terminal is sent from PC to PIC to PC.
Talking to the PC: RS232.

Module7

Main concept: local statics and scope

This module shows you how to create a local function variable that is not lost when the function is finished (local static variables).

It also describes the rules of visibility of variables or scope.

To demonstrate local static operation the value of a local static is increased every keypress and it's value output to the serial port.

Also included in this module is information on compact operators (a short hand way of writing C mathematical operations).
Local statics
and compact operators.

Module8

Main concept: State Machines

This module shows you how to make up your own types using enumerations (numbers!).

These are then used to create a state machine which is described in detail.

After this the dice project is converted to use a state machine so you can see exactly how a state machine works.

You then program the dice hardware to see the state machine operation.

State machines are useful as they let you create complex operations by first describing the operations visually (state machine diagram).

The visual state machine diagram is much easier than C code to read so you will be able to see how the code is going to work.  In addition the visual state machine is a useful way of documenting how the code is working.
Enumerated Types, typedef and Preprocessing.

Module9

Main concept : passing multiple arguments

This module discusses how to transfer more than one variable into or out of a function using pointers.

It goes over what a pointer is and what it can be used for and then goes on to compare two functions one using normal arguments and one using pointer arguments (passed by reference).

As well as outputting the result of these functions to the RS232 port - to show real operation the module also reads two ADC input pins (using pointer functions) and outputs theses values to the RS232 port.

In addition a simple technique of using VT100 commands to Hyperterminal is used to stop the screen scrolling continuously as data is displayed.
Pointers and pass by reference.

Module10

Main concept :  structures and PWM

This module shows you how to use struct types to store more than one variable in a group object.  

The reason for this is that it lets you group variables that are related to each other or which have a common purpose into a single entity.  You can then easily refer to each variable in the group.

It also shows how to use bit fields in a structure definition to save RAM space.

Again the RS232 port is used to show real variable output data at the hardware in this case from the bit field and from the structure variable.

The first part of the curve tracer is constructed using two opamps and the output is read back by the ADC and displayed at the RS232 interface.

Detailed information is given on using and understanding the PWM internal peripheral.
Structures & PWM.

Module11

Main concept : Multiple file structure

This module shows you how to split a project into sub-blocks using multiple files.  It discusses header files necessary to make this work.

As part of the header file discussion the module also introduces prototypes which are necessary for header file operation.

In addition casting is presented (or how to make the compiler do what you want it to do!).

The rest of the circuit for the curve tracer is presented and the PC software to show the graph is available for download.
Multiple project file structure.






footer for microcontroller page