|
|
View previous topic :: View next topic |
Author |
Message |
deltatech
Joined: 22 Apr 2006 Posts: 87
|
Can PIC receive Serial data with 2 stop bits ? |
Posted: Mon Jun 26, 2006 8:47 am |
|
|
I was wondering if it is possible to receive serial data with 2 stop bits with a PIC .
I need to set 300 Baud, Parity Even , 2 Stop bits , 7 Bits Data .
I am told the second stop Bit couldn’t matter to the PIC as it will consider it as Extra Idle Bit .
But Its Not working ,
I get the correct data on the HyperTerminal
But Rubbish when I connect the PIC
This is how I have set up the use parameters
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=300,parity=e,bits=7,xmit=PIN_C6, rcv=PIN_C7)
This is what I am getting on the Monitor
Any advice will be appreciated
Code: |
\0A06/26/2006 15:29:48.359 --> Buffered data => ||w~?~\0D
\0A06/26/2006 15:29:50.328 --> Buffered data => \0D
\0A06/26/2006 15:29:52.062 --> Buffered data => s??V~\0D
\0A06/26/2006 15:29:53.859 --> Buffered data => ~qwffsv\0D
\0A06/26/2006 15:29:55.718 --> Buffered data => ?~N?vw|f}\0D
\0A06/26/2006 15:29:57.656 --> Buffered data => wf|g~w~y?~\0D
\0A06/26/2006 15:29:59.640 --> Buffered data => ?O{}^|~\0D
\0A06/26/2006 15:30:01.640 --> Buffered data => ~~?~g_wvwxv_~w\0D
\0A06/26/2006 15:30:03.765 --> Buffered data => Go~v{p~~~\0D
\0A06/26/2006 15:30:05.703 --> Buffered data => ~_|Y~|vw\0D
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: Can PIC receive Serial data with 2 stop bits ? |
Posted: Mon Jun 26, 2006 9:02 am |
|
|
deltatech wrote: | I was wondering if it is possible to receive serial data with 2 stop bits with a PIC .
I need to set 300 Baud, Parity Even , 2 Stop bits , 7 Bits Data .
I am told the second stop Bit couldn’t matter to the PIC as it will consider it as Extra Idle Bit .
But Its Not working ,
I get the correct data on the HyperTerminal
But Rubbish when I connect the PIC
This is how I have set up the use parameters
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=300,parity=e,bits=7,xmit=PIN_C6, rcv=PIN_C7)
This is what I am getting on the Monitor
Any advice will be appreciated
Code: |
\0A06/26/2006 15:29:48.359 --> Buffered data => ||w~?~\0D
\0A06/26/2006 15:29:50.328 --> Buffered data => \0D
\0A06/26/2006 15:29:52.062 --> Buffered data => s??V~\0D
\0A06/26/2006 15:29:53.859 --> Buffered data => ~qwffsv\0D
\0A06/26/2006 15:29:55.718 --> Buffered data => ?~N?vw|f}\0D
\0A06/26/2006 15:29:57.656 --> Buffered data => wf|g~w~y?~\0D
\0A06/26/2006 15:29:59.640 --> Buffered data => ?O{}^|~\0D
\0A06/26/2006 15:30:01.640 --> Buffered data => ~~?~g_wvwxv_~w\0D
\0A06/26/2006 15:30:03.765 --> Buffered data => Go~v{p~~~\0D
\0A06/26/2006 15:30:05.703 --> Buffered data => ~_|Y~|vw\0D
|
|
Yes it can and you are correct about it not mattering. What you failed to show us is the code you are using to print out the rubbish. I suspect that the printing code is wrong. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 12:29 pm |
|
|
2 stop bits are not a problem. However, the 7 data bits using the hardware is a problem. The hardware will transmit only 8 or 9 data bits. I am not sure if CCS handles parity correctly. I did a test with the code that you sent me. Here is some working code. You will need to adjust the baud and freq.
Code: | #include <18F252.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=1200,parity=n,bits=8,xmit=PIN_C6, rcv=PIN_C7)
#define BUFFER_SIZE 30
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void serial_isr()
{
int t;
// Get rid of the parity. You may want to check it. This code doesn't!
buffer[next_in]=(getc()&0x7F);
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void putc_parity(char c)
{
int i;
int count = 0;
for(i=0;i<7;i++)
{
if (bit_test(c,i))
count++;
}
if (bit_test(count,0))
bit_set(c,7);
putc(c);
}
void main() {
enable_interrupts(global);
enable_interrupts(int_rda);
printf(putc_parity,"\r\n\Running...\r\n");
// The program will delay for 10 seconds and then display
// any data that came in during the 10 second delay
do {
delay_ms(10000);
printf(putc_parity,"\r\nBuffered data => ");
while(bkbhit)
putc_parity( bgetc() );
} while (TRUE);
}
|
|
|
|
deltatech
Joined: 22 Apr 2006 Posts: 87
|
|
Posted: Mon Jun 26, 2006 1:49 pm |
|
|
No I am still geting Garbage with the code you sent me .
You say the hardware will only transmit 8 or 9 bits i dont want transmit i want to receive 7 bits .
How ddid you test it ? did you input serial data comming in once a second at 300 baud,7bits,even parity . on pin C6 ? 15 ascii chareters . like this .
145414220060623 |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Mon Jun 26, 2006 2:09 pm |
|
|
I think you can still use the hardware usart but you would have to set it up with 9 data bits 1 stop bit and no parity.
For recieve
Check bit 1 for the first stop bit
Check bit 2 for the parity
Bits 3 to 9 are the data bits
For transmit
Leftshift your 7 databits twice. (Make sure it is a long)
Calculate the parity and insert into bit 2
Clear bit 1 to simulate the first stop bit
The second stop bit will be sent automatically
Make sure you enable the LONG_DATA |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 2:21 pm |
|
|
deltatech wrote: | No I am still geting Garbage with the code you sent me .
You say the hardware will only transmit 8 or 9 bits i dont want transmit i want to receive 7 bits .
How ddid you test it ? did you input serial data comming in once a second at 300 baud,7bits,even parity . on pin C6 ? 15 ascii chareters . like this .
145414220060623 |
No! I only have a 10MHz xtal handy so I used that. That only allowed me to use 1200 baud as the slowest speed. 7 data bits plus 1 parity bit is 8 bits! The hardware must transmit 8 or 9 bits of data. I fooled this by setting up to send 8 data bits using the 8th as the parity bit. Look at the code! You will see what I did. To test this, I used Hyperterminal set up as 1200, 7, E, no handshake. I typed chars and those chars were repeated on the screen so the code definitely works, trust me . The receive comes in on C7 and not C6. |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Mon Jun 26, 2006 2:22 pm |
|
|
Mark wrote: | 2 stop bits are not a problem. However, the 7 data bits using the hardware is a problem. The hardware will transmit only 8 or 9 data bits. I am not sure if CCS handles parity correctly. I did a test with the code that you sent me. Here is some working code. You will need to adjust the baud and freq.
|
Would the second stop bit cause a problem? Could it start reception of the next byte since there is always a start bit? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 2:23 pm |
|
|
Eugeneo wrote: | I think you can still use the hardware usart but you would have to set it up with 9 data bits 1 stop bit and no parity.
For recieve
Check bit 1 for the first stop bit
Check bit 2 for the parity
Bits 3 to 9 are the data bits
For transmit
Leftshift your 7 databits twice. (Make sure it is a long)
Calculate the parity and insert into bit 2
Clear bit 1 to simulate the first stop bit
The second stop bit will be sent automatically
Make sure you enable the LONG_DATA |
Nope. The data is shifted out LSB first.
Last edited by Mark on Mon Jun 26, 2006 2:29 pm; edited 1 time in total |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 2:24 pm |
|
|
Eugeneo wrote: | Mark wrote: | 2 stop bits are not a problem. However, the 7 data bits using the hardware is a problem. The hardware will transmit only 8 or 9 data bits. I am not sure if CCS handles parity correctly. I did a test with the code that you sent me. Here is some working code. You will need to adjust the baud and freq.
|
Would the second stop bit cause a problem? Could it start reception of the next byte since there is always a start bit? |
Since a stop bit is high and a start bit is low, nope. |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Mon Jun 26, 2006 2:24 pm |
|
|
Why not? |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 2:27 pm |
|
|
Like I said, stop bit is HIGH! For the next byte to start, the signal would have to be low. Having an extra stop bit will only give the pic more time to process the byte. That's a good thing. If Deltatech will test the code I gave him with Hyperterminal, he'll see that it works or else he has a hardware problem. I did test the code with a PICDEM 2 PLUS board. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 26, 2006 2:30 pm |
|
|
I tested the program shown below with HyperTerminal Private Edition
and it worked OK. I could type keys on the PC keyboard and see them
echo'ed back from the PIC and displayed on the HyperTerminal window.
http://www.hilgraeve.com/htpe/download.html
I setup HyperTerminal for:
Connection: Direct connection to Com1
Bits per Second: 300
Data bits: 7
Parity: Even
Stop bits: 2
Flow Control: None
Advanced: No fifos enabled
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=300, xmit=PIN_C6, rcv=PIN_C7, ERRORS, PARITY=E, BITS=7)
//======================================
void main()
{
char c;
while(1)
{
c = getc(); // Wait for char from PC
putc(c); // Then send it back to PC
}
} |
|
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Mon Jun 26, 2006 2:31 pm |
|
|
Mark wrote: |
Like I said, stop bit is HIGH! For the next byte to start, the signal would have to be low. Having an extra stop bit will only give the pic more time to process the byte. That's a good thing. If Deltatech will test the code I gave him with Hyperterminal, he'll see that it works or else he has a hardware problem. I did test the code with a PICDEM 2 PLUS board. |
You're right.
Just trying to help |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Mon Jun 26, 2006 2:38 pm |
|
|
That makes sence. Brain fart.
Last edited by Eugeneo on Mon Jun 26, 2006 2:46 pm; edited 1 time in total |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 26, 2006 2:42 pm |
|
|
Hey PCM,
Try this out:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=300, xmit=PIN_C6, rcv=PIN_C7, ERRORS, PARITY=E, BITS=7)
//======================================
void main()
{
char c;
while(1)
{
printf("\r\n\Running...\r\n");
c = getc(); // Wait for char from PC
putc(c); // Then send it back to PC
}
} |
The printf doesn't appear to work. I tried these settings earlier but didn't have time to look at the lst file. Something isn't quite right with CCS's code so I just used 8 bits with no parity and generated my own parity bit and it works with Hyperterminal. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|