Top Banner
Introduction to PIC Microcontrollers I/O PORTS, Interrupts, ADC
38

Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Mar 19, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Introduction to PIC Microcontrollers

I/O PORTS, Interrupts,

ADC

Page 2: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

PIC16F877A Pin diagram from datasheet

Page 3: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

PIC16F877A

Page 4: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Typical I/O Port

Page 5: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Typical I/O Port

Page 6: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Typical I/O Port

Page 7: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Typical I/O Port

Page 8: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

DC Electrical Characteristics

Page 9: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Noise Margin

Page 10: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Source & Sink Currents

• Source current – coming out of microcontroller

• Sink current – going into the microcontroller

Page 11: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Fan-out • Number of inputs that can be driven without damaging

the IC

Page 12: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

High- Impedance Pin

Page 13: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

PORT A

Page 14: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

‘Hello World’

Page 15: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Programming Languages

Page 16: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Programming Languages

Page 17: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

C Programming /* C Program Structure

Sample Program */

# include <pic.h> // Directives

# define _XTAL_FREQ 4e6

unsigned int i ; // Declarations – data types

char H;

Void main () // function

{

command;

command;

}

Page 18: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Flash an LED

Page 19: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Flash an LED

R =

Port selection

Page 20: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Flash an LED /* Flashing LED */

# include <pic.h> // Directives

unsigned int i ; // Declarations – data types

void main()

{

TRISB0=0;

while(1)

{

RB0=0;

for(i=1;i<=50;i++)

{

}

RB0=1;

for(i=1;i<=50;i++)

{

}

}

}

Page 21: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Flash an LED /* Flashing LED */

# include <pic.h>

unsigned int i ;

void delay(int);

void main()

{

TRISB0=0;

while(1)

{

RB0=0;

delay(100);

RB0=1;

delay(100);

}

}

void delay(int d)

{

for(i=0; i<=d;i++)

{

}

}

Page 22: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Flashing an LED with

pushbutton

Exercise 2

Page 23: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Flash an LED with pushbutton

R =

Port selection

Page 24: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Flash an LED with pushbutton /* Flashing LED with pushbutton in another port */

# include <pic.h>

void main()

{

TRISB=0;

TRISC=1;

while(1)

{

RB0=0;

if(RC0)

RB0=~RB0;

}

}

Page 25: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Interrupts

Page 26: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Interrupts

Suspends the execution of main program and a jump executed to an

interrupt service subroutine that takes some action and then returns

back

External resources and internal resources

External – switches, sensors, ADCs

Internal – Timers, CCP

Register used in control and status - INTCON

Page 27: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Page 28: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Page 29: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Flash an LED using Interrupts

Port selection

Port B RB0 – external interrupt pin

Port C – LED1

Port D – LED2

Page 30: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

#include<pic.h>

#define LED1 RB1 //Create meaningful

names for pins

#define LED1_TRIS TRISB1

#define LED2 RB2

#define LED2_TRIS TRISB2

int i;

void main()

{

LED1_TRIS = 0; //LED1 is an output

LED2_TRIS = 0; //LED2 is an output

INTF = 0; //reset the external interrupt flag

INTEDG = 1; //interrupt on the rising edge

INTE = 1; //enable the external interrupt

GIE = 1; //set the Global Interrupt Enable

LED2 =0;

while(1)

{

LED1 = 1; //Flash LED1

for(i=0;i<50;i++)

{

}

LED1 = 0;

for(i=0;i<50;i++)

{

}

}

}

void interrupt isr ()

{

INTF = 0; //reset the interrupt flag

LED2 =1; //flip the bit

}

Page 31: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

ADC

Page 32: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

ADC

ADC module has up to eight analog inputs

Results in 10-bit digital number

Has four registers

A/D Result High Register ADRESH

A/D Result Low Register ADRESL

A/D Control Register0 ADCON0

A/D Control Register1 ADCON1

ADCON0 – controls the operation of the module

ADCON1 – configures the functions of the port pins

Page 33: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

A/D Result Registers • ADRESH:ADRESL is the location of 10-bit A/D result which is 16-

bits wide

• A/D Format select bit ADFM controls the justification to select 10-

bits

Page 34: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Page 35: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Page 36: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

A/D conversion steps

Page 37: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

ADC conversion

Page 38: Introduction to PIC Microcontrollers I/O PORTS, Interrupts ...eeeforum.weebly.com/uploads/1/0/2/5/10254481/pic_io_adc.pdf · Introduction to PIC Microcontrollers I/O PORTS, Interrupts,

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Mini Project

• Speed control of DC motor using

Potentiometer