Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

serial communication in pic18f46k22

Status
Not open for further replies.

shreyas_patel21

Full Member level 3
Joined
Jan 4, 2011
Messages
181
Helped
15
Reputation
30
Reaction score
14
Trophy points
1,298
Activity points
2,325
hello everyone
i am trying to interface pic18f46k22 with pc
i used the code below from library.
but the hyperterminal shows random data
any suggestion would be helpful

Code:
#include<stdio.h>
#include<p18f46k22.h>
#include "usart.h"

//#define USE_OR_MASKS

unsigned char Rxdata[8];
unsigned char Txdata[] = "test";
void delay(unsigned int i)
{
	
	for(;i>1;i--);
}
void main(void)
{
    unsigned char config=0,spbrg=0,baudconfig=0,i=0,j='a';
	OSCCONbits.IRCF0 =1;
	OSCCONbits.IRCF1 = 1;
	OSCCONbits.IRCF2 = 1;
	OSCTUNEbits.PLLEN = 1;

    ANSELA = 0x00;
    ANSELB = 0x00;
    ANSELC = 0x00;
    ANSELD = 0x00;
    ANSELE = 0x00;
    
	TRISA=0x00;
	TRISB=0X0000;	//output
    TRISC=0Xffff;	//output
    TRISD=0X00;	//output
    TRISE=0X00;	//output
    
    Close1USART();  //turn off usart if was previously on

//-----configure USART -----
    config = USART_TX_INT_OFF | USART_RX_INT_OFF | USART_ASYNCH_MODE | USART_EIGHT_BIT | USART_SINGLE_RX | USART_BRGH_LOW;
//-----SPBRG needs to be changed depending upon oscillator frequency-------
    spbrg = 51;                    //At 8Mhz of oscillator frequency & baud rate of 2400.

    Open1USART(config, spbrg);        //API configures USART for desired parameters

    baudconfig =  BAUD_8_BIT_RATE | BAUD_AUTO_OFF;
    baud1USART (baudconfig);

//------USART Transmission ----
    while(Busy1USART());             //Check if Usart is busy or not
    puts1USART((char *)Txdata);                //transmit the string



    Close1USART();
    while(1);                        //end of program

}
 

yuvko

Member level 4
Joined
Sep 5, 2007
Messages
74
Helped
19
Reputation
38
Reaction score
17
Trophy points
1,288
Activity points
1,596
check your bit rate:
- Does your PC configured for 2400 bps?
- Does your oscillator frequency is 8Mhz as in assumed by the code?

does both your PIC and PC uarts configured the same: bit-rate, stop bits, data length, parity bits etc. ?
 

shreyas_patel21

Full Member level 3
Joined
Jan 4, 2011
Messages
181
Helped
15
Reputation
30
Reaction score
14
Trophy points
1,298
Activity points
2,325
thanks yuvko!

i have already solved it and problem was same as you said!
i changed oscillator configuration bits and baud rate in code.
also i used custom functions instead of using library functions.
now i can send and receive data.
 

Ahmmed Razu

Advanced Member level 4
Joined
Jul 7, 2011
Messages
100
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
Bangladesh
Activity points
1,967
thanks yuvko!

i have already solved it and problem was same as you said!
i changed oscillator configuration bits and baud rate in code.
also i used custom functions instead of using library functions.
now i can send and receive data.
Can you please upload the corrected code? i am in some troubles.
 

katela

Banned
Joined
May 9, 2013
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
0
Check your communication settings (bit, baud rate...) they must match the pc settings)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top