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

PIC184520 USART Lab - Virtual Terminal

Discussion in 'Circuit Simulation & PCB Design' started by leovivaldi, Nov 25, 2017.

  1. leovivaldi

    leovivaldi New Member

    Joined:
    Nov 25, 2017
    Messages:
    7
    Likes:
    0
    Hi,
    I am using the PIC18F4520 on Proteus 8. I want to use the USART to display the 'U' character all the time on the virtual terminal.
    The baud rate is 19200 and I changed it on the virtual terminal as it was displaying wrong characters to baud rate set as 9600 (default).
    I am getting 4 dashes at a time ----- instead of Us. See attached.

    Can you help and explain why I am getting this? Or give a hint or point to the part in the source code that is causing it to do this?

    The source code is:


    #pragma config OSC = HS //set Osc mode to HS high speed clock
    #pragma config WDT = OFF // set watchdog timer off
    #pragma config LVP = OFF // Low Voltage Programming Off
    #pragma config DEBUG = OFF // Compile without extra Debug compile Code
    // Include Files

    #include <p18f4520.h> // Device used is the PICF4520
    #include <delays.h> // Include delays headers


    void main (void)
    {
    TXSTA = 0x24;// Select high baud rate, 8 bit
    SPBRG = 64;// 19200 bps, 20MHz clock
    TXSTAbits.TXEN = 1;// Transmit enable
    RCSTAbits.SPEN = 1; // Enable serial port
    while(1){
    while (PIR1bits.TXIF == 0) {;} // Wait until Peripheral Interrupt Request Register Transmit Flag is set // (transmit complete, TXREG empty)
    TXREG = 'U';
    Delay10KTCYx(50);
    }
    }
     

    Attached Files:

  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,152
    Likes:
    339
    Location:
    Brisbane Australia
    ONLINE
    Either BRG16 (BAUDCON) is getting set or your clock isn't 20MHz.

    Mike.
     
  3. leovivaldi

    leovivaldi New Member

    Joined:
    Nov 25, 2017
    Messages:
    7
    Likes:
    0
    Hi Mike. Thanks for your reply. This is making me pull my hair. :(
    How do you check which one of these scenarios is causing this?
     
  4. dave

    Dave New Member

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


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,152
    Likes:
    339
    Location:
    Brisbane Australia
    ONLINE

    First do a blinking led to check if it's running at 20MHz and maybe use a scope to check the RS232. Also, have you checked the polarity? Do you need a max232?

    Mike.
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,303
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    BRGH = 1;

    Also! The default terminal window speed is 9600 baud so you need to change it.. As you are using C18 why not just use

    OpenUSART ( USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE
    & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 64);
    and include <usart.h>

    Then just readUSART(); and putcUSART();
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,303
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    No! Proteus terminal is TTL..
     
  8. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,051
    Likes:
    519
    Location:
    Cleveland, OH, USA
    ONLINE
    You don't mention a crystal or crystal resonator. What frequency is your crystal/resonator?
     
  9. leovivaldi

    leovivaldi New Member

    Joined:
    Nov 25, 2017
    Messages:
    7
    Likes:
    0
    Hi Ian. Yes I am using C18. I know that I can use the header file and access the usart.h functions. I am doing this lab and it has two examples. This is the first and the second uses the functions from the library of the MPLABC18 header.
    However, when I tried both examples. I get the same results.
    As for the virtual window. I thought of that as well . I have changed it. take a look at the screenshot.
    I know I need to calculate the baud rate using the formula and I get 64 for 20MHz oscillator and 19200 baud.
    Please help. I understand the theory, the code (functions, steps of setting up USART, etc) but cannot debug this. I cannot even upload the Proteus file because I know it's against forum rules!


    The following is the source code for the example using functions of the usart.h. The only difference is that I need to output a statement:

    /* Serial Communication Transmit Example using MPLAB C18 libraries */ /* Using Port C as a UART Port */
    #pragma config OSC = HS /*set osc mode to HS
    #pragma config WDT = OFF /* set watchdog timer off */
    #pragma config LVP = OFF /* Low Voltage Programming Off */
    #pragma config DEBUG = OFF /* Compile without extra Debug compile Code */
    /* Include the following header files */
    #include <p18cxxx.h> // device PIC18 family
    #include <delays.h> // include the delays library
    #include <usart.h> // include the USART header files
    void main ()
    {
    /* Make Port C O/P */
    TRISC = 0x00; /* Open the USART for communication and set the following operational parameters: Set USART Parameters: Baud rate 19.200, TX & RX interrupts disabled, asynchronous operation, 8 data bits, Continuous reception, High Baud rate, calculated value for 19200 is 64 decimal */

    OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 64);
    while(1)
    {
    putrsUSART( " PIC 18F4520 TRANSMIT EXAMPLE ! \r\n " ); // Send the string
    Delay10KTCYx(200); // Delay between data transmissions​
    }
    }
     

    Attached Files:

  10. leovivaldi

    leovivaldi New Member

    Joined:
    Nov 25, 2017
    Messages:
    7
    Likes:
    0
    Hi jpanhalt.
    I know what you mean what follows is also confusing.
    I have asked my tutors and they don't seem to know. the configuration for the Oscillator is HS which means I am using a crystal. Now, I have completed all my previous labs such as ADC and what not, BUT I have never interfaced the crystal circuit with the OSC1 and OSC2 pins on the PIC18F4520 and it worked . Can you explain that please?

    To answer your question; yes, the frequency of the oscillator should be 20 MHz. I have used the formula to calculate the SPBRG value based on that (64).
     
  11. leovivaldi

    leovivaldi New Member

    Joined:
    Nov 25, 2017
    Messages:
    7
    Likes:
    0
    Dude . I am such an idiot. I have solved it. see attached.
     

    Attached Files:

  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,303
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    jpanhalt and Pommie

    Protues is a simulator that doesn't use oscillators, what I mean is they are not modelled, all you need is to specify the speed you wish in a properties dialogue..

    I'm assuming this was the problem judging by the response... The dialogue defaults to 4MHz...

    At least you are up and running!!
     
  13. leovivaldi

    leovivaldi New Member

    Joined:
    Nov 25, 2017
    Messages:
    7
    Likes:
    0
    Got it Ian. Thanks mate. I kind of guessed it. But I have seen online projects and exercises on youtube, forums and other MCU websites that they do interface them. So is that redundant (because they don't need to)?
     
  14. leovivaldi

    leovivaldi New Member

    Joined:
    Nov 25, 2017
    Messages:
    7
    Likes:
    0
    I think it is because of PCB Design most probably!
     

Share This Page