28
WELCOME TO PIF PIC COURSE 6 !!!

Lesson1 Htc Pic16f887 En

Embed Size (px)

Citation preview

Page 1: Lesson1 Htc Pic16f887 En

WELCOME TO PIF PIC COURSE 6 !!!

Page 2: Lesson1 Htc Pic16f887 En

CÂU LẠC BỘ NGHIÊN CỨU KHOA HỌC KHOA ĐIỆN-ĐIỆN TỬ PIFĐH BÁCH KHOA TPHCM

TOPIC 2: GPIO

Page 3: Lesson1 Htc Pic16f887 En
Page 4: Lesson1 Htc Pic16f887 En

Làm Mạch

Lí thuyết

(H+C)

Thực hành

(C)Bài tập

(H)

Nâng cao

(H)

-Main Pic-Cài MPLAB,datasheet-Tuts : tạo project, GPIO

- Main Pic- IDE-GPIO

- Nháy Led- Nút nhấn- LCD

- Keypad- Led matrix

- Led 7 segment

-…

Chương trình học ngày 10/3/2011

Page 5: Lesson1 Htc Pic16f887 En

Contents

1. MCUs & PIC16F887

2. C program & IDE MPLAB

3. GPIO in PIC16F887

4. Discussion & Homework

Page 6: Lesson1 Htc Pic16f887 En

Part 1: MCUs & PIC16F887

Page 7: Lesson1 Htc Pic16f887 En

1.1 Why MCU (uC or µC)?

Requirement: a. Blink a LED every 1s b. Turn on a led when button is pushedc. Timer, ADC, UART, Interrupt, SPI, I2C…d. ….

Solution: MCUs- Integrated circuits- Programmable

Page 8: Lesson1 Htc Pic16f887 En

1.2 µC vs. µP

Core: 8b, 16b, 32b,64b

GPIO

Timer

ADC

UART

SPI

µP:8051,PIC,AVR,ARM,…

Peripherals

µC: PIC16F887, LPC1114,

MSP430…

Page 9: Lesson1 Htc Pic16f887 En

1.3 Why PIC16F887?- Simple: hardware & software- Enough: peripherals- “Popular”: picvietnam, dientuvietnam…

Page 10: Lesson1 Htc Pic16f887 En

1.4 Overview of PIC16F887

- Power supply: 2-5.5V- XTAL: max 20Mhz- 8-bit - Harvard architecture (vs. Von Neumann)- RISC (Reduced Instructions Set Computer) (vs. Complexed Instructions Set Computer)- 35 GPIOs- Peripherals: Timers, ADC, USART, SPI, I2C, PWM, EEPROM, …

( Reference: PIC16F887 datasheet)

Page 11: Lesson1 Htc Pic16f887 En

1.4 Overview of PIC16F887

Page 12: Lesson1 Htc Pic16f887 En

1.5 MainPic Board

V C C

J _ P E PO

RT

E

123

V C C

J _ P D

PO

RT

D

12345678

J _ P C

PO

RT

C

12345678

0

0

M C L R

C _ 1 1 1 21 0 4

V C C

R _ R S1 0 K

S S *

0

P V N 1

P I C 1 6 F 8 8 7

234567

3 33 43 53 63 73 83 94 0

1 51 61 71 82 32 42 52 6

1 92 02 12 22 72 82 93 0

1 3

1 4

3 21 1

3 11 2

1

891 0

R A 0 / A N 0R A 1 / A N 1R A 2 / A N 2 / V R E F -/ C V R E FR A 3 / A N 3 / V R E F +R A 4 / T0 C K I / C 1 O U TR A 5 / A N 4 / S S * / C 2 O U T

R B 0 / I N TR B 1R B 2

R B 3 / P G MR B 4R B 5

R B 6 / P G CR B 7 / P G D

R C 0 / T1 O S O / T1 C K IR C 1 / T1 O S I / C C P 2R C 2 / C C P 1R C 3 / S C K / S C LR C 4 / S D I / S D AR C 5 / S D OR C 6 / TX/ C KR C 7 / R X/ D T

R D 0 / P S P 0R D 1 / P S P 1R D 2 / P S P 2R D 3 / P S P 3R D 4 / P S P 4R D 5 / P S P 5R D 6 / P S P 6R D 7 / P S P 7

O S C 1 / C L K I N

O S C 2 / C L K O U T

V D DV D D

V S SV S S

M C L R * / V P P

R E 0 / R D * / A N 5R E 1 / W R * / A N 6R E 2 / C S * / A N 7

S C K

0

S C L

J _ P B

PO

RT

B

12345678

0

S D I

C _ X2 2 2 pV C C

S W _ R S

S W R E S E T

S D AS D O

J _ P A

PO

RT

A

123456

TX

4 M h zXTA L

R X

MAIN MCUPIC16F887

P G C

C _ 3 2 3 11 0 4

C _ X12 2 p

P G D

Page 13: Lesson1 Htc Pic16f887 En

Part 2: C program & MPLAB IDE

Page 14: Lesson1 Htc Pic16f887 En

2.1 C programI want…

Programming in C Compiler

Page 15: Lesson1 Htc Pic16f887 En

2.1 C program

Page 16: Lesson1 Htc Pic16f887 En

2.3 MPLAB IDE (Integrated Development Environment)

- Source code editor- Compiler (Hitech-C is used in this course)- Build tool- Debugger

Page 17: Lesson1 Htc Pic16f887 En

2.4 C program with HiTech PIC: same as standard C but:

// Requirement#include <htc.h>__CONFIG(XT & WDTDIS & PWRTEN & MCLREN & UNPROTECT &

SWBOREN & IESODIS & FCMDIS & LVPDIS & DEBUGDIS); //1st config. Word

__CONFIG(BORV21); //2st config. Word#define _XTAL_FREQ 4000000 //crystal 4Mhz

// Note: new config word__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF

& BOREN_OFF & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF); //1st config. Word

__CONFIG(BOR4V_BOR21V); //2st config. Word

Page 18: Lesson1 Htc Pic16f887 En

Part 3: GPIO IN PIC16F887

Page 19: Lesson1 Htc Pic16f887 En

3.1 Registers & Pins• Register TRISX (X: A, B, C, D, E)

TRISA7=0: A7 is output pinTRISA7=1: A7 is input pin

BIT

register TRISA= 0x00: A is output portTRISA= 0xFF : A is input port

Page 20: Lesson1 Htc Pic16f887 En

3.1 Registers & Pins

RA7 =0: Logic at A7 is “0” =1: Logic at A7 is “1”

BIT

registerPORTA =0x00: port A is in low level =0xFF: port A is in high level

• Register PORTX(X: A, B, C, D, E)

Page 21: Lesson1 Htc Pic16f887 En

3.1 Registers & Pins

• Thanh ghi ANSEL và ANSELH

Page 22: Lesson1 Htc Pic16f887 En

3.2 Procedure to set IO pins//1. ANSEL REGISTERANSELH = 0;ANSEL = 0;//2. TRIS REGISTER --> 0: output pin, 1: input pinTRISE = 0x00; // output port, TRISEx//3. PORT REGISTER --> 1: high; 0: lowPORTE = 0xFF; // port is in high level, REx

Page 23: Lesson1 Htc Pic16f887 En

3.1 Ex: Blink a led

L E D 6

LED

_BU

S1

12345678

R _ L 2 1 k

L E D 8

R _ L 3 1 k

R _ L 4 1 k

L E D 7

L E D 1

V C C

R _ L 5 1 k

R _ L 6 1 k

R _ L 8 1 k

R _ L 7 1 k

L E D 2

LED MODULE

L E D 3

L E D 4

L E D 5

R _ L 1 1 k

Pin: low level --> LED On

Pin: high level --> LED Off

Page 24: Lesson1 Htc Pic16f887 En

3.1 Ex: Blink a led

#include <htc.h>__CONFIG(XT & WDTDIS & PWRTEN & MCLREN & UNPROTECT & SWBOREN & IESODIS & FCMDIS & LVPDIS & DEBUGDIS); //1st config. Word

__CONFIG(BORV21); //2st config. Word

#define _XTAL_FREQ 4000000 //tần số thạch anh 4Mhz// khai báo tần số thạch anh dùng cho lệnh delay

Page 25: Lesson1 Htc Pic16f887 En

3.1 Ex: Blink a led

//Chương trình chínhvoid main(void){

ANSEL=0;ANSELH0; //Tất cả các port là digitalTRISB=0x00; //8 pins PortB là outputPORTB=0xFF; //Xuất mức “1”while(1){

PORTB=0x00;__delay_ms(150); PORTB=0xFF;__delay_ms(150);

};}

Page 26: Lesson1 Htc Pic16f887 En

Part 4: Discussion & Homework

Page 27: Lesson1 Htc Pic16f887 En

Homework: optional

LED 7 Segment LED matrix 8x8 LCD 16x2 Keypad 4x4

Page 28: Lesson1 Htc Pic16f887 En

END