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.

why pic needs time for rs232

Status
Not open for further replies.

adnan_merter

Full Member level 3
Joined
Jan 23, 2008
Messages
160
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Location
The most beautiful city of the world
Activity points
2,526
hi,
i am trying to establish rs232 connection but something strange is happening here,

Code:
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8, stop=1,invert)

void main()
{
char a;

while(1){
a=getc();
putc(a);
}
}

this code works but i receive junk characters, my baudrate and my osc setttings are correct, my clock rate is 48Mhz

but when i give some time to pic like,

Code:
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8, stop=1,invert)
void main()
{
char a;

while(1){
a=getc();
[B]delay_us(10);[/B]
putc(a);
}
}

the code perfectly works, so my question is "why pic needs some time to process rs232, and how can i eliminate this delay?"
 

You have given no information on the compiler you are using and type of PIC.

Most likely the reason is that your 'putc()' function isn't waiting for the transmit buffer to empty before loading it with a new character. You should check the transmit buffer is empty before loading it with new data (TRMT bit) or check to see if the TXREG has emptied by checking the TXIF flag in the INTCON register.

Brian.
 

You have given no information on the compiler you are using and type of PIC.

sorry i forgot it. i am using ccs c and pic18f2550 with 20 Mhz xtal and PLL5. the registers you mentioned are handled by the compiler. i am not using hardware uart, i am using software uart and everything should be taken care by the compiler.
right?

and also, not only the putch function, but the printf function sends junk character even i give it some time.
the first character is sent correctly and the the rest are junk.
 
Last edited:

hello,

Maybe the trouble is on receiving part.
and need to manage a buffer to store all incomming char , by using recieve interrupt .
 

hello,

Maybe the trouble is on receiving part.
and need to manage a buffer to store all incomming char , by using recieve interrupt .

no when i use printf function with something like printf("my character") only first two characters are shown correctly the others are junk, it needs some time between every single character
 

There needs to be two delay timers after the start bit is detected.
1) half bit timer delay for center sampling future bits. (5us) after start bit only
2) full bit timer delay for each bit center sampled. (10us) after each data bit.
 

hello

With MPLAB C18 , code for print a char is like this
Code:
void Put_RS(char * untel)
{
       while(BusyUSART());
       WriteUSART(untel);
}

and it works fine, even at FOSC=64Mhz ( 16MHzxPLL) and 19200 bauds

and for a string in RAM
Code:
with 
char Texte[80];   // table of char
char *txt;          // pointer on table

in the main, don't forget to init the pointer
 txt=&Texte[0];




int PutStr_RS(char *s)
{
    int n;
    for (n = 0; *s; n++, s++)
    {
           Put_RS(*s);
         }
    return n;
}

and for string in ROM

Code:
strcpypgm2ram(txt,"MY TEXT ");
 k=PutStr_RS(txt);
 
Last edited:

hi guys thanks for your help,

i found a quick solution thanks to the ccs compiler,

printf function can send a string with any desired function, so i just create a function which waits 10us before sending a character,

Code:
void ex_putc(char data)
{
delay_us(10);
putc(data);
}

and use this function in printf

Code:
printf(ex_putc,"My string");

it seems working perfectly, but of course this is not a solution this is just a quick trick, any advice still be usefull
 

i am facing the same problem bro !!! but when i put 10mS delay after initial configuration of system and port than after i get perfect data received in pic32 device.

i don't know what happen to make some delay and there is no listed delay in MLA of microchip ?
 

i am facing the same problem bro !!! but when i put 10mS delay after initial configuration of system and port than after i get perfect data received in pic32 device.

i don't know what happen to make some delay and there is no listed delay in MLA of microchip ?

pic needs about 500ms after start up, it is normal but the interesting thing is, i made same circuit before and it worked with no problem. but this time it is different.

the only difference is; i clear MCLR fuse (NOMCLR) and and reset button, can this configuration cause all this?
 

I use CCS compilers, I have not had to put delays in between getc and putc. In fact the standard RS 232 test program give below works perfectly without delays.

void main(){

do{
putc(getc()+1);

}while(TRUE);
}

Are you sure the fuse settings and the #use rs232 declaration is correctly setup ??
 

I guess most contributors to this thread didn't realize that the code in post #1 uses a software UART (by defining non-UART pins A0 and A1 as RX and TX).

By it's operation principle, a software UART can only work under very restricted conditions. Using it for quasi simultaneous receive and transmit as in the post #1 example is just a NO-NO.
 

I guess most contributors to this thread didn't realize that the code in post #1 uses a software UART (by defining non-UART pins A0 and A1 as RX and TX).

By it's operation principle, a software UART can only work under very restricted conditions. Using it for quasi simultaneous receive and transmit as in the post #1 example is just a NO-NO.

hi, you are right i am using software uart because i have to establish a 7 bit rs232 connection. this can only be done by using sw uart, pics hardware uart doesnt operate 7 bit connection well, it always add one bit and when i use parity bit the received value changes.

so nowi,software uart needs a little time between each character, i got my lesson thanks for your brilliant advice.
 

Depending on what you are doing, a software UART might not work at all, because it can't receive at new frame while it's sending.

Using a 8-Bit UART for 7-Bit frame is well possible, with the exception that the receiver would overflow if the peer is sending continuous data with no pause between characters. But in thus situation, a software UART will most likely neither work.
 

You could probably use the INT0 interrupt on the receive line (USART receive). Then use kbhit() to detect presence of data on the UART port, and then using getc() read the data. I used a similar setup on the 18F2620 mcu, it worked very well at 19200 bauds.
 

You could probably use the INT0 interrupt on the receive line (USART receive). Then use kbhit() to detect presence of data on the UART port, and then using getc() read the data. I used a similar setup on the 18F2620 mcu, it worked very well at 19200 bauds.
Yes, it can work. But not for simultaneous operation of software UART getc() and putc() when new data comes in while sending a character. Either getc() orputc() will fail in this case.

You can make your own software UART based on timer interrupts, but it's not provided by the CCS built-in functions.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top