?? main.c
字號:
// Include Standard files
#include "AT91SAM7X-EK.h"
#include "AT91SAM7X256.h"
#include "lib_AT91SAM7X256.h"
#define USART0_INTERRUPT_LEVEL 6
#define USART1_INTERRUPT_LEVEL 7
#define TIMER0_INTERRUPT_LEVEL 1
#define TIMER1_INTERRUPT_LEVEL 7
#define AT91_BAUD_RATE 115200
#define WAIT_TIME AT91B_MCK
/*-----------------*/
/* Clock Selection */
/*-----------------*/
#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
int count_timer0_interrupt;
static const char szembed_header[]=
{
"\n\r================================================"
"\n\r== Test Timer Program =="
"\n\r================================================\n\r"
};
//*--------------------------------------------------------------------------------------
//* Function Name : Usart_Send_One_Character
//* Object : Test
//* Input Parameters : one char to send
//* Output Parameters : none
//*--------------------------------------------------------------------------------------
void Usart_Send_One_Char(int character)
{
unsigned int status;
AT91PS_USART COM0 = AT91C_BASE_US0;
status = COM0 -> US_CSR;
while ( (status & AT91C_US_TXRDY) == 0 )
{
status = COM0 -> US_CSR;
}
AT91F_US_PutChar(COM0 , character);
}
//*----------------------------------------------------------------------------
//* Function Name : Usart_c_irq_handler
//* Object : C handler interrupt function
//* Input Parameters : none
//* Output Parameters : none
//*----------------------------------------------------------------------------
void Usart_c_irq_handler(void)
{
AT91PS_USART COM0 = AT91C_BASE_US0;
unsigned int status;
//* get Usart status register
status = COM0->US_CSR;
if ( status & AT91C_US_RXRDY)
{
//* Get byte and send
AT91F_US_PutChar(COM0,AT91F_US_GetChar(COM0));
}
//* Check error
if ( status & AT91C_US_FRAME)
{
AT91F_US_PutChar (COM0, 'F');
}
//* Reset the satus bit
COM0 ->US_CR = AT91C_US_RSTSTA;
}
//*----------------------------------------------------------------------------
//* Function Name : Usart_init
//* Object : USART initialization
//* Input Parameters : none
//* Output Parameters : TRUE
//*----------------------------------------------------------------------------
void Usart_init ( void )
{
AT91PS_USART COM0 = AT91C_BASE_US0;
// First, enable the clock of the USART
AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_US0 ) ;
//* Configure PIO controllers to periph mode
AT91F_PIO_CfgPeriph( AT91C_BASE_PIOA,
((unsigned int) AT91C_PA0_RXD0 ) |
((unsigned int) AT91C_PA1_TXD0 ) |
((unsigned int) AT91C_PA3_RTS0 ) |
((unsigned int) AT91C_PA4_CTS0 ), // Peripheral A
0); // Peripheral B
// Usart Configure
AT91F_US_Configure (COM0, AT91B_MCK, AT91C_US_ASYNC_MODE, AT91_BAUD_RATE, 0);
// Enable usart
COM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
//* Enable USART IT error and RXRDY
AT91F_US_EnableIt(COM0, AT91C_US_FRAME | AT91C_US_RXRDY);
//* open Usart 0 interrupt
AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_US0, USART0_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, Usart_c_irq_handler);
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_US0);
AT91F_US_SendFrame(COM0,(char *)szembed_header,sizeof(szembed_header),0,0);
}
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;
//* Suppress warning variable "dummy" was set but never used
dummy = dummy;
//* Set the Mode of the Timer Counter
TC_pt->TC_CMR = Mode ;
//* Enable the clock
TC_pt->TC_CCR = AT91C_TC_CLKEN ;
}
//*----------------------------------------------------------------------------
//* Function Name : timer0_c_irq_handler
//* Object : C handler interrupt function calAT91B_LED by the interrupts
//* assembling routine
//* Output Parameters : increment count_timer0_interrupt
//*----------------------------------------------------------------------------
void timer0_c_irq_handler(void)
{
AT91PS_TC TC_pt = AT91C_BASE_TC0;
unsigned int dummy;
//* AcknowAT91B_LEDge interrupt status
dummy = TC_pt->TC_SR;
//* Suppress warning variable "dummy" was set but never used
dummy = dummy;
count_timer0_interrupt++;
//* Read the output state
Usart_Send_One_Char('\n');
Usart_Send_One_Char('\r');
}
//*----------------------------------------------------------------------------
//* Function Name : timer_init
//* Object : Init timer counter
//* Input Parameters : none
//* Output Parameters : TRUE
//*----------------------------------------------------------------------------
void timer_init ( void )
{
//init the timer interrupt counter
count_timer0_interrupt=0;
//* Open timer0
AT91F_TC_Open(AT91C_BASE_TC0,TC_CLKS_MCK1024,AT91C_ID_TC0);
//* Open Timer 0 interrupt
AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_TC0, TIMER0_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, timer0_c_irq_handler);
AT91C_BASE_TC0->TC_IER = AT91C_TC_CPCS; // IRQ enable CPC
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC0);
//* Start timer0
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG ;
}
//*----------------------------------------------------------------------------
//* Function Name : delay
//* Object : Wait
//* Input Parameters : none
//* Output Parameters : none
//* Functions calAT91B_LED : none
//*----------------------------------------------------------------------------
void delay ( void )
{
//* Set in Volatile for Optimisation
volatile unsigned int i ;
//* loop delay
for ( i = 0 ;(i < WAIT_TIME/50 );i++ ) ;
}
//*--------------------------------------------------------------------------------------
//* Function Name : Main
//* Object : Software entry point
//* Input Parameters : none.
//* Output Parameters : none.
//*--------------------------------------------------------------------------------------
int main()
{
int test_char;
Usart_init();
timer_init();
delay();
for(test_char='A';;(test_char=='Z')?(test_char='A'):(test_char++))
{
Usart_Send_One_Char(test_char);
delay();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -