一个很简单的用nios写的中断方式接受的串口例子

本文介绍了一个基于Altera Avalon UART的嵌入式系统中实现的串口接收器程序。该程序通过初始化UART接收器,并注册中断处理函数来接收串口数据,将接收到的数据存入队列,最后在主循环中读取并打印队列中的字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include"system.h"
#include"unistd.h"
#include "alt_types.h"

#include <unistd.h>

#include "altera_avalon_uart_regs.h"
#include "altera_avalon_pio_regs.h"
#include "sys/alt_irq.h"
#define quelen  2048           
char queue[quelen];
int indexc = 0;
int temp_index=0;
char ch;

static void init_uart_receiver();
static void handle_uart_interrups(void* context, alt_u32 id);


int main(void)
{

 init_uart_receiver();
printf("successful");
int f=0;
while (f<1)
{
 

 if (temp_index!=indexc)
    {
      printf("%c", queue[temp_index]);
      temp_index++;
    }
}

return 0;
}


static void init_uart_receiver()
{
void* status_ptr;
IOWR_ALTERA_AVALON_UART_CONTROL(UART_BASE, 0x80);
IOWR_ALTERA_AVALON_UART_STATUS(UART_BASE, 0x0);
IOWR_ALTERA_AVALON_UART_RXDATA(UART_BASE, 0x0);
alt_irq_register(UART_IRQ,status_ptr,handle_uart_interrups);
}


static void handle_uart_interrups(void* context, alt_u32 id)
{

 

volatile char* status_ptr =(volatile char*)context;
*status_ptr =IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);

if((IORD_ALTERA_AVALON_UART_STATUS(UART_BASE)&(0x80)) ==0x80)
{
ch =IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);


 
    queue[indexc] =ch;
    indexc++;
   
}

IOWR_ALTERA_AVALON_UART_STATUS(UART_BASE, 0x0);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值