Microcontroller Notes
Microcontroller Notes
IN MikroC
Introduction
Microcontrollers in general and the PICs in particular are components whose real
implementation requires programming. The basic programming language of these
components is the assembly language. Many difficulties related to the programming with this
language (the mastering of the instruction set, the length of the source codes, the
interpretation not obvious of the instructions…) led to the setting-up of the high-level
languages, closer to the human language and easier to handle and understand. Among them,
we enumerate MikroC and CCS (for a programming in of C language), PROTON (BASIC),
MikroPascal (Pascal)…In this small tutorial, we will learn through examples how to program
and simulate a microcontroller with MikroC and Proteus.
7
3
5
6
2 10
1
8
12
11
1 Name of the current file or program
2 ` New Project': allows to create a new project
3 ` Open Project': allows to open an already existing project
4 ` Project' Edict: allows modifying the basic parameters of the project (the PIC name,
the name of the project, the frequency of the PIC actually programmed)
5 ` Build Project': allows to build the project and to generate the file of extension
.HEX. It is the file, which is inserted in the PIC for a real implementation. It is this file
which will be useful to us in simulation
6 ` View Assembly': allows to see the assembly version of the program written
7 Shows the name of the PIC we are programming. Ex 16F84A
8 Shows the frequency of the clock to be used during the realization
9 Represents the zone of edition of the source code
10 ` Help': allows to enter the help of the software MikroC
11 Presents the errors and the warnings relating to the current program. The posted
messages give the nature of the error and its position in the source code or say if the
program is well typed
12 The library manager allows to activate or deactivate some MiKroC’s libraries
Edition of a program
Once the project created we must pass to the programming itself. We will establish in this
paragraph a squeleton which we will follow in the implementation of our source codes. We
can copy and paste this squeleton in a virgin page each time we want to write a new program.
/* main Program*/
void main()
{
/* Initializations*/
} /* fin du programme*/
Let us explore the skeleton above presented
As we can already note it, the comments on a line are given by `// '.When it is for several
lines, we use `/* ' at the beginning and ` */' at the end.
- Below ` Declaration of the variables globales' we will declare the variables to be
used in all the program.
Example:
/ /Declaration of the global variables
unsigned shorts kp, cnt; // integers
tank txt[5 ]; //Table of six characters
float deci; //a floating point Number
- Below ` Declaration of the functions if they exist' we will declare the functions. A
function is block of program which we can write and call in our main program when
the need. We will that option later
- Below ` Initializations' we will introduce all that relates to the initialization of the
PIC. For example to define which ports will be used and how their pins will be used
(as input or as output) and the initial state of the port
- In the infinite loop "do{}while(1)" we will put the program to be carried out by the
PIC.
- Our first project: To Blink all LEDs connected to the PORTB of the
microcontroller PIC16F84A
We start with the circuit
- See the file "clignote PORTB" of the folder "CIRCUITS ISIS et programmes"
-
- For the program see the same folder
- The diagram
We still consider the diagram of the previous project.
Look at the file « clignote PORTB » of the folder « CIRCUITS ISIS et programmes »
- The program
Look at the file « Allume_eteint » in the folder « CIRCUITS ISIS et programmes »
/////// Definition of the endless loop in which the program will be running permenently //// ///////
do
{
//Program to be executed
// Sequence of lighting
PORTB = 0b00000001; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000011; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00001111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00011111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b01111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b11111111; //
Delay_ms(500); // 500 milliseconds break
// Sequence lighting
PORTB = 0b11111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b01111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00011111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00001111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000011; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000001; //
Delay_ms(500); // 500 milliseconds break
} while(1); // end of the endless loop
} // end of the program
The diagram
Let’s simplify the circuit
See the file « clignote une LED sur RB0» of the folder « CIRCUITS ISIS et
programmes »
The Program
See the project located in the sub folder « ClignoteLEDsurRB0 » of the folder
« CIRCUITS ISIS et programmes »
/////// Definition of the endless loop in which the program will be permanently running //// ///////
do
{
//Programme to be executed
PORTB.F0 = 1; // RB0 is high or on
Delay_ms(500); // 500 milliseconds break
PORTB.F0 = 0; // RB0 is low or off
Delay_ms(500); // 500 milliseconds break
} while(1); // end of the endless loop
} // end of the program
See the file « Teste RA2» in the folder « CIRCUITS ISIS et programmes »
The Program
See the file in the sub folder « Teste RA2 » of the folder « CIRCUITS ISIS et
programmes »
/////// Definition of the endless loop in which the program will be permanently running //// ///////
do
{
//Programme to be executed
if(PORTA.F2 = 1) // Testing if the switch is ON
{
// blinking
PORTB.F0 = 1; // RB0 is high or on
Delay_ms(500); // 500 milliseconds break
PORTB.F0 = 0; // RB0 is low or off
Delay_ms(500); // 500 milliseconds break
}
else
{
PORTB.F0 = 0; // put RB0 off if RA2 is at logic 0 (switch off)
}
In the actual project, buttons are placed on all the pins of the PORTA and the LEDs on some
pins of PORTB. An action on a button will ON the corresponding LED on PORTB and an
action on the last button will put OFF all the LEDs ON. In fact, we will have:
- RA0 sets ON RB0
- RA1 sets ON RB1
- RA2 sets ON RB2
- RA3 sets ON RB3
- RA4 puts OFF all
- The diagram
See the file « Bouton circuit» in the folder « CIRCUITS ISIS et programmes »
See the file « Bouton circuit » of the folder « CIRCUITS ISIS et programmes »
- The program
Voir le projet contenu dans le sous dossier « UseBouton » du dossier « CIRCUITS
ISIS et programmes »
/////// Definition of the endless loop in which the program will be permanently running //// ///////
do
{
//Program to be executed
We observe that this project integrate almost all the functionalities developed in the previous
projects. For each step of the project, we will define functions that we will call when need be.
The syntax to create a function without parameters is as follows:
void name_of_the_function(parameters)
{
// statements of the function
}
NB : all the other forms of functions can still be created in MikroC
A function is defined out of the main function or program. In the skeleton of a MikroC
program, we have shown where functions can be defined. To execute a function in the main
program, especially a function without parameters, we just need to write the name of the
function, followed opened and closed brackets then with a semicolon (;).
Example
Nom_de_la_fonction() ;
- The diagram
Let’s consider our very first diagram
See the file « clignote PORTB» of the folder « CIRCUITS ISIS et programmes »
- The program
See the project in the sub folder « Fonctions » of the folder « CIRCUITS ISIS et
programmes »
///////////////// Declaration global of variables //////////////////////////////
void blink()
{
PORTB = 0b11111111; // all PORTB on
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000000; // all PORTB off
Delay_ms(500); // 500 milliseconds break
}
void lighting()
{
// Sequence of lighting
PORTB = 0b00000001; // first pin of PORTB on and all others off
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000011; // first and second pin of PORTB on and all others off
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00001111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00011111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b01111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b11111111; //
Delay_ms(500); // 500 milliseconds break
}
void extinction()
{
// Sequence of extinction
PORTB = 0b11111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b01111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00111111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00011111; //
Delay_ms(500); // Pause de 500 millisecondes
PORTB = 0b00001111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000111; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000011; //
Delay_ms(500); // 500 milliseconds break
PORTB = 0b00000001; //
Delay_ms(500); // 500 milliseconds break
}
///////////////// Main Program ////////////////////////////////////
void main()
{
initialization(); // calling the function for initialization
/////// Definition of the endless loop in which the program will be permanently running //// ///////
do
{
//Program to be executed
lighting();
extinction();
blink();
blink();
} while(1); // end of the endless loop
} // end of program
All the projects realized up to now are based on the PIC16F84 or PIC 16F84A. All the
operations done can be achieved using other PIC microcontrollers such as PIC16F877,
PIC16F876… however PIC16F877 and PIC16F876 have additional internal modules such as:
- The Analogue to Digital Converter
- The USART module
- The I2C module
- …
We did not make use of then in the previous projects, but many more examples are available
in the MikroC help.