CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Baudrate Selection Routine

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ezequiel
Guest







Baudrate Selection Routine
PostPosted: Fri Feb 07, 2003 5:57 pm     Reply with quote

Hello,
I have a program where I need to establish a comm. with a ECU (Electronic Control Unit), this unit can operate at 9600bps or 10400bps. But to start the comm. I have to send a command at 5Bps. Fortunately I can do this using Output_low and Output_High and a Delay of 200mS. Then the ECU reply with 0x55 (a good Sync Byte, but I don't know how to use it) and two Data Bytes at 9600bps or 10400bps 8Bits No Parity Inverted. The problem is that I don't know at which baudrate the ECU is going to use. I try to modify the timed_getc() routine (which appears on a FAQ in the website) with no success. Another problem is that the rest of the program has to communicate also at this baudrate (to send and receive).
I tried to use the HUSART and Set_Usart_Speed (To change between 9600bps and 10400bps) but then I cannot send the command at 5Bps using Output_low and Output_High, at I cannot set the USART to 5Bps.

I find two possible solutions.
A) Send the command (Using any pin but not the USART) at 5Bps, and look in the SyncByte (0x55) to decide which baudrate to use.
B) Send the command (Using any pin but not the USART) at 5Bps, wait for 1.5Sec the SyncByte (0x55) at 9600bps, If it comes the rest of the program has to use this baudrate. If not, change to 10400bps, re send the command, and wait for 1.5Sec the Sync Byte at 10400bps. If it comes the rest of the program has to use this baudrate, if not there is no ECU to communicate with, and goto the beginning of the program.

I would like to know if it's possible to do this, which option is more easy to implement, and if somebody can help me with this little routine.

Here I send you the code (which is very short), it's unfinished but you can see which micro I'm using and so on.

Thank you very much,
Ezequiel

#include <16F76.h>
#device *=16
#use delay(clock=20000000)
#fuses HS,NOWDT

#use RS232(baud=9600,xmit=PIN_C5,rcv=PIN_C4,Invert,Stream=ECU)

#include <LCD.C>

Unsigned Char SyncByte,KW1,KW2,ACKByte;
Unsigned Char BlkIndex,Line,Command; //Protocol Header
Unsigned Char ECUID[20];

#define TXPin PIN_C5 //Used to send Command at 5Bps


void main() {
//Configuracion de perifecicos Integrados
setup_adc_ports(NO_ANALOGS);
setup_spi(FALSE);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
delay_ms(750);
//Inicia LCD
lcd_init();

//Start Up Screen
output_low(TXPin);
output_low(Driver);
delay_ms(750);
lcd_putc('\f');
lcd_putc("ECU Test");
lcd_putc('\n');
lcd_putc("E.L. Aceto");
delay_ms(2000);

//Here I send command (0x41) at 5Bps 7bits with Parity
output_low (TXPin);
output_high(Driver);
delay_ms(750);
//Este es el comando
output_high (TXPin);
delay_ms(200);
output_low (TXPin);
delay_ms(200);
output_high (TXPin);
delay_ms(1000);
output_low (TXPin);
delay_ms(400);

//Now I have to rcv 0x55 and two data bytes at 9600bps or 10400bps
//Here it goes the rutine that implement the baudrate detection

}
___________________________
This message was ported from CCS's old forum
Original Post ID: 11417
PCM programmer



Joined: 06 Sep 2003
Posts: 20419

View user's profile Send private message

Re: Baudrate Selection Rutine
PostPosted: Fri Feb 07, 2003 6:45 pm     Reply with quote

:=Hello,
:= I have a program where I need to stablish a comm. with a ECU (Electronic Control Unit), this unit can operate at 9600bps or 10400bps. But to start the comm. I have to send a command at 5Bps. Fortunately I can do this using Output_low and Output_High and a Delay of 200mS. Then the ECU reply with 0x55 (a good Sync Byte, but I don't know how to use it) and two Data Bytes at 9600bps or 10400bps 8Bits No Parity Inverted. The problem is that I don't know at which baudrate the ECU is going to use.
-------------------------------------------------------------
The 16F76 has a hardware USART. For some reason you are not
using it. I guess the reason is because you need to send
the initial byte at 5 BPS. But if did use the USART, then
you can detect if you get a framing error. If you get one,
then switch to the other baud rate.


The following code shows the basic idea. I don't guarantee
this exact will work. For example, I don't know if the
set_uart_speed() function will actually set it for 10400 bps
because I never use that speed. Also, possibly you should
compare the received sync byte to 0x55. That may be a
sufficient test.

To use this method, you need to receive chars on PIN C7,
but transmit them on pin C5 (which is a non-USART pin).
You could set them up as different streams, perhaps.

These are just some ideas. There may be a way to do it
when using the software usart for receiving. I always
use the hardware usart, so that's why I'm giving you this
method. Other people on this forum may suggest a way to
do it with the software usart.


// Use the Hardware USART, and use the ERRORS directive.
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

<PRE>
main()
{
char c;


send_5BPS_byte();

c = getc(); // Wait for sync byte

if(bit_test(rs232_errors, FERR_BIT))
{
reset_uart(); // Reset port before changing baudrate
set_uart_speed(10400);
}

// etc.
}

//--------------------
// FUNCTIONS
void reset_uart(void)
{
bit_clear(RCSTA, SPEN_BIT); // Reset the serial port
delay_ms(1);
bit_set(RCSTA, SPEN_BIT);
delay_ms(1);
}

</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 11418
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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