| 
 | 
 
下面是我编写的一个利用nios ii中UART IP核与PC串口通信的例子,在运行的过程中串口数据可以写入开发板但是接收到的数据和发送数据并不一样。请哪位大侠指点指点。 
   #include <stdio.h>                   //包含基本的硬件描述信息 
#include <string.h> 
#include <system.h> 
#include "altera_avalon_uart_regs.h" 
#include "altera_avalon_uart.h"      //这是一个包含uart的文件 
#include "alt_types.h"               //该文件包含着数据类型       
#include "sys/alt_irq.h"             //中断文件 
#include "altera_avalon_pio_regs.h"  //pio的文件 
 
 
//UART发送一个字节子程序 
void Uart_send(unsigned char data) 
{ 
    alt_u16 status; 
    status=IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE); 
    while(!(status&0x0040))            //等待发送完成       
        status=IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE); 
    IOWR_ALTERA_AVALON_UART_TXDATA(UART_0_BASE,data); 
} 
 
//UART接收子程序 
int Uart_receive(void) 
{ 
    alt_u16 status; 
    int temp; 
    status=IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE); 
    while(!(status&0x0080))               //等待接收完成       
        status=IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE); 
    temp=IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE); 
    return temp; 
}  
 
//串口初始化 
void Uart_init() 
{ 
    IOWR_ALTERA_AVALON_UART_STATUS(UART_0_BASE, 0x0);//清状态标志 
    IOWR_ALTERA_AVALON_UART_RXDATA(UART_0_BASE, 0x0);//清接收寄存器 
} 
 
int main() 
{ 
  int temp1; 
  while(1) 
  {    
   Uart_init(); 
   temp1 = Uart_receive(); 
   IOWR_ALTERA_AVALON_PIO_DATA(0x00003020,temp1); 
   Uart_send(temp1);   
   temp1=0; 
  } 
  return 0; 
} |   
 
 
 
 |