mechatronics and measurement - 國立中興大學
TRANSCRIPT
Mechatronics and Measurement
Lecturer:Dung-An WangLecture 6
2
Lecture outline
Reading:Ch7 of text Today’s lecture:
Microcontroller
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.
4
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.
6
7.2 MICROCONTROLLERS
7
7.3 THE PIC16F84 MICROCONTROLLER
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.
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
10
Active low: pull them downto reset the IC, groundingthis pin causes the PIC to reset and restart the programstored in EEPROM.
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
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
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.
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.
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.
16
17
18
Assembly Language Instruction Details
19
20
21
22
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.
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
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.
26
27
Flowcode
High-level language compilers are available that allow us to program a PIC at a more user-friendly level
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.
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
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.
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.
32
Lab 10
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
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.
35
Program flow (Lab 9)
36
7.7 INTERFACING PIC PERIPHERALS
37
7.7.2 LCD Display
38
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.
40
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
42
DESIGN EXAMPLE 7.2 PIC Solution to an Actuated Security Device
43
44
45
46
47
48
49
50
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)
52
53
54
DC motor position and speed controller
55
56
Lab 10 Lab 11