⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 一个AT91SAM7X256 CAN计数器中断实验
💻 C
字号:
/**************************************************************************************************
*
* Copyright (C) ART Limited, 2006. All rights reserved.
*
* File: main.c
* Data: 2006.05.16
*
**************************************************************************************************/

#include "project.h"

#define IAR                      1

#define AT91B_LED1               1<<19
#define AT91B_LED4               1<<22

#define TC_CLKS                  0x7
#define TC_CLKS_MCK2             0x0
#define TC_CLKS_MCK8             0x1
#define TC_CLKS_MCK32            0x2
#define TC_CLKS_MCK128           0x3
#define TC_CLKS_MCK1024          0x4

#define TIMER0_INTERRUPT_LEVEL     1
#define TIMER1_INTERRUPT_LEVEL     7

//*-------------------------------- Internal Function -----------------------------------
//*--------------------------------------------------------------------------------------
//* Object              : Initialize Timer Counter Channel and enable is clock
//* Input Parameters    : <tc_pt>   = TC Channel Descriptor Pointer
//*                       <mode>    = Timer Counter Mode
//*                     : <TimerId> = Timer peripheral ID definitions
//* Output Parameters   : None
//*--------------------------------------------------------------------------------------
void AT91F_TC_Open(AT91PS_TC TC_pt, 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
	TC_pt->TC_CCR = AT91C_TC_CLKDIS;
	TC_pt->TC_IDR = 0xFFFFFFFF;

	//* Clear status bit
	dummy = TC_pt->TC_SR;

	//* Set the Mode of the Timer Counter
	TC_pt->TC_CMR = Mode;

	//* Enable the clock
	TC_pt->TC_CCR = AT91C_TC_CLKEN;
}

//*--------------------------------------------------------------------------------------
//* Object              : C handler interrupt function
//* Input Parameters    : none.
//* Output Parameters   : none.
//*--------------------------------------------------------------------------------------
#if IAR
__ramfunc void timer1_c_irq_handler(void)
#else
void __irq timer1_c_irq_handler(void)
#endif
{
	unsigned int dummy;
	AT91PS_TC TC_pt = AT91C_BASE_TC1;

	//* AcknowAT91B_LEDge interrupt status
	dummy = TC_pt->TC_SR;

	//* Read the output state
	if((AT91F_PIO_GetInput(AT91C_BASE_PIOB) & AT91B_LED4) == AT91B_LED4)
	{
		AT91F_PIO_ClearOutput(AT91C_BASE_PIOB, AT91B_LED4);
	}
	else
	{
		AT91F_PIO_SetOutput(AT91C_BASE_PIOB, AT91B_LED4);
	}
	AT91F_AIC_AcknowledgeIt(AT91C_BASE_AIC);
}

//*--------------------------------------------------------------------------------------
//* Object              : Init timer counter
//* Input Parameters    : none.
//* Output Parameters   : none.
//*--------------------------------------------------------------------------------------
void timer_init(void)
{
	AT91PS_AIC pAic = AT91C_BASE_AIC;

	//* Open timer1
	AT91F_TC_Open(AT91C_BASE_TC1, TC_CLKS_MCK128, AT91C_ID_TC1);

	//* Open Timer 1 interrupt
#if IAR
	AT91F_AIC_ConfigureIt(pAic,
						  AT91C_ID_TC1,
						  TIMER1_INTERRUPT_LEVEL,
						  AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
						  timer1_c_irq_handler);
#else
	//* Disable the interrupt on the interrupt controller
	pAic->AIC_IDCR = 0x1 << AT91C_ID_TC1;
	//* Save the interrupt handler routine pointer and the interrupt priority
	pAic->AIC_SVR[AT91C_ID_TC1] = (unsigned int)timer1_c_irq_handler;
	//* Store the Source Mode Register
	pAic->AIC_SMR[AT91C_ID_TC1] = TIMER1_INTERRUPT_LEVEL | AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL;
	//* Clear the interrupt on the interrupt controller
	pAic->AIC_ICCR = 0x1 << AT91C_ID_TC1;
#endif
	AT91C_BASE_TC1->TC_IER = AT91C_TC_CPCS;         // IRQ enable CPC
	AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_TC1);

	//* Start timer1
	AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG;
}

#define AT91B_MAIN_OSC  18432000  // Main Oscillator MAINCK

#define SPEED  (AT91B_MAIN_OSC/1000)

unsigned int LedSpeed = SPEED * 20;
//*--------------------------------------------------------------------------------------
//* Object              : Software waiting loop
//* Input Parameters    : none.
//* Output Parameters   : none.
//*--------------------------------------------------------------------------------------
void wait ( void )
{
	unsigned int waiting_time;
	
	for(waiting_time=0; waiting_time<LedSpeed; waiting_time++);
}

//*-------------------------------- External Function -----------------------------------
//*--------------------------------------------------------------------------------------
//* Object              : Software entry point
//* Input Parameters    : none.
//* Output Parameters   : none.
//*--------------------------------------------------------------------------------------
int main()
{
	// Enable User Reset and set its minimal assertion to 960 us
//	AT91C_BASE_RSTC->RSTC_RMR = AT91C_RSTC_URSTEN | (0x04<<8) | (0xA5<<24);

	// First, enable the clock of the PIO
	AT91F_PMC_EnablePeriphClock(AT91C_BASE_PMC, 1<<AT91C_ID_PIOB);

    // Then, we configure the PIO Lines corresponding to LED1 to LED4
	AT91F_PIO_CfgOutput(AT91C_BASE_PIOB, (AT91B_LED1|AT91B_LED4));

	// Init timer interrupt
	timer_init();

	// Loop forever
	for(;;)
	{
		AT91F_PIO_ClearOutput(AT91C_BASE_PIOB, AT91B_LED1);
		wait();
		AT91F_PIO_SetOutput(AT91C_BASE_PIOB, AT91B_LED1);
		wait();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -