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.

Uart1 & Uart2 working together = problem

Status
Not open for further replies.

hitachicm615

Newbie level 5
Joined
Feb 16, 2012
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,361
Hello,

I have a slave card with a ultrasounds detector which work on Uart2 and MAX485 on Uart1 to share data on RS485Bus.
- RS485 Bus work well when i use only Uart1 in my code.
- Ultrasounds detector SRF02 work well when i use only Uart2 in my code.

I want share distance mesurement on my RS485 Bus.

When i use Ultrasounds detector with RS485 bus i have problem :(

It's working properly without :
Code:
if(UART2_Data_Ready())
      {
      cm=Uart2_read();
      }
I send only command Uart2_Wirte to ultrasoud detector;

But i need to listen answer of my ultrasound detector on Uart2.
If I only add read from Uart2, it's doesn't work and i need to read ranging from Uart2

I can't read from Uart1 and Uart2 together. I must be an interrupt problem but how fix the problem ?

My code without reading Uart2, but in need this. How i can do :( :
Code:
int cm = 50;
int cnt;
int error = 0;

char dat[10];                          // buffer for receving/sending messages
char i,j;

sbit  rs485_rxtx_pin  at LATC2_bit;               // set transcieve pin
sbit  rs485_rxtx_pin_direction at TRISC2_bit;   // set transcieve pin direction

// Interrupt routine
void interrupt() {
 RS485Slave_Receive(dat);
 TRISA = 0b1111110; //1 eteint, 0 allumé ici RA0 allumé : rouge
            RA0_bit=1;
            RA1_bit=0;
}

void main() {

  UART2_Init(9615);
  Delay_ms(100);
  //UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);
  
  ANSELA = 0;
  ANSELB = 0;                                        // Configure AN pins as digital I/O
  ANSELC = 0;
  ANSELD = 0;

  LATB  = 0;
  LATD  = 0;
  TRISB = 0;
  TRISD = 0;
  TRISC = 0;

  UART1_Init(9615);                                  // initialize UART1 module
  Delay_ms(100);
  //UART_Set_Active(&UART1_Read, &UART1_Write, &UART1_Data_Ready, &UART1_Tx_Idle);

  RS485Slave_Init(160);

  dat[4] = 0;                                        // ensure that message received flag is 0
  dat[5] = 0;                                        // ensure that message received flag is 0
  dat[6] = 0;                                        // ensure that error flag is 0

  RCIE_bit = 1;                                      // enable interrupt on UART1 receive
  TXIE_bit = 0;                                      // disable interrupt on UART1 transmit
  PEIE_bit = 1;                                      // enable peripheral interrupts
  GIE_bit = 1;

 while(1)
 {
  do{
    //UART_Set_Active(&UART2_Read, &UART2_Write, &UART2_Data_Ready, &UART2_Tx_Idle);
    Uart2_Write(0x00); // SFR02 Address (0x00 by default)
    Uart2_Write(0x54); // Real Ranging Mode - Result in centimeters, automatically Tx range back to controller as soon as ranging is complete.
    //delay_ms(80); // Wait >70 msec to get back the ranging
    //Uart2_Write(0x5E);
    //I NEED THIS TO READ RANGING DISTANCE FROM ULTRASOUND DETECTOR
    /*
      if(UART2_Data_Ready())
      {
      cm=Uart2_read();
      }
      */

    }while(cm==0);
    delay_ms(500);

    if (cm<50)
          {
            TRISA = 0b1111110; //1 eteint, 0 allumé ici RA0 allumé : rouge
            RA0_bit=1;
            RA1_bit=0;
            dat[0] = 162;
          }

          if(cm>50)
          {
           TRISA = 0b1111101;   // RA1 Allumé : vert
           RA1_bit=1;
           RA0_bit=0;
           dat[0] = 161;
          }

    if (dat[5])
        {                   // if an error detected, signal it by
          dat[5] = 0;
        }

    if (dat[4])
        {
          dat[4] = 0;                    //   data[4] is set to 0xFF
          j = dat[3];
          Delay_ms(1);
          //UART_Set_Active(&UART1_Read, &UART1_Write, &UART1_Data_Ready, &UART1_Tx_Idle);
          RS485Slave_Send(dat,1);        //   and send it back to master
        }
    }
}

Thanks

Do you see a mistake on my code ?
 

Just a thought is it more preferable to have two interrupts.

1. high priority for your sensor
2. low priority for the RS485

To prevent them clashing
 

How i could do that on my code ? I'm begginer :(
Maybe can help, datasheet of my PIC18F45K22 **broken link removed**
 

I can't fix the problem since 3 days. Nobody have issue ?

Thanks
 

Code:
void interrupt( void )
{	
        Sensor code here				
}
void interrupt_low( void )
{
	RS485 code here
} 


  uart1 // rs485 to have low level of priority
  uart2 // Sensor to have high level of priority

Lookup interrupt examples to give you more of an idea.

Good luck
 

hello

You can keep UART1 Hardware high level interrupt
and build a software UART by using RB0 interrupt,wich is always high priority.
Use RB0 to detect the edge of the 1rst bit, and decode caractere inside the interrupt subroutine.
What are you speed for UART1 and UART2
Can you use a handshake protocole, to treat one RS232 link in the main programme
by pooling if speed is low enough. and RB0 interrupt , just to arm a flag : it's time to read the comm.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top