Page 93 of 346 Start Back Next End ZBasic System Library 81 ZBasic Microcontrollers CountTransitions Type Function returning Long Invocation CountTransitions(pin, interval) Parameter Method Type Description pin ByVal Byte The pin on which logic transitions will be counted. interval ByVal Single or Long The time interval specified in seconds or I/O Timer ticks, respectively, during which transitions will be counted. See the discussion below for information on range and resolution. Discussion When called, this routine will begin counting logic transitions on the specified pin and will continue until the specified interval has elapsed. During the counting process processor interrupts are disabled. This strategy allows high precision in measuring the interval but has the drawback that other processes that utilize interrupts will not function correctly. Among such affected processes are all serial communication and multi-tasking. For this reason, the counting interval should be kept as short as possible. RTC ticks that occur during the counting process are accumulated and the RTC is updated when the counting is finished. The specified pin, which you must configure to be an input before calling, is sampled at a fixed rate of approximately 1/36 (ATtiny, ATmega) or 1/40 (ATxmega) of the CPU frequency. The sample rate, default resolution and maximum measurement interval are shown in the table below for various CPU frequencies. You may modify the range and resolution of the measurement interval by modifying the built-in variable Register.TimerSpeed1. See the special section I/O Timer Prescaler Values for more details. Important Values for ZX Devices Processor Family Frequency Sample Rate Default Resolution Maximum Interval ATmega 7.3738 MHz 204.8 KHz 4.883 µS 10,485 sec. ATmega 14.7456 MHz 409.6 KHz 2.441 µS 5,242 sec. ATmega 18.432 MHz 512.0 KHz 1.953 µS 4,194 sec. ATxmega 29.4912 MHz 737.3 KHz 1.356 µS 2,912 sec. For generic target devices, which can operate at an arbitrary main frequency and RTC frequency, the important values related to CountTransitions are shown in the table below where F_CPU is the main processor frequency. Important Values for Generic Target Devices Processor Family Sample Rate F_SAMP Default Resolution Maximum Interval ATtiny, ATmega F_CPU/36 1/F_SAMP 2147483647/F_SAMP ATxmega F_CPU/40 1/F_SAMP 2147483647/F_SAMP Resource Usage This function uses the I/O Timer and disables interrupts during the counting process. However, RTC ticks are accumulated during the process and the RTC is updated upon completion. The maximum number of missed RTC ticks that can be tracked is 65,535. A measurement interval longer than that number of RTC fast ticks will result in incorrect RTC accumulator values. The maximum measurement interval for correct adjustment the RTC is shown in the tables above. Compatibility In BasicX missed RTC ticks are not accounted for.