View previous topic :: View next topic |
Author |
Message |
hzz
Joined: 20 Feb 2007 Posts: 314
|
Posted: Wed Aug 19, 2020 8:41 pm Post subject: Using a 16 bit timer counter value in operations |
|
|
In this safe?
Code: | Dim Ntics as Word
Ntics = TCD0_CNT - 500 |
I mean, Can the value of the counter register TCD0_CNT be changed by the timer in the middle of the operation leading to a bad result? In which case only the following will be safe
Code: | Dim Ntics as Word
Ntics = TCD0_CNT
Ntics = Ntics - 500 |
(BASCOM-AVR version : 2.0.8.3 ) |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Thu Aug 20, 2020 1:13 am Post subject: |
|
|
Both are the same, the timer is read as two bytes, and as long as Bascom reads them in the correct order ( which I am sure it does ) then a low byte rollover is prevented before the upper byte is transferred.
Does not matter whether you do the read as part of an arithmetic operation, or just as a transfer. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Thu Aug 20, 2020 1:36 am Post subject: Re: Using a 16 bit timer counter value in operations |
|
|
hzz wrote: | I mean, Can the value of the counter register TCD0_CNT be changed by the timer in the middle of the operation leading to a bad result? |
Without having disassembled a sample code, I'd say it's save.
First, the timer register is read by a 16bit read and therefore a temporary internal register is used, which makes sure, that no race condition affects the correct read.
Second, in both examples the timer value will be read only one time into processor working registers, thus the running counter does not influence the result while the calculation is in progress. |
|
Back to top |
|
|
hzz
Joined: 20 Feb 2007 Posts: 314
|
Posted: Thu Aug 20, 2020 9:22 am Post subject: |
|
|
Quote: | both examples the timer value will be read only one time into processor working registers |
Thanks, this is exactly what I wanted to know. I suppose it holds for any arithmetic operation, such as division:
Code: | Dim Ntics as Word
Ntics = TCD0_CNT / 10 |
As AdrianJ says, I have no doubt that BASCOM always reads 16 bit registers correctly, LSB first. It is interesting how the processor does it to ensure that the low and high bytes of 16-bit registers are always accessed simultaneously:
Quote: | For a read operation, the low byte of the 16-bit register must be read before the high byte. When the low byte register is
read by the CPU, the high byte of the 16-bit register is copied into the temporary register in the same clock cycle as the
low byte is read. When the high byte is read, it is then read from the temporary register. |
|
|
Back to top |
|
|
|