uart between 2 pics

Thread Starter

Guinness

Joined Dec 31, 2009
81
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.

Cheers
 

spinnaker

Joined Oct 29, 2009
7,830
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.

Cheers
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.
 

eblc1388

Joined Nov 28, 2008
1,542
The pics I am using are PIC16F690, writing in C and using HITECH compiler.
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.
 

Thread Starter

Guinness

Joined Dec 31, 2009
81
Thanks for all the replies,

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.
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?.

Is it the C programming that is the trouble?
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?

cheers
 

t06afre

Joined May 11, 2009
5,934
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?
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.
 

Thread Starter

Guinness

Joined Dec 31, 2009
81
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.
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.
 

t06afre

Joined May 11, 2009
5,934
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.
Pickit and MPLAB sounds like a good combination. Post your code here and let us have a look.
 

Thread Starter

Guinness

Joined Dec 31, 2009
81
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
Rich (BB code):
/* This is a sample program to transmit a 8 bit binary
 * number to another pic using UART. Will be using 4Mhz clock
 * and a Baud Rate of 2400.
 */
 
 #include <htc.h> //HITECH CC header file
 
 __CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT);
 // Internal clock, Watchdog off, MCLR off, code unprotected
 
 main()
 {
  ANSEL = 0;  //Set all pins to digital
  ANSELH = 0;
  CM1CON0 = 0; //Comparator 1 off
  CM2CON0 = 0; //Comparator 2 off
  
  SPBRG = 25; //From chart in datasheet
  BRGH = 0;  //Sets Low Baud Rate
  SYNC = 0;  //Sets Asynchronous mode
  SPEN = 1;  //Enables EUSART
  TXEN = 1;  //Enables the EUSART transmiter
  
  TXREG = 0b01101011; //Transmit the binary number
 }
The Reciever PIC code
Rich (BB code):
/* Program to recieve a binary code 01101011 through using
 * the UART and lighting an LED to confirm it has been
 * recieved. Will be running at 4Mhz clock and using a 
 * Baud rate of 2400.
 */
 
 #include <htc.h> //HITECH CC Header file
 
 __CONFIG (INTIO & WDTDIS & MCLRDIS & UNPROTECT);
 // Internal clock, Watchdog off, MCLR off, code unprotected
 
 unsigned char compare = 0; //create variable and clear
 
 main()
 {
  ANSEL = 0;  //Set all pins to digital
  ANSELH = 0;
  CM1CON0 = 0; //Comparator 1 off
  CM2CON0 = 0; //Comparator 2 off
  PORTA = 0;  //clear portA
  TRISA = 0;  //All portA set to outputs
  
  SPBRG = 25; //Chart from datasheet
  SYNC = 0;  //Asynchronous mode
  SPEN = 1;  //Enables EUSART
  CREN = 1;  //Enables UART receiver
  
  while(1)
  {
   while (!RCIF); //stay untill RCIF flag equals 1
   compare = RCREG; //copy recieved data to variable
   
   if(0b01101011 == compare)//compare recieved data
   {
    RA0 = 1; //Light the LED
   }
  }
 }
Please point out anyway it wont work or something I have missed.

cheers
 

Markd77

Joined Sep 7, 2009
2,806
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).
 

Thread Starter

Guinness

Joined Dec 31, 2009
81
I think trisb<6:7> must be set to 1 (section 12.0 of datasheet)
I was about to set the TRISB for the TX and RX but then I seen these 2 notes.


Note 1:​
When the SPEN bit is set the RX/DT I/O pin
is automatically configured as an input,
regardless of the state of the corresponding
TRIS bit and whether or not the EUSART
receiver is enabled. The RX/DT pin data
can be read via a normal PORT read but
PORT latch data output is precluded.

2:​
The TXIF transmitter interrupt flag is set

when the TXEN enable bit is set.

Note:​
When the SPEN bit is set the TX/CK I/O
pin is automatically configured as an
output, regardless of the state of the
corresponding TRIS bit and whether or not
the EUSART transmitter is enabled. The
PORT latch is disconnected from the
output driver so it is not possible to use the

TX/CK pin as a general purpose output.
Would I still need to set the TRISB bits or does these notes only apply for certain things?

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).
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.

cheers
 

Markd77

Joined Sep 7, 2009
2,806
Would I still need to set the TRISB bits or does these notes only apply for certain things?
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.
 

t06afre

Joined May 11, 2009
5,934
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
 

Thread Starter

Guinness

Joined Dec 31, 2009
81
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
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 :)
 
Top