40130064 发表于 2010-5-22 11:44:06

求一操作寄存器的NIOSII串口接收发送程序

void Uart_send(unsigned char data)
{
alt_u16 status;
status=IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
while(!(status&0x0040))//等待发送完成
status=IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,data);
}
这样的 网上找了个有些问题

tear086 发表于 2010-5-22 13:57:45

#include <unistd.h>                     // UNIX API
#include <fcntl.h>                      // UNIX API
#include <stdio.h>                      // printf()
#include <string.h>                     // strlen()
#include "alt_types.h"

//++++++++++++++++++++++++++++++++++++++
// 修改UART名称 开始
// 根据SOPC Builder设置修改
//++++++++++++++++++++++++++++++++++++++
#include "system.h"

#define my_uart_name UART_NAME
//--------------------------------------
// 修改UART名称 开始
//--------------------------------------

#define CHAR_NUM 8

int main()
{
int fd;
alt_u8 *ptr;
alt_u8 charBuf;                  // 100字节的字符缓存
alt_u8 num_temp1, num_temp2;          // 临时变量
alt_u8 *msg1 = "Please Enter 8 charaters : ";
alt_u8 *msg2 = " has been Rcved\n";

//++++++++++++++++++++++++++++++++++++
// 初始化 开始
//++++++++++++++++++++++++++++++++++++
ptr       = charBuf;                  // 指向字符缓冲区
num_temp2 = CHAR_NUM;               // 欲读入CHAR_NUM个字符
num_temp1 = num_temp2;
//-----------------------------------
// 初始化 结束
//-----------------------------------   

//++++++++++++++++++++++++++++++++++++
// 打开UART 开始
//++++++++++++++++++++++++++++++++++++
fd = open(my_uart_name, O_RDWR, 0666);   // 以可读写方式打开设备文件
if (fd < 0)                           // 如果打开失败
{
   printf("error\n ");
   return 1;
}
//------------------------------------
// 打开UART 结束
//------------------------------------

//++++++++++++++++++++++++++++++++++++
// 输出msg1 开始
//++++++++++++++++++++++++++++++++++++
write(fd, msg1, strlen(msg1));
//------------------------------------
// 输出msg1 结束
//------------------------------------

//++++++++++++++++++++++++++++++++++++
// 读取CHAR_NUM个ASCII码字符 开始
//++++++++++++++++++++++++++++++++++++
while(num_temp1)                         // 读取CHAR_NUM个ASCII码字符
{
    num_temp2= read(fd, ptr, num_temp2);
    ptr       += num_temp2;
    num_temp1 -= num_temp2;
    num_temp2= num_temp1;
}
//------------------------------------
// 读取CHAR_NUM个ASCII码字符 结束
//------------------------------------

//++++++++++++++++++++++++++++++++++++
// 输出读入字符和msg2 开始
//++++++++++++++++++++++++++++++++++++
write(fd, charBuf, 8);                // 输出读入字符
write(fd, msg2, strlen(msg2));      // 输出msg2
//------------------------------------
// 输出读入字符和msg2 结束
//------------------------------------

//++++++++++++++++++++++++++++++++++++
// 关闭UART 开始
//++++++++++++++++++++++++++++++++++++
close(fd);                            // 关闭设备
//------------------------------------
// 关闭UART 结束
//------------------------------------

return 0;
}

40130064 发表于 2010-5-22 14:08:55

谢谢 这个太高级了

我要这样的
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);
}

tear086 发表于 2010-5-22 17:46:45

回复【2楼】40130064
-----------------------------------------------------------------------

无语。

avic 发表于 2010-5-22 18:01:39

可以看看我的博客,上面有,kingst.cnblogs.com

40130064 发表于 2010-5-24 11:00:33

回复【4楼】avic
-----------------------------------------------------------------------

相当历害,收我为徒吧

40130064 发表于 2010-5-24 16:01:01

回复【4楼】avic
-----------------------------------------------------------------------

老大上个源程序吧 我调了一下物 通是能通过了 不发数据 没反应啊
页: [1]
查看完整版本: 求一操作寄存器的NIOSII串口接收发送程序