RS485 communication with computer

Discussion in 'Embedded Systems and Microcontrollers' started by Kei-Ki-Kun, Oct 17, 2014.

  1. Kei-Ki-Kun

    Thread Starter New Member

    Oct 17, 2014
    2
    0
    Hi
    I use Pic16f877a to communicate with computer through COM port.But I only can send "hello" to computer but cannot received any signal from computer.I write program by CCS C and Proteus for simulation.So anyone can help me

    #include <16f877a.h>
    #device pic16f877*=16 adc=10
    #fuses hs,nowdt,noprotect,nolvp,brownout
    #use delay(clock=20mhz)
    #use rs232(baud=9600,parity=N,xmit=pin_c6,rcv=pin_c7,bits=8)

    #byte portB=0x06
    #byte portC=0x07
    #byte portD=0x08

    char datareceive='0';
    #int_rda
    void rda_isr()
    {
    output_high(pin_d0);
    output_high(pin_d5);
    datareceive = getc();
    if(datareceive=='1')
    output_low(pin_b0);
    if(datareceive=='2')
    output_low(pin_b1);
    if(datareceive=='3')
    output_low(pin_b2);

    }


    void main()
    {
    set_tris_d(0x00);
    set_tris_b(0x00);
    portb=0xff;
    portd=0x00;
    enable_interrupts(int_rda);
    enable_interrupts(global);
    printf("hello");
    printf("hello");
    while(1);
    }
     
  2. Chalma

    Member

    May 19, 2013
    54
    2
    Greetings Kei,

    not sure if this is what you need but basically your program above is only good for sending the text "hello" through the communication and showing up on hyperterminal (or whatever port reading program you are using). It is much more complex than that. you need to setup routines to check the buffer for data and tell the micro what to do with the said data as well.
    Code (Text):
    1.  
    2. byte get_char(void)
    3. {
    4.   byte c;
    5.  
    6.   if(S_BUFFER_HEAD == S_BUFFER_TAIL)
    7.   {
    8.   //return(0xff);  /* buffer empty */
    9.   }
    10.   if(S_BUFFER_HEAD > 16){S_BUFFER_HEAD = 0;}
    11.    while(!RI);
    12.   c = S_BUFFER[S_BUFFER_HEAD];
    13.   S_BUFFER_HEAD++;
    14.    new_cmd_recieved=1;
    15.   return(c);
    16.  
    17. }
    above is a function I use alot to check if there is a character in the buffer. I would do a google search and try to find examples that pertain to your project. There are also really good books on amazon that walk you through this. Good luck with your project
     
  3. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,539
    2,369
    In most cases TX out of the 877 is done outside of an interrupt, the RX however is normally activated on receipt of a character in the input buffer.
    I assembler so I cannot help with the code.
    Max.
     
  4. Kei-Ki-Kun

    Thread Starter New Member

    Oct 17, 2014
    2
    0
    Thank you Chalma for replying
    May be I will add function to make sure PIC buffer receive signal from computer
     
Loading...