Top  Previous  Next


Set or retrieve the COUNTER0 or COUNTER1 variable.

For 8052 TIMER2 compatible chips, COUNTER2 can be used too.










A byte, Integer/Word variable or constant that is assigned to the counter.




Use counterX = 0 to reset the counter.

The counter can count from 0 to 255 in mode 2 (8-bit auto reload).

And to 65535 in mode 1(16-bit).

In mode 0 the counter can count to 8192. The MSB and 5 bits of the LSB are used in that case. When you assign a constant to a TIMER/COUNTER in mode 0, the bits will be placed in the right place :

COUNTER0 = &B1_1111_1111_1111_1111 '13 bits

Will be translated for mode 0 into 1111_1111_0001_1111


The counterx variables are intended to set/retrieve the TIMER/COUNTER registers from BASCOM. COUNTER0 = TL0 and TH0.

So the COUNTERx reserved variable is a 16 bit variable.


To set TLx or THx, you can use : TL0 = 5 for example.


Note that the COUNTERx variable operates on both the TIMERS and COUNTER because the TIMERS  and COUNTERS are the same thing except for the mode they are working in. To load a reload value, use the LOAD statement.


noticeAfter access to the counter, the timer/counter is stopped. So when it was running, start it with the statement START COUNTERx





'                     (c) 1995-2006 MCS Electronics


'  file: COUNTER.BAS

'  demo: COUNTER


'     Connect the timer input P3.4 to a frequency generator

'     *TIMER/COUNTER 1 is used for RS-232 baud rate generator


Dim A As Byte , C As Integer

Config Timer0 = Counter , Gate = Internal , Mode = 1

'Timer0 = counter : timer0 operates as a counter

'Gate = Internal  : no external gate control

'Mode = 1         : 16-bit counter



Counter0 = 0                           'clear counter

Start Counter0                         'enable the counter to count

Do                                     'set up a loop

A = Inkey                           'check for input

C = Counter0                         'get counter value

Print C                               'print it

Start Counter0 're-start it because it was stopped by accessing the COUNTER

Loop Until A = 27                       'until escape is pressed






For the next example the ASM code is shown:

COUNTER0 = 1000


Generated code :

Clr TCON.4

Mov tl0,#232

Mov th0,#3