Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/25/07 15:34
Modified:
  04/25/07 15:39

Read: times


 
Msg Score: +1
 +1 Good Answer/Helpful
#137933 - another example
Responding to: ???'s previous message
hi,

assume you use UART for sending some bytes as fast as possible. So you optimize your code to do it like that:
; R0 points to a data to be sent
	MOV	R7,#5		; 5 bytes, for example
NEXT_BYTE:
	MOV	A,@R0		; fetch byte to be ready to sent
	JNB	TI,$		; wait till previous transmission is done
	MOV	SBUF,A		; send next byte immediately
; !!! (point A - see below)
	CLR	TI		; clear flag of previous transmission
	INC	R0
	DJNZ	R7,NEXT_BYTE
	RET

For first look it seems as a fastest way.
But what happens if at point A there is a long interrupt takes place? If UART is configured for high baudrate (for example, it is in mode 0) or/and there is a long innerrupt routine of other source (timer, /INTx etc) in your code then what?
Imagine that during ISR of such interrupt UART transmission has been done. UART sets TI flag of current transmission and after your code returns to line CLR TI it clears flag of not previous transmission but current one. In result your code has chance going into dead loop in the whole.
Solution is either to be sure that there is no such long interrupt in your program or exchange lines placement to:
CLR TI
MOV SBUF,A

so it clears the bit firslty and then put new value into data send register (indeed with lost of some performance).

Edited: either you may disable interrupt(s) as well, indeed.

Regards,
Oleg

List of 12 messages in thread
TopicAuthorDate
lesson learned            01/01/70 00:00      
   "atomic"            01/01/70 00:00      
      atomicity            01/01/70 00:00      
      if thatb is so            01/01/70 00:00      
         Indeed            01/01/70 00:00      
   Bit test and clear            01/01/70 00:00      
      JBC            01/01/70 00:00      
         favourite atomic test-and-set instructions            01/01/70 00:00      
      SDCC also            01/01/70 00:00      
         Classic atomic problem            01/01/70 00:00      
            and/or ...            01/01/70 00:00      
   another example            01/01/70 00:00      

Back to Subject List