microprocessor and microcontroller based systems

20
Microprocessor and Microcontroller Based Systems Instructor: Eng.Moayed N. EL Mobaied The Islamic University of Gaza Faculty of Engineering Electrical Engineering Department م ي ح ر ل ا ن م ح ر ل ه ا ل ل م ا س بEELE 4315 — Fall 2010 Lecture 8

Upload: moe

Post on 11-Jan-2016

61 views

Category:

Documents


2 download

DESCRIPTION

بسم الله الرحمن الرحيم. The Islamic University of Gaza Faculty of Engineering Electrical Engineering Department. Microprocessor and Microcontroller Based Systems. EELE 4315 — Fall 2010. Instructor: Eng.Moayed N. EL Mobaied. Lecture 8. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Microprocessor and Microcontroller Based Systems

Microprocessor and Microcontroller Based Systems

Instructor: Eng.Moayed N. EL Mobaied

The Islamic University of GazaFaculty of EngineeringElectrical Engineering Department

الرحمن الله بسمالرحيم

EELE 4315 — Fall 2010

Lecture 8

Page 2: Microprocessor and Microcontroller Based Systems

Initial Program Design – Flow Diagram (chart)

Read Actual Temperature TA

Read DemandTemperature TD

TA > TD ?

ActivateCompressor

Switch offCompressor

TA >> TD ?

ActivateAlarm

Yes

Yes

No

No

Page 3: Microprocessor and Microcontroller Based Systems

An Alternative Program Visualisation - State Diagrams

Fillwater

Heatwater

WashRinse

Spin Fault

Ready

User initiates

Full leveldetected

Timeout

Requiredtemperature

reached

Out ofbalance

Out ofbalance

Motorfailure

Motorfailure

Functioncomplete

Functioncomplete

Functioncomplete

Faultcleared

Start!

Door closed

Timeout

Page 4: Microprocessor and Microcontroller Based Systems

Conditional Branching

Subtract A from B

Result = 0?

Add C to D

No

Yes

One of the most important features of any microprocessor or microcontroller program is its ability to make “decisions”, i.e. to act differently according to the state of logical variables. Many microprocessors have within their instruction sets a number of instructions which allow them to test a particular bit, and either continue program execution if the condition is not met, or branch to another part of the program if it is. Often these variables are bit values in the Status Registers.

The PIC 16 Series microcontrollers have four conditional “skip” instructions. These test for a certain condition, and skip just one instruction if the condition is met, and continue normal program execution if it is not. The most versatile and general-purpose of these are:

btfsc f,bbtfss f,b

Page 5: Microprocessor and Microcontroller Based Systems

Testing and Manipulating Single BitsTesting and Manipulating Single Bits

;The “main” program starts heremovlw 00 ;clear all bits in port A and Bmovwf portamovwf portb

loop bcf portb, 3 ;preclear port B, bit 3btfss porta, 3bsf portb, 3 ;but set it if button pressed

;bcf portb, 4 ;preclear port B, bit 4btfss porta, 4bsf portb, 4 ;but set it if button pressed

goto loopend

Page 6: Microprocessor and Microcontroller Based Systems

SubroutinesSubroutines

Do this

Do that

Do something else

Call SR1

Call SR2

Call SR1

Main Program

SR1

Return

SR2

Subroutine 1 Subroutine 2

Return

.......

.......

.......

.......

.......

.......

.......

.......

.......

.......

.......

.......

.......

.......

Do that

The subroutine is a program section structured in such a way that it can be called from anywhere in the program. Once it has been executed the program continues to execute from wherever it was before.

The PIC 16 Series subroutine call and return instructions are call and return.. Subroutine Call and Return instructions must always work in pairs.

Page 7: Microprocessor and Microcontroller Based Systems

A Subroutine Example: Software Time DelaysA Subroutine Example: Software Time Delays

As long as the microcontroller is driven by a stable clock oscillator, we know precisely how long any instruction takes to execute. We can therefore write time delays of very high accuracy.

Load Counter1

Decrement Counter1

(Padding, eg "No Ops.")

Is Counter1 Zero?No

Yes

Load Counter 2

Decrement Counter2

Is Counter2 Zero?No

Yes

(Disable all Interrupts)

Load Counter

Decrement Counter

(Padding, eg "No Ops.")

Is Counter Zero?No

Yes

(Disable all Interrupts)

Load Counter

Decrement Counter

Call Delay subroutine

Is Counter Zero?No

Yes

(Disable all Interrupts)

Page 8: Microprocessor and Microcontroller Based Systems

A Subroutine Example: Software Time DelaysA Subroutine Example: Software Time Delays

;Delay of 5ms approx. Instruction cycle time is 5us.delay5 movlw D'200' ;200 cycles called, each taking 5x5=25us movwf delcntr1del1 nop ;1 inst. cycle nop ;1 inst. cycle decfsz delcntr1,1 ;1 inst. cycle, when no skip goto del1 ;2 inst. cycles return

A Delay Subroutine

;500ms delay (approx) ;100 calls to delay5delay500 movlw D'100' movwf delcntr2del2 call delay5 decfsz delcntr2,1 goto del2 return

Nested Subroutines for Greater Delay

Page 9: Microprocessor and Microcontroller Based Systems

Time calculationTime calculation

Instruction MOVLW .10 take one cycle and instruction MOVWF take one cycle.

Instruction DECFSZ take one cycle when escape don’t make but it take 2cycle when escape is made ,so it repeat 9 times as one cycle and take two cycles for one time. NOP take one cycle but it repeat 10 times.

GOTO take 2cycle but it repeat 9 time,

Page 10: Microprocessor and Microcontroller Based Systems

Time calculationTime calculation

Page 11: Microprocessor and Microcontroller Based Systems

Time calculationTime calculation

Where Tc is the time of cycle in seconds.

Page 12: Microprocessor and Microcontroller Based Systems

Time calculationTime calculation

Where Tc is the time of cycle in seconds.

Page 13: Microprocessor and Microcontroller Based Systems

Time calculationTime calculation

Where Tc is the time of cycle in seconds.

Page 14: Microprocessor and Microcontroller Based Systems

OPCODEOPCODE

The PIC 16 Series has four possible instruction word formats, The instruction word is made up of 14 bits. These appear as bits 0 to 13. The opcode, the actual instruction part of the instruction word, always occupies the highest bits of the instruction word.

f (Register file) :

f can specify an address from 0(00h) to 127(7Fh) because it is 7 bits.In case of PIC16F84A, because the register memory is 80 bytes including SFR, it is possible to be specified by f if being 7 bits.

Page 15: Microprocessor and Microcontroller Based Systems

ADDRESSING MODESADDRESSING MODES

RAM memory locations can be accessed directly or indirectly.

Direct Addressing is done through a 9-bit address. This address is obtained by connecting 7th bitof direct address of an instruction with two bits (RP1, RP0) from STATUS register

Page 16: Microprocessor and Microcontroller Based Systems

Direct Addressing modeDirect Addressing mode

Page 17: Microprocessor and Microcontroller Based Systems

Direct Addressing modeDirect Addressing mode

Any access to SFR registers can be an example of direct addressing.

Page 18: Microprocessor and Microcontroller Based Systems

indirect Addressing modeindirect Addressing mode

It does not take an address from an instruction, but it derives it from IRP bit of STATUS and FSR registers. Addressed location is accessed through INDF register which in fact holds the address indicated by the FSR. Indirect addressing is very convenient for manipulating data arrays located in GPRregisters. In this case, it is necessary to initialize FSR register with a starting address of the array, and the rest of the data can be accessed by incrementing the FSR register.

Page 19: Microprocessor and Microcontroller Based Systems

Example for indirect addressingExample for indirect addressing

Page 20: Microprocessor and Microcontroller Based Systems

Configuration registerConfiguration register

The configuration bits can be programmed (read as '0'),or left un programmed (read as '1'), to select various device configurations. These bits are mapped in program memory location 2007h. Address2007h is beyond the user program memory space and it belongs to the special test/configuration memory space (2000h - 3FFFh). This space can only be accessed during programming.