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

📄 sci.c.bak

📁 pic16F877A的串口功能C语言软件
💻 BAK
字号:
#include <htc.h>
#include <string.h>
#include "sci.h"
#include "main.h"

/* Routines for initialisation and use of the SCI
 * for the PIC processor.
 */

/* other options:
 * frame errors
 */


//const char* const text = "Hello world.";
//const unsigned char text[] = "Hello world.";

unsigned char
sci_Init(unsigned long int baud, unsigned char ninebits)
{
	int X;
	unsigned long tmp;
	
	/* calculate and set baud rate register */
	/* for asynchronous mode */
	tmp = 16UL * baud;
	X = (int)(FOSC/tmp) - 1;
	if((X>255) || (X<0))
	{
		tmp = 64UL * baud;
		X = (int)(FOSC/tmp) - 1;
		if((X>255) || (X<0))
		{
			return 1;	/* panic - baud rate unobtainable */
		}
		else
			BRGH = 0;	/* low baud rate */
	}
	else
		BRGH = 1;	/* high baud rate */
	SPBRG = X;	/* set the baud rate */

	SYNC = 0;	/* asynchronous */
	SPEN = 1;	/* enable serial port pins */
	CREN = 1;	/* enable reception */
	SREN = 0;	/* no effect */
	TXIE = 0;	/* disable tx interrupts */
	RCIE = 0;	/* disable rx interrupts */
	TX9  = ninebits?1:0;	/* 8- or 9-bit transmission */
	RX9  = ninebits?1:0;	/* 8- or 9-bit reception */
	TXEN = 1;	/* enable the transmitter */

	return 0;
}

void
sci_PutByte(unsigned char byte)
{
	while(!TXIF)	/* set when register is empty */
		continue;
	TXREG = byte;

	return;
}

void Uart_SendByte(int data)
{
        if(data=='\n')
        {
            while(!TXIF);
//            DelayUs(10);                 //because the slow response of hyper_terminal 
            TXREG='\r';
        }

        while(!TXIF);   //Wait until THR is empty.
//        DelayUs(10);
        TXREG=data;
} 

void Uart_SendString(unsigned char *pt)
{
    while(*pt)
    Uart_SendByte(*pt++);
}

//void Uart_SendString(char *pt)
//{
//	char aa;
//	unsigned int i,n;
////	aa=*pt;
//
//
////	n=strlen(*pt);
//	for(i=0;i<n;i++)
//	{
//		Uart_SendByte(*(pt+i));
//
//	}
//			aa=*(text+i);
//
//
//	sci_PutByte(text[i%LENGTH(text)])
//    while(*pt)
//       Uart_SendByte(*pt++);
//}



unsigned char
sci_GetByte(void)
{
	while(!RCIF)	/* set when register is not empty */
		continue;

	return RCREG;	/* RXD9 and FERR are gone now */
}

unsigned char
sci_CheckOERR(void)
{
	if(OERR)	/* re-enable after overrun error */
	{
		CREN = 0;
		CREN = 1;
		return 1;
	}
	
	return 0;
}

#define sci_PutNinth(bitnine)	(TX9D = bitnine?1:0;)

unsigned char
sci_GetNinth(void)
{
	while(!RCIF)
		continue;
	
	return RX9D;	/* RCIF is not cleared until RCREG is read */
}

unsigned char
sci_GetFERR(void)
{
	while(!RCIF)
		continue;
	
	return FERR;	/* RCIF is not cleared until RCREG is read */
}

void Uart_transmit(void)
{
//	unsigned int i,n;
//	unsigned char *pt;
//	n=strlen(*pt);
//		
//	sci_Init(BRATE, NINEBITS);
//
//	str=*text;
//	for(i=0;;i++) 
//	{
////		sci_PutByte(text[i%LENGTH(text)]);
//		pt=text;
//		n=*pt;
//		Uart_SendString(text);
////	    Uart_SendString("123");
//	}
}

⌨️ 快捷键说明

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