|
|
View previous topic :: View next topic |
Author |
Message |
jackie Guest
|
How can I switch between two RS232 baud rates? |
Posted: Mon Jan 10, 2005 10:08 am |
|
|
Hi, I am trying to collect serial data at 1200 baud and pass it out at 9600.
Switching between two different bauds (with #use rs232) does not work
Do i need two seperate RS232 controllers?
Do i have to use different pins?
What am i doing wrong?
Please HELP! |
|
|
Ttelmah Guest
|
Re: How can I switch between two RS232 baud rates? |
Posted: Mon Jan 10, 2005 10:18 am |
|
|
jackie wrote: | Hi, I am trying to collect serial data at 1200 baud and pass it out at 9600.
Switching between two different bauds (with #use rs232) does not work
Do i need two seperate RS232 controllers?
Do i have to use different pins?
What am i doing wrong?
Please HELP! |
The #use RS232 system can be used, but requires very careful layout and programming to make it work. Basically, the 'last encountered' #use statement, must be the one you require. For the hardware UART, it is simpler, and better to use 'set_uart_speed', which allows you to change the rate the UART is using.
If one system is using the hardware UART, and the other the software UART, then these can just be set in the #use RS232 commands, and the stream ability used to change speed.
Best Wishes |
|
|
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
Just use streams |
Posted: Mon Jan 10, 2005 1:01 pm |
|
|
This statement is not perfectly true.
//===========================
The #use RS232 system can be used, but requires very careful layout and programming to make it work. Basically, the 'last encountered' #use statement, must be the one you require.
//===========================
You can use streams to specify different speeds i.e.
#use rs232(baud=1200 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, stream=PORT_K_1200,RESTART_WDT)
#use rs232(baud=2400 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, stream=PORT_K_2400,RESTART_WDT)
#use rs232(baud=4800 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, stream=PORT_K_4800,RESTART_WDT)
You can then case on the desired speed i.e.
byte fgetcK2Speed_K() {
byte result;
switch (Protocol_Speed) {
case PS_1200:
result = fgetc(PORT_K_1200);
break;
case PS_2400:
result = fgetc(PORT_K_2400);
break;
case PS_4800:
result = fgetc(PORT_K_4800);
break;
case PS_9600:
result = fgetc(PORT_K_9600);
break;
}
return(result);
}
CCS will create appropriate code for ach stream |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Mon Jan 10, 2005 1:10 pm |
|
|
Will the stream trick work if the hardware UART is used? I would imagine it does work for the software UART code generated by the compiler.
Quick check of the July2003 manual says SET_UART_SPEED(baud,[stream]) is only good for the HARDWARE UART. _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Last edited by rwyoung on Mon Jan 10, 2005 3:46 pm; edited 1 time in total |
|
|
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
To answer the first question |
Posted: Mon Jan 10, 2005 1:33 pm |
|
|
You can use a hardware UART and a software UART. A software UART is just a bit of code that emulates a serial port. The CCS compiler generates the code automatically if it does not recognize the pins specified in your code to be the hardware UART pins.
The way I propose would be to use the hardware UART at 1200 BAUD. Then to use the receive interrupt to trigger the posting of the 9600 baud character.
// hardware uart
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS,stream=PORT_PC,RESTART_WDT) // PC Comm speed
// software uart
#use rs232(baud=1200 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, stream=PORT_K_1200,RESTART_WDT)
PS. I don't know if the stream trick will work for the hardware UART but I suppose that I you specify the stream, then the compiler is obliged to generate the appropriate code. I think that the last #USE RS232 applies only to the default (unspecified) stream. |
|
|
jackie Guest
|
thx! |
Posted: Tue Jan 11, 2005 11:03 am |
|
|
thanx guys! |
|
|
Ttelmah Guest
|
Re: Just use streams |
Posted: Tue Jan 11, 2005 11:15 am |
|
|
hillcraft wrote: | This statement is not perfectly true.
//===========================
The #use RS232 system can be used, but requires very careful layout and programming to make it work. Basically, the 'last encountered' #use statement, must be the one you require.
//===========================
You can use streams to specify different speeds i.e.
#use rs232(baud=1200 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, stream=PORT_K_1200,RESTART_WDT)
#use rs232(baud=2400 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, stream=PORT_K_2400,RESTART_WDT)
#use rs232(baud=4800 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, stream=PORT_K_4800,RESTART_WDT)
You can then case on the desired speed i.e.
byte fgetcK2Speed_K() {
byte result;
switch (Protocol_Speed) {
case PS_1200:
result = fgetc(PORT_K_1200);
break;
case PS_2400:
result = fgetc(PORT_K_2400);
break;
case PS_4800:
result = fgetc(PORT_K_4800);
break;
case PS_9600:
result = fgetc(PORT_K_9600);
break;
}
return(result);
}
CCS will create appropriate code for ach stream |
In what way is the statement about the last #use statement 'not true'?. This was used to change baud rates, long before the stream ability, and worked fine. The problem is that it is very difficult to work out how the program flow will actually 'encounter' the #use statements (they have to be embedded in the code for this to work). The behaviour can at times be quite 'perverse', but I still have code that was written before streams were added, which happily changes baud rates using this method... :-)
It is however almost impossible to use, if you have streams as well. This should though still work for the soft UART:
Code: |
byte fgetcK2Speed_K() {
byte result;
switch (Protocol_Speed) {
case PS_1200:
#use rs232(baud=1200 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, RESTART_WDT)
result = getc();
break;
case PS_2400:
#use rs232(baud=2400 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, RESTART_WDT)
result = getc();
break;
case PS_4800:
#use rs232(baud=4800 ,parity=N,xmit=PIN_K_TX,rcv=PIN_K_RX,bits=8,ERRORS, RESTART_WDT)
result = getc();
break;
}
return(result);
}
|
Best Wishes |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Jan 11, 2005 2:21 pm |
|
|
Has anyone tried just setting up the standard stream, as a default, then on the 9600 transmit just change the spbrg/brgh value with #byte. TRANSMIT, then change the spbrg/brgh back for 1200? Or is this also too slow? This may be just what set_uart_speed does. |
|
|
karth
Joined: 10 Sep 2003 Posts: 29
|
!!!!These works for me!!!! |
Posted: Tue Jan 11, 2005 2:41 pm |
|
|
set_uart_speed(115200);
delay_ms(10);
printf("baud rate set to 115200");
set_uart_speed(4800);
delay_ms(10);
printf("baud rate set to 4800");
good luck |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
Re: !!!!These works for me!!!! |
Posted: Tue Jan 11, 2005 4:57 pm |
|
|
karth wrote: | set_uart_speed(115200);
delay_ms(10);
printf("baud rate set to 115200");
set_uart_speed(4800);
delay_ms(10);
printf("baud rate set to 4800");
good luck |
Yes this will work for HARDWARE UARTs only as per the manual. But if you are using a software UART you need to change the speed via the #use RS232 "function" _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
jackie Guest
|
How can I switch between two RS232 baud rates? |
Posted: Thu Jan 13, 2005 6:47 am |
|
|
OK, I got the speed to switch, but it will not allow change of parity!
which method should i be using to do this?
Thanx again! |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: How can I switch between two RS232 baud rates? |
Posted: Mon Jan 17, 2005 3:41 pm |
|
|
jackie wrote: | OK, I got the speed to switch, but it will not allow change of parity!
which method should i be using to do this?
Thanx again! |
Declare 3 streams on the same pins each with different parity. Then use a switch statement to select the parity you wish to use and call the stream you want. There must be 3 difreent putC statements each with a different stream name. This is done because the hardware does not suport parity (it must be calculated in software). You also have to call the #use when you switch parity. It can be done but is not a simple task. |
|
|
Guest
|
Software UART overhead/latency/gotchas? |
Posted: Fri Feb 04, 2005 3:45 pm |
|
|
I would like to use streams as described in this topic. I'm curious if fputc() will block when writing to the software UART, and if not how much latency there might be. For example, if I want to echo bytes coming in on the hardware UART, can I just put fputc(c,SOFTUART) in the interrupt handler for the hardware UART receive? |
|
|
Guest
|
Re: Software UART overhead/latency/gotchas? |
Posted: Fri Feb 04, 2005 5:30 pm |
|
|
Also... how does kbhit() know which UART to check? Right now, my code uses the hardware UART and recieve is done via #INT_RDA and a while ( kbhit() ) {inByte = getc();} kind of thing. The manual says kbhit() must be called 10x the bit rate for software UARTs. It's not clear to me how both of these would work when using both software and hardware UARTs. |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Sat Feb 05, 2005 11:38 am |
|
|
Use the stream name as the argument in kbhit(). _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
|
|
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
|