Asynchronous Serial Communication on Proteus using PIC18F4520

Thread Starter

leodavinci90

Joined Oct 22, 2014
57
Hi,
I want to display the 'U' character 5 times on the virtual terminal.
I have set the baud rate as 19200 and also changed it on the virtual terminal as it was displaying wrong characters to baud rate set as 9600 (default).
I have to set the micro at 20Mhz and I still I am getting 5 dashes ----- instead of UUUUU.

Can you help and explain why I am getting this?

Dashes instead of U.JPG

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

Thread Starter

leodavinci90

Joined Oct 22, 2014
57
Are you certain you have a 20MHZ clock?
Thanks for replying.
OK, first I must declare the following even though I think you already know it.

I am using the MCC18 compiler so all questions will pertain to its library.

To start I want to understand your question if you may before going back to the original one that I posted.

1) So according to the datasheet, the PIC18F4520 has an Operating Frequency of 40MHz. Does this mean the maximum frequency is 40 MHz?
2) Does this info in the datasheet mean that it has an INTERNAL OSCILLATOR operating at 40 MHz max? Or does it mean you can interface it with an external oscillator with a max frequency reaching 40MHz?
3) I have put this line in the configuration settings above.
#pragma config OSC = HS //set Osc mode to HS high speed clock
What speed is it running because of this configuration and is that what I need to change to make it run at 20MHz?
4) Spinakker : do you do any sailing is that why you have this nickname? :)
 

spinnaker

Joined Oct 29, 2009
7,835
You need to explicitly set the clock or it will run at its default 31hz. That chip is only capable of a max of 8HZ with internal clock.


upload_2017-11-22_7-12-20.png


HS is only valid for crystals

upload_2017-11-22_7-15-14.png

Please read the datasheet.


Yes I used to sail years ago. That is where the name came from. Should my boat. Too expensive and too much of a hassle.
 

Attachments

Thread Starter

leodavinci90

Joined Oct 22, 2014
57
Thanks for your reply.
1) OK, I believe the SPBRG = 111 for 19200 and 20 MHz. Correct?
2) So from what you stated, I gather that the HS mode is configured when I want to interface an external oscillator with the MCU. correct?
3) if above is correct. I have never interfaced an external oscillator with the MCU and always had it on HS mode. How can that be?
4) Finally, I have always used the following configuration bits:
#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*/​
How do I know which configuration bits do I need to set up . and what happens if I ignore them? because I know that there are other configuration fuses that I can set up.
Please answer or provide link as numbered. :)
 

spinnaker

Joined Oct 29, 2009
7,835
You DO NOT set clock speed with configuration bits. Simply clock source and type. Read the data sheet. I laid it all out for you above. Sounds to be like you did not read it.

Both MpLab and MPlabX have a tool to help you setting configuration bits. For internal osc you would want R/C.
 

Thread Starter

leodavinci90

Joined Oct 22, 2014
57
OK. So I am now I am reading the datasheet for the PICmicro Mid-Range MCU Family to learn the basics about the oscillator configuration used in the micro.

I'll re-phrase one of my questions:
I have used the HS mode all the time which requires the circuitry in the attached image.
But I have never used that circuit in any of the previous labs on Proteus and it still works. Do you have an explanation for that?
 

Attachments

spinnaker

Joined Oct 29, 2009
7,835
Ypu have been wrong in every other Proteus application then and just fortunate it worked.

I don't have Proteus . But my guess, Proteus is not the real world.
 
Top