• AVR Freaks

Hot!pic18F45k40 communication issue.

Page: 12 > Showing page 1 of 2
Author
AMPS
Super Member
  • Total Posts : 588
  • Reward points : 0
  • Status: offline
2020/10/15 22:12:10 (permalink)
0

pic18F45k40 communication issue.

Dear all
I am using pic18F45k40 with 8mhz crystal. I have developed code for modbus RTU with function code 1.Here i have attached schematic . request and response waveform for reference
 
I have 2 product connected in daisy chain manner with 120 E terminator resistor. i have checked individually each product its working well without any single pole mismatch. when i introduce both device modbus time out error i will get.
 
if i turn off any one of device , device start communicating. I would like to know what is error in my code or in hardware so that i get proper response when both are connected.
 
 


void Serial_Interrupt() {

  if (PIR3bits.RC1IF == 1) {

    for (index = 0; index < 8; index++) {
      rxbuf[index] = Serial_Receive_byte();
    }
    if (index >= 8) {
      rec_flag = 1;
      DE = 1;
    }
    if (DE == 1) {
      __delay_us(5000);
      serial_access();
    }
    if (index >= 20) {
      index = 0;
    }

    if (1 == RCSTAbits.FERR) {

      RCSTAbits.SPEN = 0;
      RCSTAbits.SPEN = 1;
      // DE=1;
    }
    if (1 == RCSTAbits.OERR) {

      RCSTAbits.CREN = 0;
      RCSTAbits.CREN = 1;

    }

  }

}

void ISR_Routine() {

  Serial_Interrupt();

  if (PIR0bits.TMR0IF == 1) {

    BLINKCOUNT2++;
    if (BLINKCOUNT2 > 300) {
      //230 intial value

      BLINKCOUNT2 = 0;
      if (bits.BLINKFLAG2 == 0)
        bits.BLINKFLAG2 = 1;
      else
        bits.BLINKFLAG2 = 0;
    }
    PIR0bits.TMR0IF = 0;
  }

}

void EUSART1_Initialize(void) {
  PIE3bits.RC1IE = 0;
  PIE3bits.TX1IE = 0;
  BAUD1CON = 0x09;

  // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN enabled; SREN disabled;
  RC1STA = 0x98;

  // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
  TX1STA = 0x24;

  // SP1BRGL 207;
  SP1BRGL = 0xCF;

  // SP1BRGH 0;
  SP1BRGH = 0x00;

}

void main() {
  SYSTEM_Initialize();

  while (1) {

  }

}

post edited by AMPS - 2020/10/17 20:41:30

Attached Image(s)


Amps
*.*.*.*.*.*.*.*.*.*.*.*.*
#1

33 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28694
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/16 00:21:15 (permalink)
    0
    Change
    void Serial_1_Send_byte(uint8_t trbuf1) {
      TX1REG = trbuf1;
      while (0 == TX1STAbits.TRMT);
      while (0 == PIR3bits.TX1IF);
      PIR3bits.TX1IF = 0;
      //while (!TX1IF);
    }

    to
    void Serial_1_Send_byte(uint8_t trbuf1) {
      while (0 == PIR3bits.TX1IF);
      TX1REG = trbuf1;
    }

     
    You have a whole lot of what appears to be interrupt receive code, but the RC1IE bit is cleared so they will never be invoked, which is just as well, as you are breaking all the rules of interrupt use.
    You should never loop inside an interrupt waiting for things.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    ric
    Super Member
    • Total Posts : 28694
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/16 01:05:11 (permalink)
    0
    Assuming that RC1IE is actually set somewhere else in code you have not posted, you really need to redo the interrupt service from scratch.
    All the ISR should do is add each received bytes to a FIFO buffer, so it can be handled by your non-interrupt code when it has been received.
    THIS code just stays inside the ISR until the whole block has been received, which is a terrible way to do it.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    vexorg
    Senior Member
    • Total Posts : 170
    • Reward points : 0
    • Joined: 2019/09/27 10:59:40
    • Location: 0
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/16 01:37:46 (permalink)
    -1 (1)
    wrong thread
    post edited by vexorg - 2020/10/16 01:39:14
    #4
    AMPS
    Super Member
    • Total Posts : 588
    • Reward points : 0
    • Status: offline
    Re: pic18F45k40 communication issue. 2020/10/16 03:41:52 (permalink)
    0
    I am beginner here , is there any example code available for modbus RTU format. I have got sample example done for dispic device . i am converting it to pic18F device , where i found its working well(DISPIC).
     
    mean while in above code can you guide me what would be standard procedure to receive data. can get some example code to establish receive data,
     
    Do you think my Serial_Access() code portion is proper. i am facing issue when both device connected and slave sending response. If you see patern only last byte is missing when both device made parallel.
     
     
    I would like to know from experts here in above attached code which portion are correct and which parts need to modify.modify portion what would be steps to make it work
     
    In my code i could able to receive respond without any single pole mismatch. i have kept device for testing 2 days where i could able to get send and receive response properly..
     
     
     

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #5
    vexorg
    Senior Member
    • Total Posts : 170
    • Reward points : 0
    • Joined: 2019/09/27 10:59:40
    • Location: 0
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/16 05:05:35 (permalink)
    0
    There's no real right or wrong way to do things just better or worse.
     
    In general I'd always have an interrupt do the minimum possible. That allows a more multitasking feel. If you need it to do nothing else, then it'll work in the interrupt, just depends on the context of what you want it to do.
    #6
    ric
    Super Member
    • Total Posts : 28694
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/16 13:33:32 (permalink)
    0
    Was this code created under MCC?
    You call a SYSTEM_Initialize() function that you did not show the code for.
    Presumably the timer and interrupts are being set up in there.
    Also, you do not have a real interrupt service. ISR_Routine() must be called from the real one, which again you have not shown.
    I am going to guess that you don't ever set the RC1IE bit, and the serial is only sort of working because you are polling the RC1IF bit inside the timer interrupt service.
     
    Can we see your real code?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    AMPS
    Super Member
    • Total Posts : 588
    • Reward points : 0
    • Status: offline
    Re: pic18F45k40 communication issue. 2020/10/17 00:01:45 (permalink)
    0
    OK here is code . what i converted from DISPIC device. It has been used in one of device and its working on multiple slave device . I have few doubts on below dispic code conversion
     
    unsigned char starthgh;
    unsigned char Rx_Count;
    unsigned char Tx_Count;
    unsigned char Frame_Dly_flag, Transmit_flag;
    unsigned char Rec_Data;
    unsigned char Command_ID; // Command type of current receiving frame
    unsigned char starthigh; // start high byte of current receiving frame
    unsigned char startlow; // start lower byte of current receiving frame
    unsigned char reghgh; // start higher byte of current receiving frame
    unsigned char reglow; // start lower byte of current receiving frame
    unsigned char chcksumhgh;
    unsigned char chcksumlow;
    float
    var;
    unsigned char start_byte;
    unsigned char byte_count;
    unsigned char ptr;
    unsigned char Data_Len_Count; // Length of current receiving frame
    unsigned char Check_Sum = 0; // Current Checksum(receiving process)
    unsigned int Data_reg;
    unsigned char write_reg;
    //unsigned char Check_Sumhgh=0;
    // unsigned char Check_Sumlow=0;

    unsigned char crc_msb;
    unsigned char crc_lsb;

    unsigned char Tx_Count; // transmit byte counter
    unsigned char * Tx_Ptr; // transmit buffer pointer
    unsigned char UART_Rx_Timer; // timer to count when every frame receiving begining
    unsigned char temp_id;
    unsigned char i;
    unsigned char para1, para2, para3;
    unsigned char temp_id;

    volatile union {
      unsigned char BYTEM;
      struct {
        unsigned slavid: 1; // flag of got slaveid ----- 00
        unsigned funcode: 1; // flag of got functioncode ----- 00
        unsigned starthgh: 1; // flag of got starthigh
        unsigned startlow: 1; // flag of got startlow
        unsigned reghgh: 1; // flag of got register to read higher byte
        unsigned reglow: 1; // flag of got register to read lower byte
        unsigned chcksumlow: 1; // flag of got checksum lower byte
        unsigned chcksumhgh: 1; // flag of checksum higher byte
        //unsigned Frame: 1; // flag of successfully received a frame, this flag is shared
      };

      unsigned char BYTE;
      struct {
        unsigned Head0: 1; // flag of got Header0 ----- 00
        unsigned Head1: 1; // flag of got Header1 ----- 00
        unsigned CMD: 1; // flag of got Data ID
        unsigned FrmLth: 1; // flag of got Frame Length
        unsigned DtFld: 1; // flag of got Data Field
        unsigned Chksum: 1; // flag of got Checksum
        unsigned End: 1; // flag of got the End of the frame
        unsigned Frame: 1; // flag of successfully received a frame, this flag is shared
        // in other application,here it's the same as Rec_Flag.End
      };
    }
    Rec_Flag;

    unsigned char par_val[50];
    unsigned char uart_buf[20];
    unsigned char Device_match_M;

    void ChecksumCal(char No_Bytes) {

      unsigned char highbyte, lowbyte;
      unsigned short crc, thisbyte, i, shift, lastbit;
      crc = 0xFFFF;
      No_Bytes = No_Bytes - 1;
      for (i = 0; i <= No_Bytes; i++) {
        thisbyte = uart_buf[i];
        crc = crc ^ thisbyte;
        for (shift = 1; shift <= 8; shift++) {
          lastbit = crc & 0x0001;
          crc = (crc >> 1) & 0x7fff;
          if (lastbit == 0x0001) {
            crc = crc ^ 0xa001;
          }
        }
      }
      lowbyte = (crc >> 8) & 0xff;
      highbyte = crc & 0xff;
      crc_msb = highbyte;
      crc_lsb = lowbyte;
    }

    void UART_Receive(void) {
      unsigned char i, j;

      Device_ID = 9;
      Rec_Data = RC1REG; //ReadUART1();
      uart_buf[Rx_Count] = Rec_Data;
      Rx_Count++;

      if (Rx_Count > 8) {
        Rx_Count = 0;
        Rec_Flag.BYTEM = 0; // The data being received is not header1, back to non-receive status
        Rec_Flag.starthgh = 0;
        Check_Sum = 0;
        UART_Rx_Timer = 0;
        return;
      }

      if (!Rec_Flag.slavid) // Not got header0 --- 0x00
      {
        if ((Rec_Data == Device_ID) && (Device_ID > 0)) {

          Device_match_M = 1;
          Rec_Flag.slavid = 1; // Indicating header0 got
          Check_Sum = 0;
        } else {
          Device_match_M = 0;
          Rx_Count = 0;
          Rec_Flag.BYTEM = 0; // The data being received is not header1, back to non-receive status
          Rec_Flag.starthgh = 0;
          Check_Sum = 0;
          UART_Rx_Timer = 0;
        }
      } else if (!Rec_Flag.funcode) // Not got header1--- 0xff
      {
        if (Rec_Data == 1) // CHANGED T0 01
        {
          Rec_Flag.funcode = 1;
        }
        // Indicating header1 got
        else {
          Rec_Flag.BYTEM = 0;
          Rec_Flag.starthgh = 0; // The data being received is not header1, back to non-receive status
          Check_Sum = 0;
          UART_Rx_Timer = 0;
          Rx_Count = 0;
        }
      } else if (starthgh == 0) // Not got ID
      {
        if (Rec_Data == 0x00) {
          asm("nop");
          Rec_Flag.starthgh = 1; // Indicating header1 got
        } else {
          Rec_Flag.BYTEM = 0;
          Rec_Flag.starthgh = 0; // The data being received is not header1, back to non-receive status
          Check_Sum = 0;
          UART_Rx_Timer = 0;
          Rx_Count = 0;
        }

      } else if (!Rec_Flag.startlow) // Not got ID
      { // aaudge the ID legality here
        Rec_Flag.startlow = 1;
        uart_buf[3] = Rec_Data;
        start_byte = uart_buf[3];

      } else if (!Rec_Flag.reghgh) // Not got ID
      {
        UART_Rx_Timer = 1;
        if (Rec_Data == 0) {
          Rec_Flag.reghgh = 1; // Indicating header1 got
          uart_buf[4] = Rec_Data;
        } else {
          Rec_Flag.BYTEM = 0; // The data being received is not header1, back to non-receive status
          Rec_Flag.starthgh = 0;
          Check_Sum = 0;
          UART_Rx_Timer = 0;
          Rx_Count = 0;

        }
      } else if (!Rec_Flag.reglow) {
        Rec_Flag.reglow = 1;
        UART_Rx_Timer = 1;
        uart_buf[5] = Rec_Data;
        Data_reg = uart_buf[5];
      } else if (!Rec_Flag.chcksumlow) {
        Rec_Flag.chcksumlow = 1; // Check_Sum has aaust xored with the received checksum
        uart_buf[6] = Rec_Data;
      } else if (!Rec_Flag.chcksumhgh) {
        Rec_Flag.chcksumhgh = 1;
        uart_buf[7] = Rec_Data;
        ChecksumCal(6);
        if ((uart_buf[6] == crc_msb) && (uart_buf[7] == crc_lsb)) {
          Rec_Flag.BYTEM = 0;
          Rec_Flag.starthgh = 0;
          Frame_Dly_flag = 1;
          Rx_Count = 0;
          PIE3bits.RC1IE = 0;
          //IEC0bits.U1RXIE=0;

        } else {
          Rec_Flag.BYTEM = 0;
          Rec_Flag.starthgh = 0; // The data being received is not header1, back to non-receive status
          Check_Sum = 0;
          Rx_Count = 0;
        }

        UART_Rx_Timer = 0;
      }

    }

    void WriteUART1(uint8_t txData) {
      if (1 == PIE3bits.TX1IE) {
        TX1REG = txData;
      } else {
        TX1REG = txData & 0XFF;
        PIE3bits.TX1IE = 0;

      }
      PIE3bits.TX1IE = 1;

    }

    void UART_Transmit(void) {

      if (Device_match_M == 1) {
        if (Tx_Count) //if transmit buffer has data to send, send it out
        {
          //DE = 1;//direction
          asm("nop");
          asm("nop");
          asm("nop");
          asm("nop");
          asm("nop");
          asm("nop");

          if (Tx_Count > Data_Len_Count - 3) {

            WriteUART1(uart_buf[Data_Len_Count - Tx_Count]);
            // EUSART1_Write(uart_buf[Data_Len_Count-Tx_Count]);

          } else if (Tx_Count > 3) {
            WriteUART1( * (Tx_Ptr));
            //EUSART1_Write(*(Tx_Ptr));
            uart_buf[ptr] = * (Tx_Ptr);
            Tx_Ptr++;
            ptr++;

          } else if (Tx_Count == 3) {
            ChecksumCal(Data_Len_Count - 3);
            WriteUART1(crc_msb);
            //EUSART1_Write(crc_msb);
          } else if (Tx_Count == 2) {
            WriteUART1(crc_lsb);
            //EUSART1_Write(crc_lsb);

          } else if (Tx_Count == 1) {
            Tx_Ptr = 0;

            PIE3bits.TX1IE = 0;
            PIR3bits.RC1IF = 0;

            DE = 0; //direction
            PIE3bits.RC1IE = 1; // Enable RxD Interrupt
          }
          Tx_Count--;
        }
      }

    }

    void UART_HandleRightFrame(unsigned char ucDt) {
      unsigned char i, j;
      unsigned char par, para1, para2, para3;
      unsigned int para4;

      par_val[0] = FAULT_WINDOW[0];
      par_val[1] = FAULT_WINDOW[1];
      par_val[2] = FAULT_WINDOW[2];
      par_val[3] = FAULT_WINDOW[3]; // introduced for 4 bytes sending.

      Rec_Flag.Chksum = 0;
      uart_buf[0] = Device_ID;
      uart_buf[1] = 1; //CHANGED TO 1
      uart_buf[2] = 04; // changed t0 4 from 3 for 4 bytes data
      Tx_Count = (3 + 7); // changed from 6 to 7 for 4 bytes data
      Data_Len_Count = Tx_Count;
      ptr = 3;
      Tx_Ptr = & par_val[(0)];
      TX1STAbits.TXEN = 0;
      //U1STAbits.UTXEN = 0;
      asm("nop");
      asm("nop");
      TX1STAbits.TXEN = 1;
      // U1STAbits.UTXEN = 1;
      UART_Transmit();
      PIE3bits.TX1IE = 1; //enable TX interrupt

    }

    //*************************************************
    // this routine to be called by main
    //*************************************************
    void process_UART(void) {
      unsigned char i = 0;

      if (Tx_Count)
        PIE3bits.TX1IE = 1; //enable TX interrupt
      else
        PIE3bits.TX1IE = 0; //enable TX interrupt

      if (Transmit_flag == 1) //modified for RIl
      {
        Rec_Flag.Frame = 1;
        Transmit_flag = 0;
      }

      if (Rec_Flag.Frame) // if successfully received a frame?
      {
        Rec_Flag.BYTE = 0;
        Rec_Flag.BYTEM = 0;
        Rec_Flag.starthgh = 0;
        Rec_Flag.Chksum = 0;
        Rx_Count = 0;
      }

      if (RCSTAbits.FERR == 1) {
        RCSTAbits.FERR = 0;
        RCSTAbits.SPEN = 0;
        RCSTAbits.SPEN = 1;
        Rec_Flag.starthgh = 0;
        Rec_Flag.BYTE = 0;
        Rec_Flag.BYTEM = 0;
        Check_Sum = 0;
        Rx_Count = 0;
      }

      if (RCSTAbits.OERR == 1) {
        RCSTAbits.OERR = 0;
        RCSTAbits.CREN = 0;
        RCSTAbits.CREN = 1;
        PIE3bits.RC1IE = 1; // Enable RxD Interrupt
        // RCSTAbits.CREN=1;
        Rec_Flag.starthgh = 0;
        Rec_Flag.BYTE = 0;
        Rec_Flag.BYTEM = 0;
        Check_Sum = 0;
        Rx_Count = 0;
      }
    }

    //**********************************************************************************************
    ////Receive Interrupt Call
    //**********************************************************************************************

    void SerialReceive_Interrupt() {
      if (PIR3bits.RC1IF == 1) {
        PIR3bits.RC1IF = 0;
        UART_Receive();
      }

    }

    //**********************************************************************************************
    ////Receive Interrupt Call
    //**********************************************************************************************

    void SerialSend_Interrupt() {
      if (PIR3bits.TX1IF == 1) {
        PIR3bits.TX1IF = 0;
        UART_Transmit();
      }

    }

    void Frame_Check() {
      if (Frame_Dly_flag == 1) {
        Frame_Dly_Counter++;
        if (Frame_Dly_Counter > 40) {
          Frame_Dly_Counter = 0;
          Frame_Dly_flag = 0;
          Transmit_flag = 1;
        }
      }

    }

    void ISR_Routine() {

      process_UART();

      SerialReceive_Interrupt();
      SerialSend_Interrupt();

      if (PIR0bits.TMR0IF == 1) {

        if (UART_Rx_Timer || Rx_Count) {
          UART_Rx_Timer++;

        }
        Frame_Check();

        PIR0bits.TMR0IF = 0;
      }

    }

    void main()

    {
      SYSTEM_Initialize();

      while (1) {

        process_UART();
      }

    }

     
    Here these are functions called in dispic device like this. let me know weather these functions are ok
    ****************** 
    void __attribute__((__interrupt__)) _T2Interrupt(void) {
      // flag.bits.T2_overflow = 1;

      if (UART_Rx_Timer || Rx_Count)
        UART_Rx_Timer++;

      IFS0bits.T2IF = 0;
    }

    //******************************************************************************************
    void __attribute__((__interrupt__)) _U1TXInterrupt(void) {
      UART_Transmit();
      IFS0bits.U1TXIF = 0;
    }

    //******************************************************************************************
    void __attribute__((__interrupt__)) _U1RXInterrupt(void) {
      IFS0bits.U1RXIF = 0;
      UART_Receive();
    }
    //******************************************************************************************
    void __attribute__((__interrupt__)) _T3Interrupt(void) {
      PR3 = 0x2467;
      if (Frame_Dly_flag == 1) {
        Frame_Dly_Counter++;
        if (Frame_Dly_Counter > 40) {
          Frame_Dly_Counter = 0;
          Frame_Dly_flag = 0;
          Transmit_flag = 1;
        }
      }

      IFS0bits.T3IF = 0;
    }

     
     

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #8
    ric
    Super Member
    • Total Posts : 28694
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/17 00:13:11 (permalink)
    0
    It gets very draining when you do not answer ANY of the questions I asked.
    Have you noiticed that the dsPIC has individual interrupts for each peripheral, but the PIC18 does not.
    You still have not shown the real interrupt service code, so I'm not going to bother guessing any longer.
    Bye.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    AMPS
    Super Member
    • Total Posts : 588
    • Reward points : 0
    • Status: offline
    Re: pic18F45k40 communication issue. 2020/10/17 00:23:26 (permalink)
    0
    Here i have attached function generated from MCC. I havent used MCC generated files for programming i have just commented out those function. Kindly check

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #10
    ric
    Super Member
    • Total Posts : 28694
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/17 00:36:22 (permalink)
    0
    ajitnayak87
    I havent used MCC generated files for programming i have just commented out those function.

    What does this mean?
    Do you mean you just borrowed bits of code from what MCC generated?
    As I already said multiple times, you did NOT post all your code at the start, and we still don't know what code you are really running.
    I am pretty sure you are not setting USART interrupts up at all, so they are only sort of working becuase you poll the state in the timer interrupt, which is the only one that is actually enabled.
    That is a recipe for disaster, and I'm not surprised you're having problems.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    Mysil
    Super Member
    • Total Posts : 3814
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/17 02:07:34 (permalink)
    0
    Hi,
    Have tried to put together code shown above in messages #8 and #10.
    Now, I am trying this on a PIC18F26K80, so there are some differences in register naming and interrupt registers.
     
    Adjusting for those, there are still some loose ends:
        FAULT_WINDOW[?]    // ???
        Device_ID          // ?
        DE            // ???
        Frame_Dly_Counter    // ???
     
    In Interrupt.c  there is a layout for interrupt handling set up by MCC,
    but everything for EUSART is commented out and ignored.
    All the header files generated by mcc are missing,
    so there are missing #include files, and missing function prototypes.
     
    I could run MCC myself, or borrow something from similar projects, but it sure would become different from what you are working with.
     
    In MPLAB X there is a 'Package' facility to bundle All those files needed to transfer and restore a project,
    into a .zip file that may be provided as attachment to a forum message.
     
        Mysil
    post edited by Mysil - 2020/10/17 03:29:25
    #12
    AMPS
    Super Member
    • Total Posts : 588
    • Reward points : 0
    • Status: offline
    Re: pic18F45k40 communication issue. 2020/10/17 07:29:13 (permalink)
    0
    #8 and #10 its not complete code. I am trying to modify the code example for dispic(working) code into pic18f45k40
     
    For example below i can take argument for testing 
    FAULT_WINDOW=0X00;
    Device_ID = 1; this will be my slave address
    DE is Driver enable bit used for RS485 IC
    Frame_Dly_Counter   used to enable and disable transmit flag.
     
    i would like to know weather format is ok for framing modbus rtu?? I didnt get any example code for modbus rtu to get started. i have sample code for dispic working & i have modified in thread. 
    #8 i have mentioned dispic function . I would like to know how it can be called in pic18f45k40??
    So that i could match DISPIC waveform with PIC18F45k40 to make it working.
     
    The code i have written #1 . as RIC say even its working but facing issue while receive multiple device.
    if my #8 thread is proper i could work to achieve successful transmission. 
     
     

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #13
    Mysil
    Super Member
    • Total Posts : 3814
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/17 10:58:01 (permalink)
    +1 (1)
    Hi,
    For PIC18, Interrupt Service code from dsPIC33 or PIC24, as shown in message #8 cannot be used. 
    This is because PIC18 have at most 2 different interrupt vectors,
    and you have set up to use only one.
    So all interrupts must be serviced from the same code.
     
    You have a starting point in Interrupt.c, as shown in message #10, may be modified like this:
    /**
      Generated Interrupt Manager Source File

      @Company:
        Microchip Technology Inc.

      @File Name:
        Interrupt.c

     *  Modified for PIC18F45K80
     *  Modified for PIC18F26K80 by Mysil 2020-10-17
    */

    #include <xc.h>
    #include "interrupt_manager.h"
    #include "mcc.h"
    void UART_Transmit(void);    /* Function in main program file. */
    void UART_Receive(void);    /* Function in main program file. */
    void ISR_Routine(void);        /* Function in main program file. */

    void  INTERRUPT_Initialize (void)
    {
        // Disable Interrupt Priority Vectors (16CXXX Compatibility Mode)
    //    INTCONbits.IPEN = 0;
        RCONbits.IPEN = 0;
    }

    void __interrupt() INTERRUPT_InterruptManager (void)
    {
        // interrupt handler
    #if _18F45K40
        if(PIE0bits.TMR0IE == 1 && PIR0bits.TMR0IF == 1)        /* PIC18F45K40 */
    #elif _18F26K80
        if(INTCONbits.TMR0IE == 1 && INTCONbits.TMR0IF == 1)    /* PIC18F26K80 */
    #endif
        {
    //        TMR0_ISR();                /* MCC generated Timer Interrupt handler. */
            
            TMR0_Reload();            /* Or do it directly here. */
            ISR_Routine();            /* Application timer interrupt routine. */
            TMR0IF = 0;                /* Clear interrupt Flag. */
        }
        else if(INTCONbits.PEIE == 1)
        {
    #if _18F45K40
            if(PIE3bits.TXIE == 1 && PIR3bits.TXIF == 1)
            {
    //            EUSART_TxDefaultInterruptHandler();
                PIR3bits.TX1IF = 0;            /* This line is not actually needed. */
                UART_Transmit();
            }
            else if(PIE3bits.RCIE == 1 && PIR3bits.RCIF == 1)
            {
    //            EUSART_RxDefaultInterruptHandler();
                PIR3bits.RC1IF = 0;            /* This line is not actually needed. */
                UART_Receive();
            }
    #elif _18F26K80
            if(PIE1bits.TXIE == 1 && PIR1bits.TXIF == 1)
            {
    //            EUSART_TxDefaultInterruptHandler();
                UART_Transmit();
            }
            else if(PIE1bits.RCIE == 1 && PIR1bits.RCIF == 1)
            {
    //            EUSART_RxDefaultInterruptHandler();
                UART_Receive();
            }
    #endif
            else
            {    //Unhandled Interrupt
                __debug_break();
            }
        }      
        else
        {    //Unhandled Interrupt
            __debug_break();
        }
    }

     
    Be aware that for 8-bit PIC microcontrollers, global interrupts are disabled from reset.
    This is different from PIC24 and dsPIC devices.
    So for PIC18... global and peripheral interrupts must be enabled in main, if interrupts are used:
    void main(void)
    {
        SYSTEM_Initialize();

        INTCONbits.GIE = 1;            /* Enable global Interrupts. */
        INTCONbits.PEIE = 1;        /* Enable peripheral interrupts. */
       
        while (1)
        {   process_UART();
        }
    }

     
        Mysil
    #14
    1and0
    Access is Denied
    • Total Posts : 11345
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: pic18F45k40 communication issue. 2020/10/17 17:54:46 (permalink)
    -1 (1)
    Mysil
    This is because PIC18 have at most 2 different interrupt vectors, ...

    The newer PIC18F devices, such as the K42 and Q43 families, have interrupt vector table with a unique vector for each interrupt source.
     
    #15
    AMPS
    Super Member
    • Total Posts : 588
    • Reward points : 0
    • Status: offline
    Re: pic18F45k40 communication issue. 2020/10/17 20:40:48 (permalink)
    0
    @mysil. Thanks i would like to other wise my routine functions are ok to perform madbus RTU function 
    If have changed these things 
    What about below function.
    MCC generated file Esuart.c will affect these functions??
     
    Weather my 2 below functions are correct since. WriteUart1 functions are defined for dispic but no literature what does function does internally.
     
     

    void WriteUART1(uint8_t txData) {
      if (1 == PIE3bits.TX1IE) {
        TX1REG = txData;
      } else {
        TX1REG = txData & 0XFF;
        PIE3bits.TX1IE = 0;

      }
      PIE3bits.TX1IE = 1;

    }
    void __attribute__((__interrupt__)) _T3Interrupt(void) {
      PR3 = 0x2467;
      if (Frame_Dly_flag == 1) {
        Frame_Dly_Counter++;
        if (Frame_Dly_Counter > 40) {
          Frame_Dly_Counter = 0;
          Frame_Dly_flag = 0;
          Transmit_flag = 1;
        }
      }

      IFS0bits.T3IF = 0;
    }

    post edited by AMPS - 2020/10/17 20:43:21

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #16
    ric
    Super Member
    • Total Posts : 28694
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: pic18F45k40 communication issue. 2020/10/17 20:58:37 (permalink)
    0
    Is this code, that you wrote, or that you are trying to translate?
    Either way, it looks insane...

    void WriteUART1(uint8_t txData) {
      if (1 == PIE3bits.TX1IE) {
        TX1REG = txData;
      } else {
        TX1REG = txData & 0XFF;
        PIE3bits.TX1IE = 0;

      }
      PIE3bits.TX1IE = 1;

    }

     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #17
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 12019
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: pic18F45k40 communication issue. 2020/10/17 21:28:23 (permalink)
    0
    it looks insane..

     
    As I read it, it is equivalent to
     
    TX1REG = txData;
    PIE3bits.TX1IE = 1;
    #18
    1and0
    Access is Denied
    • Total Posts : 11345
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: pic18F45k40 communication issue. 2020/10/17 21:39:11 (permalink)
    0
    ric
    it looks insane...

    that either he ignored what you said in Post #2, or maybe he just does not understand it. :(
     
    #19
    1and0
    Access is Denied
    • Total Posts : 11345
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: pic18F45k40 communication issue. 2020/10/17 21:42:24 (permalink)
    +1 (1)
    ajitnayak87
    Weather my 2 below functions are correct since. WriteUart1 functions are defined for dispic but no literature what does function does internally.

    Seriously, do you know what your function does? or you just toast toss lines of code together and hope it work?!
     
    post edited by 1and0 - 2020/10/17 21:45:22
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5