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

PIC UART Software Code

Status
Not open for further replies.

engshahrul

Banned
Joined
Apr 27, 2009
Messages
330
Helped
97
Reputation
194
Reaction score
94
Trophy points
1,308
Location
Malaysia
Activity points
0
Hi, I'm doing UART Software code. That's I wan't transmit and receive using normal pin.
Here my code, using Hi-Tech compiler. Baudrate 9600.
Code:
for(;;){
	i=0;
	for(;;){
		data[i]=uart_soft_receive();
		if(i==0){if(data[i]=='$') i++;}
		else if(data[i]!='.') i++;
		else break;}

	lcd_goto(0x40);
	i=1;
	while(data[i]!='.') lcd_write(data[i++]);
	while(i<17){
		lcd_write(' ');
		i++;}
}
I want read data starts with '$' and ending with '.' . Here my function for transmit and receive
void uart_soft_transmit(char data)
{int i,j;
TX1=0;
for(j=0;j<baud_delay;j++) continue;
for(i=0;i<=7;i++){
TX1=data;
data=data>>1;
for(j=0;j<baud_delay;j++) continue;}
TX1=1;
for(j=0;j<baud_delay;j++) continue;
}

unsigned char uart_soft_receive(void)
{unsigned char data=0;
int i,j;
while(RX1==1) continue;
for(j=0;j<baud_delay;j++) continue;
for(i=0;i<=7;i++){
data=(data>>1)|(128*RX1);
for(j=0;j<baud_delay;j++) continue;}
return data;}

void uart_soft_string(const char *s)
{
while(*s)
uart_soft_transmit(*s++);
}
With this code, I only can send 6 byte from hyperterminal and vb without error.
Ex, I send $1234. The LCD display 1234
If send more than that, PIC dosn't recognize the '.' character anymore.

Would anyone comment something, compare UART Software on CCS or Micro C. Or anyone have idea to improve this.
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,200
Helped
1,142
Reputation
2,303
Reaction score
1,119
Trophy points
1,403
Location
Brazil
Activity points
53,579
Once your serial communication was implemented by software, without use of interrupt to achieve precise timming, maybe you need perform some fine adjust at baud_delay parameter. I sugest you to use a scopemetter to evaluate if baud is according specified.

PS : CCS have ready-to-use API´s to do that feature.

+++
 

    V

    points: 2
    Helpful Answer Positive Rating

engshahrul

Banned
Joined
Apr 27, 2009
Messages
330
Helped
97
Reputation
194
Reaction score
94
Trophy points
1,308
Location
Malaysia
Activity points
0
PS : CCS have ready-to-use API´s to do that feature.

+++
If CCS can do that, CCS function can receive many character and is that waiting for uart signal or not? The use while(RX==1) will stuck if no UART signal coming.
 

    V

    points: 2
    Helpful Answer Positive Rating

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,200
Helped
1,142
Reputation
2,303
Reaction score
1,119
Trophy points
1,403
Location
Brazil
Activity points
53,579
I mean that maybe you are facing to timming problem.
That´s the typical behaviour : After a certain amount of characters, comunication is lost, due to difference of baud.

If you use a function built by compilers, there are no need to perform fine adustement to delay parameter.

+++
 

upand_at_them

Full Member level 2
Joined
Feb 26, 2010
Messages
131
Helped
21
Reputation
42
Reaction score
21
Trophy points
1,298
Location
Pennsylvania, U.S.
Activity points
1,942
After "while(RX1==1) continue;" you should delay for 1/2 bit so that you are in the middle of the bit. Then do the loop, using a full bit delay. Also, as already mentioned...you may need to check the accuracy of your delay routines.
 

engshahrul

Banned
Joined
Apr 27, 2009
Messages
330
Helped
97
Reputation
194
Reaction score
94
Trophy points
1,308
Location
Malaysia
Activity points
0
If you use a function built by compilers, there are no need to perform fine adustement to delay parameter.
The UART module pin, I already use for another device.
There is no UART software function provided for Hi-Tech compiler.
After "while(RX1==1) continue;" you should delay for 1/2 bit so that you are in the middle of the bit. Then do the loop, using a full bit delay. Also, as already mentioned...you may need to check the accuracy of your delay routines.
Yes, thanks. I will try that way.
 

Pheetuz

Full Member level 3
Joined
Feb 12, 2010
Messages
163
Helped
21
Reputation
42
Reaction score
19
Trophy points
1,298
Location
Brighton
Activity points
2,835
Sounds like it could be a problem to do with baud rate, have you tried using the auto baud rate detect option? You have to send a certain character first to calibrate it but after that it works fine... You can also use the baud rate detect option to find out the correct value to put into the baud rate generator register (because it will automatically put the right value in there after calibration) and then change your code to just use that value instead of the auto-detect, saves using the equations which always seem to come out wrong, atleast for me anyway!
 

    V

    points: 2
    Helpful Answer Positive Rating

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,200
Helped
1,142
Reputation
2,303
Reaction score
1,119
Trophy points
1,403
Location
Brazil
Activity points
53,579
I believe that baud-rate detection feature is suitable just to choose the baudrate, by selecting the one that works.
I´m not sure if calibration is needed, due the common baud-rates are known values.

+++
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top