欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
我的單片機控制6188藍牙模塊程序
[打印本頁]
作者:
phl
時間:
2019-5-9 12:22
標題:
我的單片機控制6188藍牙模塊程序
這是我之前做的用STC89C516單片機芯片做的控制6188藍牙模塊的程序
都是自己原創(chuàng)的,還有很多不是很完善的地方(比如按鍵響應(yīng)有點長了點)
單片機源程序如下:
#include<reg51.h>
#include"lcd.h"
typedef unsigned int u16; //對數(shù)據(jù)類型進行聲明定義
typedef unsigned char u8;
#define GPIO_KEY P1
#define U8 u8
#define U16 u16
#define K_PLAY 0x01//F
#define K_PREV 0x02//B
#define K_NEXT 0x03//7
#define Bt_PlayPause_Cmd "MA" //音樂播放\暫停 AT+MA\r\n
#define Bt_Stop_Cmd "MC" //音樂停止 AT+MC\r\n
#define Bt_Next_Cmd "MD" //下一曲 AT+MD\r\n
#define Bt_Previous_Cmd "ME" //上一曲 AT+ME\r\n
#define TxLEN_MAX 16
#define RxLEN_MAX 36
#define MAX_INFO_NUM 0x0A
#define MAX_INFO_LEN 5
bit btlink = 0;
xdata U8 UartRxBuf[RxLEN_MAX];
xdata U8 UartRxIdx;
xdata U8 UartTimeout;
xdata U8 UartTxBuf[TxLEN_MAX+1];
xdata U8 UartTxIdx;
xdata U8 UartTxLen;
#define UARTRI 0X01
#define UARTTI 0X02
#define UART_RI 0X01
#define UART_TI 0X02
unsigned char KeyValue;
unsigned char dg_ram1=0,dg_ram2 = 0;
//用來存放讀取到的鍵值
unsigned char KeyState;
//用來存放按鍵狀態(tài)
unsigned char PuZh[]=" Pechin Science ";
unsigned char dat[]="0123456789ABCDEF";
void Delay10ms(); //延時50us
void KeyDown(); //檢測按鍵函數(shù)
/*******************************************************************************
* 函 數(shù) 名 : main
* 函數(shù)功能 : 主函數(shù)
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void scan_key(void);
void uart_cmd(U8 *cmd);
void UsartInit()
{
SCON=0X50; //設(shè)置為工作方式1
TMOD=0X20; //設(shè)置計數(shù)器工作方式2
PCON=0X00;//0x80; //0x80是波特率加倍波特率變19200
TH1=0XFD;//D;//3; //計數(shù)器初始值設(shè)置
TL1=0XFD;//D;//3;
ES=1; //打開接收中斷
EA=1; //打開總中斷
TR1=1; //打開計數(shù)器
}
code U8 _bt_info[MAX_INFO_NUM][2] = { {'I', 'I'}, // A connect A2DP
{'M', 'A'}, // music pause
{'M', 'B'}, // music play
{'C', '1'},
{'C', '0'},
{'I', 'A'},
};// 通話中
void uart_dat(void)
{
if(UartTimeout)
{
if(UartTimeout>2)
{
UartTimeout=0x80;
}
else
{
UartTimeout++;
}
}
if(UartRxIdx>=0x80)
{
U8 j;
for(j=0; j<MAX_INFO_NUM; j++)
{
dg_ram1++;
if(dg_ram1>0x0f)
dg_ram1 = 0;
if ((_bt_info[j][0] == UartRxBuf[0])||(_bt_info[j][1] == UartRxBuf[1])||(0x0d == UartRxBuf[2]))
{
dg_ram2 = j;
break;
}
}
if(j < 4)
{
btlink = 1;
}
else if(j < MAX_INFO_NUM)
{
btlink = 0;
}
UartRxIdx = 0;
}
if(UartTimeout>=0x80)
{
UartTimeout = 0;
UartRxIdx = 0x00;
}
if(dg_ram2>0x0f)
dg_ram2 = 0;
}
void main(void)
{
unsigned char i;
LcdInit();
UsartInit();
UartTxIdx = 0;
UartTxLen = 0;
UartRxIdx = 0;
UartTimeout = 0;
KeyState=0;
for(i=0;i<16;i++)
{
LcdWriteData(PuZh[i]);
}
while(1)
{
// UsartInit();
uart_dat();
KeyDown();
scan_key();
if(KeyState)
{
KeyState=0;
LcdWriteCom(0x80+0x40);
LcdWriteData(dat[KeyValue]);
}
}
}
/*******************************************************************************
* 函 數(shù) 名 : KeyDown
* 函數(shù)功能 : 檢測有按鍵按下并讀取鍵值
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void KeyDown(void)
{
char a;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)
{
Delay10ms();
if(GPIO_KEY!=0x0f)
{
KeyState=1;
//測試列
GPIO_KEY=0X0F;
// Delay10ms();
switch(GPIO_KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
// default: KeyValue=17; //檢測出錯回復(fù)17意思是把數(shù)碼管全滅掉。
}
//測試行
GPIO_KEY=0XF0;
Delay10ms();
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
// default: KeyValue=17;
}
while((a<100)&&(GPIO_KEY!=0xf0)) //檢測按鍵松手檢測
{
Delay10ms();
a++;
}
a=0;
}
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
U8 Uart1TxData(U8 *dat, U8 len)
{
U8 i;
if(UartTxIdx == UartTxLen)
{
for(i=0; i<len; i++)
{
UartTxBuf[i] = *(dat+i);
}
UartTxIdx = 0x0;
UartTxLen = len;
SCON &= ~UARTTI; //清TI
SBUF = *dat; //輸入寫入緩存后硬件自動發(fā)送
return 1;
}
else
return 0;
}
//-----------------------------------------------------------------------------
void uart_cmd(U8 *cmd)
{
U8 cmdbuf[7];
cmdbuf[0] = 'A';
cmdbuf[1] = 'T';
cmdbuf[2] = '+';
cmdbuf[3] = cmd[0];
cmdbuf[4] = cmd[1];
cmdbuf[5] = 0x0d;
cmdbuf[6] = 0x0a;
Uart1TxData(cmdbuf, 7);
}
//-----------------------------------------------------------------------------
void scan_key(void)
{
if(KeyState)
{
switch(KeyValue)
{
case K_PREV:
if(1)
{
uart_cmd(Bt_Previous_Cmd);
}
break;
////////////////////////////下一曲/////////////////////////////////////////
case K_NEXT:
if(1)
{
uart_cmd(Bt_Next_Cmd);
}
break;
////////////////////////////播放暫停////////////////////////////////////////
case K_PLAY:
if(1)
{
uart_cmd(Bt_PlayPause_Cmd);
}
break;
default:
break;
}
}
}
void Usart() interrupt 4
{
u8 cchr;
if(RI==1)//接收的中斷
{
cchr = SBUF;
UartTimeout = 1;
if(UartRxIdx<MAX_INFO_LEN)
{
UartRxBuf[UartRxIdx++] = cchr;
if(cchr == 0x0A)
{
……………………
…………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
6199藍牙模塊ok的 - 副本.rar
(1002.5 KB, 下載次數(shù): 14)
2019-5-9 12:22 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1