📄 sci.c
字号:
/*waits for the completion of the transmission*/
PtrtoString++; /*Moves the pointer to the next location*/
}
#endif
#ifdef SCI_ITDRV_WITHOUTBUF_TX
PtrToSCIBuffTx = PtrtoString; /*Copies the adress of the user string into
the global variable*/
SCI_Tx_String = 1; /*copies default value to recognize string
transmission in the interrupt subroutine*/
SCI_Tx_Buff = 0;
temp = SCISR;
SCIDR = *PtrToSCIBuffTx;/*transmits first character to generate interrupt*/
SCICR2 |= (unsigned char)TDRE; //Enable INterrupt
#endif
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_9thBit_TxRx
INPUT : TRUE or FALSE
OUTPUT : TRUE or FALSE
DESCRIPTION : Writes the 9th bit to be transmitted in the SCICR1 register
and returns the 9th bit received
COMMENTS : 9 bit mode must be enabled.
-----------------------------------------------------------------------------*/
BOOL SCI_9thBit_TxRx(BOOL Bit9_Val)
{
if (Bit9_Val == TRUE) /*If 9th bit is to be transmittes as 1*/
{
SCICR1 |= 0x40;
}
else
{
SCICR1 &= 0xBF;
}
if (SCICR1 & 0x80) /*If the 9th bit is received as 1*/
{
return (TRUE);
}
else
{
return (FALSE);
}
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_GetByte
INPUT : None
OUTPUT : Received Data
DESCRIPTION : Retunrs a data received to the user
COMMENTS : None
-----------------------------------------------------------------------------*/
unsigned char SCI_GetByte(void)
{
#if defined SCI_POLLING_RX
return(SCIDR); /*Returns the current value of the Data
register to the user*/
#else
#if defined SCI_ITDRV_WITHOUTBUF_RX
SCI_Error = 0x00;
return(SCI_MY_DATA);
#else
return (SCIDR);
#endif
#endif
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_GetBuffer
INPUT : *PtrtoBuffer,NbOfBytes
OUTPUT : Error Status
DESCRIPTION : Receives a continuous data,stores it into the user buffer and
returns the error status to the user
COMMENTS : Reception stops as soon as an error occurs
-----------------------------------------------------------------------------*/
#ifdef SCI_POLLING_RX
SCI_RxError_t SCI_GetBuffer(unsigned char *PtrtoBuffer,unsigned char NbOfBytes)
{
unsigned char Temp = 0x00 ;
for (;((NbOfBytes > 0)&& (!(Temp))); --NbOfBytes)
/*Checks if all the bytes are transmitted*/
{
Temp |= (unsigned char)(SCISR & SCI_ErrValue);
while (!(SCISR & SCISR_RDRF_OR));
Temp |= (unsigned char)(SCISR & SCI_ErrValue);
/*waits for the next received byte*/
*PtrtoBuffer = SCIDR;
/*copies the received byte into the user buffer*/
PtrtoBuffer++; /*Moves the pointer to the next location*/
}
return(Temp);
}
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_GetString
INPUT : *PtrtoString
OUTPUT : Error Status
DESCRIPTION : Receives a string,Stores it in the user buffer and returns
the error status to the user
COMMENTS : Reception stops as soon as an error occurs
-----------------------------------------------------------------------------*/
#ifdef SCI_POLLING_RX
SCI_RxError_t SCI_GetString(unsigned char *PtrtoString)
{
unsigned char Temp = 0x00, Val = 0xff;
for (;((Val != '\0')&&(Temp == 0));PtrtoString++)
{
Temp |= (unsigned char)(SCISR & SCI_ErrValue);
while (!(SCISR & SCISR_RDRF_OR)); /*waits for the received byte*/
Temp |= (unsigned char)(SCISR & SCI_ErrValue);
/*Temp stores any error that occurred during the reception*/
*PtrtoString = SCIDR;
/*Copies the received character into the user string*/
Val = *PtrtoString;
}
return(Temp); /*Returns the error status to the user*/
}
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_GetBuffer
INPUT : *PtrtoBuffer, NbOfBytes
OUTPUT : None
DESCRIPTION : Starts the reception in the interrupt subroutine
COMMENTS : Any data received before calling this function is ignored
-----------------------------------------------------------------------------*/
#ifdef SCI_ITDRV_WITHOUTBUF_RX
void SCI_GetBuffer(unsigned char *PtrtoBuffer,unsigned char NbOfBytes)
{
PtrToSCIBuffRx = PtrtoBuffer;/*Copies user buffer to the global variable */
SCI_Rx_Buff = NbOfBytes;
SCI_Rx_Buff++;
SCI_Error = 0x00;
SCICR2 |= (unsigned char)RDRF; /*Enable Interrupt*/
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_GetString
INPUT : *PtrtoString
OUTPUT : None
DESCRIPTION : Starts the reception of a string in the interrupt subroutine
COMMENTS : Any data received before calling this function is ignored
-----------------------------------------------------------------------------*/
void SCI_GetString(unsigned char *PtrtoString)
{
PtrToSCIBuffRx = (PtrtoString); /*Copies the user buffer to global buffer*/
SCI_Rx_String = 1; /*For String recognition in ISR*/
SCI_Error = 0x00;
SCICR2 |= (unsigned char)RDRF; /*Enable Interrupt*/
}
#endif
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_IsReceptionCompleted
INPUT : None
OUTPUT : Error Status
DESCRIPTION : Returns the error status occurred during the reception in the
interrupt subroutine if the reception is completed.
COMMENTS : Must be called after SCI_GetBuffer and SCI_GetString in
Interrupt Driven mode to get the reception status.
Must be called before SCI_GetByte in interrupt Driven mode.
-----------------------------------------------------------------------------*/
SCI_RxError_t SCI_IsReceptionCompleted (void)
{
#if defined SCI_ITDRV_WITHOUTBUF_RX
SCI_Pol_Stat = 0;
if ((SCI_Rx_String == 1) && (*(PtrToSCIBuffRx) != '\0') && (SCI_Error == 0))
/*String reception request undergoing*/
{
return(SCI_STRING_ONGOING);
}
else if ((SCI_Error == 0) && (SCI_Rx_Buff > 1))
/*Buffer Reception request undergoing*/
{
return(SCI_BUFFER_ONGOING) ;
}
else if ((SCI_Rx_Buff == 0) && (SCI_Rx_String == 0) && (SCI_Pol_Stat == 0))
/*Single Byte not received*/
{
return (SCI_RX_DATA_EMPTY);
}
else /*Reception is completed*/
{
SCI_Pol_Stat = 0;
SCI_Rx_Buff = 0;
SCI_Rx_String = 0;
return (SCI_Error);
}
#else
#if defined SCI_POLLING_RX
if(SCISR & SCISR_RDRF_OR)
{
return ((unsigned char)(SCISR & SCI_ErrValue));
}
else
{
return (SCI_RX_DATA_EMPTY); /*Returns the error status to the user*/
}
#else
return (SCI_RX_DATA_EMPTY); /*Dummy return statement*/
#endif
#endif
}
/*-----------------------------------------------------------------------------
ROUTINE NAME : SCI_IT_Function
INPUT : None
OUTPUT : None
DESCRIPTION : Carries out communiction in Interrupt driven mode.
COMMENTS : This function must be called inside the SCI interrupt service
routine for Interrupt driven communication
-----------------------------------------------------------------------------*/
void SCI_IT_Function (void)
{
/******************************************************************************
Reception in Interrupt Driven Without Buffer mode
******************************************************************************/
#ifdef SCI_ITDRV_WITHOUTBUF_RX
unsigned char temp;
if(SCISR & ORE) /*If Interrupt is generated due to overrun error*/
{
SCI_Error = (unsigned char)(SCISR & SCI_ErrValue);
temp = SCIDR;
SCI_Pol_Stat = 2;
}
else if (SCISR & RDRF)
{
if((SCI_Rx_String == 1) && (*PtrToSCIBuffRx != '\0') && (SCI_Error == 0))
/*If String Reception*/
{
SCI_Error = (unsigned char)(SCISR & SCI_ErrValue);
*PtrToSCIBuffRx = SCIDR;
if (*PtrToSCIBuffRx != '\0')
{
PtrToSCIBuffRx++;
}
else
{
SCICR2 &= (unsigned char)(~RDRF); //Disableinterrupt
}
SCI_Pol_Stat = 2;
}
else if ((SCI_Rx_Buff > 1) && (SCI_Error == 0))
{
SCI_Error = (unsigned char)(SCISR & SCI_ErrValue) ;
*PtrToSCIBuffRx = SCIDR;
SCI_Rx_Buff--;
SCI_Pol_Stat = 2; /*If a buffer is received*/
PtrToSCIBuffRx++;
if(SCI_Rx_Buff == 1)
{
SCICR2 &= (unsigned char)(~RDRF); //Disableinterrupt
}
}
else if ((SCI_Rx_Buff == 0) && (SCI_Rx_String == 0))
/*If interrupt is generated before calling SCI_GetBuffer/SCI_GetString*/
{
SCI_Error = (unsigned char)(SCISR & SCI_ErrValue) ;
SCI_MY_DATA = SCIDR;
SCI_Pol_Stat = 1;
}
else
{
SCI_Pol_Stat = 1;
temp = SCIDR;
}
}
#endif
/******************************************************************************
Transmission in Interrupt Driven without Buffer mode
******************************************************************************/
#ifdef SCI_ITDRV_WITHOUTBUF_TX
if((SCISR & TC)&& (SCICR2 & TC))/*Checks if transmit complete flag is set*/
{
if ((SCI_Tx_String == 1) && (*PtrToSCIBuffTx != '\0'))
/*Checks if complete string is transmitted*/
{
PtrToSCIBuffTx++;
SCIDR = *PtrToSCIBuffTx;
if (*PtrToSCIBuffTx == '\0')
{
SCICR2 &= (unsigned char)(~TC);
/*Disable IT if complete string is transmitted*/
}
}
else if (SCI_Tx_Buff > 1) /*Checks if all the bytes are transmitted*/
{
SCI_Tx_Buff--;
PtrToSCIBuffTx++; /*Moves the pointer to next location*/
SCIDR = *PtrToSCIBuffTx; /*Transmits a single byte*/
}
else if(SCI_Tx_Buff == 1)
{
SCI_Tx_Buff--;
SCICR2 &= (unsigned char)(~TC);
/*Disable IT if complete string is transmitted*/
}
}
else if ((SCISR & TDRE)&&(SCICR2 & TDRE)) /*Checks if TDRE Flag is set*/
{
if ((SCI_Tx_String == 1)&&(*PtrToSCIBuffTx != '\0')) /*Checks if complete
string is transmitted*/
{
PtrToSCIBuffTx++;
SCIDR = *PtrToSCIBuffTx;
if (*PtrToSCIBuffTx == '\0')
{
Nop();
SCICR2 &= (unsigned char)(~TDRE);
/*Disable IT if complete string is transmitted*/
Nop();
}
}
else if (SCI_Tx_Buff > 1) /*Checks if all the bytes are transmitted*/
{
SCI_Tx_Buff--;
PtrToSCIBuffTx++; /*Moves the pointer to next location*/
SCIDR = *PtrToSCIBuffTx; /*Transmits a single byte*/
}
else if(SCI_Tx_Buff == 1)
{
SCI_Tx_Buff--;
SCICR2 &= (unsigned char)(~TDRE);
/*Disable IT if complete string is transmitted*/
}
}
#endif
}
/**** (c) 2002 STMicroelectronics *************************** END OF FILE **/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -