1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

PIC18F and USART

Discussion in 'Microcontrollers' started by adrianvon, Oct 24, 2013.

  1. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    I did it as you suggested:

    Code (text):
    while(1)
    {
      if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set TO ZERO
      {
            while(!TXSTAbits.TRMT );// Wait until TXREG is empty
            {
                TXREG = RCREG; // echo received data back to sender
            }
      }
    }
     
    .. but it is still not working.
     
  2. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    I did it as you suggested:

    Code (text):
    while(1)
    {
      if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set TO ZERO
      {
            while(!TXSTAbits.TRMT );// Wait until TXREG is empty
            {
                TXREG = RCREG; // echo received data back to sender
            }
      }
    }
     
    .. but it is still not working (echoing the characters mentions before only).
     
  3. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    You can't say that "these characters work", but "these characters do not work". The communication either works or does not work.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    What kind of incorrect symbols? Can you give few samples (sent - received). Maybe there is a bit-pattern that reveals the problem.
     
  6. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Hi misterT.

    attached is a screenshot showing the echoed characters/symbols. a,b,c... are the characters i types followed by the echo.

    Also,

    I took the f character (since it is being echoed well) and did the following modification to the code so that when f is received, the PIC will turn on two LEDs. But this is also not working. Why is that if the character f is echoed well?

    Code (text):
        SPBRG = 25;            // "25" calculated using formula from table 20-1 in the PIC18F4550 datasheet
        RCSTAbits.SPEN = 1;    // enable serial port (configures RX/DT and TX/CK pins as serial port pins)
        TXSTAbits.TXEN = 1;    // transmit enabled
        RCSTAbits.CREN = 1;    // enable receiver

        LATBbits.LATB0 = 0;  // RB-0 to High  //turn LEDs of on start up
        LATBbits.LATB1 = 0;  // RB-1 to High

    while(1)
    {
      if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set TO ZERO
      {
            while(!TXSTAbits.TRMT );// Wait until TXREG is empty
            {
                TXREG = RCREG; // echo received data back to sender

    if(a == 'f')
    {
      LATBbits.LATB0 = 1;  // RB-0 to High
      LATBbits.LATB1 = 1;  // RB-1 to High
    }
            }
      }
    }
     
    NOTE: a was declared before the main function ( char a; )
     

    Attached Files:

  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I'm just going out.... When I get back I'll sim it and find out what gives....

    I'll be about an hour...
     
  8. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Thanks Ian Rogers, appreciate your help.
     
  9. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    This is just a guess.. The bit pattern of character f is '01100110'. If your baudrate is slightly off, that bit-pattern will transfer correctly, but maybe some other bit-patterns (01010101) do not transfer correctly.

    The screenshot you posted does not help, because it only shows what is received. It does not show what is sent.
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Yes!! But I think we can assume the baud rate is out anyway... but not by much!!

    What's this by the way..... '129' cannot be correct... a number in apostrophe's will be cast to ascii..
     
  11. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Well spotted.. I stared at the code for a long time hoping to find a mistake. Did not spot that one. That is one mistake that I have never seen before. I consider that a "valuable mistake". I learned from it.
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    According to one of your posts you are using a 20Mhz crystal..... Either SPBRG = 129 (9600) or 64 (19200)
     
  13. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Thanks for your feedback. The '129' was fixed in the last code that I uploaded.

    I found a mistake in the MAX232 circuit. the capacitor at pin 2 was connected to ground instead to Vcc. I tried the code again, but still not working. Can the IC be damaged?

    Also, when setting the baud rate, it it placed into the SPBRG register or into another register?
     
  14. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    In your code that checks for 'f', you probably want to replace this:

    Code (text):
    TXREG = RCREG; // echo received data back to sender
    if(a == 'f')
    with something like this:

    Code (text):
    a = RCREG;
    TXREG = a; // echo received data back to sender
    if(a == 'f')
    I would also move reading outside of the waiting cycle, but this probably doesn't matter.

    It would be helpful to check for bad characters and see if you receive them right. This will give you a clue on whether the problem is on the sending or receiving end.
     
  15. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,718
    Likes:
    37
    when i had a problem like that its was start/stop bit related, either in my code or in terminal settings, or in the time delay(as Ian mentioned) , when i was debugging my own code, i found it helps to send characters 01010101, 11001100, and 11110000, then you can see where the binary is getting lost.
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Its a framing error.... You have set SPBRGH to 129.... You need to set SPBRG to 129 and SPBRGH to 0....

    Its working form me now

    Code (c):

    // INCLUDE PIC18F4550 HEADERS (LIBRARIES) -------------------------------------------------------------------------------------------------------------------------------------------------------------

    #include<p18f4550.h>
    #include<delays.h>  //Library for delays IMPORTANT TO INCLUDE THIS LIBRARY FOR DELAYS ()()()()()()()()()()()()()()()()()()()()()()()()()()                        

    // COMPILER DIRECTIVES FOR CHIP CONFIGURATION BITS ---------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #pragma config FOSC  = HSPLL_HS  // Since a 20MHz Xtal is used, set FOSC to HSPLL_HS (HS stands for high speed)
    #pragma config PLLDIV = 5          // Divide the 20MHz by 5 to provide 4 MHz input to the 96 MHz PLL
    #pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock
    #pragma config USBDIV = 2          // USB clock coming from 96 MHz PLL output is divided by 2
    #pragma config FCMEN  = OFF      // Fail-Safe Clock Monitor disabled (this is used to stop operation when frequency gets out of tollerance--usually used for medical applications)
    #pragma config IESO  = OFF      // Oscillator Switchover mode disabled
    #pragma config PWRT  = OFF      // Enable Power-up timer (This causes the PIC to delay until full operating voltage is reached before the program starts)
    #pragma config BOR    = ON        // Enable Brown-out reset (This automatically put the PIC in reset if the power coming into the chip isn't sufficient to work reliably, hence preventing malfunctions)
    #pragma config VREGEN = ON        // Use internal USB voltage regulator from 5V to 3.3V
    #pragma config WDT    = OFF      // Disable Watchdog timer (This is used so when the system see that it may not be working as expected, it automatically resets the PIC)
    #pragma config MCLRE  = ON        // Enable MCLR Enable (MCLR is pin 1 of the PIC. When ground is applied it resets the PIC)
    #pragma config LVP    = OFF      // Disable low voltage ICSP (This is another way to program the PIC. Since not used - Disable)
    #pragma config ICPRT  = OFF      // Disable dedicated programming port (44-pin devices) (This is a Dedicated In-Circuit Debug/Programming Port)
    #pragma config CP0    = OFF      // Disable code protection (Used to protect the program from being coppied)


    // INITIALIZE FUNCTIONS (Prototyping the functions) ---------------------------------------------------------------------------------------------------------------------------------------------------------------------


    // MAIN FUNCTION -----------------------------------------------------------------------------------------------------------------------------------------------------------------------


    void Configurate_USART (void)
    {

      //LEDs

      TRISB = 0xF0 ; // PORT B Setting: Set all the pins in port B to Output.

      // USART configuration

      TXSTAbits.TX9  = 0;    // 8-bit transmission
      TXSTAbits.TXEN = 1;    // Transmit enabled
      TXSTAbits.SYNC = 0;    // Asynchronous mode
      TXSTAbits.BRGH = 1;    // High speed

      RCSTAbits.SPEN = 1;    // Enable serial port - configures RX/DT and TX/CK pins as serial port pins
      RCSTAbits.RX9  = 0;    // 8-bit reception
      RCSTAbits.CREN = 1;    // Enable receiver

      BAUDCONbits.BRG16 = 0; // 8-bit baud rate generator

      SPBRG = 129;          //calculated
      SPBRGH = 0;  
      TRISCbits.RC6 = 1;    // make the TX pin a digital output
      TRISCbits.RC7 = 1;    // make the RX pin a digital input

    }

    void main(void)
    {
        char Temp_Instruction;
        Configurate_USART();

    while(1)
    {

      if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set
        {
             Temp_Instruction = RCREG;
            /*
            if(Temp_Instruction == 'a')
            {
                LATBbits.LATB0 = 1;  // RB-0 to High
                LATBbits.LATB1 = 1;  // RB-1 to High

                RCREG = 0; //Empty register
            }

            if(Temp_Instruction == 'b')
            {
                LATBbits.LATB0 = 0;  // RB-0 to Low
                LATBbits.LATB1 = 0;  // RB-1 to Low

                RCREG = 0; //Empty register
            }*/

            while(!TXSTAbits.TRMT);
                TXREG =Temp_Instruction; // echo received data back to sender
            }
        }

     
     
    • Like Like x 2
  17. languer

    languer Active Member

    Joined:
    Oct 26, 2010
    Messages:
    193
    Likes:
    25
    Location:
    Arizona
    Right on
     
  18. languer

    languer Active Member

    Joined:
    Oct 26, 2010
    Messages:
    193
    Likes:
    25
    Location:
    Arizona
    Adrianvon, don't get discouraged. Within 24hrs of posting it here you are almost there. I wish I could have anything even close to that at work.
     
  19. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Ian Rogers, did you use Hyper Terminal to test it? I copied the code and it is still not working for me.
     
  20. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Thanks for your support :)
     
  21. tunedwolf

    tunedwolf Well-Known Member

    Joined:
    Aug 9, 2006
    Messages:
    1,080
    Likes:
    62
    Location:
    UK
    According to the tables in the datasheet I have to hand for the 18F4550 (page 249), for 9600 baud and Fosc=20Mhz, Sync=0, BRG16=0, BRGH=1, SPBRG=129 :)
     

Share This Page