uart between 2 pics

Discussion in 'Embedded Systems and Microcontrollers' started by Guinness, Jan 12, 2011.

  1. Guinness

    Thread Starter Member

    Dec 31, 2009
    Hi everyone,

    I am trying to find out where I can find info on how to get one pic to send info to another pic. The pics I am using are PIC16F690, writing in C and using HITECH compiler.

    After looking around the best way people are saying is to use UART, I have tried to read up on it but I cant find enough info on the subject. All I want to do is gather info with one pic, then send it to another. Which the second pic will use that info in its program.

    I have programmed pics before for different projects, but this is the first time I need to communicate between 2 of them.

    Im not looking for someone to write me the code or anything, just a help where to start, what to look at and any tutorials out there you know off that could help.

  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    You need to look at your chip and see if it supports serial I/O. If it does not then you can still "bit bang" using a couple of regular I/O chips.

    Most compilers have a library for both.
  3. nsaspook

    AAC Fanatic!

    Aug 27, 2009
  4. eblc1388

    Senior Member

    Nov 28, 2008
    The 16F690 contains 1 USART and also have SPI inbuilt.

    For communication between two PICs, just connect the RX pin of one PIC to the TX pin of another. You don't need to use any level conversion chips like MAX232..etc.

    Or you can choose to use SPI to do the communication instead.
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    Is it the C programming that is the trouble?
  6. Guinness

    Thread Starter Member

    Dec 31, 2009
    Thanks for all the replies,

    Thats what im looking for, connecting the TX to the RX pins. I have read that is all thats needed for the hardware side, unless there is a bit more to it?.

    This is the part I am struggling on getting any info on, I dont know if there is any special registers that need setting? Or the commands used to send or recieve the data?

  7. t06afre

    AAC Fanatic!

    May 11, 2009
    You must setup the serial port, by setting the correct registers. This is described in the data sheet. In section 12. In Hi-Tech C it is none built in library functions for serial communication. But it is some examples in the samples folder. However I do not know if they work with your chip. So the best thing will be to write all the code from scratch. And this is not so hard. Start with functions for setting up the UART. Then functions for transmitting, and then receiving data.
    What kind of equipment and software/hardware do you plan to use. Do have some RS232 port/USB to serial converter you can use for debugging? If you choose to use a PC for serial testing you must also include a TTL to RS232 level converter. Google max232.
  8. Guinness

    Thread Starter Member

    Dec 31, 2009
    So it is just the UART functions then:), I will do that then cheers. I will write a simple program to send data from one pic to another and see if it does anything. At least then I suppose I will have some code to post.

    I use the PicKit2 device with MPLAB for debugging.
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    Pickit and MPLAB sounds like a good combination. Post your code here and let us have a look.
  10. Guinness

    Thread Starter Member

    Dec 31, 2009
    This is all the code I could figure out from looking at the datasheet. I have done code for both PICS, one transmits a number, the other one compares the number with a set value. If they match it light an LED. I can't try the code on actual PIC's yet as I have only 1 and am waiting for the other one to be delivered. Both programs compiled ok.

    The transmit PIC code
    Code ( (Unknown Language)):
    1. /* This is a sample program to transmit a 8 bit binary
    2.  * number to another pic using UART. Will be using 4Mhz clock
    3.  * and a Baud Rate of 2400.
    4.  */
    6.  #include <htc.h> //HITECH CC header file
    9.  // Internal clock, Watchdog off, MCLR off, code unprotected
    11.  main()
    12.  {
    13.   ANSEL = 0;  //Set all pins to digital
    14.   ANSELH = 0;
    15.   CM1CON0 = 0; //Comparator 1 off
    16.   CM2CON0 = 0; //Comparator 2 off
    18.   SPBRG = 25; //From chart in datasheet
    19.   BRGH = 0;  //Sets Low Baud Rate
    20.   SYNC = 0;  //Sets Asynchronous mode
    21.   SPEN = 1;  //Enables EUSART
    22.   TXEN = 1;  //Enables the EUSART transmiter
    24.   TXREG = 0b01101011; //Transmit the binary number
    25.  }
    The Reciever PIC code
    Code ( (Unknown Language)):
    1. /* Program to recieve a binary code 01101011 through using
    2.  * the UART and lighting an LED to confirm it has been
    3.  * recieved. Will be running at 4Mhz clock and using a
    4.  * Baud rate of 2400.
    5.  */
    7.  #include <htc.h> //HITECH CC Header file
    10.  // Internal clock, Watchdog off, MCLR off, code unprotected
    12.  unsigned char compare = 0; //create variable and clear
    14.  main()
    15.  {
    16.   ANSEL = 0;  //Set all pins to digital
    17.   ANSELH = 0;
    18.   CM1CON0 = 0; //Comparator 1 off
    19.   CM2CON0 = 0; //Comparator 2 off
    20.   PORTA = 0;  //clear portA
    21.   TRISA = 0;  //All portA set to outputs
    23.   SPBRG = 25; //Chart from datasheet
    24.   SYNC = 0;  //Asynchronous mode
    25.   SPEN = 1;  //Enables EUSART
    26.   CREN = 1;  //Enables UART receiver
    28.   while(1)
    29.   {
    30.    while (!RCIF); //stay untill RCIF flag equals 1
    31.    compare = RCREG; //copy recieved data to variable
    33.    if(0b01101011 == compare)//compare recieved data
    34.    {
    35.     RA0 = 1; //Light the LED
    36.    }
    37.   }
    38.  }
    Please point out anyway it wont work or something I have missed.

  11. Markd77

    Senior Member

    Sep 7, 2009
    I think trisb<6:7> must be set to 1 (section 12.0 of datasheet)
    It should be possible to do some testing with the PIC you have by connecting the rx and tx pins (check voltages first in case something isn't set up right - you don't want 2 outputs connected if it's possible for them to be high and low).
  12. Guinness

    Thread Starter Member

    Dec 31, 2009
    I was about to set the TRISB for the TX and RX but then I seen these 2 notes.

    Would I still need to set the TRISB bits or does these notes only apply for certain things?

    Do you mean connect the TX to the RX on the same PIC?
    If that works then thats great, I can see if the program works.

  13. wannaBinventor


    Apr 8, 2010
    Ya, that's what he's saying.

    You will of course need to change one of the programs to do both TX and RX.
  14. Markd77

    Senior Member

    Sep 7, 2009
    Confusing.. I guess it can't hurt to set them.
    Just as a thought, you can use the PICKIT2 as a logic analyser if you don't have a scope to see the usart signal. Just download the PICKIT2 software from microchip.
    Guinness likes this.
  15. t06afre

    AAC Fanatic!

    May 11, 2009
    It is often more easy to do serial port debugging with a PC using hyperterm, or a similar terminal program. And in that case it is worth mention that a PICKIT 2 may also work as a serial to USB analyzer/terminal program. I have not done this myself. But how to do it is described in the PICKIT 2 manual section 7. If you open the PICKIT2 app (not MPLAB) and go to help the manual should open. But the document name is PICkit2 User Guide 51553E.pdf at least on my computer
  16. Guinness

    Thread Starter Member

    Dec 31, 2009
    cheers, I have managed to get the USART to work now, took a lot of attempts though!
    I have had a quick look at the user guide and will deff use it when I write the final code. Prob would have saved me a lot of time if I had set it up before.

    Cheers again :)