# PIC UART Software Code

Status
Not open for further replies.

#### engshahrul

##### Banned
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(;;){
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 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_luis

##### Super Moderator
Staff member
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

#### engshahrul

##### Banned
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

#### andre_luis

##### Super Moderator
Staff member
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
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
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
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

#### andre_luis

##### Super Moderator
Staff member
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.