0% found this document useful (0 votes)
76 views

Microcontroller Notes

The document provides an introduction to programming the PIC16F84A microcontroller using MikroC. It describes creating a new MikroC project and setting parameters like the microcontroller, frequency, and file path. It then presents three example projects: 1) Blinking all LEDs on PORTB, 2) Gradually turning LEDs on and off on PORTB, and 3) Blinking a single LED connected to pin RB0 by setting it as an output in the TRISB register. The document provides the circuit diagrams and code for each example project.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Microcontroller Notes

The document provides an introduction to programming the PIC16F84A microcontroller using MikroC. It describes creating a new MikroC project and setting parameters like the microcontroller, frequency, and file path. It then presents three example projects: 1) Blinking all LEDs on PORTB, 2) Gradually turning LEDs on and off on PORTB, and 3) Blinking a single LED connected to pin RB0 by setting it as an output in the TRISB register. The document provides the circuit diagrams and code for each example project.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

FIRST STEPS WITH THE PIC 16F84A TROUGH EXAMPLES

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.

Presentation of the interface


We present here the buttons mostly used in this software and their functions.

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

Creation of a new project


During the creation of a new project, a certain number of choices must be operated:
 the name of the project
 the path (it is preferable to create a file in advance where all the files of the
project will be safeguarded)
 name of the PIC to be programmed
 The choice of its frequency of clock
 The activation of certain additional parameters such as the Watchdog, the
protection of code
 The type of oscillator (with quartz, RC circuit …)
 And others.
When we click on the button ` New Project' (2), the following pages appear and all the
parameters mentioned above can be located and adjusted.
Click on ‘Next’

Select the microcontroller and Click on ‘Next’


Select the frequency and Click on ‘Next’

Select the path and give the name of the project


Click on ‘Next’
When all is set according to our own needs, click on 'Finish' to validate. One returns in the
editing page of MikroC. We can now start typing our source code.

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.

/* Declaration of the global variables*/

/* Déclaration of functions if there exist*/

/* main Program*/
void main()
{
/* Initializations*/

/* Definition of an endless loop in which the program must run*/


do
{

/*Program to be carried out*/

} while (1); /*end of the endless loop*/

} /* 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

///////////////// Declaration of global variables //////////////////////////////


///////////////// Declaration of functions //////////////////

///////////////// Main program ////////////////////////////////////


void main()
{
/////// Initializations ///////
TRISB = 0x00; // setting PORTB as output
TRISA = 0x1F; // all PORTA is set as input

/////// Definition of the endless loop //// ///////


do
{
//Program to carry out
PORTB = 0b11111111; // PORTB high
Delay_ms(500); // 500 milliseconds delay
PORTB = 0b00000000; // all PORTB off
Delay_ms(500); // 500 milliseconds delay
} while(1); // endless loop
} // end of the program

Generation of the file of extension .HEX


For that, we will click on "Build Project" (5)

Simulation with ISIS


For the simulation to be effective, the program have to be loaded in our PIC. The procedure is as
follows:
- Double click on the PIC
- set the frequency, and browse to look at the .HEX file from the folder containing ourproject
Project 2: Gradually put ON and OFF the LEDs connected to the PORTB
of the PIC16F84A

- 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 »

///////////////// Declaration des variables globales //////////////////////////////

///////////////// Declaration des fonctions s'il y en a //////////////////

///////////////// Main Program ////////////////////////////////////


void main()
{
/////// Initializations ///////
TRISB = 0x00; // setting all PORTB as output
TRISA = 0x1F; // setting all PORTA as input
PORTB = 0x00; // PORTB is initialized to 0

/////// 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

Project 3: Blink the only LED connected to the RB0 pin


The aim of this project is to show how to target a particular pin and control it. For that
purpose, we will set the concerned pin (RB0) as output. It means that we will attribute the
value zero to the bit corresponding to the concerned pin in the TRISB register.
Therefore, we have:
TRISB = 0b11111110; // RB0 is set as output while the others are inputs
We can also use hexadecimal numbers to configure the TRISB register. Therefore, the
following statement will have the same impact that the previous one:
TRISB =0xFE; // RB0 is set as output while the others are inputs

 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 »

///////////////// Declaration of global variables //////////////////////////////

///////////////// Declaration of functions if they are //////////////////

///////////////// Main Program ////////////////////////////////////


void main()
{
/////// Initializations ///////
TRISB = 0xFE; // RB0 is set as output while the others are inputs
TRISA = 0x1F; // all pins of PORTA set as input

/////// 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

As we can see, the statement to act only on RB0 is :


PORTB.F0 = 1; // RB0 is high or on
In this statement, PORTB shows the port where the concerned pin is located. F0 shows the
pin number on the choosen port. In this case, it is RB0 pin. In order to generalize this
situation, we could have written Fi, whre i can varie from 0 to 7 depending on the port.

Project 4: Testing the logic state of a pin and take a decision


Since the beginning, we have been using ports only as output. In this example, we introduce
the usage of ports as input. An input in a system can be a signal coming from a sensor. In this
project, we will the LED connected to the RB0 pin under the condition that the switch is ON.
In other words, if the switch is closed, the LED will blink. Otherwise, the LED will remain
OFF. According to the diagram of the project, when the switch is closed, a logic 1 is sent to
RA2 pin. When opened, a logic 0 is present at the same pin.
 The diagram

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 »

///////////////// Declaration of global variables //////////////////////////////

///////////////// Declaration of functions if they are //////////////////

///////////////// Main Program ////////////////////////////////////


void main()
{
/////// Initializations ///////
TRISB = 0xFE; // RB0 is set as output while the others are inputs
TRISA = 0x1F; // all pins of PORTA set as input

/////// 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)
}

} while (1); // end of the endless loop


} // end of the program
Project 5: Using bouttons
In this example, we will see how to make use of buttons in a circuit. We can use buttons to
send commands to a system. Signals produced by the action on a button are brief. We must
therefore be tactful to make use of that signal. The signal of a button sometimes generates
ripples. We must look a way to eliminate them. In the MikroC environment, buttons are
managed with a function called Button. The syntax to use that function is as follows:

Button (&PORT, pin Number , ON time, Active state);

&PORT is used to select the port on which the button is connected


pin Number points on the particular pin where the button is connected. It is number whose
value varie from 0 to 7, depending on the port.
ON time is a short integer used to indicate the maximum duration of the action on the button.
It is given in milliseconds
Active state determines the logic state of the targeted pin when the button is pressed.

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 »

///////////////// Declaration of global variables //////////////////////////////

///////////////// Declaration of functions if they are //////////////////

///////////////// Main Program ////////////////////////////////////


void main()
{
/////// Initializations ///////
TRISB = 0x00; // all PORTB set as output
TRISA = 0x1F; // all PORTA set as input

PORTB = 0x00; // initialization of PORTB to 0

/////// Definition of the endless loop in which the program will be permanently running //// ///////
do
{
//Program to be executed

if (Button(&PORTA, 0, 100, 1))


{
PORTB.F0 = 1; // RB0 is high or on
}

if (Button(&PORTA, 1, 100, 1))


{
PORTB.F1 = 1; // RB1 is high or on
}

if (Button(&PORTA, 2, 100, 1))


{
PORTB.F2 = 1; // RB2 is high or on
}

if (Button(&PORTA, 3, 100, 1))


{
PORTB.F3 = 1; // RB3 is high or on
}
// we put off all
if (Button(&PORTA, 4, 100, 1))
{
PORTB.F0 = 0; // RB0 is low or off
PORTB.F1 = 0; // RB1 is low or off
PORTB.F2 = 0; // RB2 is low or off
PORTB.F3 = 0; // RB3 is low or off
}
} while(1); // end of the endless loop
} // end of the program
Project 6: Create and use functions
As we mentioned earlier, a function is a sub-program that we can call and execute at any time
in the main program. MikroC compiler provides many functions, and we have been using
some of them (Delay_ms(), Button()…). For a better use of functions, we propose to realize a
project whose logbook is as follows:
- Initialize the PIC
- Gradually the LEDs connected on PORTB
- Gradually put OFF LEDs on PORTB, starting on the last one
- Blink all the LEDs on PORTB twice.

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 //////////////////////////////

///////////////// Declaration of functions if they are //////////////////


void initialization()
{
/////// Initializations ///////
TRISB = 0x00; // all PORTB set as output
TRISA = 0x1F; // all PORTA set as input
PORTB = 0x00; // put off all PORTB
}

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.

You might also like