对STC12C5A60S2的双串口的封装

2016-5-26 海滨 程序就是一个世界

好久没有写文章了,最近一直忙,但是忙不出个所以然。今天抽空研究了STC12C5A60S2的双串口,对其进行了封装。代码参考自STC官网手册,略微修改和整合。请使用KEIL编译。


main.c 实现调用

#include "COM.h"

void delay(unsigned int i)
{
	unsigned char j=255;
	while(i--)
		while(j--);
}


void main()
{
	//初始化串口
	UART_init();
	UART2_init();
	UartRcvChar1 = 0x00;
	UartRcvChar2 = 0x00;
	uart1_bit9 = 0;
	uart2_bit9 = 0;
	while(1)
	{
			//串口1发送字符串
		_com_send_string("I am UART1\n");
		if(UartRcvChar1 !=0x00)
		{
			_com_send_char(UartRcvChar1);
			UartRcvChar1 = 0x00;
		}
		//串口2发送字符串
		_com2_send_string("I am UART2\n");
		if(UartRcvChar2 !=0x00)
		{
			_com_send_char(UartRcvChar1);
			UartRcvChar2 = 0x00;
		}
		delay(2000);
	}
}
COM.h 对函数的生成和一些变量的声明

#ifndef __COM_H__
#define __COM_H__

#define FOSC      22118400UL   /*使用22.1184M晶体*/
#define BAUDRATE   9600UL       // set 

/*Define UART parity mode*/
#define NONE_PARITY 0
#define ODD_PARITY 1
#define EVEN_PARITY 2
#define MARK_PARITY 3
#define SPACE_PARITY 4
#define PARITYBIT NONE_PARITY

#define S2RI 0x01		// S2CON.0
#define S2TI 0x02		// S2CON.1
#define S2RB8 0x04		// S2CON.2
#define S2TB8 0x08		// S2CON.3


extern bit uart1_bit9;
extern bit uart2_bit9;

extern unsigned char UartRcvChar1;
extern unsigned char UartRcvChar2;

/**
 * 串口1初始化设置
 * return void
 */
extern void UART_init(void);

/**
 * 串口2初始化设置
 * return void
 */
extern void UART2_init(void);


/**
 * 串口1发送单个字符
 * @param char 要发送的字符串
 * return void
 */
extern void _com_send_char(char);


/**
 * 串口1发送字符串 \0结束的
 * @param char*
 * return void 
 */
extern void _com_send_string(char*);

/**
 * 串口1接收中断函数
 * return void 
 */
extern void UartRcv(void);


/**
 * 串口2发送单个字符
 * @param char 要发送的字符串
 * return void
 */
extern void _com2_send_char(char);


/**
 * 串口2发送字符串 \0结束的
 * @param char*
 * return void 
 */
extern void _com2_send_string(char*);

/**
 * 串口1接收中断函数
 * return void 
 */
extern void UartRcv2(void);


#endif

COM.c实现了对串口功能的封装

#include "STC12C5A60S2.H"
#include "COM.h"


unsigned char UartRcvChar1 = 0x00;
unsigned char UartRcvChar2 = 0x00;
bit uart1_bit9 = 0;
bit uart2_bit9 = 0;

void UART_init()
{
	#if (PARITYBIT == NONE_PARITY)
	SCON = 0x50;	//	8-bit varaible UART
	#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
	SCON = 0xDA;	//	9-bit varaible UART,parity bit initial to 1
	#elif (PARITYBIT == SPACE_PARITY)
	SCON = 0xD5;	//	9-bit varaible UART,parity bit initial to 0
	#endif
	TMOD = 0x20;	// set Timer1 as 8-bit auto reload
	TH1 = TL1 = (unsigned char)(256-(FOSC/12/32/BAUDRATE));	//set auto-load value
	TR1 = 1;	// Enable timer1
	ES = 1;	//Enable UART interrupt
	EA = 1;	//Open master interrupt switch
}

void UART2_init()
{
	#if (PARITYBIT == NONE_PARITY)
	S2CON = 0x50;	//	8-bit varaible UART
	#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
	S2CON = 0xDA;	//	9-bit varaible UART,parity bit initial to 1
	#elif (PARITYBIT == SPACE_PARITY)
	S2CON = 0xD5;	//	9-bit varaible UART,parity bit initial to 0
	#endif
	BRT = (unsigned char)(256-(FOSC/32/BAUDRATE));	//set auto-load value of Bbaudrate generator
	AUXR = 0x14;	// Baudrate generate work in 1T mode
	IE2 |= 0x01;		// Enable UART2 interruprt
	EA = 1;	//Open master interrupt switch
}


void UartRcv() interrupt 4
{
	if(RI)
	{
		RI = 0;
		UartRcvChar1 = SBUF;
		uart1_bit9 = RB8;
	}
}


void UartRcv2() interrupt 8
{
	if(S2CON & S2RI)
	{
		S2CON &= ~S2RI;
		UartRcvChar2 = S2BUF;
		uart2_bit9 = (S2CON & S2RB8);	//P2.2 show parity bit
	}
}


/*-----------
Send a bytr data to UART
Input:dat(data to be send)
Output:None
-------------*/
void _com_send_char(char dat)
{
	if(P)
	{
	#if (PARITYBIT == ODD_PARITY)
		TB8 = 0;
	#elif (PARITYBIT == EVEN_PARITY)
		TB8 = 1;
	#endif
	}
	else
	{
	#if (PARITYBIT == ODD_PARITY)
		TB8 = 1;
	#elif (PARITYBIT == EVEN_PARITY)
		TB8 = 0;
	#endif
	}
	SBUF = dat;
	while(!TI);
	TI = 0;
}


/*-----------
Send a bytr data to UART
Input:dat(data to be send)
Output:None
-------------*/
void _com2_send_char(char dat)
{
	if(P)
	{
	#if (PARITYBIT == ODD_PARITY)
		S2CON &= ~S2TB8;	// set parity bit to 0
	#elif (PARITYBIT == EVEN_PARITY)
		S2CON |= S2TB8;		// set parity bit to 1
	#endif
	}
	else
	{
	#if (PARITYBIT == ODD_PARITY)
		S2CON |= S2TB8;		// set parity bit to 1
	#elif (PARITYBIT == EVEN_PARITY)
		S2CON &= ~S2TB8;		// set parity bit to 1
	#endif
	}
	S2BUF = dat;
	while((S2CON & S2TI)==0);
	S2CON &= ~S2TI;
}

void _com_send_string(char *s)
{
	while(*s)
	{
		_com_send_char(*s++);
	}
}

void _com2_send_string(char *s)
{
	while(*s)
	{
		_com2_send_char(*s++);
	}
}

标签: 忙碌 STC12C5A60S2.STC12C5A60S2双串口

发表评论:


Powered by 海滨Blog
sitemap