7

Click here to load reader

Designing with a PIC Microcontroller - College of … Microcontroller.pdf · Designing with a PIC Microcontroller (Updated: Feb 21, 2010) ... (i.e. simulating a dimmer action). The

Embed Size (px)

Citation preview

Page 1: Designing with a PIC Microcontroller - College of … Microcontroller.pdf · Designing with a PIC Microcontroller (Updated: Feb 21, 2010) ... (i.e. simulating a dimmer action). The

University of Saskatchewan 3-1 EE 392 Electrical Engineering Laboratory III

Designing with a PIC Microcontroller (Updated: Feb 21, 2010)

Objectives: This lab provides an opportunity to learn more about the use of a microcontroller (MCU) and its interfacing to other external devices. The lab uses the PIC16F886 microcontroller. It has a number of features that are useful for control and monitoring applications. Study the data sheet and sample application notes to become familiar with its popular features. The lab has two parts. In part 1, the MCU reads an analog voltage across a potentiometer and displays the digital voltage on two 7-segment displays. In part 2, the use of hardwired pulse width modulation (PWM) is explored. Assembly language will be used to program the 16F886 MCU. Preparation: The connection diagram is given in Fig 1. Take a moment to read the diagram and understand the connections. You should already be familiar with the architecture and instruction set of the 16F886. Refer to EE331 course notes for more information. To facilitate the lab experiment, a code template to initialize the analog to digital converter (ADC), 7-segment display, and PWM module have been posted on the course website. You should review the code and note the entry points to the subroutines. Procedure: Part 1 Analog to Digital Conversion PIC16F886 has a powerful 8-channel 10-bit ADC (Fig. 2) that is controlled by two special function registers (SFR): ADCON0 and ADCON1. The result of the conversion is placed into ADRESH and ADRESL. Other SFRs that have an important impact are TRISA and TRISC (used to configure the data flow).

Safety The activity prescribed in this laboratory will be conducted in an environment where hazardous electrical potentials exist. The student should be aware of normally expected electrical laboratory hazards and follow procedures to minimize risk. Please refer to general safety precautions in the Laboratory Manual and those posted in the labs. The voltages used in this experiment are less than 10 V and normally do not present a risk of shock. However, you should always follow safe procedures when working on any electronic circuit. Assemble or modify a circuit with the power off or disconnected. Don’t touch different nodes of a live circuit simultaneously, and don’t touch the circuit if any part of you is grounded. Do not touch a circuit if you have a cut or sore that might come in contact with a live wire. Check the orientation of polarized capacitors before powering a circuit, and remember that capacitors can store charge after the power is turned off. Never remove a wire from an inductor while current is flowing through it. Components can become hot if a fault develops or even during normal operation; so, use appropriate caution when touching components.

Page 2: Designing with a PIC Microcontroller - College of … Microcontroller.pdf · Designing with a PIC Microcontroller (Updated: Feb 21, 2010) ... (i.e. simulating a dimmer action). The

University of Saskatchewan 3-2 EE 392 Electrical Engineering Laboratory III

01

23

Obtain one 16F886 MCU and one Quad 7-segment Display Board (Q7SD) from the tech office. The Q7SD board has four 7-segment displays (common-anode configuration, parts # LSHD-5601) connected to four octal latches. The circuit diagram of the Q7SD is given in Appendix B. Obtain other components and build the circuit shown in Fig. 1. Review the SVN_SEG subroutine in the sample code and figure out the connections of the data lines inside the dotted box. Attach a copy of it with your lab book. You may omit the dotted portion of the circuit marked “Part 2” for now.

A procedure must be followed to properly complete one conversion. After the module has been configured, from the user’s perspective digitizing a selected analog channel is relatively straightforward. Refer to the data sheet (pp. 103, [3]) for the complete conversion procedure. Note that, we will be using 8-bit resolution. As a result, we need to read only the higher 8-bits from the ADRESH and ADRESL registers. [Setting the format as “left justified” would certainly help, as in that case, we are required to read from ADRESH only (pp. 102, [3]).]

Figure 1: Connection diagram

Page 3: Designing with a PIC Microcontroller - College of … Microcontroller.pdf · Designing with a PIC Microcontroller (Updated: Feb 21, 2010) ... (i.e. simulating a dimmer action). The

University of Saskatchewan 3-3 EE 392 Electrical Engineering Laboratory III

Once the circuit is built, write an assembly program (using the code template provided on the course website) to repeatedly sample the voltage across the pot connected to port RA0. Vary the pot and observe the displayed digital voltage, and verify with the mapping given in Fig. 3. Note that, we are using only two 7-segment displays. Using the appropriate Latch Enable (LEx) pins, you should update the on-board latches one at a time. Answer the following questions:

1. What is the maximum error in this process? 2. What is the best possible complete conversion time using this setup? 3. What is the maximum sampling rate using one channel? 4. Attach a copy of your assembly code with the lab book.

Figure 2: The PIC16F87X 10-bit 8-channel A/D module [2]

Page 4: Designing with a PIC Microcontroller - College of … Microcontroller.pdf · Designing with a PIC Microcontroller (Updated: Feb 21, 2010) ... (i.e. simulating a dimmer action). The

University of Saskatchewan 3-4 EE 392 Electrical Engineering Laboratory III

Part 2 Hardware Pulse Width Modulation

In this part, we will use the internal PWM to control the light intensity of an LED (i.e. simulating a dimmer action). The output pulse is displayed on the oscilloscope. There are two CCP (Capture/Compare/PWM) modules in PIC16F886. Each CCP module contains:

• A 16-bit Capture register • A 16-bit Compare register • A PWM Master/Slave Duty Cycle register

The ECCP (CCP1) module has an associated control register (CCP1CON) to set the mode. When configured as PWM (as shown in Fig. 4), (TMR2 + Prescale) is used to implement period (10-bits), while (CCPR1L + CCP1CON[5:4]) is used to set duty cycle (10-bits). The CCP1 pin (#13) is the PWM output. The equations are as follows:

4 ( 2 1){ 1 }

OSC

OSC

Period t PS ratio PRDuty t PS extended CCPR H

= × × × +

= × ×

Refer to the data sheet (pp. 128, [3]) for a detailed description of the operation. Build the remaining circuit (marked as Part 2 in Fig. 1). Extend the assembly program (written in part 1) to use the internal PWM. For an 8-bit resolution, load PR2 with h’FF’ (the maximum value). CCPR1L is loaded with the converted digital voltage (read from ADRESH/L).

0

0.40.8

1.21.6

22.42.8

3.23.6

44.4

4.8

FF F4 EA E0 D6 CC C1 B7 AD A3 99 8E 84 7A 70 66 5B 51 47 3D 33 28 1E 14 0A 00

Digital voltage (displayed)

An

alog

vol

tage

(sam

pled

)

Figure 3: Mapping of displayed digital volt and sampled analog voltage

Page 5: Designing with a PIC Microcontroller - College of … Microcontroller.pdf · Designing with a PIC Microcontroller (Updated: Feb 21, 2010) ... (i.e. simulating a dimmer action). The

University of Saskatchewan 3-5 EE 392 Electrical Engineering Laboratory III

Once the program is compiled and downloaded into the MPU, vary the pot and observe the dimming action. Connect an oscilloscope to view the PWM waveform. Measure the width of the pulse from the oscilloscope for a certain pot position and compare it with the actual analog voltage (from the voltmeter) and converted digital voltage (from 7-segment display).

References 1. Course website: EE331: (http://www.engr.usask.ca/classes/EE/331) 2. The Quintessential PIC® Microcontroller, Sid Katzen, 2nd edition, 2005, ISBN: 978-1-

85233-942-5 (available for download from UofS library), Chapter 13 & 14. 3. PIC16F886 Data sheet (DS41291F): http://www.engr.usask.ca/classes/EE/391/notes/PIC16F886.pdf 4. Helpful resources and sample codes: http://seng.ulster.ac.uk/eme/sidk/quintessential/index.html

Figure 4: Timer 2 and the PWM CCP mode [2]

Page 6: Designing with a PIC Microcontroller - College of … Microcontroller.pdf · Designing with a PIC Microcontroller (Updated: Feb 21, 2010) ... (i.e. simulating a dimmer action). The

University of Saskatchewan 3-6 EE 392 Electrical Engineering Laboratory III

Appendix A – PICkit 2 Programmer

(Extracted from the EE391 Electronic Governor lab manual) To select the programmer, you simply go to “Programmer” in MPLAB IDE user interface and select PICkit 2, the IDE will then look for the PICkit 2 and initialize it. Then just look at the “Program” menu for all the required functions (program, read, erase, etc.).

Right angle pin sets area available to easily connect the PICkit 2 programmer to your prototype board.

Page 7: Designing with a PIC Microcontroller - College of … Microcontroller.pdf · Designing with a PIC Microcontroller (Updated: Feb 21, 2010) ... (i.e. simulating a dimmer action). The

University of Saskatchewan 3-7 EE 392 Electrical Engineering Laboratory III

Appendix B – Quad 7-segment Display Board (Q7SD)

l b d h h

160

160

160

160

160

160

160

160

+5V

+5V +5

V

0.1u

AGN

D

74AC373

160

160

160

160

160

160

160

16074AC373

160

160

160

160

160

160

160

16074AC373

160

160

160

160

160

160

160

16074AC373

AGN

DAG

ND

AGN

D

+5V

+5V

+5V

10u

AGN

D

AGN

D

0.1u 0.1u 0.1u

D1

R 1

R 2

R 3

R 4

R 5

R 6

R 7

R 8

C2

NOEP1

OUT_0 P2D_0P3

D_1P4 OUT_1 P5

OUT_2 P6D_2P7

D_3P8 OUT_3 P9

GND P10

LEP11

OUT_4 P12D_4P13

D_5P14 OUT_5 P15

OUT_6 P16D_6P17

D_7P18 OUT_7 P19

VCCP20

U$1

12345678910111213141516

JP1

D2

R 9

R 10

R 11

R 12

R 13

R 14

R 15

R 16NOEP1

OUT_0 P2D_0P3

D_1P4 OUT_1 P5

OUT_2 P6D_2P7

D_3P8 OUT_3 P9

GND P10

LEP11

OUT_4 P12D_4P13

D_5P14 OUT_5 P15

OUT_6 P16D_6P17

D_7P18 OUT_7 P19

VCCP20

U$2

D3

R 17

R 18

R 19

R 20

R 21

R 22

R 23

R 24NOEP1

OUT_0 P2D_0P3

D_1P4 OUT_1 P5

OUT_2 P6D_2P7

D_3P8 OUT_3 P9

GND P10

LEP11

OUT_4 P12D_4P13

D_5P14 OUT_5 P15

OUT_6 P16D_6P17

D_7P18 OUT_7 P19

VCCP20

U$3

D4

R 25

R 26

R 27

R 28

R 29

R 30

R 31

R 32NOEP1

OUT_0 P2D_0P3

D_1P4 OUT_1 P5

OUT_2 P6D_2P7

D_3P8 OUT_3 P9

GND P10

LEP11

OUT_4 P12D_4P13

D_5P14 OUT_5 P15

OUT_6 P16D_6P17

D_7P18 OUT_7 P19

VCCP20

U$4

C1 C3 C4 C5

DATA[0..7],LE[0..3]

DAT

A[0.

.7],L

E[0.

.3]

DAT

A[0.

.7],L

E[0.

.3]

DATA0

DATA0

DATA0

DATA0

DATA0

DATA1

DATA1

DATA1

DATA1

DATA1

DATA2

DATA2

DATA2

DATA2

DATA2

DATA3

DATA3

DATA3

DATA3

DATA3

DATA4

DATA4

DATA4

DATA4

DATA4

DATA5

DATA5

DATA5

DATA5

DATA5

DATA6

DATA6

DATA6

DATA6

DATA6

DATA7

DATA7

DATA7

DATA7

DATA7

LE1

LE1

LE0

LE0

LE2LE2

LE3

LE3

NOTE: Bypass capacitors

abcde

fgPAA

abcde

fgPAA

abcde

fgPAA

abcde

fgPAA