4469 views|0 replies

1379

Posts

0

Resources
The OP
 

Internal Hardware Resources of PIC Microcontroller 16F84 (V) [Copy link]

7 Delay and timing
  When designing a microcontroller application system, you often encounter a situation where a process (such as heating, pressurization, etc.) needs to be continued for a period of time, such as continuous pressurization for 1 minute, power on for 2 minutes, etc. How can the microcontroller correctly determine this period of time? This can be achieved in two ways, namely delay and timing. Take a look at the following example.
  In the application system, the RAO terminal of the PIC16F84 microcontroller is required to control a light-emitting diode to flash at a certain frequency, which can be achieved through the circuit on the right. At the same time, a program must be compiled for the 16F84 microcontroller. From the circuit diagram, it can be seen that to make the light-emitting diode LED flash at a certain frequency, just make the RAO terminal output a changing high → low → high... level. Therefore, the following source program is designed (List 1):
  list P=16F84, F=INHX8M
  ; ...
     ORG 0
     MOVLW 0; Main program starts
     TRIS 5; Set port RA as output
     BCF 5, 0; Clear port RA 0 bit
  LOOP: CALL DELAY; Flash delay
     COMF 5; Invert port RA, light-off alternation
     GOTO LOOP; Loop
  ; ...
  DELAY; The following is the delay subroutine
      MOVLW D'50
      MOVWF 8
  LOOP1: MOVWF 9
  LOOP2: DECFSZ 9, F
      GOTO LOOP2
     DECFSZ 8, F
     GOTO LOOP1
  RETLW 0
  From List 1, it can be seen that when the main program starts, the working register W is first cleared, and then the content of the W register is sent to the TRISA register to clear it to set port RA as output. Then the 5th bit of port RA is cleared to make the LED off at the beginning. Then it continues for a period of time, that is, the delay subroutine is executed, and then the RA port is inverted, and it becomes a high-level output, the LED lights up, and then the delay is made, and the RA port is inverted again, and the LED goes out... In this way, the LED is dark and bright, and it keeps alternating.
  Here, the LED is made bright and dark for a period of time by the microcontroller executing the delay subroutine DELAY. The core of this delay program is to let the CPU of the microcontroller repeatedly execute the instruction DECFSZ that reduces the register content by 1. That is, the decimal number 50 is loaded into the general registers F8 and F9 respectively to perform 50×50=2500 times of reduction by 1 operation. If it takes 1 instruction cycle to execute the DECFSZ instruction once (2 cycles are required for jumping), if the oscillation frequency is set to 100kHz, that is, the instruction cycle is 40μs, then the delay time is 2500×40=100000μs=100ms, that is, 0?1 seconds. In fact, it is slightly larger. This delay time has exceeded the visual retention time of the human eye. Therefore, we can clearly see the alternation of light and dark of the LED.
  If we need a longer delay time, we can follow the above example and load a larger number or introduce multiple loops. Therefore, in principle, the delay time can be extended as needed.
  However, the method of using a delay program to continue a process has defects. Delay is to make the CPU "circle" on certain instructions. The longer the delay, the more "circles". At this time, the CPU can no longer perform other operations, such as monitoring temperature, humidity, etc. This is not allowed in some real-time control systems. For this reason, a special "alarm clock" is set in the 16F84 microcontroller - timer TMR0. How long a process needs to last can be "dialed" into TMR0, and then it will "interrupt" and tell the CPU that the timing time is up. The CPU is required to suspend other work, turn around to execute the "interrupt subroutine", complete tasks such as outputting on and off signals, and then go back to execute the interrupt work. In this way, the CPU's work efficiency is improved. Therefore, the use of delay is limited, and the use of timer TMR0 can be used in various occasions.
This post is from Microchip MCU
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list