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
    Hi,

    I am using USART with the PIC18F4550 and Visual Studio. Can someone help me find what is causing errors in the coding below please?

    Code (text):
    void main(void)
    {
      Configurate_USART();
     
      char Temp_Instruction;

    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
            }
        }
    }

    }
    Also, is there a neater way to to this?

    Thanks in advance.
     
  2. tunedwolf

    tunedwolf Well-Known Member

    Joined:
    Aug 9, 2006
    Messages:
    1,080
    Likes:
    62
    Location:
    UK
    Retracted :)
     
    Last edited: Oct 24, 2013
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I need to know what errors you are getting....

    What compiler are you using I don't recognise " Configurate_USART();"...
     
  4. dave

    Dave New Member

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


     
  5. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2


    Hi, I'm getting a syntax error on char Temp_Instruction;.

    " Configurate_USART();" will go to this function that I made.

    Code (text):
    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

      SPBRGH = '129';        //calculated

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

    }
     
  6. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    This is the first time that I am using UART, so please let me know if it it not set correctly. Any help will be appreciated.
     
  7. languer

    languer Active Member

    Joined:
    Oct 26, 2010
    Messages:
    193
    Likes:
    25
    Location:
    Arizona
    Are we to assume you have programmed the USART before and know it to be working (i.e. SPBRGH:SPBRG, SYNC bit, SPEN bit,RXDTP bit, RCIE bit, RX9 bit, CREN bit). You can easily test the USART is configured properly by writing back to the USART TX what you received on the RX.
     
  8. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Is there a program that when sending data to the COM port, it will display the transmitted data?
     
  9. languer

    languer Active Member

    Joined:
    Oct 26, 2010
    Messages:
    193
    Likes:
    25
    Location:
    Arizona
    You could use any terminal program (hyperterm, terraterm, procom, etc). The simplest way to start is by connecting the TX and RX pins on the cable. The terminal program would then echo back the characters you typed. Next you remove this connection on the cable and connect the cable to the PIC. You then write your PIC program so it echoes the received data back to transmit pin. You know you have everything configured properly on the PIC when you receive back what you typed.
     
  10. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    And if I want to give instructions to the PIC when certain data is sent, it it done as the code attached above please? or is there another way?
     
  11. languer

    languer Active Member

    Joined:
    Oct 26, 2010
    Messages:
    193
    Likes:
    25
    Location:
    Arizona
    Yes, your overall idea on how to give the instructions to the PIC based on received USART data is ok. But you first need to make sure the PIC is listening. If you can successfully loop back the received data, and send it back to the terminal you know the USART setup is correct; and then your only concern is really what you want to do. But it looks like at this point you can't say for sure your USART setup is correct.
     
  12. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    And if I want to give instructions to the PIC when certain data is sent, it it done as the code attached above please? or is there another way?
     
  13. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Thanks for your replies. I did install hyperterm, but cannot really understand how to use it. Do I sent data from hyperterm itself or from Visual Studio? When I pressed SEND from hyperterm, a window popped up telling me to select a file. Isn't it just characters that you send?
     
  14. tunedwolf

    tunedwolf Well-Known Member

    Joined:
    Aug 9, 2006
    Messages:
    1,080
    Likes:
    62
    Location:
    UK
    When starting up Hyperterminal, you need to tell it to use a Com Port and configure it for the number of bits, baudrate etc to use. After that anything you type is sent out the Com Port you selected. If you want to see what you are typing, set local echo on. If your PIC is working as expected, you should see what you type being echoed back to you, or you should see it being repeated if you have local echo switched on in Hyperterminal :)
     
  15. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Thanks. So I'm assuming I have some problems :) or should that be :( When I typed A, it echoed Y. Than I typed C and Y and there was no echo back. After that, the program is not letting me type more characters. Do you know of any easy to follow tutorial on UART for PIC18F please? I spent the last three days trying to make this work.
     
  16. tunedwolf

    tunedwolf Well-Known Member

    Joined:
    Aug 9, 2006
    Messages:
    1,080
    Likes:
    62
    Location:
    UK
  17. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Thanks for the link. I will try to follow this tutorial, maybe I will have more luck this time :)
     
  18. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Could you describe the whole system you are building? And post the full code also. "Syntax error" does not mean that the communication (rs232) is not working. It means that your code did not compile because of "Syntax error".

    Yes, how deep you want to go? Do you take the red pill, or the blue pill?
     
    Last edited: Oct 25, 2013
  19. adrianvon

    adrianvon Member

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

    I did the following myself:

    Code (text):
    // 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 main(void)
    {

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

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

        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
      {
            if(PIR1bits.TXIF == 1) // check if the TXREG is empty
            {
                TXREG = RCREG; // echo received data back to sender
            }
      }
    }

    }
    When using Hyperterminal, only the characters e, t, d, f, w, m, o, u, g, v are being echoed correctly. The rest are echoing other symbols. What can be causing this please? Is the baud rate correct for a 20MHz Crystal??

    Please if someone can help me solve this it would be much appreciated. This is the forth day trying to make this work :(
     
  20. adrianvon

    adrianvon Member

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

    I did the following code:

    Code (text):
    // 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 main(void)
    {

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


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

        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
      {
            if(PIR1bits.TXIF == 1) // check if the TXREG is empty
            {
                TXREG = RCREG; // echo received data back to sender
            }
      }
    }

    }
    When using Hyperterminal, only the characters e, t, d, f, w, m, o, u, g, v are being echoed correctly. The rest are echoing other symbols. What can be causing this please? Is the baud rate correct for a 20MHz Crystal??

    Please if someone can help me solve this it would be much appreciated. This is the fourth day trying to make this work :( but at least I did some improvement because yesterday only the characters w, m, o, u were being echoed correctly.
     
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    This will not work.... I found this out myself a while ago.... I will still drop characters.

    Use this..
    Code (c):

    while(!TXSTAbits.TRMT );// Wait until empty
    TXREG = RCREG;
     
     

Share This Page