-1

I know questions have been asked a lot on this function, but after searching for hours on here I cannot find an answer that helps me figure this out.

Here is the function to read in a string through the UART that I'm trying to implement.

void UART_Read_Text(char *Output, unsigned int length)
{
  unsigned int i;
  for(int i=0;i<length;i++)
  Output[i] = UART_Read();
}

I have a 16 character array called label defined globally. I want to receive the UART data into this array.

Here is how I'm attempting to use it.

UART_Read_Text(label,16);

Can someone please shed some light on what I'm doing wrong? Keep in mind, I have UART TX function working well, so I have that set up properly. Also, I'm trying not to have to use interrupts, but maybe that is the way that I have to go. Any help would be appreciated.

supertopi
  • 3,469
  • 26
  • 38
NickW
  • 1
  • 2
  • Please provide some additional information, about what kind of error you are experiencing and what UART_read is. – MikeMB Nov 15 '15 at 22:33
  • Thanks for your reply. UART_Read_Text() is the function (defined above in my first post) that I'm trying to read an incoming character array and place it into the array label[16]. The error that I'm experiencing is that the array is not being populated with the incoming data. I'm sorry if I'm being unclear. I'll try to provide any additional information necessary if needed. – NickW Nov 15 '15 at 22:44
  • Hi, I was asking about `UART_Read()` not `UART_Read_Text`. I would also recommend to read http://stackoverflow.com/help/mcve, and http://stackoverflow.com/help/how-to-ask which will give you some idea, about how to ask a good question. – MikeMB Nov 15 '15 at 22:56
  • Aside from the fact that `unsigned int i;` doesn't have any effect, I don't see any errors in your code. However, If MCs and interaction with the environment are involved, there are a dozen things that might go wrong. So unless you provide a lot more information, about your system (OS, system, libraries, configuration), what you tried, what works, what doesn't work, what tutorial you followed ... there is hardly any chance for us to provide you with an answer. – MikeMB Nov 15 '15 at 22:57
  • Thanks a lot for the responses. Here is the UART_Read() function. `char UART_Read()` `{` `while(!RCIF); ` `return RCREG; ` `} ` I am not really using any other libraries. I'm running a pic16f1824, using a pickit 3, and MPLABX v2.35. – I will also take a look at the links that you posted, MikeMB, thanks. I apologize for the poor formatting as well. I'l still trying to figure the post formatting on this site. – NickW Nov 15 '15 at 23:14

2 Answers2

0

Have you tried using a debugger to place a breakpoint and step though your code? You can also try printing the output of UART_Read() to see if it is outputting anything at all.

Also, remove

unsigned int i;

It is confusing since it contradicts the statement after it. Maybe the compiler is removing the initialization of i to 0 since it declares it as an int in the same statement?

dmh
  • 163
  • 1
  • 5
  • Thanks for your input. The functions are part of sample code that was found online, and supposedly it worked. Should there be "{" and "}" after the for loop in the UART_Read_Text function, around the `"Output[i] = UART_Read();"` – NickW Nov 15 '15 at 23:21
  • I've also tried to use the dubugger. I'm receiving a array of 16 characters, and it is being transmitted over a bluetooth link. I'm getting the uart to properly set up the bluetooth modules using the transmit function, but cannot get the read function to work. The data stream coming in will still be operating in real time from the other bluetooth radio, so if I try to step through the code, I think it will be too slow for it to function properly. I suppose I could try to just use a serial terminal on the pc to send data to the MCU, and try the debugger again. – NickW Nov 15 '15 at 23:29
  • Put a printf call after you write to Output. Then place a breakpoint on the print statement. It shouldn't matter if you're communicating with another device at that point because the data would already be on the mcu. If you are getting data via uart successfully, I suspect the index variable is out of wack. – dmh Nov 15 '15 at 23:34
  • Thanks so much. I'll give that a try when I get a chance here. I do really appreciate all of the advice. – NickW Nov 15 '15 at 23:54
0

I got it working tonight. I actually ended up using an interrupt service routine. I'm going to post it, in hopes that it may help someone else.

void interrupt ISR() 
{
    if (PIR1bits.RCIF)          // see if interrupt caused by incoming data
    {
        UART_Read_Text(Output,16); //replace "Output" with the array that you want populated.
        PIR1bits.RCIF = 0;      // clear interrupt flag
    }
}

Also, these versions of the functions seem to be working just fine. Although, there is some cleaning up to do, this should get someone started.

char UART_Read()
{
  while(!RCIF);
  return RCREG;
}

void UART_Read_Text(char *Output, unsigned int length)
{
  unsigned int i;
  for(int i=0;i<length;i++)
  Output[i] = UART_Read();
}

I give credit to the author at the following link. I found helpful information in this code. Be sure to set all of the bits that control the interrupt. They can also be found at the following link.

https://gist.github.com/gshrikant/8549474

But here they are:

    RCIF=0;   // make sure receive interrupt flag is clear
    RCIE=1;   // enable UART Receive interrupt, 1 to enable
    PEIE = 1; // Enable Peripheral interrupts //WAS ORIGINALLY 1
    GIE = 1;  // Enable global interrupts

Thanks again to everyone that tried to help out. It is always appreciated.

NickW
  • 1
  • 2