1. 程式人生 > >C51微控制器-序列口2-藍芽模組-應用例程

C51微控制器-序列口2-藍芽模組-應用例程

一、例程簡介

    本例程51微控制器與藍芽模組連線,可通過藍芽模組接收和傳送字串,從而控制測試燈的亮滅。其中使用51微控制器的序列口2的工作方式1,即8位UART,波特率可變。波特率設為9600。預設UART2在P1口。

    測試程式實現的功能:

    1、藍芽模組接收到“0”~“6”字串時,分別實現LED0~4的不同亮滅效果;

    2、執行字串“6”對應效果後,通過藍芽模組傳送字串“\rHello!”到模組連線的藍芽裝置。

二、硬體部分

C51晶片:STC12C5A60S2 PDIP-40

藍芽模組:HC-05

晶振:11.0592MHz

-- 連線電路 --

最小系統

51微控制器最小系統

(測試用BST-V51 51微控制器學習板)

藍芽模組

+5V 接 微控制器VCTC

GND 接 微控制器GND

TX 接 P1.2/RxD2

RX 接 P1.3/TxD2

其它引腳懸空

三、軟體部分

-- C語言程式碼 --

#include "reg51.h"
#include "intrins.h"
#include "string.h"

typedef unsigned char uchar;
typedef unsigned int uint;

#define FOSC 11059200L //System frequency
#define BAUD 9600 //UART baudrate
#define PARITYBIT NONE_PARITY //Testing none parity

/*Define UART parity mode*/
/* Copy from STC12C5A60S2 Data Sheet*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity

/*Declare SFR associated with the UART2 */
/* Copy from STC12C5A60S2 Data Sheet*/
sfr AUXR = 0x8e; //Auxiliary register
sfr AUXR1 = 0xa2;
sfr S2CON = 0x9a; //UART2 control register
sfr S2BUF = 0x9b; //UART2 data buffer
sfr BRT = 0x9c; //Baudrate generator
sfr IE2 = 0xaf; //Interrupt control 2
#define S2RI 0x01 //S2CON.0
#define S2TI 0x02 //S2CON.1
#define S2RB8 0x04 //S2CON.2
#define S2TB8 0x08 //S2CON.3

/* Define variables associated with the UART2*/
char UART_buff;
char Str[16];
uchar j = 0;

/*Define pin of LED for testing*/
sbit LED0 = P1^0;
sbit LED1 = P1^4;
sbit LED2 = P1^5;
sbit LED3 = P1^6;
sbit LED4 = P1^7;

// delay for x ms
void delayxms(uint x)
{
	uint i;
  uchar a,b;
	for(i=0;i<x;i++)
    for(b=18;b>0;b--)
        for(a=152;a>0;a--) ;
}

/* Copy from STC12C5A60S2 Data Sheet*/
void Uart2_Init()
{
	#if (PARITYBIT == NONE_PARITY)
		S2CON = 0x50; //8-bit variable UART
	#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
		S2CON = 0xda; //9-bit variable UART, parity bit initial to 1
	#elif (PARITYBIT == SPACE_PARITY)
		S2CON = 0xd5; //9-bit variable UART, parity bit initial to 0
	#endif
	BRT = -(FOSC/32/BAUD); //Set auto-reload vaule of baudrate generator
	AUXR = 0x14; //Baudrate generator work in 1T mode
//	AUXR1  |= 0x10; // If needed, switch UART2 Pin from P1 to P4
	IE2 = 0x01; //Enable UART2 interrupt
	EA = 1; //Open master interrupt switch
}

/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
	int i = 0;
	int l = strlen(s);
	for(i; i<l; i++)
	{
		S2BUF = s[i];
    while(!(S2CON&S2TI)) ;
    S2CON &= ~S2TI;;
	}  
}

void main()
{
	Uart2_Init();
	
	LED0 = 1;
	LED1 = 1;
	LED2 = 1;
	LED3 = 1;
	LED4 = 1;
	
	while(1) ;
}

/*----------------------------
UART2 interrupt service routine
----------------------------*/
void Uart2() interrupt 8 using 1
{
	if (S2CON & S2RI)
	{
		S2CON &= ~S2RI; //Clear receive interrupt flag
		UART_buff = S2BUF; //UART_buff to save UART data
		
		if(UART_buff != '\0')	// When it's not the end of message,
		{
			Str[j++] = UART_buff; // Continue to record character.
		}
		else // When message ends, do the specific job
		{
			Str[j] = UART_buff;
			if(strcmp(Str, "0") == 0)
			{
				LED0 = ~LED0;
				delayxms(100);
			}
			else if(strcmp(Str, "1") == 0)
			{
				LED1 = ~LED1;
				delayxms(100);
			}
			else if(strcmp(Str, "2") == 0)
			{
				LED2 = ~LED2;
				delayxms(100);
			}
			else if(strcmp(Str, "3") == 0)
			{
				LED3 = ~LED3;
				delayxms(100);
			}
			else if(strcmp(Str, "4") == 0)
			{
				LED4 = ~LED4;
				delayxms(100);
			}
			else if(strcmp(Str, "5") == 0)
			{
				LED1 = 0;
				LED2 = 0;
				LED3 = 0;
				LED4 = 0;
				delayxms(100);
			}
			else if(strcmp(Str, "6") == 0)
			{
				LED1 = 1;
				LED2 = 1;
				LED3 = 1;
				LED4 = 1;
				delayxms(100);
				SendString("\rHello!");
			}
			strcpy(Str, "");
			j = 0;
		}
	}
	if (S2CON & S2TI)
	{
		S2CON &= ~S2TI; //Clear transmit interrupt flag
	}
}