Download presentation
Presentation is loading. Please wait.
Published byNigel McDonald Modified over 8 years ago
1
Department of Electronic & Electrical Engineering Lecture 4. ➢ Loops ➢ Delays ➢ Conditional instructions ➢ Simple clock example.
2
Department of Electronic & Electrical Engineering Delays Delays are useful. ➔ Display a value for while then continue. ➔ Making a simple clock. ➔ Wait when interfacing with other devices.
3
Department of Electronic & Electrical Engineering Programming a delay. ● Most PIC16F84A instructions take 4 clock cycles (see DATA sheet) ● Our PIC uses a 4MHz clock. ● So 1 instruction takes 1 S To make a delay ● Make PIC do lots of instructions (N)... ● Delay Time = N * 1 S
4
Department of Electronic & Electrical Engineering Delay Loop COUNT EQU H'10' ; register of for delay counter ; stuff movlw D’100’ ; initialize counter with 100 (1) movwf COUNT (1) delay_loop: ; start of loop decfsz COUNT,f ; decrement counter - skip if zero (100 x 2) goto delay_loop ; not zero so loop ; delay = 1 + 1+ 100 x 2 = 202 uS ;....... more stuff
5
Department of Electronic & Electrical Engineering
6
Nested loop delay movlw H’FF’ movwf COUNT1 ; inner counter movlw H’FF’ movwf COUNT2 ; middle counter movlw H’100' movwf COUNT3 ; outer counter delay_loop: ; start of loop decfsz COUNT1,f ; inner most loop FF goto delay_loop decfsz COUNT2,f ; middle FF goto delay_loop decfsz COUNT3,f ; outer loop 100 goto delay_loop
7
Department of Electronic & Electrical Engineering Notes: ➢ For an 8bit register ➢ When you decrement 0 you will get H'FF' ➢ decfsz takes 2 instruction cycles if the skip condition is true.
8
Department of Electronic & Electrical Engineering Delay subroutine delay: movlw D’FF’ movwf COUNT1 ; inner counter movlw H’FF’ movwf COUNT2 ; middle counter movlw H’100' movwf COUNT3 ; outer counter delay_loop ; start of loop decfsz COUNT1,f ; inner most loop goto delay_loop decfsz COUNT2,f ; middle goto delay_loop decfsz COUNT3,f ; outer loop goto delay_loop return
9
Department of Electronic & Electrical Engineering Using the delay subroutine ; stuff call delay ; wait a bit ; carry on
10
Department of Electronic & Electrical Engineering Top of Stack (remember where to got back to) (small print) Advanced: 2048 block must contain routine and calling statement.
11
Department of Electronic & Electrical Engineering Simple Clock ● Use seven segment LED display ● Use a delay routine between digits. ● Count from 0 to 9 then reset
12
Department of Electronic & Electrical Engineering Simple clock. ● reset – COUNT=0 ● loop – if COUNT == 10 goto reset – Display COUNT on 7seg display – Wait a bit – Increment COUNT – goto loop
13
Department of Electronic & Electrical Engineering Testing for a number? XOR with 10 and see if we get zero ? See example from debugger lecture. Converting COUNTER to 7seg code See convert routine from last lecture
14
Department of Electronic & Electrical Engineering Demo Demo of simple clock..... misim
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.