PIC 2 PIC. Best solution? Is it USART? *urgent*

General discussion on mikroC.
Post Reply
Author
Message
_Peter_
Posts: 45
Joined: 28 Apr 2005 08:57
Location: Sweden

PIC 2 PIC. Best solution? Is it USART? *urgent*

#1 Post by _Peter_ » 28 May 2005 19:36

What is the best way to communicate from one pic to another pic?

I've tried this with no result... I'm using two PIC18F452 with 10MHZ crystals. Powerup timer is on and WDT is off...

Code: Select all


//this is the sending pic

        unsigned short send;
main()
      {
      ADCON1 = 0x80;
      TRISA = 0xff;      //input
      Usart_Init(9600);
      do {
          send = Adc_Read(0);
          Usart_Write(send);
          } while (1);

      }

Code: Select all


//this is the recieving pic

        unsigned short recieve;

main()
      {
      TRISB = 0x0;       //output
      Usart_Init(9600);
      do {
         if (Usart_Data_Ready()) {   // If data is received
            recieve = Usart_Read();         // Read the received data
            if (recieve > 10) PORTB = 0xff;           // PORTB = 1
            }
        } while (1);

      }

nothing happends... whats wrong?

/Peter

pizon
mikroElektronika team
Posts: 823
Joined: 11 Oct 2004 08:53

Re: PIC 2 PIC. Best solution? Is it USART? *urgent*

#2 Post by pizon » 28 May 2005 19:51

Do you communicate through MAX232 or directly, USART-to-USART? The current version of the USART library supports only the former way, to do the latter you need to invert the data bit. This feature is being tested, and will become part of the standard mikroC USART library, but I can't give you the exact date.
pizon

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#3 Post by gambrose » 28 May 2005 20:20

If you do not have MAX232 on both chips it will not matter (inverting twice gets you back to where you started).
If you have used one MAX232 then there will be problems and if you have a long cable line then you should be using MAX232

I would check that you have the RX connected to the TX on the other chip its easy to connect RX to RX and TX to TX.
Graham Ambrose

_Peter_
Posts: 45
Joined: 28 Apr 2005 08:57
Location: Sweden

#4 Post by _Peter_ » 28 May 2005 20:28

gambrose wrote:
...I would check that you have the RX connected to the TX on the other chip its easy to connect RX to RX and TX to TX.
I've tried both before I posted this subject... no result :?

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#5 Post by LGR » 28 May 2005 20:31

SPI is a viable alternative. One advantage over USART is that it is insensitive to clock variations. But USART-USART should work.
If you know what you're doing, you're not learning anything.

_Peter_
Posts: 45
Joined: 28 Apr 2005 08:57
Location: Sweden

#6 Post by _Peter_ » 28 May 2005 20:42

LGR wrote:SPI is a viable alternative. One advantage over USART is that it is insensitive to clock variations. But USART-USART should work.
hmm never used SPI....

How do you translate the USART code to SPI? Can you connect it directly to another PIC or is it necessary to have an extra component.

I'm trying to control four stepper motors via two analog joystick. One pic reads the analog joysticks and sends it to the other pic which controls the stepper motors.

/Peter

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#7 Post by LGR » 28 May 2005 20:59

1. SPI is syncronous, and meant for very close range; usually on the same PCB. It uses no driver hardware, but can easily be adapted to use drivers, because each line is unidirectional.

2. For details, read the Microchip data sheet, and the SPI library documentation. It is extremely simple in concept; it is just a shift register, which connects around to another shift register.

3. Warning: One very confusing aspect is that SPI always reads and writes at the same time. If you read, you must furnish a dummy argument which will be written.

4. You must designate one PIC as master and the other as slave.
If you know what you're doing, you're not learning anything.

_Peter_
Posts: 45
Joined: 28 Apr 2005 08:57
Location: Sweden

#8 Post by _Peter_ » 28 May 2005 21:03

Thanks for the tip LGR.
Although I don't think I'll be using SPI for this project since I probably need a wireless communication...

/Peter

Zed
Posts: 41
Joined: 20 Apr 2005 08:47
Location: UK

#9 Post by Zed » 28 May 2005 22:09

You can halve your trouble very quickly. Confirm the sending pic first by connecting it serially to your PC, and run the mikroC terminal program, or Windows Hyperterminal, to monitor the pic output. I would just have it send a single char, ie

Code: Select all


main()

TRISC  = 0b10XXXXXX;            // prep the RS i/o bits - needed?
unsigned short send;

{
    Usart_Init(9600);
    send = 77;
    while(1)
    {
         Usart_Write(send);
         delay_ms(1000);
    }
}
Then you will know which end to hit with a big stick...

_Peter_
Posts: 45
Joined: 28 Apr 2005 08:57
Location: Sweden

#10 Post by _Peter_ » 28 May 2005 23:07

Hmm...
Not working... probably a hardware fault then. Although a got a lot of M's like in ascii 77 = m from hyperterminal so it might be a glitch. Better get the soldering iron heated :D

/Peter

gambrose
Posts: 369
Joined: 28 Sep 2004 17:34
Location: uk

#11 Post by gambrose » 29 May 2005 13:35

In your code for the sending pic you assign an unsigned int to an unsigned short

Code: Select all

send = Adc_Read(0);
This should really throw an error, as it has not been explicitly cast.

Probably not effecting usart but worth mentioning I thought
Graham Ambrose

Post Reply

Return to “mikroC General”