Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

UART communication betwen 2 PIC

Status
Not open for further replies.

gojkosisa

Junior Member level 3
Joined
Sep 27, 2012
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,536
Hello,

I have some electronic device for process monitoring which is connected to PC via UART. This device use PIC16F887 microcontroller and send all information about process ( sensors reading ) to PC and software display it on PC. Now I want to build another device which I'll use instead of PC. Main hardware parts are 128x64 GLCD and PIC18F4620. My idea is to connect 2 PICs with UART and instead of displaying information on PC I will display it on GLCD. I build the hardware and it works very well but I have problems with programming. To find what kind of data that device send I connected it to terminal and simulate different sensors reading. Here are information from terminal:

Received: 100
Received: 5
Received: 101
Received: 9
.........
This information I receive every 5 seconds. And I found that 100 is signal for pressure sensor and 5 is value, and 101 is temperature sensor and 9 is value. I'm using mikroC and this is my code for receiving data from UART:
Code:
void interrupt () 
{
  if (PIR1.RCIF) 
   {          
    rxchar = UART1_Read();  
   
  } 
 } 
//main 
if (flag ==1)
 {
    uart_rd=rxchar;

 if(old_uart==100)
 {
    Display uart_rd on display (pressure)
}
if(old_uart==101)
{
 Display uart_rd on display (temperature)
}
    old_uart=uart_rd;
    flag = 0;
 }
This is working but when I press OFF button I receive data in this format:
Received: 100 5 150 10 160 50 131 20 101
Received: 9
Received: 105
Received: 9

or if alarm is activated like this:

Received: 100 5 120 16 149 55 121 20 198 34 109 2 100
Received: 9
Received: 105
Received: 9
I have issues as I loose some data because UART1_read() return only one byte. I try to receive data and put it in array but still doesn't working.
 

can u tell me,

what should normal output when u pressed OFF button

and when alarm is activated what should be output
 

This signals that I receive are only for visualization. On PC I have software which display info about process like motor rotation, temperature changing, and when is ALARM activated it pop up alarm message, stop motor rotation and etc. My problem is that when I receive more than 2 bytes very fast, because all information after first 2 bytes are lost. Should I use circular buffer or some other method? Please, help :D
 

Hello, thank you for helping.

Here is my code for storing received byte in an array:
Code:
if (PIR1.RCIF) 
{
             rxchar=  UART1_Read();
             rxarray[ii] = rxchar;
             ii++;
       if(ii==3) //            4 bytes
             flag = 1;
 }

Main:
Code:
 if (flag ==1) {
    jj=0;
    flag=0;
while(jj<ii)
 {
    uart_rd=rxarray[jj];
    if(old_uart==100)
     {
         Display uart_rd on display (pressure)
     }
    if(old_uart==101)
    {
        Display uart_rd on display (temperature)
    }
    old_uart=uart_rd;
    jj++;
 }
}

With this code I'm still losing data even if I change it to receive 10 or more bytes.
 

Baud rate is 9600, and I'm not sure how to check error in which data sheet I can find that formula?

- - - Updated - - -

It is a project and has about 4-5k code lines.
 

the error in this case can be of the low current try adding capacitors to the vdd and vss of the MCU and add free wheeling diodes and caps to alarm circuit too.
 

When I send 193 from terminal it turn OFF device and send me this data:

Received: 100 5 150 10 160 50 131 20 101

As I said in my first post 100 is temperature and 5 is value of temperature sensor, 150 is motor and value of 10 means that motor is braking . When motor is stopped I receive 150 and 1.

And this is my code for sending signal for switching OFF device.

Code:
if(input==OFF)
 {
     uartSTATE=193;
     UART1_Write(uartSTATE);
 }

- - - Updated - - -

the error in this case can be of the low current try adding capacitors to the vdd and vss of the MCU and add free wheeling diodes and caps to alarm circuit too.

There is not any hardware issue because it communicate with PC perfectly. Instead of connecting device with PC I just connected it with another PIC.
 

I can only turn OFF and turn ON device. If I send 193 it will turn OFF device and with 192 it will turn ON device. Also I can change parameters such as max/min temperature, max pressure, motor speed..etc. I don't have problems with sending. Receiving data is only for displaying values on display and animation of motor, pump, and turbine.
 

I send commands to PIC16F887 and that microcontroller is controlling motor speed, temperature alarm, turbine and pressure alarm. That is 4 devices
 

that means u are passing totally around 8 byte data..........

what u have to do is in transmitting part is after u transmit 8 bytes who have to give some delay for operation of received data in receiving controller....

In receiving part what u have to first store all 8 bytes and then perform action on received data and then go to receiving the data gain........

hope this was help full........
 

I dont understand you very well, do you have some code example or could you write pseudo code?
 

Are you using mikroC Pro? Post the full code so that I can study it and write the code for you.
 

Are you using mikroC Pro? Post the full code so that I can study it and write the code for you.

Yes, I'm using mikroC Pro. Finally I solved this problem but now I have new issue.
Sorry I can't post the full code, but please check this:

Initialization:
Code:
void uartInit()
{
       PIE1.RCIE=1;    //enable receive interrupt
       INTCON.GIE = 1;
       INTCON.PEIE = 1;
       UART1_Init(9600);
       Delay_ms(200);    // Wait for UART module to stabilize
}

Interrupt:

Code:
#define RX_BUFFER_SIZE 11
char volatile  rx_buffer[RX_BUFFER_SIZE];
unsigned volatile char rx_wr_index,rx_rd_index,rx_counter;
unsigned volatile char *data__;

void interrupt()
{
 if (PIR1.RCIF)
   {
       
     flag=1;
     data__ =UART1_Read();
     rx_buffer[rx_wr_index]=data__;
     if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
     if (++rx_counter == RX_BUFFER_SIZE)
        {
            rx_counter=0;
       }
   }
}

Main code:
Code:
unsigned volatile char newUart;
unsigned volatile char oldUart;
unsigned volatile char stringUart;
char *someText;

void main ()
{
  uartInit();
  while(1)
  {
     newUart=getChar();
      if(flag==1)
       {
         flag=0;
        if(oldUart==100)         //pressure sensor
                  {
                    stringUart=newUart;
                    ByteToStr(stringUart,someText);
                    Glcd_write_Text(someText,100,0,1);
                  }
        if(oldiUart==101 )     //temperature sensor
                  {
                     stringUart=newUart;
                    ByteToStr(stringUart,someText);
                    Glcd_write_Text(someText,49,0,1);
                  }
       }
   if(oldUart==150 )
  {
   switch(newUart)
   {
   case 10:{ animationUart[7]=1; break;}   //motor ON
   case 20:{ animationUart[7]=0; break;}   //motor OFF
   case 30:{ animationUart[1]=1; break;}  //turbine ON
   case 40:{ animationUart[1]=0; break;}  //turbine OFF
  }

if(oldUart==160) 
  {
    case 10:{ animationUart[4]=1; break;}  //pump ON
    case 20:{ animationUart[4]=0; break;}  //pumpa OFF
    case 30:{ animationUart[3]=1; break;}  //ventilator ON
    case 40:{ animationUart[3]=0; break;}  //ventilator OFF
  }

if(oldUart==170) 
  {
    case 10:{ animationUart[6]=1; break;}  //alarm pressure ON
    case 20:{ animationUart[6]=0; break;}  //alarm pressure OFF
    case 30:{ animationUart[2]=1; break;}  //alarm temperature ON
    case 40:{ animationUart[2]=0; break;}  //alarm temperature OFF
  }
oldUart=newUart;
animateProcess(); 

  }
 
  }
}

//get byte from buffer
char getchar(void)
{
    char _data;
  // while (rx_counter==0);
    _data=rx_buffer[rx_rd_index];
    if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
    --rx_counter;
    return _data;
}
//display animation of motor rotating, turbine, ventilator..

void animateProcess()
{
         if(animationUart[7]==1) //pump animation
      {
      
       switch (animation3)
          {
            case 1:{ Glcd_PartialImage(39,15, 15, 28, 15, 28, pump1); break;  }
            case 2:{ Glcd_PartialImage(39,15, 15, 28, 15, 28, pump2); break;}
            case 3:{ Glcd_PartialImage(39,15,15, 28, 15, 28, pump3); break;  }
            case 4:{ Glcd_PartialImage(39,15, 15, 28, 15, 28, pump2); break;}
          }
      }
   
       if(animationUart[7]==1)  //turbine rotating
      {
      
       switch (animation1)
          {
            case 1:{ Glcd_PartialImage(39,15, 15, 28, 15, 28, turbine1); break; }
            case 2:{ Glcd_PartialImage(39,15, 15, 28, 15, 28, turbin2); break;}
    
          }
      }
                                  .
                                  .
                                  .     
                                  .
                                  etc...
}

As I said before, on the terminal value of temperature and pressure sensor I receive every 5 seconds. But with this code every 20 seconds or more. Probably code for circular buffer isn't working properly. Could you please share your code for circular buffer in mirkroC?
 

What is your new issue? what is this
Code:
 if(old [B] i [/B] Uart==101 )
Should it be
Code:
 if(oldUart==101 )

Shouldn't you reset PIR1.RCIF in interrupt routine?

Code:
void interrupt()
{
 if (PIR1.RCIF)
   {
     PIR1.RCIF = 0;  
     flag=1;
     data__ =UART1_Read();
     rx_buffer[rx_wr_index]=data__;
     if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
     if (++rx_counter == RX_BUFFER_SIZE)
        {
            rx_counter=0;
       }
   }
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top