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.

[PIC] how to send string through serial port

Status
Not open for further replies.

Deexith Hasan

Advanced Member level 4
Joined
Oct 24, 2014
Messages
111
Helped
4
Reputation
8
Reaction score
3
Trophy points
18
Activity points
740
the following code works well in proteus but not in my development board ...received k alone y?..
Code:
#include <htc.h>

void delay()
{
unsigned char i;
for(i=0;i<255;i++);
}

void serialwrite(unsigned char data1,unsigned char data2,unsigned char data3)
{
TXREG=data1;
while(TXIF==0);
TXIF=0;
TXREG=data2;
while(TXIF==0);
TXIF=0;
TXREG=data3;
while(TXIF==0);
TXIF=0;

}




void main()
{
TXSTA=0X20;
RCSTA=0X90;
SPBRG=129;
BRGH=1;
TRISC7=1;
TRISC6=0;
TRISA=0XFF;
TRISD=0X00;
TRISB=0X00;
serialwrite('k','b','c');
while(1);


}
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,869
Helped
4,860
Reputation
9,738
Reaction score
4,643
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
126,564
PIC type?
What baud rate are you expecting?
What is the clock speed?
Is TXIF already set when you enter serialwrite()?

Brian.
 

FvM

Super Moderator
Staff member
Joined
Jan 22, 2008
Messages
48,299
Helped
14,233
Reputation
28,727
Reaction score
12,925
Trophy points
1,393
Location
Bochum, Germany
Activity points
279,669
I don't see a problem with the code. Writing to TXIF is useless, but doesn't hurt (bit is RO, see datasheet). It's cleared automatically when the TXREG data is transferred to UART shift register. So consequently, you wait for TXIF being set before writing to TXREG.

May be wrong baud rate? Or receiver set to 9-bit frame? (Parity enabled or two stop bits).
 

MUKESH.K.S

Full Member level 1
Joined
Jan 31, 2014
Messages
98
Helped
14
Reputation
28
Reaction score
13
Trophy points
8
Location
KERALA
Activity points
607
post your project folder as zip file including simulation....
Which frequency crystal u r using ? have u changed frequency of pic in simulation according to your circuit?
 

Deexith Hasan

Advanced Member level 4
Joined
Oct 24, 2014
Messages
111
Helped
4
Reputation
8
Reaction score
3
Trophy points
18
Activity points
740
PIC18f4520
20Mhz crystal
9600 baud rate
i am doing temp data logging my actual code is
Code:
#include <htc.h>


unsigned char temp2;
unsigned char temp3;
unsigned char lsb;
unsigned char msb;
unsigned char mmb;


void dataconv(unsigned char);
void dataconv(unsigned char data)
{
lsb=data%10;
lsb=48|lsb;
data=data/10;
mmb=data%10;
mmb=48|mmb;
msb=data/10;
msb=48|msb;
}


void delay()
{
int i;
for(i=0;i<12275;i++);


}


void serialwrite(unsigned char data)
{
TXREG=data;
while(TXIF==0);
}

void interrupt ISR(void)
{
if(RCIF==1)
{
RC0=1;

if(RCREG=='a'){
serialwrite('b');
dataconv(temp2);
serialwrite(msb);
delay();
serialwrite(mmb);
delay();
serialwrite(lsb);

dataconv(temp3);
serialwrite(msb);
delay();
serialwrite(mmb);
delay();
serialwrite(lsb);
}


}
}



void main()
{
TXSTA=0X20;
RCSTA=0X90;
SPBRG=129;
BRGH=1;
TRISC7=1;
TRISC6=0;
TRISA=0XFF;
TRISD=0X00;
TRISB=0X00;
ADCON0=0X01;
ADCON1=0X0C;
ADCON2=0X2D;
PEIE=1;
GIE=1;
RCIE=1;
serialwrite('a');
delay();
serialwrite('b');
while(1)
{

delay();
ADCON0=0X05;
GODONE=1;
while(GODONE==1);
PORTD=ADRESH;
temp2=ADRESH;
delay();
ADCON0=0X09;
GODONE=1;
while(GODONE==1);
temp3=ADRESH;

}

}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top