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

📄 main.c

📁 自已编at91rm9200的串口通讯程序。可供初学者参考
💻 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              : Basic twi example. Write a byte into EEPROM and read it
//* Creation            : NL   25/11/02
//*
//*----------------------------------------------------------------------------
#include "main.h"

#define AT91_SYS                ((AT91PS_SYS) AT91C_BASE_SYS)

#define PUART0                 ((AT91PS_USART)AT91C_BASE_US0)

#define AT91MASTER_CLOCK      60000000 //60MHz


// Standard Asynchronous Mode : 8 bits , 1 stop , no parity
#define AT91C_US_ASYNC_MODE ( AT91C_US_USMODE_NORMAL + \
                        AT91C_US_NBSTOP_1_BIT + \
                        AT91C_US_PAR_NONE + \
                        AT91C_US_CHRL_8_BITS + \
                        AT91C_US_CLKS_CLOCK )
                        
#define TXD0       AT91C_PIO_PA17
#define RXD0       AT91C_PIO_PA18
#define BUFSIZEMAX            1024
#define BUFSIZE               16

static int bufsizetx = BUFSIZE;
static int bufsizerx = BUFSIZE;
static unsigned char BUFTX[BUFSIZEMAX];
static unsigned char BUFRX[BUFSIZEMAX];

static void UartInit(void);
static unsigned int UartBaudrate(const unsigned int main_clock,const unsigned int baud_rate);
static void UartBaudConfig(AT91PS_USART puart, unsigned int baudrate, unsigned int timeguard);
static void UartEnableTx(AT91PS_USART puart);
static void UartDisableTx(AT91PS_USART puart);
static unsigned int UartTxReady(AT91PS_USART puart);
static void UartPutChar(AT91PS_USART puart,unsigned char character);
static void UartResetTx(AT91PS_USART puart);
static void UartEnableRx(AT91PS_USART puart);
static unsigned int UartRxReady(AT91PS_USART pUSART);
static unsigned char UartGetChar(const AT91PS_USART puart);
static void UartResetRx(AT91PS_USART puart);
static void UartClose(AT91PS_USART puart);
static unsigned int UartError(AT91PS_USART puart);

/*
 * Enable Configuration TXD0 RXD0 interface.
 */
static void UartInit(void) {
                                                                                             
    // enable direct Peripheral A 
    AT91_SYS->PIOA_ASR |= TXD0 | RXD0;
    AT91_SYS->PIOA_BSR |= 0;
    // disable pio interface
    AT91_SYS->PIOA_PDR |= TXD0 | RXD0;
 	AT91_SYS->PMC_PCER |= 1 << AT91C_ID_US0;
}

static unsigned int UartBaudrate(
	const unsigned int main_clock, // \arg peripheral clock
	const unsigned int baud_rate)  // \arg UART baudrate
{
	unsigned int baud_value = ((main_clock*10)/(baud_rate * 16));
	if ((baud_value % 10) >= 5)
		baud_value = (baud_value / 10) + 1;
	else
		baud_value /= 10;
	return baud_value;
}

static void UartBaudConfig(AT91PS_USART puart, unsigned int baudrate, unsigned int timeguard)
{
    // Disable interrupts
    puart->US_IDR = 0xFFFFFFFF;

    // Reset receiver and transmitter
    puart->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS | AT91C_US_TXDIS ;

	// Define the baud rate divisor register
	puart->US_BRGR = UartBaudrate(AT91MASTER_CLOCK, baudrate);
		
	// Write the Timeguard Register
	puart->US_TTGR = timeguard ;

    // Define the USART mode
    puart->US_MR = AT91C_US_ASYNC_MODE  ;

}

// Enable sending characters
static void UartEnableTx(AT91PS_USART puart) // arg pointer to a USART controller
{
    // Enable  transmitter
    puart->US_CR = AT91C_US_TXEN;
}

// Disable Transmitter
static void UartDisableTx(AT91PS_USART puart) // arg pointer to a USART controller
{
    //* Disable transmitter
    puart->US_CR = AT91C_US_TXDIS;
}

// Return 1 if a character can be written in US_THR
static unsigned int UartTxReady (AT91PS_USART puart)  // arg pointer to a USART controller
{
    return (PUART0->US_CSR & AT91C_US_TXRDY);
}

//brief Send a character,does not check if ready to send
static void UartPutChar (AT91PS_USART puart,unsigned char character)
{
    puart->US_THR = character;
}

// Reset Transmitter and re-enable it
static void UartResetTx(AT91PS_USART puart) // \arg pointer to a USART controller
{
	//* Reset transmitter
	puart->US_CR = AT91C_US_RSTTX;
    //* Enable transmitter
    //puart->US_CR = AT91C_US_TXEN;
}

//Enable receiving characters
static void UartEnableRx(AT91PS_USART puart) // \arg pointer to a USART controller
{
    //* Enable receiver
    puart->US_CR = AT91C_US_RXEN;
}

//Return 1 if a character can be read in US_RHR
static unsigned int UartRxReady(AT91PS_USART puart )  // \arg pointer to a USART controller
{
    return (puart->US_CSR & AT91C_US_RXRDY);
}

// Receive a character,does not check if a character is
static unsigned char UartGetChar(const AT91PS_USART puart)
{
    return((unsigned char)((puart->US_RHR) & 0xFF));
}

// Disable Receiver
static void UartDisableRx(AT91PS_USART puart)
{
	//* Disable receiver
    puart->US_CR = AT91C_US_RXDIS;
}

// Reset Receiver and re-enable it
static void UartResetRx (AT91PS_USART puart) // \arg pointer to a USART controller
{
	//* Reset receiver
	puart->US_CR = AT91C_US_RSTRX;
    //* Re-Enable receiver
   // puart->US_CR = AT91C_US_RXEN;
}

// Close USART: disable IT disable receiver and transmitter
static void UartClose (AT91PS_USART puart) // \arg pointer to a USART controller
{
    //* Reset the baud rate divisor register
    puart->US_BRGR = 0 ;

    //* Reset the USART mode
    puart->US_MR = 0  ;

    //* Reset the Timeguard Register
    puart->US_TTGR = 0;

    //* Disable all interrupts
    puart->US_IDR = 0xFFFFFFFF ;

    //* Disable receiver and transmitter and stop any activity immediately
    puart->US_CR = AT91C_US_TXDIS | AT91C_US_RXDIS | AT91C_US_RSTTX | AT91C_US_RSTRX ;
}

// Return the error flag
static unsigned int UartError (AT91PS_USART puart) // \arg pointer to a USART controller
{
    return (puart->US_CSR &
    	(AT91C_US_OVRE |  // Overrun error
    	 AT91C_US_FRAME | // Framing error
    	 AT91C_US_PARE));  // Parity error
}

unsigned char buf = 0;
int main()
{
	
	unsigned int i=0;
	UartInit();
	UartBaudConfig(PUART0, 9600, 0);
	//UartResetTx(PUART0);
	UartEnableRx(PUART0);
	
	while(1)
	{
		i++;
		while(!UartRxReady(PUART0))
			;
			
		//UartPutChar(PUART0, buf[i]);
		buf = UartGetChar(PUART0);
		
		
		if(i==19)
			i=0;
		
	}
	
		
}

⌨️ 快捷键说明

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