69 of 318
ZBasic System Library 59 ZX Microcontroller Family 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 (ATxmega) 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 on Timers for more details. Important CountTransitions Values Processor Family Frequency Sample Rate Default Resolution Maximum Interval Max. RTC Adj. Interval ATmega 7.3738 MHz 204.8 KHz 4.883 µS 10,485 sec. 128 sec. ATmega 14.7456 MHz 409.6 KHz 2.441 µS 5,242 sec. 64 sec. ATmega 18.432 MHz 512.0 KHz 1.953 µS 4,194 sec. 65 sec. ATxmega 29.4912 MHz 737.3 KHz 1.356 µS 2,912 sec. 64 sec. 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 fast RTC ticks will result in incorrect RTC accumulator values. The maximum measurement interval for correct adjustment the RTC is shown in the table above. Compatibility In BasicX missed RTC ticks are not accounted for.