/*----------------------------------------------------------------------------
* Copyright ◎ 2011,Dian团队激光测距组
* All rights reserved.
* 文件名称: usart.c
* 文件用途: 串口功能检测
*----------------------------------------------------------------------------*/
#include "AT91SAM7A3.h"
//定义PDC
AT91PS_PDC u_pPDC0 = AT91C_BASE_PDC_US0;
AT91PS_PDC u_pPDC1 = AT91C_BASE_PDC_US1;
AT91PS_PDC u_pPDC2 = AT91C_BASE_PDC_DBGU;
unsigned char a[10];
void delay()
{
unsigned int i;
for(i=0;i<400000;i++);
}
void sendchar0(unsigned char ch)//最基本的发送函数
{
while (!(*AT91C_US0_CSR&AT91C_US_TXRDY)==1);
*AT91C_US0_THR = ((ch & 0x1FF));//和0x1FF取与,只保留低8位
}
void sendchar1(unsigned char ch)//最基本的发送函数
{
while (!(*AT91C_US1_CSR&AT91C_US_TXRDY)==1);
*AT91C_US1_THR = ((ch & 0x1FF));//和0x1FF取与,只保留低8位
}
void sendchar2(unsigned char ch)//最基本的发送函数
{
while (!(*AT91C_US2_CSR&AT91C_US_TXRDY)==1);
*AT91C_US2_THR = ((ch & 0x1FF));//和0x1FF取与,只保留低8位
}
void sendcharD(unsigned char ch)//最基本的发送函数
{
while (!(*AT91C_DBGU_CSR&AT91C_US_TXRDY)==1);
*AT91C_DBGU_THR = ((ch & 0x1FF));//和0x1FF取与,只保留低8位
}
__irq void irq_in(void)
{
{
int i;
*AT91C_PIOB_CODR|=(AT91C_PIO_PB0);
delay();
*AT91C_PIOB_SODR|=(AT91C_PIO_PB0);
delay();
for(i=0;i<10;i++)
sendchar1(a[i]);
while((*AT91C_US1_CSR&AT91C_US_RXRDY));
*AT91C_US1_RCR = 10;
*AT91C_AIC_EOICR=0x01;
}
}
void init(void)
{
//*AT91C_PIOA_PER = 0xffffffff; //设置为POIO
*AT91C_PIOA_IFER = 0xffffffff; //打开虑波
AT91C_AIC_SVR[AT91C_ID_US1]=(unsigned long) *irq_in;//把中断函数的地址赋给源向量寄存器
//AT91C_AIC_SMR[AT91C_ID_PIOA]=0x01<<5;//对源状态寄存器赋值,以确定触发方式
*AT91C_PIOB_OER=(AT91C_PIO_PB0);
*AT91C_PIOB_SODR|=AT91C_PIO_PB0; //初始化led
*AT91C_PMC_PCER = 1<<2; //打开外设A时钟
*AT91C_PIOA_IER = 1<<7;
*AT91C_AIC_ICCR = 1<<AT91C_ID_US1; //清除中断
*AT91C_AIC_IECR = 1<<AT91C_ID_US1; //打开AIC中断
while(*AT91C_PIOA_ISR);
*AT91C_US1_IER = AT91C_US_ENDRX;
}
//时钟频率的初始化
void InitFrec(void)
{
//Watchdog Disable看门狗关闭
AT91C_BASE_WDTC->WDTC_WDMR= AT91C_WDTC_WDDIS;
//设置启动时间
*AT91C_PMC_MOR = (( AT91C_CKGR_OSCOUNT & (0x06 <<8) | AT91C_CKGR_MOSCEN ));
//等待主振荡器稳定
while(!(*AT91C_PMC_SR & AT91C_PMC_MOSCS));
//配置PLL
*AT91C_PMC_PLLR = ((AT91C_CKGR_DIV & 5) | (AT91C_CKGR_PLLCOUNT & (28<<8)) | (AT91C_CKGR_MUL & (25<<16)));
// 等待开始时间
while(!(*AT91C_PMC_SR & AT91C_PMC_LOCK));
while(!(*AT91C_PMC_SR & AT91C_PMC_MCKRDY));
//选择主时钟
//选择预分频数
*AT91C_PMC_MCKR = AT91C_PMC_CSS_PLL_CLK | AT91C_PMC_PRES_CLK_2 ;
//等待主时钟就绪
while(!(*AT91C_PMC_SR & AT91C_PMC_MCKRDY));
}
void InitUSART0(void)//初始化串口函数
{
*AT91C_PIOA_PDR/*PIO禁用寄存器*/ |= AT91C_PIO_PA2 | AT91C_PIO_PA3; //Disables the PIO from controlling the corresponding pin (enables peripheral control of the pin).
*AT91C_PIOA_ASR/*外设A 选择寄存器*/ |= AT91C_PIO_PA2 | AT91C_PIO_PA3; //Assigns the I/O line to the Peripheral A function.
//使能外设时钟
*AT91C_PMC_PCER = 1<<AT91C_ID_US0;
//*AT91C_PMC_PCER = 1<<AT91C_ID_US1;
*AT91C_US0_BRGR = 26; //((47923200)/9600x16) //fosc=18432000MHz
//配置发送器时间保障寄存器
*AT91C_US0_TTGR/*发送器时间保障寄存器*/ = 0;
//设置串口模式
*AT91C_US0_MR = 0x08c0;
//Enable the RX and TX PDC transfer requests
u_pPDC0->PDC_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTEN;
//Enable usart
*AT91C_US0_CR/*控制寄存器*/ = 0x50;
*AT91C_US0_RPR = (unsigned long)a;
*AT91C_US0_RCR = 10;
}
void InitUSART1(void)//初始化串口1函数
{
*AT91C_PIOA_PDR/*PIO禁用寄存器*/ |= AT91C_PIO_PA7| AT91C_PIO_PA8;
*AT91C_PIOA_ASR/*外设A 选择寄存器*/ |= AT91C_PIO_PA7| AT91C_PIO_PA8;
//使能外设时钟
*AT91C_PMC_PCER = 1<<AT91C_ID_US1;
*AT91C_US1_BRGR = 26; //((47923200)/9600x16) //fosc=18432000MHz
//配置发送器时间保障寄存器
*AT91C_US1_TTGR/*发送器时间保障寄存器*/ = 0;
//设置串口模式
*AT91C_US1_MR = 0x08c0;
//Enable the RX and TX PDC transfer requests
u_pPDC1->PDC_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTEN;
//Enable usart
*AT91C_US1_CR/*控制寄存器*/ = 0x50;
*AT91C_US1_RPR = (unsigned long)a;
*AT91C_US1_RCR = 10;
}
void InitUSART2(void)//初始化串口1函数
{
*AT91C_PIOA_PDR/*PIO禁用寄存器*/ |= AT91C_PIO_PA9| AT91C_PIO_PA10;
*AT91C_PIOA_ASR/*外设A 选择寄存器*/ |= AT91C_PIO_PA9| AT91C_PIO_PA10;
//使能外设时钟
*AT91C_PMC_PCER = 1<<AT91C_ID_US2;
*AT91C_US2_BRGR = 26; //((47923200)/9600x16) //fosc=18432000MHz
//配置发送器时间保障寄存器
*AT91C_US2_TTGR/*发送器时间保障寄存器*/ = 0;
//设置串口模式
*AT91C_US2_MR = 0x08c0;
//Enable the RX and TX PDC transfer requests
u_pPDC2->PDC_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTEN;
//Enable usart
*AT91C_US2_CR/*控制寄存器*/ = 0x50;
}
void InitUSARTD(void)//初始化串口2函数
{
*AT91C_PIOA_PDR/*PIO禁用寄存器*/ |= AT91C_PIO_PA30| AT91C_PIO_PA31; //Disables the PIO from controlling the corresponding pin (enables peripheral control of the pin).
*AT91C_PIOA_ASR/*外设A 选择寄存器*/ |= AT91C_PIO_PA30| AT91C_PIO_PA31; //Assigns the I/O line to the Peripheral A function.
//使能外设时钟
//*AT91C_PMC_PCER = 1<<DBGU_CIDR;
*AT91C_DBGU_BRGR = 26; //((47923200)/9600x16) //fosc=18432000MHz
//配置发送器时间保障寄存器
//设置串口模式
*AT91C_DBGU_MR = 0x08c0;
//Enable the RX and TX PDC transfer requests
u_pPDC2->PDC_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTEN;
//Enable usart
*AT91C_DBGU_CR/*控制寄存器*/ = 0x50;
}
int main()
{
delay();
InitFrec();
InitUSART0();
InitUSART1();
InitUSART2();
InitUSARTD();
init();
while(1)
{
sendchar0('A');
delay();
sendchar1('B');
delay();
sendchar2('C');
delay();
sendcharD('D');
delay();
}
return 0;
}