#include "alt_types.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "system.h"
#include "altera_avalon_uart_regs.h"
#include "altera_avalon_pio_regs.h"
//#include "sys/alt_flash_regs.h"
#include "sys/alt_flash.h"
#include "sys/alt_irq.h"
#define BUF_SIZE 1024
void alt_busy_sleep(alt_u32 temp);
#ifdef UART_BASE
void uart_handle(void *context,alt_u32 interrupt)
{
unsigned short int data,status;
status = IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
while (!(status & ALTERA_AVALON_UART_STATUS_RRDY_MSK))
status = IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
data =IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);
//write status reg;
status = ALTERA_AVALON_UART_STATUS_TRDY_MSK;
IOWR_ALTERA_AVALON_UART_STATUS(UART_BASE, status);
IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE, data);
IOWR_ALTERA_AVALON_UART_STATUS(UART_BASE, 0);
}
void uart_init()
{
alt_u32 control;
int divisor;
//ALTERA_AVALON_UART_CONTROL_TRDY_MSK |
control =
ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_E_MSK;;
IOWR_ALTERA_AVALON_UART_CONTROL(UART_BASE, control);
divisor = (int)(50000000/38400+0.5);
IOWR_ALTERA_AVALON_UART_DIVISOR(UART_BASE, divisor);
if (alt_irq_register(UART_IRQ, NULL, uart_handle))
{
IOWR_ALTERA_AVALON_PIO_DATA(PIO_LED_BASE, 0x7f);
}
}
#endif
int main(void)
{
char *msg="hello word!";
uart_init();
while(1)
{
printf(msg);
}
return 0;
}
这以上代码不断往串口发字符“hello word!”.用调试工具在电脑上是收到的。你给NIOS发啥它就给你回啥,这都是正常的。
然后我看一些资料。我把main里的代码改成以下
char* msg = "hello world";
FILE* fg;
fg = fopen("/dev/uart","w");
if(fp)
{
fprintf(fp, "%s",msg);
fclose(fp);
}
一看什么东西也没收到啊 好像是没有打开设备。然后我从串口随便发一个字符过去,代码好像死了
烦请版主指导一下是什么问题。谢谢!!!!!!!!!!!!!!!!!!!!!!
楼主既然可以通讯了就可以和可以通讯模块比较一下就可以判断出什么问题。
楼主有没有调用初始化模块,在新的main里。
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |