Hot!Working with SERIAL COMMUNICATION on PIC16F887

Author
Neraildes
Starting Member
  • Total Posts : 50
  • Reward points : 0
  • Joined: 2017/10/11 07:13:25
  • Location: 0
  • Status: offline
2018/05/17 06:44:17 (permalink)
0

Working with SERIAL COMMUNICATION on PIC16F887

Good Morning,
I'm trying to make serial communication work but it's not working right.
The microcontroller that is using the PIC16F887 with the internal of 8 Mhz. Below is the code:
 
void Usart_Init(unsigned int baudrate){
if(baudrate==115200)
                {
                //................................USART.....................................
                SPBRG =16; //BaudRate |
                BAUDCTLbits.BRG16 = 1; // |
                TXSTAbits.BRGH = 1; //Trasmitter hight speeed |
                RCSTAbits.SPEN = 1;
                TXSTAbits.SYNC = 0; //Async mode |
                TXSTAbits.TX9 = 0; //without 9o bit |
                RCSTAbits.CREN = 1;
                INTCONbits.PEIE = 1; //Periferical Interrupt Enable |
                PIE1bits.RCIE = 1; //USART receiver RX |
                }
}

 
thank you
#1

14 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 15832
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 06:52:17 (permalink)
    0
    What does not working right mean?
    Did you look at the app but with a scope?
    Are you sure the internal oscillator is set to 8 megahertz?
    #2
    KTrenholm
    Super Member
    • Total Posts : 397
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: online
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 07:08:22 (permalink)
    0
    "Not Working Right" = ???
     
    Issues with Transmission?
    Issues with Reception?
    Garbage Characters?
    Nothing at all?
     
    You show some initialization code for the UART but how are you actually sending/receiving data?  Show us that code.
    NKurzman has the right idea, look at your Rx/Tx on an Oscilloscope or Logic Analyzer.  What do you see? (and for that matter, what are you expecting to see?)
    #3
    Neraildes
    Starting Member
    • Total Posts : 50
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 07:22:39 (permalink)
    0
    What does not working right mean?
    I'm sending a string but it does not come out in TX.

    Did you look at the app but with a scope?
    I have the oscilloscope on the microcontroller pin TX.

    Are you sure the internal oscillator is set to 8 megahertz?
    Yes.
    OSCCONbits.IRCF0=1;
    OSCCONbits.IRCF1=1;
    OSCCONbits.IRCF2=1;
    #4
    Neraildes
    Starting Member
    • Total Posts : 50
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 07:24:28 (permalink)
    0
    I turned on TXSTA.TXEN and it did not work yet.
    #5
    KTrenholm
    Super Member
    • Total Posts : 397
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: online
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 07:26:23 (permalink)
    0
    So presumably you are seeing nothing on Tx?  No action?
    Can you post your code for sending characters?
     
    #6
    Neraildes
    Starting Member
    • Total Posts : 50
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 07:32:09 (permalink)
    0
    "Not Working Right" = ???
     
    Issues with Transmission?
    Issues with Reception?
    Garbage Characters?
    Nothing at all?
     
    You show some initialization code for the UART but how are you actually sending/receiving data?  Show us that code.
    NKurzman has the right idea, look at your Rx/Tx on an Oscilloscope or Logic Analyzer.  What do you see? (and for that matter, what are you expecting to see?)

    -------------------------------------------------------------------------------------------
    I have problem with transmission and reception.
    The send/receive of data is ok.
     
    I'll post the transmission code.
    #7
    Neraildes
    Starting Member
    • Total Posts : 50
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 07:35:47 (permalink)
    0

    #include "global.h"
    #include "usart.h"

    extern volatile unsigned int tmr_led_usart;


    /*----------------------------------------------------------------------------*/
    void USART_init(unsigned int baudrate)
         {
         unsigned char erro;
        
         #ifdef XTAL_8MHZ
            if(baudrate==19200)
              {
              //................................USART.....................................
              RCSTAbits.SPEN = 1;
              RCSTAbits.CREN = 1;

              TXSTAbits.BRGH  = 1; //Trasmitter hight speeed                             |
              TXSTAbits.SYNC  = 0; //Async mode                                          |
              TXSTAbits.TX9   = 0; //without 9o bit                                      |
              SPBRG           =12; //BaudRate                                            |
              INTCONbits.PEIE = 1; //Periferical Interrupt Enable                        |
              PIE1bits.RCIE   = 1; //USART receiver RX                                   |
              }
            else if(baudrate==115200)
                    {
                    //................................USART.....................................
                    SPBRG             =16; //BaudRate                                            |
                    BAUDCTLbits.BRG16 = 1;  //                                                   |
                    TXSTAbits.BRGH    = 1; //Trasmitter hight speeed                             |
                    TXSTAbits.TXEN    = 1;
                    RCSTAbits.SPEN    = 1;
                    TXSTAbits.SYNC    = 0; //Async mode                                          |
                    TXSTAbits.TX9     = 0; //without 9o bit                                      |               
                    RCSTAbits.CREN    = 1;
                    INTCONbits.PEIE   = 1; //Periferical Interrupt Enable                        |
                    PIE1bits.RCIE     = 1; //USART receiver RX                                   |                
                    }
            
         #endif
         
         #ifdef XTAL_20MHZ
            if(baudrate==19200)
              {
              //................................USART.....................................
              RCSTAbits.SPEN = 1;
              RCSTAbits.CREN = 1;

              TXSTAbits.BRGH  = 1; //Trasmitter hight speeed                             |
              TXSTAbits.SYNC  = 0; //Async mode                                          |
              TXSTAbits.TX9   = 0; //without 9o bit                                      |
              SPBRG           =64; //BaudRate                                            |
              INTCONbits.PEIE = 1; //Periferical Interrupt Enable                        |
              PIE1bits.RCIE   = 1; //USART receiver RX                                   |
              }
         #endif
        
         TRISC6= 1;
         TRISC7= 1;
        
         CREN=0;
         CREN=1;
         for(unsigned char i=0;i<15;i++) erro=RCREG;        
         }



    void USART_putc(unsigned char value)
    {   
        
        //tmr_led_usart=LED_CONSTANT;
        Delay_Led_Usart=DEFAULT_LEDS;
        
        TXREG=value;           
        TXSTAbits.TXEN  = 1;
        while(!TXSTAbits.TRMT) continue;//Registrador vazio    
        TXSTAbits.TXEN  = 0;
        
    }


    void USART_put_buffer(unsigned char *vetor, unsigned int size)
    {       
        unsigned char loop=0;
        unsigned char *ptr;
        ptr =(unsigned char *) &vetor;
        
        while(loop<size)
        {
            USART_putc(*(ptr));        
            ptr++;
            loop++;
        }
    }



    unsigned char USART_input_buffer(void){
        unsigned int  time=0;
        unsigned char result=FALSE;
        
        while(time<RX_MAX_WAIT_TIME){
            if(flag_usart_rx){
               flag_usart_rx=FALSE;
               result = TRUE;
               break;
               }
            time++;
            __delay_ms(1);        
        }
        return result;
    }



    void USART_put_int(unsigned int value){
         USART_putc(value>>8);
         USART_putc(value);
    }


    void USART_put_long(unsigned long value){
         USART_putc(value>>24);
         USART_putc(value>>16);
         USART_putc(value>>8);
         USART_putc(value);
    }


    void USART_put_float24(float value){
         unsigned char *pt;
         pt=(unsigned char *) &value;
         USART_putc(*(pt++));
         USART_putc(*(pt++));
         USART_putc(*(pt++));     
    }


    void USART_put_string(unsigned char *vetor)
    {       
        unsigned char *ptr;
        ptr=vetor;
        while(*(ptr)){
            USART_putc(*(ptr));
            ptr++;
        }
        USART_putc(*(ptr));
    }

    #8
    1and0
    Access is Denied
    • Total Posts : 7855
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 08:04:32 (permalink)
    +1 (1)
    I have not looked thru all your code, but an unsigned int is only 16-bit yet you are checking for 115200 baud. Also, try checking the TXIF flag before writing data to TXREG; and do not turn TXEN on and off for each data transmitted.
    #9
    Neraildes
    Starting Member
    • Total Posts : 50
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 10:15:35 (permalink)
    0
    You're right. Now the Usart_Init call is passing unsigned long type parameter.
    Data transmissions are not happening yet.
    #10
    Neraildes
    Starting Member
    • Total Posts : 50
    • Reward points : 0
    • Joined: 2017/10/11 07:13:25
    • Location: 0
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 10:22:17 (permalink)
    0
    After changing the data send routine, as the "1and0" sucked, the program started to work fine. Thank you all!grin: grin
    #11
    Jim Nickerson
    User 452 _
    • Total Posts : 4650
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 10:35:40 (permalink)
    +1 (1)
    sucked -> suggested
    I love auto correct Smile: Smile
    #12
    Jim Nickerson
    User 452 _
    • Total Posts : 4650
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 10:36:10 (permalink)
    0
    Neraildes
    After changing the data send routine, as the "1and0" sucked, the program started to work fine. Thank you all!grin: 




    #13
    qɥb
    Monolothic Member
    • Total Posts : 2498
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/17 13:27:40 (permalink)
    0
    The posted code enables receive interrupts, yet no interrupt service routine is shown.
     
    You never posted your final transmit function, but rather than
        TXREG=value;           
        TXSTAbits.TXEN  = 1;
        while(!TXSTAbits.TRMT) continue;//Registrador vazio    
        TXSTAbits.TXEN  = 0;

    You should do this
        while(!PIR1bits.TXIF);    // wait until TXREG is available
        TXREG=value;           

    That's it. Nothing more except your LED setting instruction if you still want it.

    PicForum "it just works"
    #14
    1and0
    Access is Denied
    • Total Posts : 7855
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Working with SERIAL COMMUNICATION on PIC16F887 2018/05/18 02:35:40 (permalink)
    +1 (1)
    Neraildes
    After changing the data send routine, as the "1and0" sucked, the program started to work fine. 

    Things like this will get you a club membership into my Block List. mr green: mr green
    #15
    Jump to:
    © 2018 APG vNext Trial Version 4.5