Bon je reprend mon post^^
J'ai modifier 2-3 trucs dans mon code, notamment pour qu'il détecte la vitesse d'emmission. Cependant lors de la détection de la vitesse il me met à chaque fois baud not found. Je vois pas pourquoi.
Mon code modidié :
// C Source File
// Created 10/07/2007; 15:31:05
#include <tigcclib.h>
#define LINK_STATUS 0x60000C
#define LINK_PORT 0x60000E
#define CLK_L_DAT_L 0x0300
#define CLK_L_DAT_H 0x0200
#define CLK_H_DAT_H 0x0000
#define READ_DATA 0x0400
#define MAX_SERIAL_DATA 100
volatile int *pLinkPort = (int*) LINK_PORT;
int *pLinkStatus = (int*) LINK_STATUS;
int iOldPortStatus = 0;
unsigned BitPeriod1 = 0;
unsigned BitPeriod1Tx = 0;
unsigned BitPeriod15 = 0;
unsigned BaudRateIndex = 5;
unsigned BaudSense = 0;
unsigned char SerialDataTx[ MAX_SERIAL_DATA ] = "0";
unsigned BaudRateBreakPoints[ 9 ] = { 110, 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600 };
unsigned BaudSenseBreakPoints[ 9 ] = { 1659, 555, 164, 81, 39, 18, 7, 3, 0 };
int passage=0;
void DoDelay( unsigned DelayTime );
void SendSerialData( unsigned BytesToSend );
void CloseLinkPort();
void OpenLinkPort ();
float RoundFloat( float ValueToRound );
int SenseBaudRate( unsigned PeriodsToCompare );
void DrawParameters();
// Main Function
void _main(void)
{
//Open the COM port
OpenLinkPort();
*pLinkPort = CLK_L_DAT_H; // Setting Tx (white) low, inactive and
// setting Rx (red) line high so we can sense its state.
//Set a baudrate
SetBaudRate( BaudRateIndex );
//Print parameters
DrawParameters();
ClrScr();
//Print text
DrawStr( 0, 0, "Press a key for sensing...", A_NORMAL );
ngetchx();
ClrScr();
//Print text
DrawStr( 30, 0, "Sensing Baud ", A_REPLACE);
//Call the function to know the baud rate
if ( SenseBaudRate( 0x000A ) )
{
//Print an error text
DrawStr( 30, 0, "Baud Not Found", A_REPLACE);
}
else
{
//Print a text
DrawStr( 30, 0, "Done Sensing ", A_REPLACE);
passage++;
}
if(passage == 1)
{
//Print parameters
DrawParameters();
DrawStr( 30, 0, "Press a key to send a data", A_REPLACE);
ngetchx();
//Send a date to the COM port
SendSerialData('a');
CloseLinkPort();
}
ngetchx();
}
//Function : print the baudrate
void DrawParameters()
{
printf("Baud Rate = %5u", BaudRateBreakPoints[ BaudRateIndex ] );
}
//Function : open the COM port
void OpenLinkPort ()
{
printf("open COM port\n");
iOldPortStatus = *pLinkStatus;
*pLinkStatus = 0xFF00;
return;
}
//Function : close the COM port
void CloseLinkPort()
{
printf("Close COM port\n");
*pLinkPort = 0x0000;
*pLinkStatus = iOldPortStatus;
return;
}
//Function : send a data to the COM
void SendSerialData( unsigned BytesToSend )
{
printf("Send data\n");
unsigned ByteIndex, BitCycle;
unsigned char CurrentByte;
OSSetSR (0x0700); // Disable all interrupts, so our critical
// timing is pure and not affected
*pLinkPort = CLK_L_DAT_H;
for ( ByteIndex = 0; ByteIndex < BytesToSend; ByteIndex++ )
{
CurrentByte = SerialDataTx[ ByteIndex ];
if ( BytesToSend == 0xFFFF )
{
if ( CurrentByte == 0 )
{
break;
}
}
*pLinkPort = CLK_H_DAT_H; // Send Start Bit
DoDelay( BitPeriod1Tx );
for (BitCycle = 0; BitCycle < 8; BitCycle++)
{
if ( CurrentByte & ( 1 << BitCycle ) )
{
*pLinkPort = CLK_L_DAT_H;
}
else
{
*pLinkPort = CLK_H_DAT_H;
}
DoDelay( BitPeriod1Tx );
}
*pLinkPort = CLK_L_DAT_H;
DoDelay( BitPeriod1Tx ); // Send Stop Bit
}
OSSetSR (0x0000);
return;
}
void DoDelay( unsigned DelayTime )
{
int Temp;
while ( DelayTime-- )
{
Temp = *pLinkStatus;
}
return;
}
void SetBaudRate( unsigned BaudRateIndexToSet )
{
BaudRateIndex = BaudRateIndexToSet;
switch ( BaudRateIndexToSet )
{
case 0: // 110
BitPeriod1 = 11658;
BitPeriod15 = 17475;
BitPeriod1Tx = BitPeriod1;
break;
case 1: // 300
BitPeriod1 = 4259;
BitPeriod15 = 6397;
BitPeriod1Tx = BitPeriod1;
break;
case 2: // 1200
BitPeriod1 = 1048;
BitPeriod15 = 1577;
BitPeriod1Tx = BitPeriod1;
break;
case 3: // 2400
BitPeriod1 = 515;
BitPeriod15 = 778;
BitPeriod1Tx = BitPeriod1;
break;
case 4: // 4800
BitPeriod1 = 247;
BitPeriod15 = 362;
BitPeriod1Tx = BitPeriod1;
break;
case 5: // 9600
BitPeriod1 = 113;
BitPeriod15 = 162;
BitPeriod1Tx = BitPeriod1;
break;
case 6: // 19200
BitPeriod1 = 46;
BitPeriod15 = 76;
BitPeriod1Tx = 44;
break;
case 7: // 38400
BitPeriod1 = 13;
BitPeriod15 = 27;
BitPeriod1Tx = 11;
break;
case 8: // 57600
BitPeriod1 = 2;
BitPeriod15 = 10;
BitPeriod1Tx = 0;
break;
}
return;
}
int SenseBaudRate( unsigned PeriodsToCompare )
{
unsigned ActiveTime, InactiveTime, BaudIndex, BitTimeMin = 0xFFFF;
float fBitPeriod1, fBitPeriod15, fBaudSense;
OSSetSR (0x0700); // Disable all interrupts, so our critical
// timing is pure and not affected
*pLinkPort = CLK_L_DAT_H;
while ( --PeriodsToCompare > 0 )
{
if ( (*pLinkPort & READ_DATA) == 0 ) // Serial port going high (+V) (Start Bit)
{
ActiveTime = 0;
while ( (*pLinkPort & READ_DATA) == 0 )
{
++ActiveTime;
if ( ActiveTime == 0xFFFF )
{
break;
}
}
if ( ActiveTime < BitTimeMin )
{
BitTimeMin = ActiveTime;
}
}
if ( *pLinkPort & READ_DATA ) // Serial port going low (-V)
{
InactiveTime = 0;
while ( *pLinkPort & READ_DATA )
{
++InactiveTime;
if ( InactiveTime == 0xFFFF )
{
break;
}
}
if ( InactiveTime < BitTimeMin )
{
BitTimeMin = InactiveTime;
}
}
}
if ( BitTimeMin == 0xFFFF ) // No valid sense has occurred
{
OSSetSR (0x0000); // Enable Interrupts
return 1;
}
BaudSense = BitTimeMin;
fBaudSense = (float) BitTimeMin;
for ( BaudIndex = 0; BaudIndex <= 8; BaudIndex++ )
{
if ( BaudSense > BaudSenseBreakPoints[ BaudIndex ] )
{
break;
}
}
BaudRateIndex = BaudIndex;
// Use a linear interpolation on Bit Periods based off of BaudSense
//
fBitPeriod1 = ( fBaudSense * 4.8051 ) - 8.7589;
fBitPeriod15 = ( fBaudSense * 7.2 ) - 6.8;
BitPeriod1 = (int) RoundFloat( fBitPeriod1 );
BitPeriod15 = (int) RoundFloat( fBitPeriod15 );
switch ( BaudRateBreakPoints[ BaudRateIndex ] )
{
// Patch in the bit periods for the fastest speeds. They cannot be calculated
// accurately because BaudSense has little resolution at these speeds.
// These speeds will not "self correct" to battery voltage and clock offset.
case 57600:
BitPeriod1 = 2;
BitPeriod15 = 10;
BitPeriod1Tx = 0;
break;
case 38400:
BitPeriod1 = 13;
BitPeriod15 = 27;
BitPeriod1Tx = 11;
break;
case 19200:
BitPeriod1Tx = 44;
break;
default:
BitPeriod1Tx = BitPeriod1;
break;
}
OSSetSR (0x0000); // Enable Interrupts
return 0;
}
float RoundFloat( float ValueToRound )
{
float Frac, RoundedValue, *pTempCrap = &RoundedValue;
Frac = modf( ValueToRound, pTempCrap );
if ( Frac >= 0 )
{
if ( Frac > 0.5 )
{
RoundedValue = ceil( ValueToRound );
}
else
{
RoundedValue = floor( ValueToRound );
}
}
else
{
if ( Frac < -0.5 )
{
RoundedValue = floor( ValueToRound );
}
else
{
RoundedValue = ceil( ValueToRound );
}
}
return ( RoundedValue );
}