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.

[SOLVED] eusart code for PIC18f4550

Status
Not open for further replies.

embRTS

Full Member level 4
Full Member level 4
Joined
Sep 8, 2013
Messages
201
Helped
36
Reputation
72
Reaction score
33
Trophy points
28
Location
Bangalore
Visit site
Activity points
1,191
Hello I am trying to write a sample uart code
but it's giving garbage data

i have checked on different baud rates also
by changing the value of SPBR

here is my code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <p18F4550.h>
#include <stdio.h>
#include <string.h>
#include <usart.h>
void delay(unsigned int i)   //Function for delay
{
unsigned int j;
for(j=0;j<i;j++);
}
 
void main()
{
 unsigned char config=0,spbrg=0,baudconfig=0,i=0,j='a';
 
config = USART_TX_INT_OFF | USART_RX_INT_OFF | USART_ASYNCH_MODE | USART_EIGHT_BIT | USART_SINGLE_RX | USART_BRGH_LOW;
spbrg = 25;  //Baud for 9600
 
 baudconfig =  BAUD_8_BIT_RATE | BAUD_AUTO_OFF;
baudUSART (baudconfig);
 
OpenUSART(config,spbrg);
 
WriteUSART('A');
 
putsUSART("THIS IS UART TEST\n");  //String: welcome message
 
}



output is junk data on the terminal

please help me out of this
thanks in advance
 
Last edited by a moderator:

I have checked the baud rates
I mean I checked it even for different baud rates
 

Zip and post MPLAB project files. While(1) is missing. Is config words set in IDE? If not config words should be set in code. Disable WDT. If you are receiving continuous garbage data when while(1); is missing then WDT must be resetting.
 

I have attached the "usart.h" file from which I was using the function foe Eusart
 

Attachments

  • usart.rar
    4.2 KB · Views: 72

Config words are set in IDe only
There is Menu bar of Config setting there as u said I have disabled the WDT
 

Zip and post the mplab project files. If there is max232 between uC and PC then use RealTerm to capture data and check invert data option and see.

@nick

He is using C18 Compiler.
 

Here are the files
I have done some changes but still not getting results
 

Attachments

  • uart_mplab.rar
    16.8 KB · Views: 46
  • usart.rar
    4.2 KB · Views: 49

Config words are not set in code. In IDE it is set to use config words from code. Mention Clock frequency used.

I have used Fosc of 20 MHz. It is working. putsUSART() is not working. I don't know why. Debug and see. Use ISR for USART receive.
 

Attachments

  • USARTC18.rar
    27.1 KB · Views: 49
Last edited:

I have defined the value of HS
by
#pragma HS = 16000000
#pragma FOSC = HS

as HS is set in the FOSC configuration setting
Now character whatever i sent through keyboard now is receiving and printing back to PC but continuously the same character is printed in Terminal
till I press another key from keyboard
for example - I pressed 'A' then it will print "A" till i press some another key

also putsUSART("HELLO_WORLD\n");
gives JUNK data on the terminal





meanwhile i also tried like i have done in AVR also

the code is attached in .txt format but still that one is also not working
like when i give some char to it
that char is not printed and some another char is printed

like i pressed '8' from keyboard then '0' is printed like wise ti's happening
I have attached one image Screen Shot also

in that the blue Colour letter are sent by PC
and RED one is output from PIC
the file is attached here with
 

Attachments

  • Files.rar
    14.2 KB · Views: 93

Ya I sought out that

like unsigned char rt[]="HELLO"
putsUSART(rt);

then it will print the character well

But now the same problem is still here that one char received pic is printing it continuously until next char is not sent to pIC
 

ok now try simple code run and tell this type code simulation time return continuously or not.
Code:
#include <p18F4550.h>
#include <stdio.h>
#include <string.h>

//#include <usart.h>
#define F_CPU 16000000

#pragma HS = F_CPU
#pragma FOSC = HS

void delay(unsigned int i);
void eusart_init(unsigned int BAUD);
void eusart_tx(unsigned char data);
unsigned char eusart_rx();
void sendstring_eusart(unsigned char sdata[]);

void delay(unsigned int i)   //Function for delay
{
unsigned int j;
for(j=0;j<i;j++);
}


void eusart_init(unsigned int BAUD)
{
TRISCbits.RC7 = 1;
TRISCbits.RC6 = 0;

BAUDCONbits.BRG16 = 0; //8 bit Baud Rate generator SPBRG only
                      


SPBRG = ((F_CPU/(BAUD * 64))-1);  //Assigning Value to SPBRG

TXSTAbits.SYNC = 0;  //Asynchronous Mode
TXSTAbits.BRGH = 1;  //Asynchronous High Speed

RCSTAbits.SPEN =1;   //Serial PORT Enabled (Rx and Tx pins)

TXSTAbits.TXEN = 1;  //Transmission enabled
TXSTAbits.TX9 = 0;  //8 bit Mode

RCSTAbits.CREN =1;   //Enable Receiver 
RCSTAbits.RX9 = 0;   // 8 Bit Mode 


}

void main()
{

unsigned char dat;
 
eusart_init(9600);  //EUSART Initialization

while(1)
{
    while(PIR1.RCIF == 0); //wait till RCIF bit is low

    dat = RCREG;           // received data moved to “dat” variable

    TXREG = dat;           //dat value is given to TXREG for transmit

    while(PIR1.TXIF == 0); //wait till TXIF bit is low	  

}

}
 

If echo character is turned ON then turn it OFF.


Code C - [expand]
1
2
3
4
5
6
7
while(PIR1.RCIF == 0); //wait till RCIF bit is low
 
    dat = RCREG;           // received data moved to “dat” variable
 
    TXREG = dat;           //dat value is given to TXREG for transmit
    dat = '\0';
    while(PIR1.TXIF == 0); //wait till TXIF bit is low



or


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
unsigned char send = 0;
 
while(PIR1.RCIF == 0); //wait till RCIF bit is low
 
    dat = RCREG;           // received data moved to “dat” variable
    if(dat != '\0')send = 1;
    if(send){
    TXREG = dat;           //dat value is given to TXREG for transmit
    dat = '\0';
   send = 0;
}
    while(PIR1.TXIF == 0); //wait till TXIF bit is low

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top