📄 main.c
字号:
/*--------------------------------------------------------------------------------------
* ATMEL Microcontroller Software Support - ROUSSET -
*--------------------------------------------------------------------------------------
* The software is delivered "AS IS" without warranty or condition of any
* kind, either express, implied or statutory. This includes without
* limitation any warranty or condition with respect to merchantability or
* fitness for any particular purpose, or against the infringements of
* intellectual property rights of others.
*--------------------------------------------------------------------------------------
* File Name : main.c
* Object : Initialize a Timer Counter in Capture mode to
* : measure the low period, the high period or the period
* : on a TIOx Input.
*
* Version | mm | dd | yy | author :
* 1.0 05 16 06 PFi : Creation
*
*--------------------------------------------------------------------------------------
*
* 1)Description:
*
* This test uses one of the timer channel 1 (TC1) in capture mode on TIOA pin to measure
* the period of a signal.
*
* A signal generated on timer channel 0 (TC0) in waveform mode is used to
* generate the signal to capture. The frequency of the signal generated is 1MHz.
*
* 2)Configuration and pins used in this example:
*
* - Input capture on TIOA1
* - Output waveform on TIOA0 @ 1MHZ
* WARNING : These two pins TIOA0 & TIOA1 must be connected together externaly.
*
* 3) Remarks:
*
* This example runs in polling mode.and makes no use of IRQs. To have further details
* and implementation example about using IRQs, refer to any Interrupt project examples,
* like AT91SAM7S64-Interrupt-IAR4_XX-X_X
* available on www.at91.com --> KIT --> AT91SAM7S-EK --> Software
*
***************************************************************************************/
// Include Standard files
#include "project.h"
#include <math.h> /* use of float */
/* Global variables */
unsigned int volatile TimerCounterClockDiv;
/*----- Types and Constants Definition -----*/
/***********************************************************************
Configuration of the timer to measure the period of the signal
TIOA ______ ______________
(input) | | |
|______________| |___
^ ^ ^
| | |
reset & start TC load RA load RB & disable the TC's Clock
A TIOA falling edge resets and starts the counter
A next TIOA rising edge loads RA.
A TIOA falling edge loads RB (to disable the counter)
RB contains the duration of the period
*/
/* configuration of the timer to measure the of the signal */
#define MEAS_TIOA_PERIOD (AT91C_TC_LDRA_RISING|AT91C_TC_LDRB_FALLING|AT91C_TC_ABETRG|AT91C_TC_LDBDIS|AT91C_TC_ETRGEDG_FALLING)
/* Load RA with rising edge */
/* Load RB with falling edge and disable counter*/
/* TIOA used as external trigger on a falling edge */
/* RB contains the duration of the period */
/* External trigger on rising edge */
/*************************************************************************/
/*-----------------*/
/* Clock Selection */
/*-----------------*/
#define TC_CLKS 0x7
#define TC_CLKS_MCK2 0x0 /* Timer Clock = MCK / 2 */
#define TC_CLKS_MCK8 0x1 /* Timer Clock = MCK / 8 */
#define TC_CLKS_MCK32 0x2 /* Timer Clock = MCK / 32 */
#define TC_CLKS_MCK128 0x3 /* Timer Clock = MCK / 128 */
#define TC_CLKS_MCK1024 0x4 /* Timer Clock = MCK / 1024 */
#define MAX_TC_VALUE 0xFFFF /* Max rollover value for 16-bit counter */
#define FALSE 0
#define TRUE 1
#define OUTPUT_FREQ 1000000 /* frequency to generate with timer 0 (in Hz)*/
//*------------------------- Internal Function --------------------------------
//*----------------------------------------------------------------------------
//* Function Name : AT91F_TC_Open
//* Object : Initialize Timer Counter Channel and enable is clock
//* Input Parameters : <pTC> = TC Channel Descriptor Pointer
//* <mode> = Timer Counter Mode
//* : <TimerId> = Timer peripheral ID definitions
//* Output Parameters : None
//*----------------------------------------------------------------------------
void AT91F_TC_Open ( AT91PS_TC pTC, unsigned int Mode, unsigned int TimerId)
{
unsigned int dummy;
/* First, enable the clock of the TIMER */
AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1<< TimerId ) ;
/* Disable the clock and the interrupts before to configure it */
pTC->TC_CCR = AT91C_TC_CLKDIS ;
pTC->TC_IDR = 0xFFFFFFFF ;
/* Clear status bit */
dummy = pTC->TC_SR;
/* Suppress warning variable "dummy" was set but never used */
dummy = dummy;
/* Set the Mode of the Timer Counter */
pTC->TC_CMR = Mode ;
/* Enable the clock */
pTC->TC_CCR = AT91C_TC_CLKEN ;
}
//*----------------------------------------------------------------------------
//* Function Name : void AT91F_TC_Close ( AT91PS_TC pTC)
//* Object : Stop a Timer Counter Channel and disable its clock
//* Input Parameters : <pTC> = TC Channel Descriptor Pointer
//* : <TimerId> = Timer peripheral ID definition
//* Output Parameters : None
//* Functions called : none
//*----------------------------------------------------------------------------
void AT91F_TC_Close ( AT91PS_TC pTC, unsigned int TimerId )
{
//* Disable the clock and interrupts
pTC->TC_CCR = AT91C_TC_CLKDIS ;
pTC->TC_IDR = 0xFFFFFFFF ;
//* Stop the Clock of the Timer
AT91F_PMC_DisablePeriphClock (AT91C_BASE_PMC, 1<< TimerId);
}
//*---------------------------------------------------------------------------------------------------------------
//* Function Name : AT91F_TimerCounter_MeasureTioaPeriod ( AT91PS_TC pTC, unsigned int Mode, unsigned int TimerId)
//* Object : Measure TIOA number of MCKI cycles depending on mode
//* Input Parameters : <pTC> = TC Channel Descriptor Pointer
//* : <TimerId> = channel to initialize
//* : <Measurement> = Measure the period, low period or high period
//*
//* Output Parameters : The number of Timer Counter clock cycle
//* Functions called : none
//*---------------------------------------------------------------------------------------------------------------
unsigned int AT91F_TimerCounter_MeasureTioaPeriod (AT91PS_TC pTC, unsigned int TimerId, unsigned int Measurement)
{
unsigned int status;
AT91F_TC_Open ( pTC, Measurement, TimerId);
while ( ((status = pTC->TC_SR) & AT91C_TC_LDRBS) == 0 );
//* Suppress warning variable "status" was set but never used
status = status ;
return ( pTC->TC_RB ) ;
}
//*---------------------------------------------------------------------------------------------------------------
//* Function Name : AT91F_TimerCounter_ComputeCompareValue (unsigned int MCK, unsigned int OutputFreq)
//* Object : Compute the value to write into the compare register (RA, RB or RC) accoring to the
//* : Master clock of the system (MCK in Hz) and the frequency of the signal to generate with the timer
//* : counter in Hz
//* Input Parameters : <MasterClock> = Master clock Frequency of the system in Hz
//* : <OutputFreq> = Frequency to generate on a TIOx pin in Hz
//*
//* Output Parameters : Value to write in the RA, RB or RC register.
//* Functions called : none
//*
//*---------------------------------------------------------------------------------------------------------------
unsigned int AT91F_TimerCounter_ComputeCompareValue (unsigned int MasterClock, float OutputFreq)
{
float OutputPeriod = (1/OutputFreq) ;
unsigned int CompareValue ;
float DivMin ;
DivMin = OutputPeriod * (MasterClock/MAX_TC_VALUE);
if ( (DivMin > 0) && (DivMin < 2) )
{
DivMin = 2;
TimerCounterClockDiv = TC_CLKS_MCK2;
}
if ( (DivMin > 2) && (DivMin < 8) )
{
DivMin = 8;
TimerCounterClockDiv = TC_CLKS_MCK8;
}
if ( (DivMin > 8) && (DivMin < 32) )
{
DivMin = 32;
TimerCounterClockDiv = TC_CLKS_MCK32;
}
if ( (DivMin > 32) && (DivMin < 128) )
{
DivMin = 128;
TimerCounterClockDiv = TC_CLKS_MCK128;
}
if ( (DivMin > 128) && (DivMin < 1024) )
{
DivMin = 1024;
TimerCounterClockDiv = TC_CLKS_MCK1024;
}
CompareValue = OutputPeriod * (MasterClock/DivMin);
return ( CompareValue );
}
//*--------------------------------------------------------------------------------------
//* Function Name : Main
//* Object : Software entry point
//* Input Parameters : none.
//* Output Parameters : none.
//*--------------------------------------------------------------------------------------
int main()
{
AT91PS_TC pTC1 = AT91C_BASE_TC1;
unsigned int RcValue = 0;
unsigned int TioaPeriod = 0;
/* Enable User Reset and set its minimal assertion to 960 us */
AT91C_BASE_RSTC->RSTC_RMR = AT91C_RSTC_URSTEN | (0x4<<8) | (unsigned int)(0xA5<<24);
/********************************************/
/* TIMER COUNTER 0 CONFIGURATION: Wave Mode */
/********************************************/
/* Open PIO for Timer Counter 0 */
AT91F_TC0_CfgPIO();
/* Enable TC0's clock in the PMC controller */
AT91F_TC0_CfgPMC ();
/* Disable the Clock Counter (within timer counter registers)*/
*AT91C_TC0_CCR = AT91C_TC_CLKDIS ;
/* The computed value must be divided by 2 since we are only using RC compare to
generate the signal*/
RcValue = (AT91F_TimerCounter_ComputeCompareValue (AT91B_MCK, OUTPUT_FREQ)/2) ;
/* Initialization of the TC0 to generate a signal via RC register */
*AT91C_TC0_CMR = (AT91C_TC_WAVE | /* Timer Counter in WAVE MODE */
TimerCounterClockDiv | /* TCCLKS computed from AT91F_TimerCounter_ComputeCompareValue */
AT91C_TC_CPCTRG | /* CPCTRG : Register C compare trigger enable */
AT91C_TC_ACPC_TOGGLE | /* ACPC : Register C compare toggle TIOA */
AT91C_TC_EEVT_XC0 ); /* EEVT : XC0 as external event (TIOB = output) */
/* Initialize the RC Compare Reg. to generate 1MHz signal. */
*AT91C_TC0_RC = RcValue ;
/* Enable the Clock counter (within timer counter registers) */
*AT91C_TC0_CCR = AT91C_TC_CLKEN ;
/* Start timer0 */
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG ;
/***********************************************/
/* TIMER COUNTER 1 CONFIGURATION: Capture Mode */
/***********************************************/
/* Set PIO pins for Timer Counter 1 */
AT91F_TC1_CfgPIO();
/* Enable TC1's clock in the PMC controller */
AT91F_TC1_CfgPMC ();
/* Measure the period: TioaPeriod will contain the number of the Timer Counter Clock Cycle */
TioaPeriod = AT91F_TimerCounter_MeasureTioaPeriod (pTC1, AT91C_ID_TC1, MEAS_TIOA_PERIOD);
/* Suppress warning variable "TioaPeriod" was set but never used */
TioaPeriod = TioaPeriod ;
for (;;);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -