me 515 mechatronics - faculty of...
TRANSCRIPT
ME 201 Mechanics of Machines 1/2/2007
1
ME 515 Mechatronics
Microcontroller Based Control of Mechanical Systems
Asanga RatnaweeraDepartment of Mechanical Engineering
Faculty of Engineering University of Peradeniya
Tel: 081239 (3627)Email: [email protected]
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
2
A microprocessorPerforms arithmetic and logic operations
Receives digital input process according to stored program (variable) digital output
Two main parts: arithmetic unit (AU) control unit (CU)
ME 201 Mechanics of Machines 1/2/2007
2
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
3
A microprocessorArithmetic unit made up of:
Arithmetic Logic Unit (ALU)arithmetic calculations + logic operations
Accumulators (ACC) where arithmetic calculations are made
Condition Code Register (CCR) set based on results of ALU; 4 common bits: Zero, Negative, Overflow, Carry
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
4
A microprocessorControl unit made up of:
Memory Address Register (MAR) holds current address where µp is accessing
Memory Data Register (MDR) holds instruction temporarily
Program Counter (PC) holds next instruction address to be read from the program memory (flash memory). Generally the counter increments each time an instruction is executed
Instruction Register (IR) The instruction at the address specified by the PC is read to this register. This operation is called a FETCH.
Control Logic (CL) synchronisation
ME 201 Mechanics of Machines 1/2/2007
3
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
5
A microprocessor
Instruction fletch cycle
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
6
Microcomputer systemµp + memory + i/o chips = µcomputer system
Memory devices – store program, dataI/O devices – read input and/or display results
Connected via buses. 3 types:Address (ex: 16-bit, unidirectional)Data (ex: 8-bit, bidirectional)Control (variable) e.g. Read, Write, Enable
ME 201 Mechanics of Machines 1/2/2007
4
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
7
Microcomputer system
MPU
ROM(program)
RAM(data) PROGRAMMABLE I/O
Control lines
16-bit address bus
8-bit data bus
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
8
Micro-controllerMicrocontrollers contain all of the functional sections (CPU, RAM, ROM, I/O, ports and timers) of a personal computer on a single integrated circuit. some people describe them as special purpose computers with several qualifying distinctions that separate them from other computers. Microcontrollers are "embedded" inside some other device (often a consumer product: kitchen appliances, TVs, VCRs, cameras, telephones) so that they can control the features or actions of the product.
ME 201 Mechanics of Machines 1/2/2007
5
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
9
Micro-controller
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
10
Building blocks of a microcontroller
Memory unitCentral Processing UnitBusInput-output unitSerial communicationTimer unitWatchdogAnalog to Digital Converter
ME 201 Mechanics of Machines 1/2/2007
6
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
11
Building blocks: Input-output unit
When working with ports, first of all it is necessary to choose which port we need to work with, and then to send data to, or take it from the port
By selecting the appropriate register
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
12
Building blocks: Serial communication
ME 201 Mechanics of Machines 1/2/2007
7
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
13
Building blocks: Timer unit Timer unit can give information about time, duration, protocol etc.
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
14
Building blocks: Watchdog This timer watches over the normal operation of the microcontroller software. It must be regularly cleared by software. When it times out the microcontroller returns to the point in the program immediately after power on. This timer is used to recover program operation when the software has a defect (or bug). Even if the program is reinitialized the bug is still in the program.
ME 201 Mechanics of Machines 1/2/2007
8
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
15
Building blocks: Analog to Digital Converter
Analog to Digital Converter is responsible for converting an information about some analog value to a binary number and for follow it through to a CPU block so that CPU block can further process it.
Not available with all µP
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
16
Embedded Systems: General Block Diagram
Microcontroller(uC)
sensor
sensor
sensor
Sensor conditioning
Output interfaces
actuator
indicator
ME 201 Mechanics of Machines 1/2/2007
9
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
17
History in Brief1959: first IC's1971: µP 4004 4Bit Intel (~2000 Tr.)1972: µP 8008 8Bit Intel (~5000 Tr.)1974: µP 8080 8Bit Intel (~6000 Tr.)1974: µP 6800 8Bit Motorola1975: µP Z80 8Bit Zilog1976: µP 9900 16Bit Texas Instruments1978: µP 8086 16Bit Intel (50.000 Tr)1982: µP 8286 16Bit Intel (100.000 Tr.)1985: µP 80386 16Bit Intel (300.000 Tr.)1989: µP 80486 16Bit Intel (106 Tr.)1993: µP 80586 16Bit Intel (5·106 Tr.) ...
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
18
Common MicrocontrollersFamiliesThere are several families of microprocessors are widely used in industrial control systems
Zilog Z8 seriesIntel 8051 seriesARM 32 bit microcontrollersMicroChip – PIC microcontrollers
ME 201 Mechanics of Machines 1/2/2007
10
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
19
PICRange of low end 8 bit microcontrollers.smallest have only 8 pins, largest 40 pins.Typical chip is an 18 pin one.Very cheap, you can pick them up at less than Rs.500 each.Targeted at consumer products, burglar alarms etc.
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
20
ME 201 Mechanics of Machines 1/2/2007
11
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
21
PIC MicrocontrollersThe term PIC stands for Programmable Interface Controller.PIC microcontrollers have become very popular because
Relatively cheapSmall in sizeOn chip memoryEasy to programReusable
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
22
PIC Microcontroller Architecturevon Neumann architecture
programs and data share a single memory area that is accessed over a common bus
Harvard architecture separate memory areas and buses for data and programs
von Neumann Architecture. Harvard Architecture.
Note : PIC microcontroller follows Harvard design
ME 201 Mechanics of Machines 1/2/2007
12
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
23
PIC Microcontroller Characteristics
CPUs8-bit16-bit
Number of I/O pins: 4-70Some chips have A/D converter and PWM
Memory types and sizes:Flash; OTP; ROM0.5k – 256k
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
24
PIC Microcontrollers: Memory varieties Microchip produces field programmable microcontrollers in three basic varieties
EPROMcan be programmed electrically and erased, if needed, by a special ultraviolet light (window type)
OTPcan be programmed only once and is the least expensive of the three
Flash can be both programmed and erased by electrical means
ME 201 Mechanics of Machines 1/2/2007
13
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
25
PIC Microcontrollers: Memory varietiesEx:
The PIC Family: SpeedPICs require a clock to work.
Can use crystals, clock oscillators, or even an RC circuit.Some PICs have a built in 4MHz RC clock
Not very accurate, but requires no external components
Instruction speed = 1/4 clock speed Tcyc = 4 * Tclk
ME 201 Mechanics of Machines 1/2/2007
14
The Minimum PIC SystemYou need some kind of a clock:
Internal 4MHz clock on some PICs, external RC, external crystal, or external clock oscillator.
And you need some power:Most PICs can operate from 2.5V - 5.5V (see Data Sheets)New 16HVXX can directly use up to 14V(Great for direct 9V or 12V battery hook ups
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
28
PIC Microcontrollers: Oscillator
If the CPU is the brain of the system then the oscillator, or clock, is the heartbeat. It provides the critical timing functions for the rest of the chip. The original version of the 16F84 can operate at up to 4-MHz while the newer chip has a 20-MHz upper limit. This section of the PIC is extremely versatile. There are three principal ways to clock a PIC chip.
ME 201 Mechanics of Machines 1/2/2007
15
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
29
PIC Microcontrollers: Oscillator cont..There are three principal ways to clock a PIC chip.
Crystal controlled oscillator
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
30
PIC Microcontrollers: Oscillator cont..
RC oscillator
Note: Not as precise as Xtal oscillator
ME 201 Mechanics of Machines 1/2/2007
16
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
31
PIC Microcontrollers: Oscillator cont..External clock
Note: This is useful when the chip must be synchronized with other components or an entire system must be operated from a single clock source
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
32
PIC Microcontrollers: PIC 16F84APIC16F84A belongs to a class of 8-bit microcontrollers of RISC architecture. Its general structure is shown below.
Microcontrollers with Harvard stands for. architecture are also called "RISC microcontrollers". RISC - Reduced Instruction Set Computer
ME 201 Mechanics of Machines 1/2/2007
17
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
33
PIC Microcontrollers: PIC 16F84A
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
34
PIC Microcontrollers: PIC 16F84
Program memory (FLASH) for storing a written program.
Since memory made in FLASH technology can be programmed and cleared more than once, it makes this microcontroller suitable for device development.
EEPROM - data memory that needs to be saved when there is no supply.RAM - data memory used by a program during its execution.
In RAM are stored all inter-results or temporary data during run-time.
ME 201 Mechanics of Machines 1/2/2007
18
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
35
PIC Microcontrollers: PIC 16F84
PORTA and PORTB are physical connections between the microcontroller and the outside world. Port A has five, and port B has eight pins.FREE-RUN TIMER is an 8-bit register inside a microcontroller that works independently of the program. On every fourth clock of the oscillator it increments its value until it reaches the maximum (255), and then it starts counting over again from zero.CENTRAL PROCESSING UNIT The PIC has only 35 opcodes in its instruction set compared with more than 256 for a DEC Vax system6.
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
36
PIC 16F84 : Pin description PIC16F84 has a total of 18 pins.
ME 201 Mechanics of Machines 1/2/2007
19
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
37
PIC 16F84 : Pin description
Master clear, active lowMCLR
Oscillator crystal inputsOSC1,OSC2
Power supply ground reference (ss – source, dd drain)Vss , Vdd
8 bits of bidirectional I/O Port BRB[0-7]
5 bits of bidirectional I/O Port ARA[0-4]DescriptionPin Identifier
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
38
PIC 16F84 A : FeaturesOnly 35 single word instructions to learn
All instructions single-cycle except for program branches which are two-cycle
Operating speed: 4 MHz clock input, 200 ns instruction cycle1024 words of program memory68 bytes of Data RAM64 bytes of Data EEPROM14-bit wide instruction words8-bit wide data bytes
ME 201 Mechanics of Machines 1/2/2007
20
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
39
PIC 16F84A : Features15 Special Function Hardware registersEight-level deep hardware stackDirect, indirect and relative addressing modesFour interrupt sources:
External RB0/INT pinTMR0 timer overflowPORTB<7:4> interrupt-on-changeData EEPROM write complete
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
40
PIC 16F84A : Peripheral Features13 I/O pins with individual direction controlHigh current sink/source for direct LED drive
25 mA sink max. per pin25 mA source max. per pinTMR0: 8-bit timer/counter with 8-bit
programmable prescaler
ME 201 Mechanics of Machines 1/2/2007
21
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
41
PIC 16F84A: Program memory
The figure below shows the register file map inside the PIC16F84A
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
42
PIC 16F84 : Registers cont…
Bank 1 is used to control the actual operation of the PIC
for example to tell the PIC which bits of Port A are input and which are output. Bank 0 is used to manipulate the data.
To change from Bank 0 to Bank 1 we tell the STAUS register.
We do this by setting bit 5 of the STATUS register to 1.To switch back to Bank 0, we set bit 5 of the STATUS register to 0.The STATUS register is located at address 03h (the ‘h’means the number is in Hexadecimal).
ME 201 Mechanics of Machines 1/2/2007
22
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
43
PIC16F877A : Features
5/6 Programming pins8 A/D channels2 Oscillator Inputs2 RS-232 inputs33 I/O ports
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
44
ME 201 Mechanics of Machines 1/2/2007
23
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
45
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
46
PIC Programming Language that microcontroller and man use to communicate is called "assembly language". Programs written in assembly language must be translated into a "language of zeros and ones" in order for a microcontroller to understand it (hex file). "Assembly language" and "assembler" are two different notions.
The first represents a set of rules used in writing a program for a microcontroller, the other is a program on the personal computer which translates assembly language into a language of zeros and ones.
A program that is translated into "zeros" and "ones" is also called "machine language". There are commercial software which could convert a C or Basic code in to PIC assembly language
ME 201 Mechanics of Machines 1/2/2007
24
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
47
PIC programming : Assembler
The assembler expects to find specific terms in three columns.
First column: LabelsSecond column: opcodes and assembler directivesThird Columns & more: operands
The three columns are used in five ways.Header sectionEquates sectionOrigin sectionProgram sectionThe end statement.
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
48
PIC programming : AssemblerLabels
A label is a mnemonic symbol assigned to an address.Ex : portb means port B whose real hexadecimal address is 0x06.Rules for defining labels
Must be positioned in the first columnMust begin with an alpha characterMay contain numeric, underscore or question markMay be up to 31 characters
ME 201 Mechanics of Machines 1/2/2007
25
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
49
PIC programming : AssemblerLiterals
Literals are constants or numbers, usually hexadecimal numbers. Ex:
start movlw 0x00Literal
InstructionLabel
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
50
PIC programming : Assembler
Header
list radix
p=16f84 hex
Assembler directive
ME 201 Mechanics of Machines 1/2/2007
26
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
51
PIC programming : AssemblerHeader
The information at the top of the source file is called a header.
;=========== pict1.asm===========list p=16f84radix hex
;-----------------------------------------------------------first line defines the type of PIC to be usedSecond line says that the number system is hexadecimal
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
52
PIC programming : Assembler
Equates
equ
Hex address or numberLabel This is an equates
ME 201 Mechanics of Machines 1/2/2007
27
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
53
PIC programming : Assembler
EquatesEquates are used to assign labels to addresses and names to numbers
Ex: portb equ 0x06
min equ 1 ; min =bit 1flag equ 0x0c ; flag = file register
Label
Hexadecimal address
Tells assembler this is an equate
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
54
PIC programming : Assembler
Origin
org
Hex address Not used This is an origin
ME 201 Mechanics of Machines 1/2/2007
28
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
55
PIC programming : AssemblerOrigin
Usually origin statements used for three purposes.
Defines where the program code startsorg 0x000
To establish the reset vector
org 0x1ffgoto start
To establish the start of an interupt service
org 0x004
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
56
PIC programming : Assembler
Program
org
Literal or labelLabel Instruction
ME 201 Mechanics of Machines 1/2/2007
29
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
57
PIC programming : Assembler
EndThis is end
end
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
58
PIC programming : AssemblerProgram formatStandard header
Equates
Program
Endend
ME 201 Mechanics of Machines 1/2/2007
30
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
59
PIC programming : AssemblerAssembler files
All source code files must be saved with the file extension .asm.
ex: pic_1.asmAssembler creates two from the source code
.lst (assembler generated listing)
.hex (hexadecimal object code which can be downloaded to the chip)
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
60
PIC programming : AssemblerThe assembler program reads each text file line , ignores it if begins with a semicolon and steps through the three columns.Style
list PIC16f84radix hex
portb equ 0x06org 0x06
Start movlw 0x00tris portbmovewf portb
circle goto circleend
ME 201 Mechanics of Machines 1/2/2007
31
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
61
PIC Programming The process of communication between a man and a microcontroller
In order to do this we have to use a development system
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
62
Development SystemsDevelopment SystemsMPLABMPLAB™™
EmulateMPLAB-ICE®PICMASTER®
ICEPIC
C CompilerAssembly
ProgramPRO MATE II ®PICSTART+®
SimulateMPLAB™SIM
Edit ProjectMPLAB™
ME 201 Mechanics of Machines 1/2/2007
32
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
63
Development Systems
MPLAB-ICEPICMASTER®
ICEPICMPLAB-SIMPICSTART PlusPRO MATE® II
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
64
Development SystemsUpgrades done in MPLAB software - firmware upgrades occasionally required for new PICmicrosub-familiesUses serial programming algorithms for all ICSP
capable devices
PICSTART PICSTART PlusPlusDEVELOPMENT PROGRAMMERDEVELOPMENT PROGRAMMER
POWERPOWERACTIVEACTIVE
RSRS--232 Serial 232 Serial InterfaceInterface 9V DC Input9V DC Input
Power ONPower ONProgrammerProgrammerActiveActive
4040--Pin ZIF Socket to accommodate Pin ZIF Socket to accommodate PICmicrosPICmicros from 8from 8-- to 40to 40--pins. 68pins. 68--pin PLCC pin PLCC can be programmed with an adaptercan be programmed with an adapter
PinPin--11
ME 201 Mechanics of Machines 1/2/2007
33
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
65
PIC16F848-bit RISC (35 instructions, pipelined)Program counter 13-bit (points to the current instruction, automatically incremented)
Stack to support subroutines/interrupts can hold 8 levels of PC
MemoryProgram memory (FLASH) 1024, 14-bit locationsData Memory
RAM: 68, 8-bit locationsEEPROM: 64, 8-bit locations (slow write access)
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
66
PIC programming : PIC16F84 instruction setPIC16F84 has 35 instruction in its instruction set
Move or define data
Note : k is a constant, f is the file register, d is destination-0 for w register and 1 for file register fNothing
ME 201 Mechanics of Machines 1/2/2007
34
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
67
PIC programming : PIC16F84 instruction setPIC16F84 has 35 instruction in its instruction set
Changing register content
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
68
PIC programming : PIC16F84 instruction setPIC16F84 has 35 instruction in its instruction set
Control program flow
ME 201 Mechanics of Machines 1/2/2007
35
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
69
PIC programming : PIC16F84 instruction setPIC16F84 has 35 instruction in its instruction set
Control Microcontroller
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
70
PIC programming : PIC16F84 instruction setPIC16F84 has 35 instruction in its instruction set
Logic
ME 201 Mechanics of Machines 1/2/2007
36
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
71
PIC programming : PIC16F84 instruction setPIC16F84 has 35 instructions in its instruction set
Arithmetic
Software: Programmers Model
Program Memory
“Burned” in by programmer (can’t
change during execution). Stored
instructions, addresses and “literals” (numbers).
<- 12/14/16 bits ->
Hardware StackStores addresses for subroutines
Program Counter-PCL(PCH)
Status
Special PurposeRegisters
I/O pin states, peripheral registers, etc.
General PurposeRegisters
RAM or “data memory”. Variables are
stored here.
W “Register”
<- 8 bits ->
ME 201 Mechanics of Machines 1/2/2007
37
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
73
Microcontroller Architecture
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
74
PIC16F84 : An Example Code
Port configurationTRISA or TRISB are used to configure Port A and Port B respectivelyIf a bit of TRSA is is high, corresponding pin of the Port A is configured as input, otherwise it is an outputEx: To configure pins 0 and 3 of Port A as outputs and 1 and 2 inputs
BSF 03h,5 ;Go to Bank 1MOVLW 06h ;Put 00110 (06h) into WMOVWF 85h ;Move 00110 onto TRISABCF 03h,5 ;Come back to Bank 0
Note : see the memory map for the address, only the program section is shown
ME 201 Mechanics of Machines 1/2/2007
38
Software: Instruction Examples
movlw 0xFF
Move (“mov”) the number (“l” for “literal”) 0xFF - that’s 256 in decimal- into the working register (“w”).
In other words, load W with the value 0xFF.
Software: Programmers Model
Program Memory
<- 12/14/16 ->
Hardware Stack
Program Counter-PCL(PCH)
Status
Special PurposeRegisters
General PurposeRegisters
W “Register”
<- 8 bits ->
0xFF
ME 201 Mechanics of Machines 1/2/2007
39
Software: Instruction Examples
PORTA 05hmovwf PORTA
Move (“mov”) the working register (“w”) into the file register(“f”) named PORTA.
In other words, load the register called PORTA with whatevernumber is in the W register.
Software: Programmers Model
Program Memory
<- 12/14/16 ->
Hardware Stack
Program Counter-PCL(PCH)
Status
Special PurposeRegisters
PORTA
General PurposeRegisters
W “Register”
<- 8 bits ->
Value in W
ME 201 Mechanics of Machines 1/2/2007
40
Software: Instruction Examples
movfPORTA, W
Move (“mov”) the the value of the file register (“f”) named PORTA into the working register (“w”) .
In other words, load W with the whatever number is in PORTA.
Software: Programmers Model
Program Memory
<- 12/14/16 ->
Hardware Stack
Program Counter-PCL(PCH)
Status
Special PurposeRegisters
PORTA
General PurposeRegisters
W “Register”
<- 8 bits ->
Value in PORTA
ME 201 Mechanics of Machines 1/2/2007
41
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
81
Writing values to port A
bcf 03h,5 ;Come back to Bank 0Start movlw 02h ; Write 02h to the W register. In binary this is
; 00010, which puts a ‘1’ on pin 2 while; keeping; the other pins to ‘0’
movwf 05h ;Now move the contents of W (02h) oto the;PortA, whose address is 05h
movlw 00h ;Write 00h to the W register. This puts a ‘0’ on;all pins.
movwf 05h ;Now move the contents of W (0h) onto the;Port A whose address is 05h
goto Start ;Go to where we say Startend
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
82
PIC16F84 : An Example Code STATUS equ 03h ;this assigns the word STATUS to the value of 03h,
;which is the address of the STATUS register.TRISA equ 85h ;This assigns the word TRISA to the value of 85h,
; which is the address of the Tri-State register for PortAPORTA equ 05h ;This assigns the word PORTA to 05h which is the
;address of Port Absf 03h,5 ;Go to Bank 1movlw 00h ;Put 00000 into Wmovwf 85h ;Move 00000 onto TRISA – all pins set to outputbcf 03h,5 ;Come back to Bank 0
Start movlw 02h ;Write 02h to the W register. In binary this is;00010, which puts a ‘1’ on pin 2 while keeping
;the other pins to ‘0’movwf PORTA ;Now move the contents of W (02h) onto the
;PortA, whose address is 05hmovlw 00h ;Write 00h to the W register. This puts a ‘0’ on
;all pins.
movwf PORTA ;Now move the contents of W (0h) onto the Port A
; whose address is 05hgoto Start ;Go to where we say Startend
ME 201 Mechanics of Machines 1/2/2007
42
2 January 2007 Asanga Ratnaweera, Department of Mechanical Engineering
83
PIC16F84 : Hardware implementation