⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 PIC18F8722 uart c program
💻 C
字号:
#include "main.h" /* for generic defines and typedefs */
#include "uart.h" /* for typedefs, prototypes, defines */
#include <p18f8722.h>  /* for 'PORTC,G' and 'TRISC,G' */
#include <delays.h> /* for Delay10TCY() */
#include <stdarg.h>

/* for definitions, see the PIC18FXX20 data sheet, page 196 */
U8b port1Initialized = 0;
U8b port2Initialized = 0;
U16b portsReInitialized = 0;

uartPort uartInit(int port,int baudRate)
{
	int result = FAILURE;
	U8b temp;
	
	if (port == 1)
	{
		if (baudRate == 9600)
		{
			TXSTA1bits.BRGH = 0; /* clear the BRGH bit for slower baud rates */
			
			/* we have a 40 Mhz clock, so see table on page 197 of data sheet for 
			   baud rates for asynchronous mode */
			SPBRG1 = 51;
			
			result = SUCCESS;
		} 

		if (result == SUCCESS)
		{
			TXSTA1bits.SYNC = 0;/* clear the SYNC bit for asynchronous operation */
			
			TRISCbits.TRISC7 = 1; /* configure bit 7 (RX1) as an input */
			TRISCbits.TRISC6 = 0; /* configure bit 6 (TX1) as an output */
			
			RCSTA1bits.SPEN = 1; /* turn on serial port */			
			RCSTA1bits.CREN = 1; /* enable reception */
            port1Initialized = 1;
		}
		
	}
	else if (port == 2)
	{
		if (baudRate == 9600)
		{
			TXSTA2bits.BRGH = 0; /* clear the BRGH bit for slower baud rates */
			
			/* we have a 40 Mhz clock, so see table on page 197 of data sheet for 
			   baud rates for asynchronous mode */
			SPBRG2 = 51;

			result = SUCCESS;
		} 

		if (result == SUCCESS)
		{
			TXSTA2bits.SYNC = 0;/* clear the SYNC bit for asynchronous operation */

			TRISGbits.TRISG2 = 1; /* configure bit 7 (RX2) as an input */
			TRISGbits.TRISG1 = 0; /* configure bit 6 (TX2) as an output */
			
			RCSTA2bits.SPEN = 1; /* turn on serial port */			
			RCSTA2bits.CREN = 1; /* enable reception */
            port2Initialized = 1;
		}
	}

	return (result); /* not implemented yet */
}

int uartGetByte(uartPort port, char *byte)
{
	int result = FAILURE;
	
	if (port == 1)
	{
		if (PIR1bits.RCIF)
		{
			*byte = RCREG1;
			result = SUCCESS;
		}
		if (RCSTA1bits.OERR || RCSTA1bits.FERR)
		{			
			if (RCSTA1bits.OERR)
				printf("\r\nOverrun Serial port 1 error\r\n");
			else
				printf("\r\nFraming port 1 error\r\n");
            RCSTA1bits.CREN = 0; /* clear any errors */
			RCSTA1bits.CREN = 1; /* re-enable reception? */
			result = WARNING;
		}
	} 
	else if (port == 2)
	{
		if (PIR3bits.RC2IF)
		{
			*byte = RCREG2;
			result = SUCCESS;
		}
		if (RCSTA2bits.OERR || RCSTA2bits.FERR)
		{
			if (RCSTA2bits.OERR)
				printf("\r\nOverrun Serial port 2 error\r\n");
			else
				printf("\r\nFraming port 2 error\r\n");
			RCSTA2bits.CREN = 0; /* clear any errors */
			RCSTA2bits.CREN = 1; /* enable reception */
			result = WARNING;
		}
	}	
	
	return (result);
}

#if SIMULATOR
char CircularBuffer[25];
char *CBUpperBound=CircularBuffer+sizeof(CircularBuffer);
char *CBP = CircularBuffer;
void SimulatorOutByte(char byte)
{
	*CBP++ = byte;
	if (CBP>= (char *) CBUpperBound)
		CBP = (char *) CircularBuffer;
}
#endif /* SIMULATOR */

int uartSendByte(uartPort port, char byte)
{
	int result = FAILURE;
	U8b temp;
	U16b i;
	
	if (((port==1) && (port1Initialized != 1))
	   || ((port==2) && (port2Initialized != 1)))
	{
		portsReInitialized++;
		uartInit(port, 9600); /* Try again */
	}
	
	if ((port == 1) && (port1Initialized == 1))
	{
		/* check the TX1IF bit */
		for (i=0; TXSTA1bits.TRMT!=1; i++)
		{
			Delay10KTCYx(1); /* Delay 10K clocks = 250us */
			if (i>(4*UART_MSEC_TIMEOUT))
				break;
		}
		if (TXSTA1bits.TRMT==1) /* TXREG1 is empty, was PIR1bits.TXIF==0 */
		{
			TXSTA1bits.TXEN = 1;
			/* now write the byte */
			TXREG1 = byte;
#if SIMULATOR
			SimulatorOutByte(byte);
#endif /* SIMULATOR */
			result = SUCCESS;
		}
	} 
	else if ((port == 2) && (port2Initialized == 1))
	{
		/* check the TX2IF bit */
		for (i=0; TXSTA2bits.TRMT!=1; i++)
		{
			Delay10KTCYx(1); /* Delay 10K clocks = 250us */
			if (i>(4*UART_MSEC_TIMEOUT))
				break;
		}
		if (TXSTA2bits.TRMT==1) /* TXREG2 is empty was PIR3bits.TX2IF */
		{
			TXSTA2bits.TXEN = 1;
			/* now write the byte */
			TXREG2 = byte;
#if SIMULATOR
			SimulatorOutByte(byte);
#endif /* SIMULATOR */
			result = SUCCESS;
		}
	}
	
	return result;
}

int uartSend(uartPort port, char *message)
{
	int result = SUCCESS;
	
	for (;*message;message++)
	{
		result = uartSendByte(port,*message);
	}
	
	return result;
}

char nibble2ascii(U8b nibble)
{
	char c;
	
	if (nibble<10)
		c = '0'+nibble;
	else
		c = 'a'+(nibble-10);	
	
	return c;
}

char dec2ascii(U8b decimal)
{  
	char c;
	
	if (decimal<10)
		c = '0' + decimal;
	else if (decimal<16)
		c = 'A' + (decimal-10);
	else  	/* range checking is done elsewhere, and ... */
		c = '?';
		
	return c;	
}

int uartSendU32b(uartPort port, U32b ival)
{
	int result = SUCCESS;
	U8b i;
	char c;
	
	for (i=0;i<8;i++)
	{
		c=nibble2ascii((ival&0xF0000000)>>28);
		result = uartSendByte(port,c);
        if (result!=SUCCESS) break;
		ival<<=4;
	}
	return result;
}

int uartSendU8b(uartPort port, U8b ival)
{
	int result = SUCCESS;
	U8b i;
	char c;
	
	for (i=0;i<2;i++)
	{
		c=nibble2ascii((ival&0xF0)>>4);
		result = uartSendByte(port,c);
        if (result!=SUCCESS) break;
		ival<<=4;
	}
	return result;
}

int uartSendDec(uartPort port, U16b ival)
{
	int result = SUCCESS;
	U8b i;
	char c;
	
	for (i=0;i<3;i++)
	{
		c=nibble2ascii((ival/100)%10);
		result = uartSendByte(port,c);
        if (result!=SUCCESS) break;
		ival*=10;
	}
	return result;
}

int uartSimplePrintf2(const rom char *format, ... )
{
	int result = SUCCESS;
	#define va_start_const(ap,l) {(ap)=((void*) &(l))-(sizeof(l)*0);}
	#define va_arg_const(ap,t) ( *(t*) ((ap)=((t*)ap-1)) )
	va_list ap;	
	const rom char *p;
	char *str;
	U32b longWord;
	U8b byte;
	char c;
	U16b shortWord;
	uartPort port=2;
	char *max;
		
	va_start_const(ap,format); 
	max = format+256;
	for (p=format; (*p) && (p<max); p++)
	{
		if (*p!='%')
		{
			result = uartSendByte(port,*p);
            if (result!=SUCCESS) break;
			continue;
		}
		switch (*++p)
		{
			case 'c':
				c=(va_arg_const(ap,char));
				result = uartSendByte(port,c);
			break;
			case 'X': /* note this is really an %8.8X */
				longWord=(va_arg_const(ap,U32b));
				result = uartSendU32b(port,longWord);
			break;
			case 'x': /* note this is really an %2.2X */
				byte=(va_arg_const(ap,U8b));
				result = uartSendU8b(port,byte);
			break;
			case 'd': /* note this is really an %3.3d */
				shortWord=(va_arg_const(ap,U16b));
				result = uartSendDec(port,shortWord);
			break;
			case 's':
				str=va_arg_const(ap, char *);
				result = uartSend(port,str);
			break;
			default:
				result = uartSendByte(port,*p);
			break;
		}
        if (result!=SUCCESS) break;
	}
	if (p>=max)
	{
		char maxMsg[]="MAX message length is 256\r\n";
		result = uartSend(port,maxMsg);
	}
	va_end(ap);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -