Page 1 of 2

configuration of uart

PostPosted: Tue Sep 30, 2014 5:29 am
by phoenix2000
hello every body .
how can i configure the stop bits,parity and flow control in c18 for uart?

Re: configuration of uart

PostPosted: Tue Sep 30, 2014 7:49 am
by vloki
There are no such things implemented in hardware.
I think you have to implement them by yourself in software.
What you need it for ?

Re: configuration of uart

PostPosted: Tue Sep 30, 2014 8:10 am
by phoenix2000
In fact,i didn't see any thing about them in OpenUSART of c18 compiler or in registers. :?: :!:
i want to connect with sim900.i can get connected to my computer properly and send and receive command.but with micro controller it seems impossible :x
i can send command but the uart can't receive proper data.it gets the first two character of my sent command.for example if i send ATE0 in RX i get AT although i get OK in RX of my computer(i connect RX of micro and computer simultaneously )!! :shock:

Re: configuration of uart

PostPosted: Tue Sep 30, 2014 8:36 am
by vloki
What is the BAUDRATE and clk frequency of your pic ?
Do you use interrupt for the receiver ?

Re: configuration of uart

PostPosted: Tue Sep 30, 2014 8:44 am
by phoenix2000
oops there is another thing ,i think that it's because of overrun error ,because it's bit is set,what should i do after clearing CREN as the datasheet said?i did this:
if(RCSTAbits.OERR==1){RCSTAbits.CREN=0;Delay10KTCYx(50);RCSTAbits.CREN=1;}
but i got the same result.

Re: configuration of uart

PostPosted: Tue Sep 30, 2014 8:45 am
by phoenix2000
vloki wrote:What is the BAUDRATE and clk frequency of your pic ?
Do you use interrupt for the receiver ?

BAUDRATE=9600 and clk frequency=8 MHZ
nope,this is the main part of my code:
Code: Select all
putrsUSART("AT\r");Delay10KTCYx(200);
putrsUSART("ATD09133957792;\r");Delay10KTCYx(200);
   while(1)
   {

    if(RCSTAbits.OERR==1){putrsXLCD("hi");RCSTAbits.CREN=0;Delay10KTCYx(50);RCSTAbits.CREN=1;}
    while (!DataRdyUSART());
    recieved_data=ReadUSART();
    putcXLCD(recieved_data);
 
   }

Re: configuration of uart

PostPosted: Tue Sep 30, 2014 9:00 am
by vloki
Overflow should never happen or your system wont work ;-)
Of course you need error handling anyway. Something like in this interrupt service routine:
Code: Select all
    if (IR_USART) // -------------------------------------------------- USART_IR
    {
        if (RCSTA1bits.OERR) // check overflow error
        {
            rxBuffer.bytes[0] = RCREG;  // clear the receiver fifo
            rxBuffer.bytes[0] = RCREG;
            rxBuffer.idx = 0;
            RCSTA1bits.CREN = 0;        // clear the OVR flag
            RCSTA1bits.CREN = 1;
//            PC_inPck.ID.all = ERRFLAG;  // send (prepare) error message
//            PC_inPck.Size = 2;
//            PC_inPck.Bytes[0] = SERR | SERR_OVR;
//            PC_inPck.Flags.CMDTODO = 1;
            return;
        }
        if (RCSTA1bits.FERR) // check framing error
        {
            rxBuffer.bytes[0] = RCREG;  // clear the receiver fifo
            rxBuffer.idx = 0;
//            PC_inPck.ID.all = ERRFLAG; // send (prepare) error message
//            PC_inPck.Size = 2;
//            PC_inPck.Bytes[0] = SERR | SERR_FRAME;
//            PC_inPck.Flags.CMDTODO = 1;
            return;
        }
        if (DataRdy1USART()){ //-------------------- new byte received ###
            if(rxBuffer.idx < MAX_RX_BUFFER){
                rxBuffer.bytes[rxBuffer.idx++] = RCREG;
            }
            else{
                status.rxOVF = 1;
            }
        }
        return;//IR_USART
    }

The IR only saves the received byte into a buffer and the main program processes it later like below
where RXbyteImport is a function that collects the incoming bytes into a package and checks for a complete message
Code: Select all
// new bytes from PC -----------------------------------------------------------
        if(rxBuffer.idx){           //###TODO### ERROR overflow !!!
            mDIS_IR_USART();
            RXbyteImport(rxBuffer.bytes[0]);
            rxBuffer.idx--;
       for(i=0;i<=rxBuffer.idx;i++){
      rxBuffer.bytes[i] = rxBuffer.bytes[i+1];
            }
            mEN_IR_USART();
//            status.rxOVF = 0;
        }


You want to do something like this http://www.open-electronics.org/mini-gs ... thout-gps/

Re: configuration of uart

PostPosted: Tue Sep 30, 2014 9:13 am
by vloki
You could change the above into:
Code: Select all
// new bytes from PC -----------------------------------------------------------
        if(rxBuffer.idx){           //###TODO### ERROR buffer overflow !!!
            mDIS_IR_USART();
            received_data = rxBuffer.bytes[0];
            rxBuffer.idx--;
            for(i=0;i<=rxBuffer.idx;i++){
                rxBuffer.bytes[i] = rxBuffer.bytes[i+1];
            }
            mEN_IR_USART();
            putcXLCD(recieved_data);
        }

Re: configuration of uart

PostPosted: Tue Sep 30, 2014 3:18 pm
by phoenix2000
;) thank you very much .
but there still questions :?: doesn't the usart of c18 have a mechanism like this ?
what is rxBuffer.idx ?is it in c18 or another compiler or you define it?

Re: configuration of uart

PostPosted: Tue Sep 30, 2014 3:25 pm
by vloki
You should view the data sheet for your PIC
and the compilers library documentation ;-)

rxBuffer is something I defined
Code: Select all
struct s_rxBuffer{                      // receiver buffer
    unsigned char idx;
    unsigned char bytes[MAX_RX_BUFFER];

...
struct s_rxBuffer    rxBuffer;       // vom PC empfangene Bytes
};
idx -> index (or a pointer to the next byte to write)