bascom and avr, interrupts

Upload: kondoritocl

Post on 03-Jun-2018

718 views

Category:

Documents


32 download

TRANSCRIPT

  • 8/12/2019 Bascom and AVR, Interrupts

    1/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Bascom and AVR, Interrupts

    Suppose you have a Bascom program that is in a Loop doing something complicated and time-consuming. You want to beable to stop this task and switch the program to do someting else. The obvious hardware solution is to add a STOP button toyour AVR:

    You would normally test if this button is pressed with the following program fragment:

    . .Conf i g Pi nd. 2 as I nput

    . .Do

    . . .

    . . . somet i ng compl i cat ed. . .

    . . .I f Pi nd. 2 = 0 ThenLcd " St op! "

    Got o Ot her t askEnd I f

    Loop

    . .Ot her Task:

    . .

    The fundamental problem with this example is that we might spend so much time doing the complicated task that pressing theSTOP button will often not be noticed once the program arrives at the Pind.2 test. Obviously we need another mechanism toreact to pressing the STOP button in an independant way from the main program.This is where interrupts are for. Interrupts are a to change the program flow to react to external as well as internal controller events. We could modify the example above to:

    interrupt-stopbutton.bas$r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out putConf i g Pi nd. 2 = I nputConf i g I nt 0 = Fal l i ng

    http://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-stopbutton.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-stopbutton.bas
  • 8/12/2019 Bascom and AVR, Interrupts

    2/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Di m Wt i me As Byt e

    On I nt 0 St opbut t on

    Cl s

    Wt i me = 255

    Enabl e I nt er r upt sEnabl e I nt 0

    DoSet Por t d. 6

    Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i me

    Loop

    Stopbutt on:Lcd "st op! "

    Ret ur n

    End

    Pind.2 is configured as input, don't forget the 10k pull-up resistor!Config Int0 Falling: Int0 is to happen only when the voltage level on Pind.2 is going from high to low.When the Int0 interrupt occurs, the program will jump to the Stopbutton label.Interrupts in general and the Int0 interrupt in particular are enabled.In the Do Loop, the 'complicated task' is flashing a Led.

    The Stopbutton routine will write "stop!" to the Lcd, then return to the program at the the point where is was interrupted.

    What will happen is that the Led flashes on and off, most of the time will be spent in the Waitms commands. When the buttonis pressed, the program will jump to the Stopbutton label and write "stop!" to the Lcd and return to flashing the Led.

    Is that what happens? Alas, no. You will see "stop!" on the Lcd, but that is not all. Why? You will find out later, first anoverview of AT90S2313 interrupts:

    The AT90S2313 interrupts

    A. Interrupts with an external source:Int0 external interrupt on PortD.2, pin 6

    Int1 external interrupt on PortD.3, pin 7Counter0 overflow interrupt, PortD.4, pin 8Counter1 overflow interrupt, PortD.5, pin 9Timer1 capture interrupt, PortD.5, pin 9Timer1 output compare A interrupt, PortD.5, pin 9Serial Rx complete interruptAnalog comparator0 interrupt, PortB.0, pin 12Analog comparator1 interrupt, PortB.1, pin 13

    B. Interrupts with an internal source:Timer0 overflow interruptTimer1 overflow interrupt

    Serial data register empty interruptSerial Tx complete interrupt

    AVR interrupts all have the same priority. This is different from a lot of other controller types where you can specify whichinterrupts get priority over others.

    If you use another type of AVR controller, use the Bascom *.def file to check which types of interrupts are available. Alsocheck the controller datasheet of course.

    Interrupts on or off If you start a Bascom program all interrupts are off. They have to be enabled. With the command:

    Enabl e I nt er r upt s

    interrupts are enabled as a group. They can be disabled as well:

    Di sabl e I nt er r upt s

  • 8/12/2019 Bascom and AVR, Interrupts

    3/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    This can be useful if you have a program segment where you do want to be interrupted at all:

    Enabl e I nt er r upt sEnabl e I nt 0Enabl e Ti mer 0

    . .Di sabl e I nt er r upt s

    . .Somet hi ng ver y i mpor t ant her e. . .

    . .Ok, r eady. . .

    . .Enabl e I nt er r upt s

    . .

    Here, all interrupts are enabled or disabled as a group. Individual interrupts must be enabled separately:

    Enabl e I nt er r upt sEnabl e I nt 0Enabl e RX0Enabl e Count er 0

    . .

    And they can be disabled individually:

    . .Di sabl e Counter 0

    . . no count er0 i nt er r upt s her e. . .Enabl e Count er 0

    . .

    Interrupt routinesEvery interrupt has to be handled in a seperate routine. A routine is a program fragment with a label, program lines and aReturn statement. For every interrupt you enable, you must specifiy which routine it has to jump to:

    On I nt 0 St opbut t onOn I nt 1 LcdMenuOn Count er 0 Revcal c

    . .Enabl e I nt er r upt sEnabl e I nt 0Enabl e I nt 1Enabl e Count er 0

    . .Mai n pr ogr am

    . .Stopbutt on:Lcd "st op! "

    . . .Ret ur n

    LcdMenu:Cl sLcd "Cal i br at e: press A". . .

    Ret ur n

    Revcal c:Revs = Count er 0 * Revf act orCount er 0 = 0. . .

    Ret ur n

    Note that all interrupt routines start with a label, a name ending with the ":" character.No interrupts in an interrupt routineAs soon as the program is interrupted and jumps to the relevant interrupt routine, all enabled interrupts are disabled as long asthe routine is busy. Once the routine reaches the Return statement and jumps back to the place where it left the main program,disabled interrupts are enabled again. This is to avoid routines from interrupting themselves and thus ending as the snake thatate himself. This behaviour that differs from most other controller brands, is in the AVR architecture, not in Bascom.

    Keep it short

    Keep all your interrupts routines as short and simple as you can. Remember that your program is >interrupted< from what iswas doing and that it should probably not be interrupted for too long. Try to do not more in interrupt routines than keeping

    track of counters or flags that are processed in the main program once the program is ready to do so.Compare this program:

    On I nt 0 St opbut t on

    Enabl e I nt er r upt sEnabl e I nt 0

  • 8/12/2019 Bascom and AVR, Interrupts

    4/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Do. . .

    LoopEnd

    Stopbutt on:Lcd "st op! ". . .Do somet hi ng compl i cat ed. . .

    Ret ur n

    with this program:

    Di m St opf l ag as Bi t

    On I nt 0 St opbut t on

    Enabl e I nt er r upt sEnabl e I nt 0

    Do. . .I f St opf l ag = 1 Then

    Reset St opf l ag. . .Do somet hi ng compl i cat ed. . .. . .

    End I f LoopEnd

    Stopbutt on:Set St opf l ag

    Ret ur n

    The Stopbutton routine is now kept to its minimum, only bit Stopflag is set. This is handled in the main program. Be awarethough, that in this way you may react later to an interrupt than desired.

    More on Int0/Int1

    Into and Int1 are external interrupts. They are typically generated by a pushbuttons, switches or pulses or level changes fromother circuits. You can select how these interrupts are honoured:

    Conf i g I nt x = Low LevelConf i g I nt x = Fal l i ngConf i g I nt x = Ri si ng

    So, interrupts are generated as long as the Intx pin is low, for any level going high to low, or for any level going low to high.Note that Low Level keeps generating interrupts for as long as the level is low. As an example:

    $r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Di m Cnt r As I nt eger

    On I nt 0 Butt onConf i g I nt 0 = Low Level

    Cl s

    Enabl e I nt er r upt sEnabl e I nt 0

    DoLocat e 1 , 1Lcd Cnt rWai t ms 250

    Loop

    But t on:I ncr Cnt r

    Ret ur n

    End

    In the interruptroutine a counter is incremented, the value of the counter is displayed four times per second on the Lcd.

    Not documented in the Bascom help files (as far as I know) is the fact that Low Level seems to be the default for the Intxconfiguration.Generating interrupts on the Falling or Rising edge of an input pulse is much more common.

    Bouncing buttons

    A perfect pushbutton will switch directly from open to close and back to open when released. Alas, real world pushbuttons

  • 8/12/2019 Bascom and AVR, Interrupts

    5/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    have a phenomenon called 'bounce'. When pressed, a button may 'oscillate' between open a close for a short while beforesettling in the closed position. The 'short while' may take as long as 50 milliseconds for some types. I made a 'snaphot' of abutton being pushed, pulling Vcc low through a 10k pull-up:

    Actually this is a very good pushbutton: it takes only 0.5 milliseconds for the button to settle in the closed position.Now, if your program would react to every falling edge, you would have to handle a lot of interrupts. The better approach is

    to use 'debouncing'. What we could do is simply add a wait in the interrupt routine:interrupt-debounce-self.bas

    $r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out putConf i g Pi nd. 2 = I nputConf i g I nt 0 = Fal l i ngConf i g Debounce = 50

    Di m Wt i me As Byt eConst Debouncet i me = 75

    On I nt 0 St opbut t on

    Cl s

    Wt i me = 255

    Enabl e I nt er r upt sEnabl e I nt 0

    DoSet Por t d. 6Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i me

    Loop

    Stopbutt on:Lcd "st op! "Wai t ms Debouncet i meGi f r = 64

    Ret ur n

    End

    In the interrupt routine a simple wait is added. In the routine, interrupts are disabled, so the program will only react once tothe button being pressed. Choose the value of Debouncetime so that is longer than the longest 'bounce' time of the pushbuttonsyou use.Bascom also has a Debounce command which you can use in the interrupt routine.

    What is this strange Gifr?

    In the example above, the interrupt routine has a strange command: Gifr = 64. What is its purpose? Remove this command and see what happens: Almost always the text "stop!" is written twice on the Lcd. How is that possible?Examine this program:

    interrupt-deb-pre.bas$r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out putConf i g Pi nd. 2 = I nputConf i g I nt 0 = Fal l i ng

    http://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-debounce-self.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-deb-pre.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-deb-pre.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-debounce-self.bas
  • 8/12/2019 Bascom and AVR, Interrupts

    6/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Di m Wt i me As Byt e

    On I nt 0 St opbut t on

    Cl sSet Por t d. 6Wai t ms 3000Reset Por t d. 6Wt i me = 255

    Enabl e I nt er r upt sEnabl e I nt 0

    DoSet Por t d. 6Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i me

    Loop

    Stopbutt on:Lcd "st op! "Wai t ms 75

    Ret ur n

    End

    After the Lcd is cleared, the Led is switched on. A 3 second wait follows, the Led is switched off, and the interrupts areenabled. Now, reset the controller and try to press the stop button during the time the Led is on. You have three seconds to dothat, so that should be no problem. After the Led is off, Int0 is enabled and you will observe that immediately the programumps to the Stopbutton routine. So, even if you press the button >before< the interrupt is enabled, the interrupt is apparently

    stored somewhere and when interrupts are enabled, it is immediately processed.

    And that is what happens in the AT90S2313 controller (and in all the other AVR's). Even if the relevant interrupt is notenabled, the moment an Int0/Int1 interrupt occurs it is stored in the General Interrupt Flag Register. This is one of the raremoments where even Bascom users have to delve into the inner architecture of the AVR's. The Gifr register has eight bitswhere bits six and seven are reserved for Int0 and Int1 respectively:

    And this is also what happens in our interrupt-debounce-self.bas program. In the interrupt routine, when interrupts aredisabled, new interrupts arrive due to button 'bounce'. The interrupts are flagged as zero bit in the Gifr register. Int0 interruptsin bit six, Int1 in bit seven. All we have to do to solve the problem is 'set' the relevant bit in the Gifr register just before theReturn statement in the interrupt routine. In this way, if the program resumes its normal course, no further (old) interrupts arehonoured.

    Reserved register names

    We now notice, that apparently there are a number of 'special' register names. Avoid using these names in your Bascomprograms. Consult the Bascom help (start Bascom help, choose find and type 'AVR internal registers') and the AT90S2313datasheet (table 1, page 15 and 16)>

    Reading a rotary encoder or keyboard on interruptRotary encoders and small keyboards are typically read on interrupt. This is described separately in the encoder and keyboard chapters.

    Timers and CountersThe AT90S2313 has two timer/counters (make sure you read from page 27 onwards). Timer0 is an eight-bit timer/counter.We can use it to measure time (by counting clock pulses) or to count external events on the T0 pin. As it is an eight-bittimer/counter, the value can be from 0 to 255.With Config we can choose from all the possibilities:

    Conf i g Ti mer 0 = Ti mer, Pr escal e = 1| 8| 64| 256| 1024

    Here, Timer0 is configured as timer with the controllerclock as input. It counts the clock pulses as they are output by aprescaler with a division ratio of 1, 8, 64, 256 or 1024. With the division ratio you can controller the time it takes for Timer0to go from 0 to 255 counts. Let us assume a 4MHz clock and a prescale ratio of 1024. Timer0 will be incremented everyPrescaler-ratio/Fclock or 1024/4.000.000 = 0.256 milliseconds. It will overflow in 255 * 0.256 = 65 milliseconds.Timer1 is identical to Timer0 but it as it is a 16-bit counter it can count to 65535 before overflowing to zero.

    http://www.qsl.net/p/pa3ckr/bascom%20and%20avr/encoders/index.htmlhttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/keyboards/index.htmlhttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/keyboards/index.htmlhttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/encoders/index.html
  • 8/12/2019 Bascom and AVR, Interrupts

    7/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Timer0 and Timer1 start running as soon as they are configured.An example of a free-running Timer0: (you only need an Lcd attached to the AT90S2313)interrupt-timer0-free.bas

    $r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out putConf i g Pi nb. 1 = Out putConf i g Ti mer 0 = Ti mer , Pr escal e = 1024

    Di m Wt i me As Byt eDi m Ti mer count er As Byt e

    Wt i me = 100Ti mer count er = 0

    DoSet Por t d. 6Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i meCl sTi mer count er = Ti mer 0Lcd " t mr cnt r : " ; Ti mercount er

    Loop

    End

    And a free running Timer1:

    interrupt-timer1-free.bas$r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out putConf i g Pi nb. 1 = Out putConf i g Ti mer 1 = Ti mer , Pr escal e = 1024

    Di m Wt i me As Byt eDi m Ti mer count er As Wor d

    Wt i me = 100Ti mer count er = 0

    DoSet Por t d. 6Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i meCl sTi mer count er = Ti mer 1Lcd " t mr cnt r : " ; Ti mercount er

    Loop

    End

    Of course the main difference is that here Timercounter is dimensioned as Word .The actual value of Timer0 and Timer1 can be read at any time and copied into a properly dimansioned variable.

    The two examples above used free-running timers. They can be started and stopped at any time:

    St ar t Ti mer 0

    St op Ti mer 0St ar t Ti mer 1St op Ti mer 1

    And as these are just normal AVR registers, you can not only read the current value but also write a value into the registers.:

    St op Ti mer 1. .

    Ti mer 1 = 132. .

    St ar t Ti mer 1

    Timer interruptsOne of the more common applications of timers is to interrupt the program at regular intervals to do something else. For example checking if an input pin has changed level, generate an output pulse etc.

    An example for generating output pulses: (use the stopbutton schematic, monitor output on pin Portb.1)interrupt-timer0-pulse.bas

    $r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out put

    http://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer0-free.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer1-free.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer0-pulse.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer0-pulse.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer1-free.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer0-free.bas
  • 8/12/2019 Bascom and AVR, Interrupts

    8/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Conf i g Pi nb. 1 = Out putConf i g Ti mer 0 = Ti mer , Pr escal e = 64

    Di m Wt i me As Byt e

    On Ti mer 0 Pul se:

    Wt i me = 100

    Enabl e I nt er r upt sEnabl e Ti mer 0

    DoSet Por t d. 6

    Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i me

    Loop

    Pul se:Toggl e Por t b. 1

    Ret ur n

    End

    The prescaler ratio is set at 64, so Timer0 will be incremented every 16 microseconds. As it can count to 255, it will overflow after 256 * 16 = 4096microseconds. At every overflow, a Timer0 interrupt occurs and Pulse: is called. In the interrupt routine the state of Portb.1 is 'toggled', meaning that if itwas high, it is made low and v.v.The result is a nice square wave with an on and off time of app. 4 milliseconds on Portb.1:

    The AT90S2313 is pretty fast! Try a prescale value of 1 and observe the result on Portb.1. The on and off time is app. 60microseconds:

    All this is working fine as long as the time spent in the interrupt routine is smaller than the on/off time of the output ! Try thisout by inserting an Cls command in the interrupt routine. You will see that the on/off time jumps from 60 microseconds to 6milliseconds!

  • 8/12/2019 Bascom and AVR, Interrupts

    9/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Other timingsIn the examples shown thus far, the interrupt was generated on a timer overflow. This means that the timings we can realiseare limited to certain number dependant on clock speed, prescaler ratio and timer register width. One way of choosing exacttimings is to not let the timer run free from zero to maximum to zero etc., but to preload the timer register after everyoverflow with a certain value. We can choose this value such that the time it takes to get from this value to overflow isexactly the time we need:

    interrupt-timer1-preload.bas$r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out putConf i g Pi nb. 1 = Out putConf i g Ti mer 1 = Ti mer , Pr escal e = 1Const Ti mer 1pre = 65100

    Di m Wt i me As Byt eSt op Ti mer 1Ti mer 1 = Ti mer 1pre

    On Ti mer 1 Pul se:St ar t Ti mer 1

    Wt i me = 100

    Enabl e I nt er r upt sEnabl e Ti mer 1

    DoSet Por t d. 6Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i me

    Loop

    Pul se:St op Ti mer 1Ti mer 1 = Ti mer 1preToggl e Por t b. 1St ar t Ti mer 1

    Ret ur n

    End

    Timer1 is loaded with the value 65100 after every overflow. Timer1 will thus count from 65100 to 65535 before overflowingand generating an interrupt. This will take (65536 - 65100) * 0.25 = 109 microseconds. Using the constant Timer1pre you cantime the on/off time of the output pulse exactly in 0.25 microsecond resolution.

    Counting external pulsesTimer0 and Timer1 can be configured to count external pulses on the T0 and T1 input pins:

    Conf i g Ti mer0 = Count er, Prescal e = 1| 8| 64| 256| 1024, Edge = Ri si ng| Fal l i ngConf i g Ti mer1 = Count er, Prescal e = 1| 8| 64| 256| 1024, Edge = Ri si ng| Fal l i ng

    You can choose to count pulses on the falling or rising edge of an input pulse. You can also choose to have the input pulsesprescaled before counting.Especially Timer1 is interesting to act as external pulse counter as it can count to 65535 before overflowing.

    The names Timerx, Counterx and Capturex in Bascom programs all refer to the same registers, so for example the namesTimer1 and Counter1 can be mixed in a program, although that would not be good programming practice.

    http://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer1-preload.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer1-preload.bas
  • 8/12/2019 Bascom and AVR, Interrupts

    10/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Note that when Timer0 and Timer1 are used to count external pulses, the controller will sample the input the level on theinput pin at the controller clock rate. This means that to accurately count input pulses, the pulse frequency must never behigher than half the controller clock frequency. (Nyquist) To be on the safe side, keep the input pulse frequency below 40% of the controller clock. So, for a clock of 4MHz, do not try to count faster than 1.6MHz.Try this out with a TTL pulse generator attached to T1 (PortD.5, pin 9 of the AT90S2313):counter1.bas

    $r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out put

    Conf i g Ti mer 1 = Counter , Edge = Fal l i ng , Pr escal e = 1St op Count er 1

    Set Por t d. 6Wai t ms 1000Reset Por t d. 6Wai t ms 1000

    Cl s

    DoCount er 1 = 0St ar t Count er 1Wai t ms 25St op Count er 1Cl sLcd " Count er 1: " ; Count er 1

    Wai t ms 100Loop

    End

    In the Do Loop, Counter1 is cleared and started. After a 25 millisecond wait, Counter1 is stopped and its value is written to the Lcd. Note that timing with aWaitms command is not very accurate, there are better ways.Slowly increase the pulse generator frequency and observe what happens above 1.6MHz.

    Timer1 can count to 65535. If that is not enough, you can generate an interrupt on Timer1 overflow and keep track of thenumber of overflows in an interrupt routine:counter2.bas

    $r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out putConf i g Ti mer 1 = Counter , Edge = Fal l i ng , Pr escal e = 1

    Di m Wt i me As Byt eDi m Ti mer count er As Wor dDi m Over f l count er As WordDi m Total count er As Long

    On Count er 1 Uphi gh

    Wt i me = 100Ti mer count er = 0Tot al count er = 0

    Enabl e I nt er r upt sEnabl e Count er 1

    DoSet Por t d. 6Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i meCl sTi mer count er = Count er 1Lcd Ti mer count er ; " " ; Over f l count erLower l i neTot al count er = Over f l count erShi f t Tota l count er , Lef t , 16Tot al count er = Tot al counter + Ti mer count erLcd " t ot al : " ; Tot al count er

    Loop

    Uphi gh:I ncr Over f l count er

    Ret ur n

    End

    Three variables are dimensioned:Overflowcounter (16-bit Word) keeps track of the number of Timer1 overflowsTimercounter (16-bit Word) has the actual value of Timer1

    http://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/counter1.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/counter2.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/counter2.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/counter1.bas
  • 8/12/2019 Bascom and AVR, Interrupts

    11/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Totalcounter (32-bit Long) gets the value of Overflowcounter shifted 16 places to the left plus the value of Timercounter.

    Timer1 Capture

    Timer1 can be configured in the 'Capture' mode. This means that Timer1 counts the controller clock through a prescaler, and when on the ICP input (PortD.6, pin 11) a pulse arrives, the contents of the Timer1 register is copied to the input captureregister. In this way it is possible to measure the time between two pulse edges exactly:

    Conf i g Ti mer1 - Ti mer, Prescal e = 1| 8| 64| 256| 1024, Capt ur e Edge = Ri si ng| Fal l i ng

    interrupt-timer1-capture.bas$r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out putConf i g Ti mer 1 = Ti mer , Pr escal e = 64 , Capt ure Edge = Ri si ng

    Di m Wt i me As Byt eDi m Ti mer count er As Wor d

    On Capt ur e1 Capt mr

    Wt i me = 100Ti mer count er = 0

    Enabl e I nt er r upt sEnabl e Capt ure1

    Do' Set Por t d. 6Wai t ms Wt i me' Reset Por t d. 6Wai t ms Wt i meCl sLcd "pwi dt h: " ; " " ; Capt ur e1

    Loop

    Capt mr :Ti mer count er = Captur e1Ti mer 1 = 0

    Ret ur n

    End

    In the interrupt routine the value of Timer1 (Capture1 is just another name for this register) is copied to Timercounter. Timer1 is then reset. The next timethat a pulse on the ICP input arrives the same happens. So, Timercounter is a measure of the time between pulses on ICP.

    Timer1 Compare

    Timer1 has a compare register: CompareA. (Bascom also mentiones CompareB, but that register is not in the AT90S2313)This register can be loaded with a certain value. When the Timer1 value equals that of CompareA, a previously defined actioncan be performed on OC1. (PortB.3, pin 15):

    Conf i g Ti mer 1 = Ti mer, Pr escal e = 1| 8| 64| 256| 1024, Compar e A = Cl ear | Set | Toggl e| Di sconnect , Cl ear Ti mer= 0| 1

    Actions are:- Set OC1

    - Clear OC1- Toggle OC1- Disconnect OC1

    With Clear Timer you can reset Timer1 when the CompareA occurs.

    Especially the toggle function is much used to generate precise frequencies on OC1.compare.bas

    $r egf i l e = " 2313def . dat"$cr yst al = 4000000

    Conf i g Pi nd. 6 = Out putConf i g Ti mer 1 = Ti mer , Pr escal e = 1 , Compar e A = Toggl e , Cl ear Ti mer = 1

    Di m Wt i me As Byt eDi m Compval As Wor d

    Wt i me = 100

    DoFor Compval = 100 To 10000 St ep 100

    http://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer1-capture.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/compare.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/compare.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-timer1-capture.bas
  • 8/12/2019 Bascom and AVR, Interrupts

    12/13

    com and AVR, Interrupts

    ://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/index.html[06-01-2014 17:03:26]

    Compar e1a = CompvalWai t ms 10

    Next CompvalSet Por t d. 6Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i me

    Loop

    End

    In the Do Loop, the Compare1A register is preloaded with Compval, which varies between 100 and 10000. When Timer1 equals Compare1A, the pin OC1is toggled and Timer1 is cleared.

    Connect a small loudspeaker through a series resistor of several hundred ohm's to OC1 (PortB.3, pin 15) and listen to themusic...

    UART interruptsThe AT90S2313 UART has three interrupt possibilities:1. Tx ready. When the last Tx bit has been sent and no more data is in the data buffer. This interrupt can be used whenworking half-duplex and you must know when to change from send to receive. In the default full-duplex mode this interruptshas no use.2. Tx data buffer empty. This interrupt is generated when a character is written from the data buffer to the Tx buffer. It can beused to signal that the next character can be written in the data buffer. You will not often need this interrupt because Bascomarranges everything when sending a string to the UART.3. Rx ready. When a complete character has been received by the UART and has been placed in the data buffer this interrupt

    is generated.This character has to be read from the data buffer as soon as possible so that the next character can be received.Bascom also deals with receiving strings through the UART, but it can be useful to interrupt a program when a character arrives, for example to change the course of the program:interrupt-rs232rx.bas

    $r egf i l e = " 2313def . dat"$cr yst al = 4000000

    $baud = 9600

    Conf i g Pi nd. 6 = Out put

    On Ur xc Get char

    Di m Wt i me As Wor dDi m I nchar As St r i ng * 1

    Const Fast bl i nk = 100Const Sl owbl i nk = 500

    Wt i me = Sl owbl i nk

    Enabl e I nt er r upt sEnabl e Ur xc

    DoPri nt Wt i meSet Por t d. 6Wai t ms Wt i meReset Por t d. 6Wai t ms Wt i me

    Loop

    Get char :

    I nchar = I nkey( )Sel ect Case I ncharCase " f " : Wt i me = Fast bl i nkCase " s" : Wt i me = Sl owbl i nk

    End Sel ectRet ur n

    End

    When a character is received, Getchar is called. If the character is a "f", in the Do Loop the Led will flash fast, if a "s" is received it will flash slowly. Allother characters are ingnored.

    Analog Comparator

    The AT90S2313 has an analog comparator on Ain0 (pin 12) and Ain1 (pin 13). The comparator can be configured such that

    when it triggers, the Timer1 Capture function is started. It is also possible to generate an interrupt:Conf i g ACI = On| Of f , Compare = On| Of f , Tri gger = Ri si ng| Fal l i ng| Toggl e

    When Compare is configured On, the Timer1 Compare is started when the comparator triggers. With Trigger the trigger moment can be set to rising (Ain0 > Ain1), falling (Ain0 < Ain1) or toggle (Ain0 > Ain1 and Ain0 < Ain1).

    http://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-rs232rx.bashttp://www.qsl.net/p/pa3ckr/bascom%20and%20avr/interrupts/interrupt-rs232rx.bas
  • 8/12/2019 Bascom and AVR, Interrupts

    13/13

    com and AVR, Interrupts

    A possible application of the analog comparator is an eight-bit DAC, but then you need the complete PortB to construct a R-2R DAC. This is rather wasteful, a better solution would be an outside chip such as the I 2C PCF8591 which has one DACand four ADC's, all eight-bit.

    TOC

    http://www.qsl.net/p/pa3ckr/index.htmlhttp://www.qsl.net/p/pa3ckr/index.html