• 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.

PIC184520 USART Lab - Virtual Terminal

Thread starter #1
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);
}
}
 

Attachments

Pommie

Well-Known Member
Most Helpful Member
#4
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.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#5
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();
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#6
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?
No! Proteus terminal is TTL..
 
Thread starter #8
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​
}
}
 

Attachments

Thread starter #9
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).
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#11
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!!
 
Thread starter #12
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)?
 

Latest threads

EE World Online Articles

Loading

 
Top