栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

NIOS II串口非阻塞方式接收数据

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

NIOS II串口非阻塞方式接收数据

Quartus II 10.0版本以上可以用fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK)将标准输入stdin设为非阻塞模式,然后用getchar,read,scanf等函数接收串口数据。
笔者用的版本是Quartus II 13.0sp1 (64-bit)。

下面的程序实现了每隔1秒钟打印一次hello world和系统毫秒计数器的值,收到串口字符时立即打印出来的功能。
用read函数接收1字节字符,函数不阻塞,如果没有字符就会返回-1,有字符则返回1。


#include 
#include 
#include 
#include 
#include 

int main()
{
	alt_u32 now, ticks = 0;
	char ch;
	int i = 0;
	int ret;

	printf("Hello from Nios II!rn");
	printf("tick_rate=%lurn", alt_ticks_per_second());

	usleep(100000);
	printf("ticks=%lurn", alt_nticks());

	fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
	while (1)
	{
		now = alt_nticks();
		if (now - ticks > 1000)
		{
			ticks = now;
			printf("Hello World! i=%d, ticks=%lurn", i, now);
			i++;
		}

		ret = read(STDIN_FILENO, &ch, 1);
		if (ret == 1)
		{
			if (isprint(ch))
				printf("UART0: %crn", ch);
			else
				printf("UART0: 0x%02xrn", ch);
		}
	}
}

Qsys里面添加了Interval Timer IP核,定时时间为1ms,无起停控制位,固定定时周期。
在BSP Editor里面的Settings -> Common -> hal -> sys_clk_timer选中这个定时器,就可以用alt_nticks()函数了。这个函数就类似于STM32单片机HAL库里面的HAL_GetTick()函数。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/699545.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号