Designing with a PIC Microcontroller - University of Saskatchewan

136 downloads 2448 Views 323KB Size Report
Designing with a PIC Microcontroller. (Updated: Feb 21, 2010). Objectives: This lab provides an opportunity to learn more about the use of a microcontroller ...
University of Saskatchewan EE 392 Electrical Engineering Laboratory III

3-1

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

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.

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

University of Saskatchewan EE 392 Electrical Engineering Laboratory III

3-2

0 1 2 3

Figure 1: Connection diagram 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]).]

University of Saskatchewan EE 392 Electrical Engineering Laboratory III

3-3

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

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 onboard 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.

University of Saskatchewan EE 392 Electrical Engineering Laboratory III

3-4

4.8

Analog voltage (sampled)

4.4 4 3.6 3.2 2.8 2.4 2 1.6 1.2 0.8 0.4

00

0A

14

28 1E

33

3D

47

51

5B

66

70

84 7A

8E

99

A3

AD

C

6

C1 B7

C

D

E0

EA

FF F4

0

Digital voltage (displayed)

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

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:

Period = 4 × tOSC × PS ratio × ( PR 2 + 1)

Duty = tOSC × PS × {extended CCPR1H } 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).

University of Saskatchewan EE 392 Electrical Engineering Laboratory III

3-5

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

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

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-185233-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

University of Saskatchewan EE 392 Electrical Engineering Laboratory III

3-6

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.

University of Saskatchewan EE 392 Electrical Engineering Laboratory III

3-7

+5V

+5V

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

LE 0

P20 P11

DATA0 DATA1 DATA2 DATA3 DATA4 DATA5 DATA6 DATA7

P3 P4 P7 P8 P13 P14 P17 P18

VCC LE

C1

C2

10u

0.1u 0.1u 0.1u 0.1u

C5

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

GND

P10

160

160

R4

R5

160

160

R6

R7

160

160

AGND

f g P A A

R8

AGND

160

R9

LE 1

P20 P11

DATA0 DATA1 DATA2 DATA3 DATA4 DATA5 DATA6 DATA7

P3 P4 P7 P8 P13 P14 P17 P18

D_0 D_1 D_2 D_3 D_4 D_5 D_6 D_7

OUT_0 OUT_1 OUT_2 OUT_3 OUT_4 OUT_5 OUT_6 OUT_7

P2 P5 P6 P9 P12 P15 P16 P19

P1

NOE

GND

P10

VCC LE

160

R 10

R 11

160

160

R 12

R 13

160

160

R 14

R 15

160

160

74AC373 U$2

JP1

D1 a b c d e

D2 f g P A A

a b c d e

R 16 160

AGND

DATA0 DATA1 DATA2 DATA3 DATA4 DATA5 DATA6 DATA7 LE 0 LE 1 LE 2 LE 3

C4

NOE

R2

R3

74AC373 U$1

+5V

16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

C3

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

AGND

+5V

NOTE : Bypass capacitors

OUT_0 OUT_1 OUT_2 OUT_3 OUT_4 OUT_5 OUT_6 OUT_7

+5V

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

P1

D_0 D_1 D_2 D_3 D_4 D_5 D_6 D_7

P2 P5 P6 P9 P12 P15 P16 P19

160

R 17

LE 2

P20 P11

DATA0 DATA1 DATA2 DATA3 DATA4 DATA5 DATA6 DATA7

P3 P4 P7 P8 P13 P14 P17 P18

D_0 D_1 D_2 D_3 D_4 D_5 D_6 D_7

OUT_0 OUT_1 OUT_2 OUT_3 OUT_4 OUT_5 OUT_6 OUT_7

P2 P5 P6 P9 P12 P15 P16 P19

P1

NOE

GND

P10

VCC LE

160

R 18

R 19

160

160

R 20

R 21

160

160

R 22

R 23

160

160

R 24

74AC373 U$3

D3 f g P A A

a b c d e

+5V

AGND

160

R 25

LE 3

P20 P11

DATA0 DATA1 DATA2 DATA3 DATA4 DATA5 DATA6 DATA7

P3 P4 P7 P8 P13 P14 P17 P18

D_0 D_1 D_2 D_3 D_4 D_5 D_6 D_7

OUT_0 OUT_1 OUT_2 OUT_3 OUT_4 OUT_5 OUT_6 OUT_7

P2 P5 P6 P9 P12 P15 P16 P19

P1

NOE

GND

P10

160

VCC LE

74AC373 U$4

d

h

160

160

R 28

R 29

160

160

R 30

R 31

160

160

R 32 160

AGND

l b

R 26

R 27

h

D4 a b c d e

f g P A A