View previous topic :: View next topic |
Author |
Message |
midoroi
Joined: 05 Apr 2013 Posts: 11
|
serial baud rate |
Posted: Fri May 10, 2013 6:25 am |
|
|
hello
i need to read in baud=1200 and write in baud=9600.
how i can do it.
i use rx433 and tx433 i have to use 1200 baud
but i have to connect the rx433 in the robot,and the serial port of the robot work in 9600. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Fri May 10, 2013 6:49 am |
|
|
Hi,
You'll need to use two serial ports, one at 1200 baud for the wireless module, and one at 9600 baud for the 'robot'.....
Most PICs have at least one hardware serial port. This port has an input buffer, and will trigger an interrupt on incoming character data with the right code. CCS also supports 'software serial ports', with somewhat more limited capability. You can define multiple software serial ports on your PIC with the CCS compiler.
Depending on the data transfer requirements of your project, one hardware serial port, and one software serial port may suffice. Otherwise, it may be better to select a PIC with two hardware serial ports.
John |
|
|
midoroi
Joined: 05 Apr 2013 Posts: 11
|
|
Posted: Fri May 10, 2013 7:02 am |
|
|
i use 16f876
and i wonna use c7 at the 1200 and the c6 at 9600 |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri May 10, 2013 8:16 am |
|
|
midoroi wrote: | i use 16f876
and i wonna use c7 at the 1200 and the c6 at 9600 |
I believe you will find this difficult.
From the data sheet c6 and c7 are the hardware serial pins.
It looks like both are driven from the one baudrate generator.
It's been suggested you use one hardware and one software serial port.
Since it's easier to do a TX than an RX in software, that dictates hardware port for the RX and software for the TX.
Someone here may know a clever way to use the c6 and c7 as you want. I don't, so I'd be using other pins for the software UART.
That's my twopence worth.
Mmike |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri May 10, 2013 8:16 am |
|
|
Quote: |
i use rx433 and tx433 |
ordinary,
intermittent RS-232 is KNOWN to be unreliable with those cheap modules.
beware....... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Fri May 10, 2013 8:58 am |
|
|
It is fairly easy.
You declare both pins as hardware RS232 using one stream, at the lower baud rate.
You declare a second stream using the TX pin only at the higher rate.
You turn off the bit TXEN in the UART.
Code: |
#USE RS232 (UART1,baud=1200,ERRORS,STREAM=RX)
#USE RS232 (XMIT=PIN_C7,baud=9600,STREAM=TX)
#bit TXEN=getenv("BIT:TXEN")
//in your main code
TXEN=FALSE;
|
Then transmit using fputc(c,TX), and receive using fgetc(RX).
The TX UART will be using software since it declares only one PIN.
The RX will use the hardware UART (and can even use interrupts).
Provided you never attempt to transmit to the RX stream, the compiler will happily allow this.
Best Wishes |
|
|
midoroi
Joined: 05 Apr 2013 Posts: 11
|
|
Posted: Fri May 10, 2013 1:34 pm |
|
|
can this work ?
Code: |
#use rs232(baud=1200,rcv=PIN_C7)
#use rs232(baud=9600,rcv=PIN_c6)
void main()
{
while (true)
{char x;
x=getc()
if (x='a')
putc(0x11)
else
putc(0x22)
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Fri May 10, 2013 2:20 pm |
|
|
Not really.
You will be using two software UART's, which means no receive buffering, and characters will be lost.
The code will only enable a hardware UART if you use both hardware pins. However having once done this _you_ can then turn off one of the pins and use just half the UART, and enable the software UART in the other direction.
This is what I show how to do. |
|
|
midoroi
Joined: 05 Apr 2013 Posts: 11
|
|
Posted: Fri May 10, 2013 3:16 pm |
|
|
My cards are ready
Maybe i have to modify it by a simple wire. |
|
|
midoroi
Joined: 05 Apr 2013 Posts: 11
|
|
Posted: Fri May 10, 2013 8:28 pm |
|
|
and this work ???
Code: |
void main()
{
#use rs232(baud=1200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
x=getc();
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
if (x='a')
putc(0x11);
else
putc(0x22);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19477
|
|
Posted: Sat May 11, 2013 12:24 am |
|
|
Not really. Problem is that anything that is received while the UART is reprogrammed to the transmission rate, will be corrupted/lost.
The point is that the hardware UART can handle just one speed.
Setup the hardware UART as the receive device - this is always the harder direction to deal with. Then understand that the hardware UART has separate enables for the transmit and receive 'sides'. TXEN, is the transmit enable. So having setup the hardware UART, you then turn off it's transmit side, and instead enable a software UART on this pin.
#use can't be used inline like this anyway. It is a preprocessor directive, not a program function. |
|
|
|