Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

problem with pic18f45k20 uart communication

Status
Not open for further replies.

ragi

Junior Member level 3
Joined
Nov 18, 2015
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
251
Hi,

I have started working on pic18f45k20 microcontroller and using pickit 3 as debugger.
I am doing simple echo program using uart with baud rate 115200 ,
The problem i am facing is , when i run my code , the hyperterminal starts taking some junk data continuously before i send any thing.

Can any one send echo code for pic18f45k20 ??

Here is my code ,

Code:
//#define _XTAL_FREQ 8000000 //The speed of your internal(or)external oscillator
#define USE_AND_MASKS
//#include <xc.h>
//#include "config.h"
#include "p18f45k20.h"
#include <usart.h>
int i = 0 , j=0;
unsigned char UART1Config = 0, baud = 0;
unsigned char MsgFromPIC[] = "PIC Rocks\r";

void SetupClock(void);
void Delay1Second(void);


#pragma config FOSC = INTIO7, FCMEN = OFF, IESO = OFF                      // CONFIG1H
#pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30                       // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768                                   // CONFIG2H
#pragma config LPT1OSC = ON, PBADEN = ON, CCP2MX = PORTC                    // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF                          // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                   // CONFIG5L
#pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF               // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                           // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
#pragma config EBTRB = OFF    

void main(int argc, char** argv) {

    SetupClock(); // Internal Clock to 8MHz

    TRISCbits.RC6 = 0; //TX pin set as output
    TRISCbits.RC7 = 1; //RX pin set as input

    UART1Config = USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_BRGH_HIGH & USART_SINGLE_RX ;
    baud = 0x33;
    OpenUSART(UART1Config,baud);

   
  //  for( j=0 ;j<3;j++) //infinite loop
while(1)
    {
       putsUSART(MsgFromPIC);
       Delay1Second();
    }

}

void SetupClock()
{
    OSCCONbits.IRCF0 = 0;
    OSCCONbits.IRCF1 = 1;
    OSCCONbits.IRCF2 = 1;
}

void Delay1Second()
{
    for(i=0;i<100;i++);             
    
}

Thank you.
 
Last edited by a moderator:

hello,


With 8Mhz.. you can't use 115200 bauds
see datasheet BAUD RATES FOR ASYNCHRONOUS MODES (CONTINUED)

even at 57,6 Kbd and SPBRG=8 you get -3.55% Bauds error..

try it before with 19200 ...and increase step by step..
or use higher FOSC .. 64 MHz ?

What hardware interface do you use ?
 
Last edited:

What about interrupt?
Did you acknowledge the interrupt.
 

What about interrupt?
Did you acknowledge the interrupt.

he doesn't use RX , so no interrupt, (& USART_RX_INT_OFF) just will transmit a message ..
if i get a good understanding of his code..

maybe use of the PLL could solve this problem (FOSC is too low at 8Mhz) ?
so 32MHz , 48Mhz ..
 

Code:
void Delay1Second()
{
    for(i=0;i<100;i++);             
    
}

I doubt that gives a one second delay either, not that you really want one.

Brian.
 

I am using PICkit 3 programmer/debugger.
Actually my pc is not having RS232 port ,so i gotto use MAX3232 level converter and PL2303 USB to Serial adapter. With this set up I tried basic echo test (shorting TXin & RXout pins of MAX3232) which passed only for baud rate 115200 and for some lower baud rates it returned value "FF".

So i am using baud rate 115200 for UART communication.

Is my UART config is correct ???
 

I dint use interrupt ,thought using interrupt causes increase in delay . But will try once using interrupt.

- - - Updated - - -

I just added lil delay while debugging. But dint feel any difference.
 

I
So i am using baud rate 115200 for UART communication.
Is my UART config is correct ???

With 8Mhz..

SYNC = 0, BRGH = 1, BRG16 = 1 or SYNC = 1, BRG16 = 1
SPBRG=16 with 2,12% error

if you are using internal 8Mhz FOSC .. adjust OSCTUNE ...
the best way is to use a quartz 16Mhz or more..

SPBRG_115Kbds.jpg
 
Last edited:
  • Like
Reactions: ragi

    ragi

    Points: 2
    Helpful Answer Positive Rating
Thank you sir :) :)

I am able to get baud rate nearly 115.2k . and I can see at least a data on hyper terminal but not the one i am transmitting. I guess it is because of baud rate error.
 

does it works OK at 56Kbds or 38,4Kbds ?
Are you using internal FOSC 8Mhz ?
if yes , you can adjust FOSC +-3% by osctune value ...
 

I tried with 56Kbds and 38Kbds , in this case output is worse than 115.2kbds. I am able to see my transmitting bits at TX pin of PIC at the baud rate 115.2kbds through oscilloscope but not in hyperterminal( settings of hyperterminal is correct.). In hyperterminal it is displaying some junk data.

My internal FOSC is 16MHz.

I have never used OSCTUNE. I will change the value of OSCTUNE reg and try.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top