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
-- 連接電路?--
最小系統
(測試用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#endifBRT = -(FOSC/32/BAUD); //Set auto-reload vaule of baudrate generatorAUXR = 0x14; //Baudrate generator work in 1T mode // AUXR1 |= 0x10; // If needed, switch UART2 Pin from P1 to P4IE2 = 0x01; //Enable UART2 interruptEA = 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 flagUART_buff = S2BUF; //UART_buff to save UART dataif(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} }總結
以上是生活随笔為你收集整理的C51单片机-串行口2-蓝牙模块-应用例程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 兔子繁殖问题
- 下一篇: 修改jenkins的镜像地址