Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Using a 16 bit timer counter value in operations

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 270

spain.gif
PostPosted: Wed Aug 19, 2020 8:41 pm    Post subject: Using a 16 bit timer counter value in operations Reply with quote

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
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2472
Location: Queensland

australia.gif
PostPosted: Thu Aug 20, 2020 1:13 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Thu Aug 20, 2020 1:36 am    Post subject: Re: Using a 16 bit timer counter value in operations Reply with quote

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
View user's profile
hzz

Bascom Member



Joined: 20 Feb 2007
Posts: 270

spain.gif
PostPosted: Thu Aug 20, 2020 9:22 am    Post subject: Reply with quote

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
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum