Purpose
This example indicates how to use the Timer Counter in capture mode and waveform mode in order to measure the pulse frequency and count the total pulse number of an external signal injected into the device's TIOA pin.
Requirements
This package can be used with SAM4 evaluation kits such as the SAM4L EK, the SAM4L Xplained Pro, and other evaluation kits. Refer to the list of kits available for the actual device on http://www.microchip.com.
It generates a waveform on the Timer Counter pin PIN_TC_WAVEFORM, and it captures a waveform from pin PIN_TC_CAPTURE. Refer to the respective device evaluation kit's board file (conf_board.h) in order to examine these #defines.
Connect PIN_TC_WAVEFORM to PIN_TC_CAPTURE on the evaluation kit.
Description
This example shows how to configure the Timer Counter in both waveform and capture operating modes. In capture mode, a pulse signal (output from PIN_TC_WAVEFORM) is connected to PIN_TC_CAPTURE, and Register A (RA) and Register B (RB) will be loaded when their programmed event occurs.
When the Timer Counter interrupt occurs, its interrupt handler reads the RA and RB register values (for computing pulse frequency) and also increases the total pulse count number. The current pulse frequency and total pulse count number are both output via the debug UART.
The code can be roughly broken down as follows:
-
Select pre-defined waveform frequency and duty cycle to be generated
-
Configure TC_CHANNEL_WAVEFORM as waveform output
-
Configure TC_CHANNEL_CAPTURE as a capture input
-
Configure capture Register A (RA) to be loaded when a rising edge on TIOA occurs
-
Configure capture Register B (RB) to be loaded when a falling edge on TIOA occurs
-
Configure a Timer Counter interrupt and enable the RB load interrupt
-
Pressing 'c' in the terminal window, starts capture
-
Pressing 's' in the terminal window, stops capture and dumps the information captured so far
Main Files
Compilation Information
This software is written for GNU GCC and IAR Embedded Workbench® for Atmel®. Other compilers may or may not work.
Usage
- Build the program and download it into the evaluation board.
- On the computer, open, and configure a terminal application. (e.g., HyperTerminal on Microsoft® Windows®) with these settings:
- 115200 baud
- 8 bits of data
- No parity
- 1 stop bit
- No flow control
- Start the application.
- In the terminal window, the following text should appear:
* --
TC capture waveform example xxx --
* -- xxxxxx-xx
* -- Compiled: xxx xx xxxx xx:xx:xx --
- Choose the item from the following menu to test:
* Menu :
* ------
* Output waveform property:
* 0: Set Frequency = 178 Hz, Duty Cycle = 30%
* 1: Set Frequency = 375 Hz, Duty Cycle = 50%
* 2: Set Frequency = 800 Hz, Duty Cycle = 75%
* 3: Set Frequency = 1000 Hz, Duty Cycle = 80%
* 4: Set Frequency = 4000 Hz, Duty Cycle = 55%
* -------------------------------------------
* s: Stop capture and display captured information
* h: Display menu
* ------