' file: INT0_test.bas ' this program sets up an interrupt on channel INT0 on the DARC Board ' and sets timer0 to run and count time between edges seen on the INTO ' channel. Note that reads as Int zero not the word into. ' There are two interrupt service routines in this program (ISRs) ' 1) Isr_int0 - whenever a falling edge occurs on INTO this ISR fires. ' the rising edge as a trigger condition is set by setting the low four bits ' of the register Mcucr ' 2) Isr_timer0overflow - when timer 0 counts to its max value of 0xFF, it ' overflows and resets to 00. In order to maintain accurate count of time ' the program needs to know when this overflow happens. In this program ' the variable cntr is incremented each time the overflow happens, thus ' cntr keeps track of how many times timer 0 has counted from 0 up to 255 ' (timer 0 is 8 bit timer) at 8 us per count for a total of 2.948 ms. ' So if cntr = 2 and timer0 count value = 3 that would measure a total ' time of 2*2.048 ms + 3*8us = 2.048008 ms or .002048008 seconds. '-------------------------------------------------------------- ' put in the cal value for your part here (marked on the 8535 case ' most are hex 9F, some 91's and some in between Osccal = &H9F $crystal = 8000000 Dim Cntr As Byte ' timer 0 is configured as a timer. Prescale 64 divides the system clock ' of 8 MHz by 64. This is 8000000 / 64 = 125000 so timer 0 clock is 125 Khz. ' So each count of timer 0 is 8 microseconds of time passed ' (1/125KHz = .000008 seconds) Config Timer0 = Timer , Prescale = 64 Dim Cur_cnt As Byte , Last_cnt As Byte , Total_cnt As Byte ' the enable interrupts command turns on global interrupts on the DARC board Enable Interrupts ' Mcucr register is MCU control register. Low 4 bits set what condition causes ' an interrupt for INT1 and INT0. this sets for rising edge trigger on INT0 Mcucr = Mcucr Or &H0F ' Global interrupts are turned on above. these two statements enable the ' specific interrupts of Int0 and Overflow on timer0 Enable Int0 Enable Ovf0 ' the On instruction specifies to what routine the program is to jump when these ' interrups occur On Ovf0 Isr_timer0overflow On Int0 Isr_int0 ' in the main routine, a rising edge on INT0 causes the isr to fire. the isr's ' capture the elapsed time and the main routine tallies the total elapsed time. ' The cur_cnt, last_cnt, and total_cnt variables are used to determine if the ' overflow of the timer0 happened since the last rising edge caused INTO to fire. ' For instance, an interrupt happens when timer0 is = 0x01. and the next happens ' at timer0 = 0x05. The total elapsed time is 5-1 = 4 counts * 8 us = 32 us. ' But if timer0 = 0xFE and the next capture happens at timer0 = 0x02 the calculation ' of 0x02 - 0xFE results in a negative number. Overflow ISR keeps track of ' the timer0 rolling over from 0xFF to 0x00 and in that condition this routine ' knows to subtract 0xFF - 0xFE = 1 then add 0x02 for total time 3 * 8us = 24 us. Main: Do Loop ' interrupt service routine - isr_int0. Program execution jumps to this routine ' whenever an interrupt for channel 0 occurs. In this program, when a rising ' edge is detected on INT0. Isr_int0: Cur_cnt = Cntr 'Print "cur " ; Cur_cnt 'Print "last " ; Last_cnt If Cur_cnt > Last_cnt Then Total_cnt = Cur_cnt - Last_cnt Else Total_cnt = 255 - Last_cnt Total_cnt = Total_cnt + Cur_cnt End If Print "total: " ; Total_cnt Last_cnt = Cur_cnt Return ' interrupt service routine - isr_timer0overflow. Program execution jumps to ' this routine when an overflow happens on timer0 (ie, when timer0's free running ' counter counts up to 255 (0xFF) and must then roll over to 0 since it is an 8 ' bit counter. in this ISR, the variable Cntr is incremented. Isr_timer0overflow: Incr Cntr 'Print "cntr:" ; Cntr Return