Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

ACT3: TIMERS AND

DELAYS

MICROCONTROLLERS
FJ22

PREPARED BY

LARITZA MÁRQUEZ CRISTANCHO


A01702253

Introduction
The following report will detail the process elaborated to comply with activity 3 of
Timers and delays. It was to be reused in PIC16F877A. This microcontroller has a lot of
configuration and ports that allow it to develop the activity.

The PIC16F877A is a microcontroller manufactured by microchip that has 40 pins, making it


ideal for prototyping on a breadboard. Inside it has a FLASH type memory which means that
we can reprogram this microcontroller for a large number of times.

This microcontroller works with a RISC architecture at an operating frequency that can reach
up to 20 MHz. For the PIC16F877A there are 35 instructions to be able to program your
algorithms inside the memory of this microcontroller and carry out your project.

The PIC16F877A is used to develop projects in which you have to use several external
elements for its operation, this can be done thanks to the large number of pins it has.

Figure 1. PIC16F887A

The PIC16F877A microcontrollers within its 40 pins have 5 ports available, port A, B, C, D
and E. In port A you can find the pins that can be used as analog inputs which are RA0,
RA1 and RA2. Port A of the PIC16F877A microcontroller occupies 6 of the 40 pins that this
microcontroller provides, that is, it goes from A0 to A5.

Port B on the other hand makes use of 8 pins, this port goes from B0 to B7 being B0 the port
for the use of programmed interrupts. Like port B, port C has 8 pins, it goes from C0 to C7.
Port C is the most used since it is where the pins for the PWM and for the UART
communication with its RX and TX pins are located.
D, like C and B, has 8 pins that we can use. And finally, port E, which is where the rest of the
pins that accept analog inputs are located, has only 3 pins. The remaining pins are used for
the connection of the external crystal and the power supply of the PIC16F877A. Figure 2

Figure 2. Schematic diagram of PIC16F877A, showing its ports

In this activity you will have two inputs that will be activated by a push button. One of them
will be a tigger and the other a reset. They will be connected as shown Figure 3

Figure 3. Circuit diagram of reset button and tigger button.


The oscillator will be connected to a 20MHz quartz crystal between pins OSC1 and
OSC2 as well as a pair of 15pF ceramic capacitors between OSC1 and ground and another
between OSC2 and ground.
Every microcontroller requires a circuit that indicates at what speed it should work. This
circuit is known as a frequency oscillator. In the case of the PIC16F884A microcontroller, pin
13 and pin 14 are used to enter the clock frequency.
There are microcontrollers that have their oscillator internally and do not require small
external electronic circuits. The way that I used the microcontroller requires an external
oscillator circuit.

Some oscillators are:

● Oscillator type "XT" (XTal) for frequencies no higher than 4 Mhz

● LP" type oscillator (Low Power) for frequencies between 32 and 200 Khz.

This mode is intended to work with a lower frequency crystal, which, as a


consequence, will cause the PIC to consume less current.

● HS" type oscillator (High Speed) for frequencies between 4 and 20 MHz

This configuration should be used when using crystals larger than 4 MHz. The
connection is the same as for a normal crystal.

● TTL oscillator

This type of oscillator is based on a crystal that contains all the circuitry to generate a
square wave.These crystal types are specially designed for TTL technologies.

Figure 4. Circuit diagram of the oscillator.


A very important component is the 7-segment display that is an opto-electronic device for
displaying numbers from 0 to 9. There are two types of display, common cathode and
common anode. It is important to mention that 7-segment displays, since they are built with
LEDs, require a maximum current. In other words, a resistor is required to limit the current.
This resistance depends on the current to be supplied to the LED as well as the voltage
drop.

Figure 5. seven- segment display

Figure 6. Schematic representation of seven-segment display.

For this activity it is necessary to obtain a truth table that will allow the programming of the
LEDs to be turned on at the desired time, which in this case will be the numbers from 0 to 9.
Figure 7. Configuration led by number.

A vital component for the development of this activity is the PickIt3, which is
Microchip's simplest and lowest cost debugging and programming tool. It is fully supported
by the MPLAB IDE and has a simple USB Full Speed connection to the computer that not
only allows programming and debugging but also updates the internal firmware of the
PICKIT3. [2]

Figure 8. Pickit3

Another important tool is MPLAB IDE (Integrated Development Environment) is a


professional software implemented by the company Microchip, compatible with XP, Vista,
and Windows 7 and with recent versions available for MAC and Linux. MPLAB IDE is used
as a powerful auxiliary for the development of systems based on PIC microcontrollers. Its
distribution is free and free of charge and can be downloaded directly from the Microchip
site.
Methodology
The first thing I worked out was the code. I used conditionals, counters and arrays. Figure 9

Figure 9. Code compilation in MPLAB.

To determine the settings of the 7-segment display in the program I used the following table

Table 1. Common anode 7 segment display


Then, I elaborated the simulation

Figure 10. Simulation circuit of the oscillator.


Figure 11. Simulation of the circuit of the leds.
Figure 12. Simulation of the 7 segments display.
Figure 13. Simulation of the reset button and tigger button .
Figure 14. Simulation of the whole circuit.
Verified the simulation the physical implementation was made in hardware. First are connections of the inputs (trigger and reset button), they
are presented as push buttons. It is the reset that will be connected to the port RC2 that is pin 17 and the tigger button is connected in port
RC3 that is pin 18 . Also these push button are connected to 74LS14 trigger inverters that send a high pulse when the push button is pressed
and the same happens with the reset button.

Figure 15. Reset button circuit and tigger button


The next sub-circuit is the oscillator formed by the quartz crystal and 15pF capacitors and the crystal has a 20MHz.

Figure 16. Oscillator circuit.


Figure 17. Connections to the microcontroller. PIC16F877A
Figure 18. Connections of 7 segment display.

Finally, connections were made to the PIC16F877A at 5 V voltage from an Arduino Uno.
Figure 19. Circuit
implementation.
Results
The results obtained are the simulation of the circuit in proteus, which can be appreciated in
the following link:

https://drive.google.com/file/d/1Xk4iZtmqHH4KmeT3VZjw1-_eob2PfH1T/view?usp=sharin
g

Likewise, the circuit is implemented in hardware:

https://youtu.be/fbqgFlzPRKA
Conclusions
This activity allowed me to remember the structure of counters, for and while cycles. At the
beginning I had a hard time programming but thanks to it I have mastered a little more
programming. On the other hand, I had a hard time with the implementation because of the
configuration of the bits of the 7-segment display. Because I had never physically connected
one, I finally managed to display on the screen what I wanted. The most important thing of
the practice was the oscillator, finally I could understand why it is used and what role it plays
in the circuit. It tells the microcontroller at what speed it should work.
References

[1]Microchip PIC16F877A. (2022). Retrieved 7 April 2022, from


https://microcontroladoress.com/pic/pic16f877a/

[2]Práctica 1: Manejo de puertos y retardo. (2014). Retrieved 7 April 2022, from


https://jonathandiarte1711.wordpress.com/2014/04/07/practica-1-manejo-de-puertos-y-retard
o-2/

[3] Dualtronica.(n.d) Retrieved 7 April 2022, from


https://dualtronica.com/microcontroladores/111-programador-pickit-3.html#:~:text=Descripci
%C3%B3n-,El%20PICKit%203%20es%20la%20herramienta%20de%20depuraci%C3%B3n
%20y%20programaci%C3%B3n,del%20firmware%20interno%20del%20PICKIT3

[4]MPLAB-IDE-SOFTWARE-DE-DESARROLLO-DE-MICROCHIP-PARA-PIC-18F2550-PUN
TO-FLOTANTE-S.A. (2022). Retrieved 7 April 2022, from
https://www.puntoflotante.net/mplab.htm

[5]Osciladores para microprocesadores o microcontroladores - Electrónica Unicrom. (2015).


Retrieved 7 April 2022, from
https://unicrom.com/osciladores-para-microprocesadores-o-microcon/

[6] (2022). types of oscillator used in microcontrollers.Retrieved 7 April 2022, from


https://microcontrollerslab.com/oscillator-types-microcontrollers/
Appendix A
Programming Code

/*
* File: Act_3.c Rocket Countdown
* Author: Laritza Márquez A01702253
* Created on 29 de marzo de 2022, 01:15 PM
*/

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data
EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write
protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code
protection off)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#define _XTAL_FREQ 20000000 //Crystal oscillator at 20 MHz

#define Delay_leds 1500


#define Green_LED RC1
#define Red_LED RC0

// 9 8 7 6 5 4 3 2 1 0
int display[10] =
{0b10010000,0b10000000,0b11111000,0b10000010,0b10010010,0b10011001,0b1011000
0,0b10100100,0b11111001,0b11000000}; //array of the numbers from 9 to 0 for 7-segment
display common anode
int i; //counter

//int display[10] =
{0b10010000,0b10000000,0b11111000,0b10000010,0b10010010,0b10011001,0b1011000
0,0b10100100,0b11111001,0b11000000}; //array of the numbers from 9 to 0 for 7-segment
display common anode

//int display[10] =
{0b10010000,0b10010000,0b10010000,0b10010000,0b10010000,0b10010000,0b100100
00,0b10010000,0b10010000,0b10010000};

void init_ports () { // function for INITIALIZE PORTS

TRISB = 0b00000000; // ALL THE PORTS ARE OUTPUTS


TRISC = 0b00011100; // RC0, RC1 ARE OUTPUT BUT RC2/RC3 ARE INPUTS

PORTB = 0b00000000; //Initialise PORTB


PORTC = 0b00000000; //Initialise PORTC

/*TRISA = 0b00000000; // INPUTS


TRISD = 0b00000000; // outputs
TRISE = 0b00000000;*/
}

void displayseg(){ //function for 7-segment display which shows all numbers from 9 to 0

for (i=0; i<10; i++){

PORTB= display[i];
__delay_ms(1000);

void letter(){ //function for 7-segment display which shows letter c


// RB7=0 RB0 RB3 RB4 RB5
PORTB = 0b11000110; // letter c
__delay_ms(500);
PORTB = 0b11111111; // letter c
__delay_ms(500);

void leds (){ //function for toggles two LEDs

while (PORTCbits.RC2 == 0){

Red_LED = 1;

Green_LED = 0;

__delay_ms(Delay_leds);

Red_LED = 0;

Green_LED = 1;

__delay_ms(Delay_leds);
}
}

void Reset (){ //function of reset


PORTC = 0b00000000;
PORTB = 0b11111111;
__delay_ms(Delay_leds);
}

void main (){


init_ports();
letter();
if(PORTCbits.RC3 == 1 ){
displayseg();
leds();
}
else if( PORTCbits.RC3 == 0) {

Reset();
}
}

Appendix B

Proteus simulation
https://drive.google.com/drive/folders/1N-WindTYsGC3NYTRPxIV02V5IWd_OeiI?usp=sha
ring

You might also like