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,

Post on 19-Mar-2020

38 Views

Category:

Documents

8 Downloads

Preview:

Click to see full reader

Transcript

Introduction to PIC Microcontrollers

I/O PORTS, Interrupts,

ADC

Department of Electrical & Electronics Engineering, Amrita School of Engineering

PIC16F877A Pin diagram from datasheet

Department of Electrical & Electronics Engineering, Amrita School of Engineering

PIC16F877A

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Typical I/O Port

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Typical I/O Port

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Typical I/O Port

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Typical I/O Port

Department of Electrical & Electronics Engineering, Amrita School of Engineering

DC Electrical Characteristics

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Noise Margin

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Source & Sink Currents

• Source current – coming out of microcontroller

• Sink current – going into the microcontroller

Department of Electrical & Electronics Engineering, Amrita School of Engineering

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

the IC

Department of Electrical & Electronics Engineering, Amrita School of Engineering

High- Impedance Pin

Department of Electrical & Electronics Engineering, Amrita School of Engineering

PORT A

‘Hello World’

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Programming Languages

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Programming Languages

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;

}

Flash an LED

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Flash an LED

R =

Port selection

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++)

{

}

}

}

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++)

{

}

}

Flashing an LED with

pushbutton

Exercise 2

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Flash an LED with pushbutton

R =

Port selection

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;

}

}

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

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Department of Electrical & Electronics Engineering, Amrita School of Engineering

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

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

}

ADC

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

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

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Department of Electrical & Electronics Engineering, Amrita School of Engineering

A/D conversion steps

Department of Electrical & Electronics Engineering, Amrita School of Engineering

ADC conversion

Department of Electrical & Electronics Engineering, Amrita School of Engineering

Mini Project

• Speed control of DC motor using

Potentiometer

top related