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.

can't transfer bytes array from pic18f4550 to another one using usart

Status
Not open for further replies.

max_evil

Newbie level 3
Joined
Jun 11, 2012
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,301
im trying to transfer bytes array from pic18f4550 to another one using usart and debug it using USB on one of them, the problem is that the reciver pic receives 2 bytes! on the first tx and then does not receive anything. im using the following code

Code:
 OpenUSART (USART_TX_INT_OFF &
             USART_RX_INT_OFF &
             USART_ASYNCH_MODE &
             USART_EIGHT_BIT &
             USART_CONT_RX &
             USART_BRGH_HIGH, 131);

static unsigned char send_dummy[4];
static unsigned char recv_dummy[4];

int n_recv = 0;
int n_send = 0;
int send_bytes( unsigned char* bytes, int len)
{
    int i = 0;
    int n = 0;
    for ( i = 0; i < len; i++)
    {
        while(BusyUSART());
        WriteUSART( bytes[i] ); // send byte
        DELAY_US_130
        n++;
    }

    return n;
}


int recv_bytes( unsigned char *bytes, int len)
{
    int i = 0;
    int n = 0;
    for ( i = 0; i < len; i++)
    {
      if ( DataRdyUSART() ) // is data aviliable in buffer?
        {
            bytes[i] = ReadUSART();
            n++;
        }else
        {
            // no data left return
            return n;
        }
     }

    return n;
}

// send load table
void send_lt(void)
{
    /* test values */
    send_dummy[0] = 0xD1;
    send_dummy[1] = 0xD2;
    send_dummy[2] = 0xD3;
    send_dummy[3] = 0xD4;
    n_send = send_bytes( send_dummy, sizeof(send_dummy));
}


void recv_lt(void)
{
    memset( recv_dummy, 0, sizeof( recv_dummy));
    n_recv = recv_bytes( recv_dummy, sizeof( recv_dummy));
       
   ....
}
--------

Code:
the first main code
void main ()
{
   
    while(1)
    {
        // check if we have something to read
        if( DataRdyUSART() )

            recv_lt();
            send_lt();

        }
}

the second main

void main()
{
         send_lt();
         recv_lt();

         LED = ~(LED);
         DELAY_MS_500
}

the compiler is C18 and 20Mhz clock with PLL. please advice !
 

I don't know how this library is working because I'm usually setting registers instead.... But I've got some issues like this last week and I was not checking FERR and OERR registers (receiving errors)... you need to add in your code something like:

If (FERR||OERR){
CREN = 0;
delay_us(5);
CREN = 1;
RCIE = 1;// in case of using interrupt
}

Read about this registers on datasheet and check if they are treated by this library....

I also suggest you to use interrupt for receiver... And you will not spend time in your main code checking if there is data on receiver buffer or not. :grin:

- - - Updated - - -

I don't know how this library is working because I'm usually setting registers instead.... But I've got some issues like this last week and I was not checking FERR and OERR registers (receiving errors)... you need to add in your code something like:

If (FERR||OERR){
CREN = 0;
delay_us(5);
CREN = 1;
RCIE = 1;// in case of using interrupt
}

Read about this registers on datasheet and check if they are treated by this library....

I also suggest you to use interrupt for receiver... And you will not spend time in your main code checking if there is data on receiver buffer or not. :grin:
 

hello,

you have to use intterrupt mode for receiving data.
and use a software protocole like XON,XOFF,
or ACk ENQ,
or by adding extra I/O hardware for DTR,CTS
management.
so exchange of data will allways be synchronized and reliable.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top