1 / 21

CSE 341 – Microprocessors Lecture 4 Md. Omar Faruqe faruqe@bracu.ac.bd http://faculty.bracu.ac.bd/~faruqe UB 1228

CSE 341 – Microprocessors Lecture 4 Md. Omar Faruqe faruqe@bracu.ac.bd http://faculty.bracu.ac.bd/~faruqe UB 1228. 1. Delay Routines. Delay Routines are very important in microcontroller based systems.

tavorian
Download Presentation

CSE 341 – Microprocessors Lecture 4 Md. Omar Faruqe faruqe@bracu.ac.bd http://faculty.bracu.ac.bd/~faruqe UB 1228

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 341 – MicroprocessorsLecture 4Md. Omar Faruqefaruqe@bracu.ac.bdhttp://faculty.bracu.ac.bd/~faruqeUB 1228 Lecture 4 1

  2. Delay Routines • Delay Routines are very important in microcontroller based systems. • Just as we saw in the labs last week, we need delay routines to be incorporated in many applications that we build using microcontroller. • How do we write a simple delay routine ? • We could use a dummy loop. A loop that makes the processor go in cycles inside the loop thus delaying the processor. Lecture 4

  3. Delay Routine The light needs to be toggled every 0.5 sec Lecture 4

  4. Requirements of the Routine Simple Flowchart showing the delay requirements. Lecture 4

  5. Delay Program ORG 0H JMPMain ORG 30H Main: SETB P1.0 JMPDelay CLR P1.0 JMPDelay JMPMain Delay: . . . . RET END Lecture 4

  6. Delay Routine Delay Routine NC = 1 + (1+2)*5 = 16 Cycles Let us suppose the Microprocessor is running a 1MHz. What is the time taken by each clock cycle ?. 1µS So the delay produced by the above program is? 16µS Lecture 4

  7. Delay Routine Remember we said that the registers were 8 bits. So what is the maximum number they can hold ? 255 Calculate the delay caused by this program? Nc = 1 + (1+2)*255 = 766 Cycles As each cycles takes, 1mS, the program therefore can generate a maximum of 766 mS. What would be the delay if the mP ran at 10 MHz? 76.6 mS. Lecture 4

  8. Delay Routine We found that using 1 dummy loop it is possible to generate max of 766 mS. Remember we needed to generate a delay of 0.5S So any ideas how we can do this ? Multiple Dummy Loops ! Lecture 4

  9. Lecture 4

  10. Delay Routines Total Number of Cycles, NC = 1 + [ 1+(1+2)*N2 + 1+2]*N1 + 2 = 1 + [ 4 + 3N2]*N1 + 2 = 3 + 4N1 + 3 N1 N2 • Now if you wanted to generate a fixed delay you would have a fixed NC and you would need to calculate N1 & N2 that gives the intended NC value. • This is a huge problem: • The calculation is cumbersome • It is even more harder when an application required varying delay. • The size of the register limits the maximum delay that you can have • Exact delays can be very hard to achieve Lecture 4

  11. Timers • Internal Timers are very important aspects of microprocessors. • Internal timers can be used to provide an interrupt after a specified period of time. • This is essential in the design of O/S. • It is also needed in applications which requires a specific time delay. • The 8051 has two 16bit timers. • Timer 0 – T0 • Timer 1 – T1 • There are two timer functions: • Counter (Can count external events) • Timer (Counts machine cycle) Lecture 4 11

  12. Timers • The timer function of the microprocessor has 4 modes (in this case) • The modes have to be specified using a SFR called TMOD. • The structure of TMOD is given below: • To understand this in details let us examine the timer architecture. Lecture 4

  13. Timers Architecture Lecture 4

  14. Timers • Gate: This acts as a switch to decide whether the timer will be controller by external interrupt or not. When Gate is set to ‘1’ the timer starts using external signals When Gate is set to ‘0’ the timer is controlled internally. When this bit is set (i.e. ‘1’), the timer may be controlled externally by 1 INT (P3.3) for timer 1 and 0 INT (P3.4). for timer 0. The timer starts when this pin is high. When the Gate bit is ‘0’, then the counter starts internally by making TR1 or TR0 set to ‘1’. T/C : This bit determines whether the internal oscillator clock or an external clock from pin T1 for timer 1 or T0 for timer 0 should be used. Lecture 4

  15. Timer Modes Lecture 4

  16. Setting up the timer • TFO - Timer overflow Bit • TRO - Timer Start Bit Lecture 4

  17. Writing a Timer based Delay The procedure for programming the timer is as follows: • Initialization of the TMOD register • Initialization of TL0 and TH0 • Timer start command • Monitor TF0 until set Lecture 4

  18. 100 mS Delay Routine So what should be the values of TH1 and TL1 if we were to achieved a 100 mS delay? TH TL 11111111 1111111 65535 The overflow occurs when the bits are set to 0. FF C9 65536 – 100 = 65436 TH TL Lecture 4

  19. 100 mS Delay Routine Lecture 4

  20. Write a 0.5s Delay Routine? Lecture 4

  21. Questions ? Lecture 4

More Related