1

I am working on a project with C18 compiler and PIC18F4520.

I am having trouble using getcUSART. When I send characters over bluetooth module, at one point it would just hang.

int input(char Arr[])
 {
       Delay10KTCYx(400); // Delay between data transmissions
       while(BusyUSART());
       // wait for user input
       while (!DataRdyUSART());
       //get string from terminal
       Arr[0] = getcUSART();       //read a byte from USART
        Delay10KTCYx(400); // Delay between data transmissions
        while(BusyUSART());
       // wait for user input
       while (!DataRdyUSART());
       //get string from terminal
       Arr[1] = getcUSART();       //read a byte from USART
        Delay10KTCYx(400); // Delay between data transmissions
        while(BusyUSART());
       // wait for user input
       while (!DataRdyUSART());
       //get string from terminal   
       Arr[2] = getcUSART();       //read a byte from USART
        Delay10KTCYx(400); // Delay between data transmissions


    return 1;
  }

Here is my code for when I am receiving characters from the terminal. The issue is that I'd like to be able the user to choose an option, such as RUN or SET, but then if it is any other option, it should ask the user for input again. The problem is that if I for example put POP in the terminal, it will indeed ask for the user to enter again, but then if I type RUN or SET, it will still think that it is not a command. Furthermore, If I type RUNbre for example, the bre will be read LATER when im using getcUSART again. I want to somehow be able to clear the buffer that determines what goes into getcUSART, but I don't know how to do that.

Any help would be appreciated, thank you.

Briget
  • 31
  • 3

1 Answers1

0

What frequency are you running the PIC at?

As a general rule it's best to test using the registers directly before using the c18 library, even though in my experience the c18 library is reliable. This will help you also with understanding microprocessor architectures better by forcing you to go through the data sheet again and again and again... you get the point.

You should first insure that your hardware is set up correctly i.e. TX to RX, RX to TX and a ground is all that is needed for UART communication. It looks like for your requirements that should be enough thought I might be wrong. Insure that you've set up UART correctly. The settings you choose affect the value of the SPBRG register that you need to feed to get something as close as possible to the baud rate you need(Therese usually a slight error).

You'll need to consult PIC18 documentation which includes the equations you need to apply depending on your chosen settings i.e. synchronous,asynchronous,high speed,low speed.

There are a lot of things for you to debug through and check, there are registers that display the status of a transmit/receive register i.e. is it full/empty? This could be the key in understanding why your code isn't working (assuming hardware is fine)