|
|
View previous topic :: View next topic |
Author |
Message |
trirath
Joined: 14 Jun 2010 Posts: 20 Location: Pathunthanee Thailand
|
7 Odd 1 Stop Bits |
Posted: Mon Jun 14, 2010 10:41 pm |
|
|
I tried to send some hex such 0xC1 or 0xF1 from PIC to PC
via RS-232: 7, Odd, 1 Stop bit.
It does not work. It look like lose 1 bits.
Example1:
Code: |
fputc(0xF1,Stream_1 ); // Note 0xF1 is 11110001
|
Result on Terminal is 0x71 - 01110001
Example2:
Code: |
fputc(0xC1,Stream_1 ); // Note 0xC1 is 11000001
|
Result on Terminal is 0x41 - 01000001
How can I do?
Sorry I am just new at PIC Programming. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Jun 15, 2010 1:59 am |
|
|
I think you mean you have your RS232 setup for 7bit, odd parity, one stop. If so, the 0xF1, won't fit... Nothing to do with the PIC.
7bit, implies what it says, sending just the low 7 bits, so hex values from 0x00 to 0x7F (binary 00000000 to 01111111). You can't send the eighth bit over a seven bit connection...
If you want to send the eighth bit, you need to have the PIC and the PC, both setup to use 8bit communication.
Best Wishes |
|
|
trirath
Joined: 14 Jun 2010 Posts: 20 Location: Pathunthanee Thailand
|
|
Posted: Wed Jun 16, 2010 8:31 am |
|
|
Many Thank , Please you give me an idea for using Pin B2 be input series port (RS232 Baud 10400 8n1)
I Found them in
http://www.elmelectronics.com/DSheets/ELM323DS.pdf
Pin B2 is in nput of series communications ,I concern they can do?
i tried to used other RB0 Be input(I think it easy by use RB0 Interrupt) of series port but it does work not perproly.Also I see ccs example SISR.C that It work properly but i can not modified to how to using function fprintf
really new in PIC
Thank you in advanced |
|
|
trirath
Joined: 14 Jun 2010 Posts: 20 Location: Pathunthanee Thailand
|
|
Posted: Wed Jun 16, 2010 9:04 am |
|
|
This is Starting code:
Code: |
#include <18F2550.h>
//configure a 20MHz crystal to operate at 48MHz
// #build(reset=0x1000, interrupt=0x1008)
// #ORG 0x0000,0x0FFF {}
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,BORV46,CPUDIV1,VREGEN
#use delay(clock=48000000)
// #use rs232(baud=9600 ,xmit=PIN_C6,rcv=PIN_C7,PARITY=n,BITS= 8,STOP=1,STREAM = OBD)
#use rs232(baud=9600,xmit=PIN_B1,rcv=PIN_B0,PARITY=n,BITS= 8,STOP=1,STREAM = OBD)
#include <lcd.c>
#include <string.h>
#include <input.c>
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
int8 msg_ready =0;
#INT_EXT
//#int_rda
void serial_isr() {
int t;
buffer[next_in]=getc();
next_in=(next_in+1) % BUFFER_SIZE;
buffer[next_in]= '\0';
msg_ready = 1;
}
void main() {
enable_interrupts(INT_EXT_H2L );
// enable_interrupts(int_rda);
enable_interrupts(global);
//printf(OBD,"\r\n\Running...\r\n");
while(true){
if(msg_ready){
// disable_interrupts(int_rda);
disable_interrupts(INT_EXT_H2L );
fprintf(OBD,"%s",buffer);
next_in=0;
msg_ready=0;
// enable_interrupts(int_rda);
enable_interrupts(INT_EXT_H2L );
}
}
}
|
I sent ABCDEFGHIJKLMNOP
Terminal Received :ACEGIKMO
It Lost 1 by 1
Remark: It work very well by # Int_rda
Please kindly assist
Thank You in advance |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Jun 16, 2010 9:57 am |
|
|
In the receive handler interrupt routine, after received an incoming char, the next byte in the
buffer is overwrited with a null char, that later it canĀ“t be viewed as an ASCII char.
Code: |
#INT_EXT
void serial_isr() {
int t;
buffer[next_in]=getc();
next_in=(next_in+1) % BUFFER_SIZE;
// buffer[next_in]= '\0'; >>>>>>> write a NULL here
msg_ready = 1;
}
|
Humberto |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jun 17, 2010 1:54 am |
|
|
Humberto he is correct in doing that to null terminate the string for printing. If another char comes in it overwrites the null and then places a null after this except he sets buffer_in to 0 after printing.
You also do not init your buffer as a null terminated string. You are relying on the memory to be zero on power up. Which is usually fine but is not guaranteed, especially after a soft restart.
buffer[0] = '\0'; before you enable interrupts is a good idea.
The problem most likely is that after a char comes in your main routine starts to print it, printing can take a lot of time in relation to other things so another char may come in just before you re-set buffer_in and the message flag to 0, this will mean you will loose the next char.
you infact dissable the interrupt preventing the next char from being stored. |
|
|
trirath
Joined: 14 Jun 2010 Posts: 20 Location: Pathunthanee Thailand
|
|
Posted: Thu Jun 17, 2010 7:36 am |
|
|
I remove already. But still does not work.
Thank you for suggestion. |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Jun 17, 2010 2:56 pm |
|
|
The point is that for a circular buffer implementation, the code in the interrupt routine expect
a stream of char, not a single char.
In such case a better option is just to copy the received char and leave the routine:
Code: |
int a;
#INT_EXT
void serial_isr() {
a=getc();
}
|
For a circular buffer implementation, msg_ready=1 should be TRUE once received the
expected string, NOT BEFORE.
msge_ready would becomes TRUE only:
- After received an expected end-of-string identifier char (EOM, CR, LF, etc)
- After a time-out window
- After received an x number of chars
Doing this, you can leave the interrupt and do another task without missing any incoming chars.
Humberto |
|
|
|
|
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
|