显示previously收到UART值 [英] Display previously received UART values

查看:206
本文介绍了显示previously收到UART值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易回答任何人都熟悉C.我想在LCD上显示一个变量previous值(在微控制器接收UART(RS-232)的寄存器)。这是我目前的执行和它的作品确定。但是,我想知道是否有花费更少的时间在我的中断程序的方法。目前,外围配置为接收在UART进一个新字符尽快跳转到中断例程。建议任何人吗?

  //初始化
焦炭U1RX_data ='\\ 0';
焦炭p0_U1RX_data ='\\ 0';
焦炭p1_U1RX_data ='\\ 0';
焦炭p2_U1RX_data ='\\ 0';
焦炭p3_U1RX_data ='\\ 0';
焦炭p4_U1RX_data ='\\ 0';
焦炭p5_U1RX_data ='\\ 0';
焦炭p6_U1RX_data ='\\ 0';
焦炭p7_U1RX_data ='\\ 0';CHAR U1buf [] = {p7_U1RX_data,p6_U1RX_data,p5_U1RX_data,
p4_U1RX_data,p3_U1RX_data,p2_U1RX_data,
p1_U1RX_data,p0_U1RX_data,U1RX_data,'\\ 0'};
disp_string(-61,17,1,U1buf); // X,Y,模式,串无效_U1RXInterrupt(无效){
    p7_U1RX_data = p6_U1RX_data;
    p6_U1RX_data = p5_U1RX_data;
    p5_U1RX_data = p4_U1RX_data;
    p4_U1RX_data = p3_U1RX_data;
    p3_U1RX_data = p2_U1RX_data;
    p2_U1RX_data = p1_U1RX_data;
    p1_U1RX_data = p0_U1RX_data;
    p0_U1RX_data = U1RX_data;    U1RX_data = U1RXREG;
    IFS0bits.U1RXIF = 0;
}


解决方案

我将创建previous值的数组,并把它当作一个循环缓冲区。中断例程然后简单地记录在下一时隙的新值,覆盖的最后一个值,并增加索引。

 的#define DIM(X)(sizeof的(X)/ sizeof的(*(X)))
静态INT索引= 0;
静态字符UART; [8]无效_U1RXInterrupt(无效){
    如果(++索引> = DIM(UART))
        索引= 0;
    UART [指数] = U1RXREG;
    IFS0bits.U1RXIF = 0;
}诠释uart_value(无符号N)
{
    INT I =指数+ DIM(UART) - (N%DIM(UART));
    如果(ⅰ> DIM(UART))
        我 - = DIM(UART);
    回报(UART [I]);
}

我假设同步,非线程操作;如果你要处理多线程的,再有就是工作,以保护该指数自变量的并发访问。它也返回0过去,但缓冲区之前一个读数已满。等等。如果你确信你的编码,可以去除模运算了。

This should be easy to answer to anyone familiar with C. I want to display the previous values of a variable (receive register of a UART (RS-232) on a microcontroller) on an LCD. This is my current implementation and it works ok. But I'd like to know if there is a way to spend less time in my interrupt routine. Currently, the peripheral is configured to jump to the interrupt routine as soon as it receives one new character in the UART feed. Suggestions anyone?

//Initialization
char U1RX_data = '\0';
char p0_U1RX_data = '\0';
char p1_U1RX_data = '\0';
char p2_U1RX_data = '\0';
char p3_U1RX_data = '\0';
char p4_U1RX_data = '\0';
char p5_U1RX_data = '\0';
char p6_U1RX_data = '\0';
char p7_U1RX_data = '\0';

char U1buf[] = {p7_U1RX_data, p6_U1RX_data, p5_U1RX_data,
	 			p4_U1RX_data, p3_U1RX_data, p2_U1RX_data,
				p1_U1RX_data, p0_U1RX_data, U1RX_data, '\0'};
disp_string(-61, 17, 1, U1buf); //X, Y, mode, string

void _U1RXInterrupt(void){
    p7_U1RX_data = p6_U1RX_data;
    p6_U1RX_data = p5_U1RX_data;
    p5_U1RX_data = p4_U1RX_data;
    p4_U1RX_data = p3_U1RX_data;
    p3_U1RX_data = p2_U1RX_data;
    p2_U1RX_data = p1_U1RX_data;
    p1_U1RX_data = p0_U1RX_data;
    p0_U1RX_data = U1RX_data;

    U1RX_data = U1RXREG;
    IFS0bits.U1RXIF = 0;	
}

解决方案

I would create an array of previous values, and treat it as a circular buffer. The interrupt routine then simply records the new value in the next slot, overwriting the last value, and incrementing the index.

#define DIM(x)  (sizeof(x)/sizeof(*(x)))
static int  index = 0;
static char uart[8];

void _U1RXInterrupt(void){
    if (++index >= DIM(uart))
        index = 0;
    uart[index] = U1RXREG;
    IFS0bits.U1RXIF = 0;        
}

int uart_value(unsigned n)
{
    int i = index + DIM(uart) - (n % DIM(uart));
    if (i > DIM(uart))
        i -= DIM(uart);
    return(uart[i]);
}

I'm assuming synchronous, non-threaded operation; if you have to deal with multi-threaded, then there's work to protect the index variable from concurrent access. It also returns zeroes for the last but one reading before the buffer is full. Etc. If you are confident of your coding, you can remove the modulo operation, too.

这篇关于显示previously收到UART值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆