NIOS串口UART问题,求大神帮忙看看
时间:10-02
整理:3721RD
点击:
#include
#include "altera_avalon_uart_regs.h"
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include "sys/alt_irq.h"
#include "string.h"
#include "unistd.h"
#define UART_BASE 0x5020
#define UART_IRQ 1
#define TIME_DELAY 1000000
unsigned char buff[100];
alt_u8 i=0;
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);
data=IORD_ALTERA_AVALON_UART_TXDATA(UART_BASE);
}
void Uart_send_n(unsigned char *ptr)
{
while(*ptr)
{
Uart_send(*ptr);
ptr++;
}
}
char Uart_receive(void)
{
alt_u16 status;
status=IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
while(status&0x0080)
{
status=IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
*(buff+i)=IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);
i++;
if(i>100)
{
i=0;
}
}
return 0;
}
void CLR_Buf(void)
{
unsigned char k;
for(k=0;k<100;k++)
{
buff[k] = 0;
}
i = 0;
}
void Uart_init()
{
IOWR_ALTERA_AVALON_UART_CONTROL(UART_BASE,0X80);
IOWR_ALTERA_AVALON_UART_STATUS(UART_BASE,0X0);
IOWR_ALTERA_AVALON_UART_RXDATA(UART_BASE,0X0);
}
int main (void) __attribute__ ((weak, alias ("alt_main")));
void alt_main (void)
{
Uart_init();
CLR_Buf();
Uart_send_n("NH");
Uart_send(0x0d);
Uart_send(0x0a);
usleep(TIME_DELAY);
Uart_receive();
while(strstr(buff,"OK")==NULL)
{
CLR_Buf();
Uart_send_n("NH");
Uart_send(0x0d);
Uart_send(0x0a);
usleep (TIME_DELAY);
Uart_receive();
}
Uart_send_n("NEXT");
Uart_send(0x0d);
Uart_send(0x0a);
usleep(TIME_DELAY);
}
我想做一个寄存器方式的收发程序,当发出你好(NH)时,收到OK时,继续发送NEXT,否则继续发送你好(NH),直到收到OK为止,该程序在串口调试助手中,总是连续发出你好(NH),不管输入什么都没有结果。谢谢大家指导一下错误所在。
#include "altera_avalon_uart_regs.h"
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include "sys/alt_irq.h"
#include "string.h"
#include "unistd.h"
#define UART_BASE 0x5020
#define UART_IRQ 1
#define TIME_DELAY 1000000
unsigned char buff[100];
alt_u8 i=0;
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);
data=IORD_ALTERA_AVALON_UART_TXDATA(UART_BASE);
}
void Uart_send_n(unsigned char *ptr)
{
while(*ptr)
{
Uart_send(*ptr);
ptr++;
}
}
char Uart_receive(void)
{
alt_u16 status;
status=IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
while(status&0x0080)
{
status=IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
*(buff+i)=IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);
i++;
if(i>100)
{
i=0;
}
}
return 0;
}
void CLR_Buf(void)
{
unsigned char k;
for(k=0;k<100;k++)
{
buff[k] = 0;
}
i = 0;
}
void Uart_init()
{
IOWR_ALTERA_AVALON_UART_CONTROL(UART_BASE,0X80);
IOWR_ALTERA_AVALON_UART_STATUS(UART_BASE,0X0);
IOWR_ALTERA_AVALON_UART_RXDATA(UART_BASE,0X0);
}
int main (void) __attribute__ ((weak, alias ("alt_main")));
void alt_main (void)
{
Uart_init();
CLR_Buf();
Uart_send_n("NH");
Uart_send(0x0d);
Uart_send(0x0a);
usleep(TIME_DELAY);
Uart_receive();
while(strstr(buff,"OK")==NULL)
{
CLR_Buf();
Uart_send_n("NH");
Uart_send(0x0d);
Uart_send(0x0a);
usleep (TIME_DELAY);
Uart_receive();
}
Uart_send_n("NEXT");
Uart_send(0x0d);
Uart_send(0x0a);
usleep(TIME_DELAY);
}
我想做一个寄存器方式的收发程序,当发出你好(NH)时,收到OK时,继续发送NEXT,否则继续发送你好(NH),直到收到OK为止,该程序在串口调试助手中,总是连续发出你好(NH),不管输入什么都没有结果。谢谢大家指导一下错误所在。
串口调试助手你输入的是“OK”还是hex?要确保两者对应的格式一致