0

I've been given a PIC32mx795f512L in order to build a spy for I2C, I will connect 3 wires on SDA, SCL and GND and then I have to detect the sent data. This part is ok. Now, once I have the data, I have to send it via RX, TX to a computer (in fact, I send it to an adapter which convert it to RS232) and here is my problem.

I do not really know how to initialise this communication. For now I put:

mPORTFClearBits(BIT_8);
TRISFbits.TRISF8=0;        // RF8 output==>TX1
TRISFbits.TRISF2=1;        // RF1 input==>RX1
U1STA = 0x1400; // Enable Tx(inv) and Rx
U1BRG = 8332; // value = (80000000 / BAUD) - 1 = 9600
U1MODE = 0x8008; // Enable UART with 1 stop bit, no parity and BRGH
OpenUART1(UART_EN | UART_BRGH_FOUR, UART_RX_ENABLE | UART_TX_ENABLE, UBRG(UART1_BAUD));

By checking the data sheet but I actually don't know if everything needed is set up. And nonetheless, I don't get how to send data via TX. I've found this topic where it says:

To make some char output (a - into this example) on the UART simply write this code:

   if(!BusyUART1()) 
   putsUART1("-\r\n"); "

But I can't find where "a" is used.

So please, If someone feels able to help me, feel free to do it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
DesireM
  • 23
  • 5

2 Answers2

0

Basically following things are important while setting up UART communication

  • UART Baud rate
  • UART pin initialization (TRIS bits in PIC)
  • How many Data bits (7 or 8)
  • How many Stop bits (1 or 1.5 or 2)
  • Parity (No or Odd or Even)

I can see all these things initialized in your code, which are all necessary. So this is answer to your first question.

For your second question, UART busy bit is checked and minus sign '-' followed by CRLF is sent over the UART Tx

Amol Saindane
  • 1,568
  • 10
  • 19
  • First of all, thank you for your answer guys. Ok so everything is ready to send data right? Oh My God, you are right... so dumb =D I've tried to put a variable at the minus place's, like for a `printf` by adding %d : `putsUART1("%d\r\n",var);` but it does not work. Is it possible to do it or I have to add a specific number and rewrite the function when I want to change this number? Thank you – DesireM Jul 06 '15 at 06:16
  • @DesireM: you can use `itoa` method then converted Ascii value buffer send over the `UART`. – Amol Saindane Jul 06 '15 at 07:18
  • @DesireM: Please upvote or accept the answer if your problem is solved so that it will be useful for others who come across same problem – Amol Saindane Jul 06 '15 at 07:20
  • I can't say that your answer was helpful. I can but it doesn't count for now. it says this to me:"Thanks for the feedback! Once you earn a total of 15 reputation, your votes will change the publicly-displayed post score" – DesireM Jul 06 '15 at 07:33
  • But, by doing this :' if(!BusyUART1()) { putsUART1("0\r\n"); }' I'm writting on the TX? Because I don't see anything with the oscilloscope – DesireM Jul 06 '15 at 08:17
  • Is your `UART` `Rx` and `Tx` lines shows High on oscilloscope? When you configure your `PIC` pins for UART then those pins should be high – Amol Saindane Jul 06 '15 at 08:44
  • `Tx` is High but `Rx` is low – DesireM Jul 06 '15 at 09:24
  • If `Rx` is low and you enabled the Receive Interrupt then controller will get continuous interrupt, it will make controller get hang. Please check your hardware connections, both should be high – Amol Saindane Jul 06 '15 at 09:41
  • `Rx` still low :/ butmy other interrupt (ChangeNotfice) is working well. And it seems like everything is working well exept this RX staying low.. – DesireM Jul 06 '15 at 09:54
  • Maybe this line is wrong `U1STA = 0x1400; // Enable Tx(inv) and Rx` It could only enable TX1 and something else but I failed in saying that's I want RX1 to be set up. – DesireM Jul 06 '15 at 10:02
0

Follow these steps to set up a UART transmission:

  1. Initialize the UxBRG register for the appropriate baud rate (refer to 21.3 “UART Baud Rate Generator”).

  2. Set the number of data and Stop bits, and parity selection by writing to the PDSEL<1:0> bits (UxMODE<2:1>) and STSEL bit (UxMODE<0>).

  3. If transmit interrupts are desired, set the UxTXIE control bit in the corresponding Interrupt Enable Control register (IEC). Specify the interrupt priority and subpriority for the transmit interrupt using the UxIP<2:0> and UxIS<1:0> control bits in the corresponding Interrupt Priority Control register (IPC). Also, select the Transmit Interrupt mode by writing to the UTXISEL bits (UxSTA<15:14>).
  4. Enable the transmission by setting the UTXEN bit (UxSTA<10>), which also sets the UxTXIF bit. The UxTXIF bit should be cleared in the software routine that services the UART transmit interrupt. The operation of the UxTXIF bit is controlled by the UTXISEL control bits.
  5. Enable the UART module by setting the ON bit (UxMODE<15>).
  6. Load data to the UxTXREG register (starts transmission)

The following steps are performed to set up a UART reception:

  1. Initialize the UxBRG register for the appropriate baud rate (see 21.3 “UART Baud Rate Generator”).
  2. Set the number of data and Stop bits, and parity selection by writing to the PDSEL<1:0> (UxMODE<2:1>) and STSEL (UxMODE<0>) bits.
  3. If interrupts are desired, set the UxRXIE bit in the corresponding Interrupt Enable Control register (IEC). Specify the priority and subpriority for the interrupt using the UxIP<2:0> and UxIS<1:0> control bits in the corresponding Interrupt Priority Control register (IPC). Also, select the Receive Interrupt mode by writing to the URXISEL<1:0> bits (UxSTA<7:6>).
  4. Enable the UART receiver by setting the URXEN bit (UxSTA<12>).
  5. Enable the UART module by setting the ON bit (UxMODE<15>).
  6. Receive interrupts are dependent on the URXISEL<1:0> bit settings. If receive interrupts are not enabled, the user can poll the URXDA bit (UxSTA<0>). The UxRXIF bit should be cleared in the software routine that services the UART receive interrupt.
  7. Read data from the receive buffer. If 9-bit transmission is selected, read a word; otherwise, read a byte. The URXDA bit is set whenever data is available in the buffer.
DesireM
  • 23
  • 5