configuration of uart

configuration of uart

Postby phoenix2000 » Tue Sep 30, 2014 5:29 am

hello every body .
how can i configure the stop bits,parity and flow control in c18 for uart?
User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: configuration of uart

Postby vloki » Tue Sep 30, 2014 7:49 am

There are no such things implemented in hardware.
I think you have to implement them by yourself in software.
What you need it for ?
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: configuration of uart

Postby phoenix2000 » Tue Sep 30, 2014 8:10 am

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:
User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: configuration of uart

Postby vloki » Tue Sep 30, 2014 8:36 am

What is the BAUDRATE and clk frequency of your pic ?
Do you use interrupt for the receiver ?
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: configuration of uart

Postby phoenix2000 » Tue Sep 30, 2014 8:44 am

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.
User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: configuration of uart

Postby phoenix2000 » Tue Sep 30, 2014 8:45 am

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);
 
   }
Last edited by phoenix2000 on Tue Sep 30, 2014 9:02 am, edited 1 time in total.
User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: configuration of uart

Postby vloki » Tue Sep 30, 2014 9:00 am

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/
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: configuration of uart

Postby vloki » Tue Sep 30, 2014 9:13 am

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);
        }
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: configuration of uart

Postby phoenix2000 » Tue Sep 30, 2014 3:18 pm

;) 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?
User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: configuration of uart

Postby vloki » Tue Sep 30, 2014 3:25 pm

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)
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Next

Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 7 guests

cron