📄 serial.c
字号:
/*
* Copyright (c) 2002, Jan Szymanski.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jan Szymanski.
* 4. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*********************************************************/
/* Name : putchar_uart0 */
/* Parameters : character to be sent */
/* Returns : none */
/* Scope : */
/* Function : sends character through UART0 */
/*-------------------------------------------------------*/
void putchar_uart0(unsigned char c)
{
while ((IFG1 & 0x80)!= 0x80){}; /* wait for TX to complete */
U0TXBUF = c;
}
/*-------------------------------------------------------*/
/* End of Function: putchar_UART0 */
/*********************************************************/
/*********************************************************/
/* Name : putstring_uart0 */
/* Parameters : pointer to string to be sent */
/* Returns : none */
/* Scope : */
/* Function : sends a string through UART0 */
/*-------------------------------------------------------*/
void putstring_uart0(unsigned char *str)
{
while (*str !=0x00)
putchar_uart0(*str++);
}
/*-------------------------------------------------------*/
/* End of Function: putstring_UART0 */
/*********************************************************/
/*********************************************************/
/* Name : getchar_uart0 */
/* Parameters : none */
/* Returns : character received from serial port */
/* Scope : */
/* Function : */
/*-------------------------------------------------------*/
unsigned char getchar_uart0(void)
{
unsigned char c;
if(prd_uart0==pwr_uart0) return(0); /* ??? */
c = *prd_uart0++; /* read and increment read pointer */
if (prd_uart0 >= &buffer_rxuart0[sizeb0])
prd_uart0 = buffer_rxuart0; /* circular buffer */
return (c);
}
/*-------------------------------------------------------*/
/* End of Function: putstring_UART0 */
/*********************************************************/
/*********************************************************/
/* Name : ischar_uart0 */
/* Parameters : none */
/* Returns : FALSE or TRUE */
/* Scope : */
/* Function : check if character available from */
/* serial port */
/*-------------------------------------------------------*/
unsigned char ischar_uart0(void)
{
if (prd_uart0==pwr_uart0) return FALSE;
else
return TRUE;
}
/*-------------------------------------------------------*/
/* End of Function: ischar_UART0 */
/*********************************************************/
/*--------------------------------------------------------*/
unsigned char low_nibble(unsigned char c)
{
c&=0x0f;
c|=0x30;
if (c>'9') c=c+('A'-'9'-1);
return (c);
}
/*--------------------------------------------------------*/
unsigned char high_nibble(unsigned char c)
{
return (low_nibble(c>>4));
}
/*--------------------------------------------------------*/
void hexout_uart0(unsigned char c)
{
putchar_uart0(high_nibble(c));
putchar_uart0(low_nibble(c));
}
/*--------------------------------------------------------*/
void hexout_uart0_int(unsigned int i)
{
unsigned int j;
j=i;
j>>=8;
hexout_uart0(j);
hexout_uart0(i);
}
/*-------------------------------------------------------*/
void hexout_uart0_long(unsigned long i)
{
unsigned long j;
unsigned char a,b,c;
j=i;
j>>=8;
c=j;
j>>=8;
b=j;
j>>=8;
a=j;
hexout_uart0(a);
hexout_uart0(b);
hexout_uart0(c);
hexout_uart0(i);
}
/*-------------------------------------------------------*/
/*********************************************************/
/* Name : putchar_uart1 */
/* Parameters : character to be sent */
/* Returns : none */
/* Scope : */
/* Function : sends character through UART1 */
/*-------------------------------------------------------*/
void putchar_uart1(unsigned char c)
{
unsigned char dummy;
unsigned int i;
//P5OUT &= 0xef; // 1110 1111 P54=BIT_EN (BSYNC)
while ((IFG2 & 0x20)!= 0x20){}; /* wait for TX to complete */
P5OUT |= 0x10; // 0001 0000 P54=BIT_EN (BSYNC)
U1TXBUF = c;
//IFG2&=0xdf;
//while ((IFG2 & 0x10)!= 0x10){};
//IFG2&=0xef;
dummy = U1RXBUF;
U1RCTL=0x00; /* 00000000 */
/* some delay here */
//for(i=0;i<4;i++){}
//P5OUT &= 0xef; // 1110 1111 P54=BIT_EN (BSYNC)
}
/*-------------------------------------------------------*/
/* End of Function: putchar_UART0 */
/*********************************************************/
/* END OF FILE SERIAL.C */
/*********************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -