PIC18f452 UART Problem

Status
Not open for further replies.

baby_1

Advanced Member level 1
Joined
Dec 3, 2010
Messages
415
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
4,277
PIC18f452 UART Problem in MPLAB

Hello

here is my program

Code:
#include <p18f452.h>
#include <stdio.h>
#include <stdlib.h>
#include "LCD.h"
#include "delays.h"


#pragma config OSC=HSPLL
#pragma config DEBUG=OFF
#pragma config WDT=OFF
#pragma config BOR=OFF
#pragma config LVP=OFF
#pragma config PWRT=ON


#define in1 PORTAbits.RA0
#define in2 PORTAbits.RA1
#define in3 PORTAbits.RA2

#define d1 PORTAbits.RA3
#define d2 PORTAbits.RA5
#define d3 PORTEbits.RE0
#define d4 PORTEbits.RE1
#define d5 PORTEbits.RE2
#define d6 PORTCbits.RC3

unsigned char i1[17];
//////////////////
void cls(void)
{
        writecommand(0x01);
        Delay1KTCYx(10);
        writecommand(0x02);
        Delay10TCYx(10);
}
unsigned int meter(unsigned int me,unsigned char states)
{
    unsigned int i;
    i=0;
    if(states==1)
    {
        INTCONbits.TMR0IF=0;
        T0CON=0xA8;
        i=TMR0H;
        i=i<<8;
        i=i | TMR0L;
        return i;
    }
    INTCONbits.TMR0IF=0;
    TMR0H=0;
    TMR0L=0;
    T0CON=0xA8;
    while(i<=me)
    {
        i=TMR0H;
        i=i<<8;
        i=i | TMR0L;
    }
    T0CON=0x00;
    return i;
}


unsigned int zav(unsigned int me,unsigned char states)
{
    unsigned int i;
    i=0;
    if(states==1)
    {
        i=TMR1H;
        i=i<<8;
        i=i | TMR1L;
        return i;
    }
    PIR1bits.TMR1IF=0;
    TMR1H=0;
    TMR1L=0;
    T1CON=0x83;
    while(i<=me)
    {
        i=TMR1H;
        i=i<<8;
        i=i | TMR1L;
    }
    T1CON=0x00;
    return i;
}
///////////////////
void main(void)
{
unsigned int i;
    ADCON1=6;
 /////timers
   TRISAbits.RA0=1;
   TRISAbits.RA1=1;
   TRISAbits.RA2=1;
   TRISAbits.RA3=1;
   TRISAbits.RA4=1;
   TRISCbits.RC0=1;
 /////  Outputs
    TRISB=0;
    TRISAbits.RA3=0;
    TRISAbits.RA5=0;
    TRISEbits.RE0=0;
    TRISEbits.RE1=0;
    TRISEbits.RE2=0;
    TRISCbits.RC3=0;
    TXSTA=0x20;
    SPBRG=5;
    TXSTAbits.TXEN=1;
    RCSTAbits.SPEN=1;
d1=0;
d2=0;
d3=0;
d4=0;
d5=0;
d6=0;
lcdinit();
i=0;

      sprintf(i1,"%d",strlen("      IDLE"));
      lcdprint(i1);
//////////////////////////
    while(1)
    {
        cls();
        if(in3==1){
            d2=0;
            d1=1;
                sprintf(i1,"Hello Man");
        }
        else
        {
            d1=0;
            d2=1;
                sprintf(i1,"Hello Iran");}
        lcdprint(i1);
        TXREG='S';
        while(PIR1bits.TXIF==0);
        Delay10KTCYx(100);
    
}
}


i calculate SPBRG=5; for 9600 baud rate with 4MHZ input frequency(in HSPLL mode) but i receive these character in Hyperterminal



what is my fault?
 
Last edited:

Hi,

From my reading of the baud rate tables in the datasheet at 16mhz you should use SPBRG=25; when BRGH=0

These are the values I use for 9600with 4mhz No PLL

movlw d'25' ; 9600 baud @ 4 Mhz Fosc 8 bit Async
movwf SPBRG ; BRGH=1
movlw b'00100100'
movwf TXSTA ;enable Async Transmission, set brgh

movlw b'10010000'
movwf RCSTA ;enable Async Reception

You might find using pUTTY better that hyperterminal - free download.

Did you get your PK3 working ?
 
Reactions: baby_1

    baby_1

    Points: 2
    Helpful Answer Positive Rating
Thanks it works fine.
so with HSPLL always in datasheet i should assume that FIN=16Mhz?
 

Thanks it works fine.
so with HSPLL always in datasheet i should assume that FIN=16Mhz?

Hi,

Yes, the PLL multiplys the Fosc x 4.

Using a 4 mhz crystal you can use OSC=HS for Fosc 4mhz or = HSPLL for 16mhz
 
Reactions: baby_1

    baby_1

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…