📄 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 : main application written in C
//* Creation : ODi 06/26/2002
//* Modification 31/03/03 JPP : add DBGU message
//*----------------------------------------------------------------------------
#include <string.h>
#include "AT91RM9200.h"
#include "lib_AT91RM9200.h"
#include "config.h"
#include "console.h"
//extern void AT91F_DBGU_Printk(char *);
#define AT91F_DBGU_Printk puts
void __irq irq_handler(void) //added by hzh
{
}
//*----------------------------------------------------------------------------
//* \fn AT91F_IRDA_putc
//* \brief This function is used to send a character to the IRDA channel
//*----------------------------------------------------------------------------
void AT91F_IRDA_putc(
char buffer) // \arg pointer to a character
{
while (!AT91F_US_TxReady((AT91PS_USART)AT91C_BASE_US2));
AT91F_US_PutChar(AT91C_BASE_US2,buffer);
}
//*----------------------------------------------------------------------------
//* \fn AT91F_IRDA_getc
//* \brief This function is used to receive a character to the IRDA channel
//*----------------------------------------------------------------------------
char AT91F_IRDA_getc()
{
while (!AT91F_US_RxReady((AT91PS_USART)AT91C_BASE_US2));
return AT91F_US_GetChar(AT91C_BASE_US2);
}
int main()
{
char c;
char IrdaMessage[] = "\
\n\n\r-I- ======================================\n\r\
-I- AT91RM3400 basic IRDA example\n\r\
-I- --------------------------------------\n\r\
-I- Hit characters, they will be echo-ed\n\r\
-I- ======================================\n\r";
char DbguMessage[] = "\
\n\n\r-I- ======================================\n\r\
-I- AT91RM3400 basic IRDA example\n\r\
-I- --------------------------------------\n\r\
-I- Set IRDA Port at 9600,8,N \n\r\
-I- ======================================\n\r";
AT91F_DBGU_Printk("main()\n\r");
// Open PIO & PMC for USART2
AT91F_US2_CfgPIO();
AT91F_US2_CfgPMC();
// Configure USART2
AT91F_US_Configure (
AT91C_BASE_US2, // USART2 base address
AT91C_MASTER_CLOCK, // 48 MHz
AT91C_US_ASYNC_IRDA_MODE , // mode Register to be programmed
115200 , // baudrate to be programmed
0); // timeguard to be programmed
// Configure filter (!!!even in transmition mode!!!)
AT91F_US_SetIrdaFilter(AT91C_BASE_US2, 30);
// Disable pullup
//AT91C_BASE_PIOA->PIO_PPUDR =AT91C_PA23_TXD2 | AT91C_PA22_RXD2;
// ============= IRDA is HALF-DUPLEX: Transmition mode ================
// Enable Transmition
AT91F_US_DisableRx(AT91C_BASE_US2);
AT91F_US_EnableTx(AT91C_BASE_US2);
// Send a welcome message
AT91F_PDC_SendFrame(
(AT91PS_PDC) &(AT91C_BASE_DBGU->DBGU_RPR),
DbguMessage, strlen(DbguMessage),
(char *) 0, 0);
// Send a welcome message
AT91F_PDC_SendFrame(
(AT91PS_PDC) &(AT91C_BASE_US2->US_RPR),
IrdaMessage, strlen(IrdaMessage),
(char *) 0, 0);
// wait for the end of transmition
while ( !(AT91C_BASE_US2->US_CSR & AT91C_US_TXEMPTY) );
printf("Do you want to test transive or receive? [t/r]\n");
while (1) {
c = getch();
if((c=='t')||(c=='r')) {
printf("%s test\n", (c=='t')?"transive":"receive");
break;
}
}
if (c=='t')
while (1) {
c = getch();
putch(c);
// ============= IRDA is HALF-DUPLEX: Transmition mode ================
// Enable Transmition
AT91F_US_DisableRx(AT91C_BASE_US2);
AT91F_US_EnableTx(AT91C_BASE_US2);
AT91F_IRDA_putc(c);
// wait for the end of transmition
while ( !(AT91C_BASE_US2->US_CSR & AT91C_US_TXEMPTY) );
}
else
while (1) {
// Receive and send 1 character using RHR & THR
// ============= IRDA is HALF-DUPLEX: Reception mode ================
// Enable reception
AT91F_US_DisableTx(AT91C_BASE_US2);
AT91F_US_EnableRx(AT91C_BASE_US2);
c = AT91F_IRDA_getc();
putch(c);
}
AT91F_US_EnableTx(AT91C_BASE_US2);
AT91F_US_EnableRx(AT91C_BASE_US2);
while (1) {
c = getch();
putch(c);
AT91F_IRDA_putc(c);
while ( !(AT91C_BASE_US2->US_CSR & AT91C_US_TXEMPTY) );
putch(AT91F_IRDA_getc());
}
}
//* EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -