no accuarate readings in uart interface with pic16f877a

Status
Not open for further replies.

rangerskm

Full Member level 4
Joined
Jan 23, 2013
Messages
199
Helped
0
Reputation
2
Reaction score
0
Trophy points
1,296
Visit site
Activity points
2,663
hi friends my output showing some of unknown values when transmitted character A to the tera term terminal in PC.
i am using PIC16F877A 20 mhz.mplab ide and hi tech c compiler.

please find below code and error file which shows in tera term.say me a solution for this.

Code:
#include <htc.h>

//#if defined(WDTE_OFF)
//__CONFIG(WDTE_OFF & LVP_OFF);
//#elif defined (WDTDIS)
//__CONFIG(WDTDIS & LVPDIS);
//#endif
int main(void)
{
    TRISC6 = 0;
    TRISC7 = 1;
    SPBRG = 25 ;
    BRGH =0 ;
    SPEN =1 ;
    SYNC = 0 ;
    SREN =1 ;
    TXEN =1;
    

    while (1)
    {
        TXREG ='A';
        while(TXIF == 0);
        TXIF=0;
    }
 return 0;
}


 

Put configuration setting properly can't see whether you are using intosc or hs

UART initialization doesn't seems complete.See the sample i used for PIC18f4550
sample :-
Code:
        TXSTA=0;                   //transmitter1 initillization
        RCSTA=0;                   //reciever1 init
        TXSTAbits.CSRC  = 0;        //Clock source select bit
	TXSTAbits.TX9   = 0;       //Selects 8-bit transmission
        TXSTAbits.SENDB = 0;      //Send Break Character bit
        TXSTAbits.TXEN=1;          // Transmit enable bit
        TXSTAbits.BRGH=0;           // High baud rate (fosc/16)
        TXSTAbits.SYNC=0;         // Asynchronous mode
        RCSTAbits.RX9 = 0;   //RX9 pin
       
        RCSTAbits.SPEN=1;       // Serial port enable bit
        RCSTAbits.CREN=1;       // Continuous receive enable bit
        BAUDCONbits.BRG16=1;
        /***BAUDRATE Configuration************/
	BAUDCONbits.ABDOVF = 0;   //No BRG rollover has occurred
	BAUDCONbits.WUE    = 0;   //RXx pin is not monitored or the rising edge detected
	BAUDCONbits.ABDEN  = 0;   //Baudrate Measurement is Disabl
        SPBRG=51;              // To get 9600 baud rate at 8MHZ frequency
        SPBRGH=0;//setting SBREG at 0
        //TRISCbits.TRISC6 = 0;
        //TRISCbits.TRISC7 = 1;
and use this in while(1) loop
Code:
while(TXSTAbits.TRMT==0);
        Nop(); //No Output
        // place the byte in TXREG for transfer
        TXREG = 'A';
 
Last edited:

Don't copy and paste it simply.I said my PIC(18f4550) was different so initialization and code part(pin number and pin configurations) will be different also.Check data sheet for exact configuration for your PIC.Find these(code terms) in data sheet and use accordingly.

- - - Updated - - -

Use this code found on net
Code:
#include<pic.h>

        

__CONFIG(0x3f72);

 

#define FOSC       20000     //20Mhz==>20000Khz

#define BAUD_RATE   9.6     //9600 Baudrate

#define BAUD_VAL   ((char)(FOSC/ (16 * BAUD_RATE )) – 1)     //Calculation For 9600 Baudrate @10Mhz

 

void main()

   {
   TRISC=0xc0;   //RC7,RC6 set to usart mode(INPUT)

   TXSTA=0x24;   //Transmit Enable

   SPBRG=BAUD_VAL; //9600 baud at 10Mhz

   RCSTA=0x90;   //Usart Enable, Continus receive enable

   TXREG='0';

 

   while(1)

   {
         if(TXIF==1)

         TXREG='A';

        

   }

}


and also check this link :- http://saeedsolutions.blogspot.in/2012/11/pic16f877-uart-code-proteus-simulation.html
 
i tried this code dude,i am getting accurate result in simulation .eg the text 'hello world' is displayed but in hardware when i connect the controller with teraterm terminal .i found some unknown readings,instead of hello world what may be the problem??is the baudrate or anything else .please explain ..
 

Yeah baud-rate is main problem most of the time.Check it if what you given is correct and also see your MAX 232 chip(UART chip) is working or not.
And last short pin 2 and 3 of your UART cable and enter some letter to see whether it is working or not .Pin layout **broken link removed**
 


You are having a baud rate issue. If you have it connected to the serial port then set it to 9600baud, otherwise you will not get the right data.
 
i checked the second and third pin by shorting them.the values are displaying.my cable is working fine.and also i set the baud rate as 9600 .but its showing many characters along with the string i send no accuracy in it sometimes if i send single character 'p' it displays as 'i' .is it because of SPBRH value and BRGH bit.can u say the correct value for that and its effect in the program. please provide me a sample working code
 

Hi
if it's working fine in proteus then most probably problem is in hardware. check your crystal, MAX232 and other connections
Also check your settings of teraterm terminal e.g. baudrate, data bits, parity, start bit, stop bit and flow control settings
and why don't you try to use hyperterminal for initial tests (hyper terminal is also available for Win7 & Vista too. u just need a quick google search)
 

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…