mechatronics and measurement - 國立中興大學

56
Mechatronics and Measurement LecturerDung-An Wang Lecture 6

Upload: others

Post on 18-Apr-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mechatronics and Measurement - 國立中興大學

Mechatronics and Measurement

Lecturer:Dung-An WangLecture 6

Page 2: Mechatronics and Measurement - 國立中興大學

2

Lecture outline

Reading:Ch7 of text Today’s lecture:

Microcontroller

Page 3: Mechatronics and Measurement - 國立中興大學

3

7.1 MICROPROCESSORS

Hardware solution: consists of a selection of specific ICs, which when hardwired on a circuit board, carry out predefined functions.

Software solution: consisting of a set of instructions to execute logic and arithmetic functions and to access

input signals and control output signals. A microprocessor is a single, very-large-scale-

integration (VLSI) chip that contains many digital circuits that perform arithmetic, logic, communication, and control functions.

Page 4: Mechatronics and Measurement - 國立中興大學

4

Page 5: Mechatronics and Measurement - 國立中興大學

5

The instructions that can be executed by the CPU are defined by a binary code called machine code. The instructions and corresponding codes are microprocessor dependent.

Microprocessors can be programmed using assembly language, which has a mnemonic command corresponding to each instruction (e.g., ADD to add a number to a register and MOV to move a register’s value to a memory location).

Assembly language must be converted to machine code, using software called an assembler, before it can be executed on the microprocessor.

Page 6: Mechatronics and Measurement - 國立中興大學

6

7.2 MICROCONTROLLERS

Page 7: Mechatronics and Measurement - 國立中興大學

7

7.3 THE PIC16F84 MICROCONTROLLER

Page 8: Mechatronics and Measurement - 國立中興大學

8

The PIC16F84 is an 8-bit CMOS microcontroller with 1792 bytes of flash EEPROM program memory, 68 bytes of RAM data memory, and 64 bytes of nonvolatile EEPROM data memory.

The 1792 bytes of program memory are subdivided into 14-bit words, because machine code instructions are 14 bits wide. Therefore, the EEPROM can hold up to 1024 (1 k) instructions.

With a clock speed of 4 MHz, an instruction is executed every microsecond and 1 million instructions can be executed every second.

The microcontroller is referred to as 8-bit, because the data bus is 8 bits wide, and all data processing and

storage and retrieval occur using bytes.

Page 9: Mechatronics and Measurement - 國立中興大學

9

The PIC16F84 can be driven at a clock speed up to 10 MHz but is typically driven at 4 MHz.

PORTA: RA0-RA4 PRRTB: RB0-RB7 13 I/O lines : bidirectional

Page 10: Mechatronics and Measurement - 國立中興大學

10

Active low: pull them downto reset the IC, groundingthis pin causes the PIC to reset and restart the programstored in EEPROM.

Page 11: Mechatronics and Measurement - 國立中興大學

11

This pin (pin 4) must be held high during normal program execution. This is accomplished with the pull-up resistor.

The PIC clock frequency can be controlled using different methods, including an external RC circuit, an external clock source, or a clock crystal.

The use of a clock crystal to provide an accurate and stable clock frequency at relatively low cost. The clock frequency is set by connecting a 4-MHz crystal across the OSC1 and OSC2 pins with the 22 pF capacitors grounded

Page 12: Mechatronics and Measurement - 國立中興大學

12

The value for capacitors C1 and C2 must be chosen so that the series combination: (C1*C2) / (C1+C2) gives a value close to the load capacitance, CL, specified by the crystal manufacturer.

From the crystal's point of view, C1 and C2 are in series, so the total that the crystal sees is 11 pF. Stray capacitance and input capacitance on the pins may account for 8-10 pF.

Cl = ((C1 * C2) / (C1 + C2)) + Cs Where Cl is load capacitance, C1 and C2 are the

capacitor values, and Cs is the stray capacitance of the board.

the crystal is designed for a 20pf load capacitance

Page 13: Mechatronics and Measurement - 國立中興大學

13

Decoupling capacitor (0.1 µF)

Power supplies are slow...they take roughly 10 us to respond (i.e. bandwidth up to 100 kHz). So when your big, bad, multi-MHz microcontroller switches a bunch of outputs from high to low, it will draw charge from the power supply (current draw), causing the voltage to start drooping until it realizes (10 us later!) that it needs to do something to correct the drooping voltage.

When you draw high currents from your power supply (like when digital logic switches state) you will see a change in supply voltage. Switching tries to draw large instantaneous currents and produces a voltage drop due to the impedance of the voltage source and the connection between the voltage source and the IC. A decoupling capacitor will help to maintain the supply voltage at the device. Placing this storage element close to the IC reduces the change in voltage at the IC.

Page 14: Mechatronics and Measurement - 國立中興大學

14

To compensate for slow power supplies, we use decoupling capacitors. Decoupling capacitors add fast "charge storage" near the IC. So when your micro switches the outputs, instead of drawing charge from the power supply, it will first draw from the capacitors. This will buy the power supply some time to adjust to the changing demands.

The "speed" of capacitors varies. Basically, smaller capacitors are faster; inductance tends to be the limiting factor, which is why everyone recommends putting the caps as close as possible to VCC/GND with the shortest, widest leads that are practical. So pick the largest capacitance in the smallest package, and they will provide the most charge as fast as possible.

Page 15: Mechatronics and Measurement - 國立中興大學

15

7.4 PROGRAMMING A PIC

To use a microcontroller in mechatronic system design, software must be written, tested, and stored in the ROM of the microcontroller. Usually, the software is written and compiled using a personal computer (PC) and then downloaded to the microcontroller ROM as machine code.

If the program is written in assembly language, the PC must have software called a cross-assembler that generates machine code for the microcontroller.

Page 16: Mechatronics and Measurement - 國立中興大學

16

Page 17: Mechatronics and Measurement - 國立中興大學

17

Page 18: Mechatronics and Measurement - 國立中興大學

18

Assembly Language Instruction Details

Page 19: Mechatronics and Measurement - 國立中興大學

19

Page 20: Mechatronics and Measurement - 國立中興大學

20

Page 21: Mechatronics and Measurement - 國立中興大學

21

Page 22: Mechatronics and Measurement - 國立中興大學

22

Page 23: Mechatronics and Measurement - 國立中興大學

23

The first four active lines ( list . . . target ) are assembler directives that designate the processor and define constants that can be used in the remaining code.

Defining constants (with the equ directive) Assemblylanguage constants such as addresses and values are written in hexadecimal, denoted with a 0x prefix.

Page 24: Mechatronics and Measurement - 國立中興大學

24

movlw, move the literal constant target into the W register and then from the W register into the count address location in memory. The target value (0x05) will be decremented until it reaches 0x00

Page 25: Mechatronics and Measurement - 國立中興大學

25

initializes the special function registers PORTA and TRISA to allow output to pins RA0 and RA1, which drive the LEDs. These registers are located in different banks of memory, hence the need for the bsf and bcfstatements in the program.

All capitalized words in the program are constant addresses or values predefined in the processor-dependent include file (p16f84.inc). The function of the TRISA register is discussed more later; but by clearing the bits in the register, the PORTA pins are configured as outputs.

Page 26: Mechatronics and Measurement - 國立中興大學

26

Page 27: Mechatronics and Measurement - 國立中興大學

27

Flowcode

High-level language compilers are available that allow us to program a PIC at a more user-friendly level

Page 28: Mechatronics and Measurement - 國立中興大學

28

Lab 9

wiring circuits to a PIC entering and compiling flowcode programs downloading executable code from a PC to the flash

memory on the PIC.

Page 29: Mechatronics and Measurement - 國立中興大學

29

Example

Program to turn on an LED every time a pushbutton switch is pressed, and turn on a second LED once it has been pressed a specified number of times

Page 30: Mechatronics and Measurement - 國立中興大學

30

Design ex 7.1

Drive the seven LED segments directly from seven output pins of a PIC

Input a number in program and run your program.

Page 31: Mechatronics and Measurement - 國立中興大學

31

Design ex 7.1

An alternative design that requires fewer output pins uses a seven-segment decoder IC (e.g., 7447). Here, only four I/O pins are required.

Page 32: Mechatronics and Measurement - 國立中興大學

32

Lab 10

Page 33: Mechatronics and Measurement - 國立中興大學

33

7.6 USING INTERRUPTS

Polling: program includes a check of the sensor inputswithin a loop to update the output accordingly

Interrupt: some inputs are connected to special input lines, designated as interrupts. When one or more of these lines changes level, the microcontroller temporarily suspends normal program execution while the change is acted on by a subprogram or function called an interrupt service routine. At the end of the service routine, control is returned to the main program at the point where the interrupt occurred

Page 34: Mechatronics and Measurement - 國立中興大學

34

Example

This program turns on an LED and waits for an interrupt on PORTB.0. When RB0 changes state, the program turns the LED off for 0.5 seconds and then resumes normal execution.

Page 35: Mechatronics and Measurement - 國立中興大學

35

Program flow (Lab 9)

Page 36: Mechatronics and Measurement - 國立中興大學

36

7.7 INTERFACING PIC PERIPHERALS

Page 37: Mechatronics and Measurement - 國立中興大學

37

7.7.2 LCD Display

Page 38: Mechatronics and Measurement - 國立中興大學

38

Page 39: Mechatronics and Measurement - 國立中興大學

39

Threaded design ex C.2

EDE1144 keypad decoder is used to monitor keypresses on the keypad and transmit them to the PIC via a serial interface.

Page 40: Mechatronics and Measurement - 國立中興大學

40

Page 41: Mechatronics and Measurement - 國立中興大學

41

7.9 METHOD TO DESIGN AMICROCONTROLLER-BASED SYSTEM 1. Define the problem 2. Draw a functional diagram 3. Identify I/O requirements 4. Select appropriate microcontroller models 5. Identify necessary interface circuits 6. Decide on a programming language 7. Draw the schematic 8. Draw a program flowchart 9. Write the code 10. Build and test the system

Page 42: Mechatronics and Measurement - 國立中興大學

42

DESIGN EXAMPLE 7.2 PIC Solution to an Actuated Security Device

Page 43: Mechatronics and Measurement - 國立中興大學

43

Page 44: Mechatronics and Measurement - 國立中興大學

44

Page 45: Mechatronics and Measurement - 國立中興大學

45

Page 46: Mechatronics and Measurement - 國立中興大學

46

Page 47: Mechatronics and Measurement - 國立中興大學

47

Page 48: Mechatronics and Measurement - 國立中興大學

48

Page 49: Mechatronics and Measurement - 國立中興大學

49

Page 50: Mechatronics and Measurement - 國立中興大學

50

Page 51: Mechatronics and Measurement - 國立中興大學

51

Design Example Power amp motor driver controlled by a potentiometer A potentiometer is attached to an A/D input in the PIC. The PIC

outputs the corresponding voltage as a digital word to a TI TLC7524 external D/A converter, which is attached to a TI OPA547 power-op-amp circuit.

The amplifier circuit can provide up to 500 mA of current to a DC motor (e.g., R179-6V-ENC-MOTOR)

Page 52: Mechatronics and Measurement - 國立中興大學

52

Page 53: Mechatronics and Measurement - 國立中興大學

53

Page 54: Mechatronics and Measurement - 國立中興大學

54

DC motor position and speed controller

Page 55: Mechatronics and Measurement - 國立中興大學

55

Page 56: Mechatronics and Measurement - 國立中興大學

56

Lab 10 Lab 11