ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 8215

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

© MCS Electronics, 1995-2007

'The following code shows how to use the TIMER0 in interrupt mode 'The code is block remarked with '( en ')

'(

'Configute the timer to use the clock divided by 1024

Config Timer0 = Timer , Prescale = 1024

'Define the ISR handler

On Ovf0 Tim0_isr

'you may also use TIMER0 for OVF0, it is the same

Enable Timer0

' enable the timer

interrupt

'allow interrupts

Enable Interrupts

to occur

Do

'your program goes here

Loop

'the following code is executed when the timer rolls over

Tim0_isr:

Print "*";

Return

')

End

CONFIG TIMER1

Action

Configure TIMER1.

Syntax

CONFIG TIMER1 = COUNTER | TIMER | PWM ,

EDGE=RISING | FALLING , PRESCALE= 1|8|64|256|1024 ,

NOISE CANCEL=0 |1, CAPTURE EDGE = RISING | FALLING ,

CLEAR TIMER = 1|0,

COMPARE A = CLEAR | SET | TOGGLE I DISCONNECT ,

COMPARE B = CLEAR | SET | TOGGLE I DISCONNECT ,

PWM = 8 | 9 10 ,

COMPARE A PWM = CLEAR UP| CLEAR DOWN | DISCONNECT

COMPARE B PWM = CLEAR UP| CLEAR DOWN | DISCONNECT

Remarks

The TIMER1 is a 16 bit counter. See the hardware description of TIMER1. It depends on the chip if COMPARE B is available or not.

Some chips even have a COMARE C.

The syntax shown above must be on one line. Not all the options need to be selected.

Here is the effect of the various options.

EDGE

You can select whether the TIMER will count on the falling or rising edge.

page -390-


© MCS Electronics, 1995-2007

Only for COUNTER mode.

CAPTURE

You can choose to capture the TIMER registers to the INPUT CAPTURE

EDGE

registers

With the CAPTURE EDGE = FALLING/RISING, you can specify to capture

on the falling or rising edge of pin ICP

NOISE

To allow noise canceling you can provide a value of 1.

CANCELING

PRESCALE

The TIMER is connected to the system clock in this case. You can select

the division of the system clock with this parameter.

Valid values are 1 , 8, 64, 256 or 1024

The TIMER1 also has two compare registers A and B

When the timer value matches a compare register, an action can be performed

COMPARE A The action can be:

SET will set the OC1X pin

CLEAR will clear the OC1X pin

TOGGLE will toggle the OC1X pin

DISCONNECT will disconnect the TIMER from output pin OC1X

And the TIMER can be used in PWM mode.

You have the choice between 8, 9 or 10 bit PWM mode

Also you can specify if the counter must count UP or down after a match to the compare registers

Note that there are two compare registers A and B

PWM

Can be 8, 9 or 10.

COMPARE A PWM PWM compare mode. Can be CLEAR UP or CLEARDOWN

Using COMPARE A, COMPARE B, COMPARE A PWM or COMPARE B PWM will set the corresponding pin for output. When this is not wanted you can use the alternative NO_OUTPUT version that will not alter the output pin.

For example : COMPARE A NO_OUTPUT , COMPARE A PWM NO_OUTPUT

Example

'-----------------------------------------------------------------------------

------------

: timer1.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: show using Timer1

'micro

: 90S8515

'suited for demo

: yes

'commercial addon needed

: no

'-----------------------------------------------------------------------------

------------

$regfile = "8515def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

page -391-


© MCS Electronics, 1995-2007

frequency

' use baud rate

$baud = 19200

$hwstack = 32

' default use 32

for the hardware stack

' default use 10

$swstack = 10

for the SW stack

' default use 40

$framesize = 40

for the frame space

Dim W As Word

'The TIMER1 is a versatile 16 bit TIMER. 'This example shows how to configure the TIMER

'First like TIMER0 , it can be set to act as a TIMER or COUNTER 'Lets configute it as a TIMER that means that it will count and that 'the input is provided by the internal clock.

'The internal clock can be divided by 1,8,64,256 or 1024

Config Timer1 = Timer , Prescale = 1024

'You can read or write to the timer with the COUNTER1 or TIMER1 variable W = Timer1

Timer1 = W

'To use it as a COUNTER, you can choose on which edge it is trigereed Config Timer1 = Counter , Edge = Falling , Prescale = 1

'Config Timer1 = Counter , Edge = Rising

'Also you can choose to capture the TIMER registers to the INPUT CAPTURE registers

'With the CAPTURE EDGE = , you can specify to capture on the falling or rising edge of

'pin ICP

Config Timer1 = Counter , Edge = Falling , Capture Edge = Falling , Prescale = 1024

'Config Timer1 = Counter , Edge = Falling , Capture Edge = Rising

'To allow noise canceling you can also provide :

Config Timer1 = Counter , Edge = Falling , Capture Edge = Falling , Noise Cancel = 1 , Prescale = 1

'to read the input capture register : W = Capture1

'to write to the capture register :

Capture1 = W

'The TIMER also has two compare registers A and B

'When the timer value matches a compare register, an action can be performed Config Timer1 = Counter , Edge = Falling , Compare A = Set , Compare B =

Toggle , , Clear Timer = 1 'SET , will set the OC1X pin

'CLEAR, will clear the OC1X pin 'TOGGLE, will toggle the OC1X pin

'DISCONNECT, will disconnect the TIMER from output pin OC1X 'CLEAR TIMER will clear the timer on a compare A match

'To read write the compare registers, you can use the COMPARE1A and COMPARE1B variables

page -392-


© MCS Electronics, 1995-2007

Compare1a = W

W = Compare1a

'And the TIMER can be used in PWM mode

'You have the choice between 8,9 or 10 bit PWM mode

'Also you can specify if the counter must count UP or down after a match 'to the compare registers

'Note that there are two compare registers A and B

Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Down , Prescale = 1

'to set the PWM registers, just assign a value to the compare A and B registers

Compare1a = 100

Compare1b = 200

'Or for better reading :

Pwm1a = 100

Pwm1b = 200

End

CONFIG TIMER2

Action

Configure TIMER2.

Syntax for the 8535

CONFIG TIMER2 = TIMER | PWM , ASYNC=ON |OFF,

PRESCALE = 1 | 8 | 32 | 64 | 128 | 256 | 1024 ,

COMPARE = CLEAR | SET | TOGGLE I DISCONNECT ,

PWM = ON | OFF ,

COMPARE PWM = CLEAR UP| CLEAR DOWN | DISCONNECT ,

CLEAR TIMER = 1|0

Syntax for the M103

CONFIG TIMER2 = COUNTER| TIMER | PWM ,

EDGE= FALLING |RISING,

PRESCALE = 1 | 8 | 64 | 256 | 1024 ,

COMPARE = CLEAR | SET | TOGGLE I DISCONNECT ,

PWM = ON | OFF ,

COMPARE PWM = CLEAR UP| CLEAR DOWN | DISCONNECT ,

CLEAR TIMER = 1|0

Remarks

The TIMER2 is an 8 bit counter.

It depends on the chip if it can work as a counter or not.

The syntax shown above must be on one line. Not all the options need to be selected.

Here is the effect of the various options.

EDGE

You can select whether the TIMER will count on the falling or rising edge.

Only for COUNTER mode.

page -393-