在sparkfun标识上使用arm lpc2148通过rs232接收/传输

时间:2015-08-05 17:37:40

标签: c lpc

我正在尝试用sparkfun对logomatic进行编程,是的,我使用了他们的论坛没有回复,并且遇到了一些问题。我正在尝试将字符发送到UART0,我希望徽标能够响应特定字符而不仅仅是回声。例如,我发送了ID?'在终端上(使用RealTerm),徽标发回' 1'。所有这一切现在都是回声。

我正在使用带有WinARM工具链的程序员记事本。以下代码段来自main.c文件。我只包括这个,因为我很确定这就是我的问题所在

void Initialize(void)
{
    rprintf_devopen(putc_serial0);   

    PINSEL0 = 0xCF351505;
    PINSEL1 = 0x15441801;
    IODIR0 |= 0x00000884;
    IOSET0 = 0x00000080;

    S0SPCR = 0x08;  // SPI clk to be pclk/8
    S0SPCR = 0x30;  // master, msb, first clk edge, active high, no ints
}

注意rprintf_devopen函数,下面是来自rprintf.c文件,由于我的平庸技巧,我不明白这段代码。如果我在main中注释掉rprintf_devopen,芯片永远不会正确初始化。

static int (*putcharfunc)(int c);

void rprintf_devopen( int(*put)(int) )
{
    putcharfunc = put;
}

static void myputchar(unsigned char c)
{
    if(c == '\n') putcharfunc('\r');
    putcharfunc(c);
}

现在,下面是来自serial.c文件。所以我的想法是我应该能够在main.c中调用其中一个putchar函数并且它可以工作,但它仍然只是回声。

int putchar_serial0 (int ch)
{
    if (ch == '\n')
    {
        while (!(U0LSR & 0x20));
        U0THR = CR;                  // output CR 
    }
    while (!(U0LSR & 0x20));
    return (U0THR = ch);
}

// Write character to Serial Port 0 without \n -> \r\n  
int putc_serial0 (int ch)
{
    while (!(U0LSR & 0x20));
    return (U0THR = ch);
}

// Write character to Serial Port 1 without \n -> \r\n  
int putc_serial1 (int ch)
{
    while (!(U1LSR & 0x20));
    return (U1THR = ch);
}

void putstring_serial0 (const char *string)
{
    char ch;

    while ((ch = *string))
    {
        putchar_serial0(ch);
        string++;
    }
}

我尝试在main中调用不同的putchar函数,也使用rprintf_devopen。仍然只是呼应。我改变了putchar函数,但仍然只是回声。我试过在main.c中写入U0THR寄存器并没有运气。请记住,我仍然是一名学生,我的专业是电气工程,因此我所参加的唯一编程课程是介绍c,以及介绍vhdl。我更像是一个数学和物理学家。我正在为我正在做的实习工作。实习结束了,但这让我感到困惑,我无法弄清楚这一点。老实说,在这个项目上的工作教会了我更多的东西。无论如何,我感谢任何可以提供的帮助,如果你想看到整个代码,请告诉我。

以下是该问题的更新。该函数在main.c中。

static void UART0ISR(void)
{
char temp;
trig = 13; //This is where you set the trigger character in decimal, in this case a carriage return.
temp = U0RBR; //U0RBR is the receive buffer on the chip, refer to datasheet.

if(temp == query1[counter1]) //This segment looks for the characters "ID?" from the U0RBR
{                             //query1 is defined at the top of the program
    counter1++;
    if(counter1 >= 3)
    {
        flag1 = 1;           //This keeps track of whether or not query1 was found
        counter1 = 0;

        stat(1,ON);
        delay_ms(50);
        stat(1,OFF);

        RX_in = 0;
        temp = 0;
        //rprintf("\n\rtransmission works\n");
        putc_serial1(49);    
    }                        
}

if(temp == query2[counter2] && flag1 == 1) //This segment looks for "protov?" from the U0RBR, but only after query1 has been found
{
    counter2++;
    if(counter2 >= 7)
    {
        flag2 = 1;             //This keeps track of whether or not query2 was found
        counter2 = 0;

        stat(1,ON);
        delay_ms(50);
        stat(1,OFF);

        RX_in = 0;
        temp = 0;
        putc_serial1(49);    
    }                        
}

if(temp == stop[counter3]) //This if segment looks for certain characters in the receive buffer to stop logging
{
    counter3++;
    if(counter3 >= 2)
    {
        flagstop = 1;       //This flagstop keeps track of whether or not stop was found. When the stop characters are found,
        flag1 = 0;          //the query1 and query2 flags will be reset. So, in order to log again these queries must be sent again
        flag2 = 0;          //this may seem obvious, but deserves mention.
        counter3 = 0;

        stat(1,ON);
        delay_ms(500);
        stat(1,OFF);

        RX_in = 0;
        temp = 0;
    }
    flagstop = 0;           //Reset the stop flag in order to wait once again for the query 1&2
}

if(RX_in == 0)
{
    memset (RX_array1, 0, 512); // This clears the RX_array to make way for new data
    memset (RX_array2, 0, 512);
}   

if(RX_in < 512 && flag1 == 1 && flag2 == 1) //We cannot log data until we see both flags 1 & 2 and after we see these flags,
{                                             //we must then see the trigger character "carriage return"
    RX_array1[RX_in] = temp;
    RX_in++;

    if(temp == trig)
    {
        RX_array1[RX_in] = 10; // delimiters
        log_array1 = 1;
        RX_in = 0;
    }
}   
else if(RX_in >= 512 && flag1 == 1 && flag2 == 1) //This else if is here in case the RX_in is greater than 512 because the RX_arrays are defined to
{                                                   //be of size 512. If this happens we don't want to lose data, so we must put the overflow into another register. 
    RX_array2[RX_in - 512] = temp;
    RX_in++;
    RX_array1[512] = 10; // delimiters
    RX_array1[512 + 1] = 13;
    log_array1 = 1;

    if(RX_in == 1024 || temp == trig)
    {
        RX_array2[RX_in - 512] = 10; // delimiters
        log_array2 = 1;
        RX_in = 0;
    }
}

temp = U0IIR; // have to read this to clear the interrupt

VICVectAddr = 0;
}

0 个答案:

没有答案