目錄
- 前言
- 接線
- USB轉TTL
- 藍牙
- 效果圖
- USB轉TTL
- 藍牙
- 手機收 電腦發
- 手機發 電腦收
- 藍牙的連接/斷開
- 參考用圖
- STM32F103
- 藍牙模塊相關
- 核心代碼
- main.c
- usart1.c
- usart3.c
前言
開發板:正點原子 STM32F103 精英版
語言:C語言
開發環境:Keil5
使用了 KEY LED USART USB轉TTL模塊 智向的藍牙模塊(ps:電腦安裝驅動CH340)
代碼下載:碼云 GitHub

代碼參考:正點原子 原始碼 串口實驗例程
功能介紹:
1、LED的0.2秒一閃,表示程式正在運行,
2、串口1收到的資料會發給串口3,串口3收到的資料會發給串口1,
3、按鍵KEY1按下會向串口1發送資料‘1’,按鍵KEY0按下會向串口3發送資料‘3’,
接線
USB轉TTL



藍牙



效果圖
USB轉TTL

藍牙
使用的手機軟體(安卓)為 BLE除錯助手

打開軟體、藍牙、給予權限等
掃描到我們的藍牙模塊,然后連接

連接成功后

點擊最下面的 Unkonwn Service,展開,有接收 和 發送 按鈕

手機收 電腦發


手機發 電腦收


藍牙的連接/斷開
藍牙收到了手機發來的 連接 和 斷開 資訊

參考用圖
STM32F103



藍牙模塊相關



核心代碼
完整代碼下載:碼云 GitHub
main.c
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart1.h"
#include "usart3.h"
// 串口收發函式 type為1,串口1收,發往串口3 type不為1,串口3,發往串口1
void usart_recv_send(u8 type);
int main(void)
{
vu8 key = 0;
delay_init(); // 延時函式初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // 設定NVIC中斷分組2:2位搶占優先級,2位回應優先級
usart1_init(115200); // USART1初始化 波特率115200 默認資料位8 停止位1 校驗位none
usart3_init(115200); // USART3初始化 波特率115200 默認資料位8 停止位1 校驗位none
LED_Init(); // LED埠初始化
KEY_Init(); // 初始化與按鍵連接的硬體介面
while (1)
{
// 串口收發
usart_recv_send(1);
usart_recv_send(3);
// 得到鍵值
key = KEY_Scan(0);
if (key)
{
switch (key)
{
case KEY1_PRES: // 向串口1發送'1'
usart1_send_byte(0x31);
break;
case KEY0_PRES: // 向串口3發送'3'
usart3_send_byte(0x33);
break;
}
}
LED0 = !LED0; //閃爍LED,提示系統正在運行.
delay_ms(100);
}
}
// 串口收發函式 type為1,串口1收,發往串口3 type不為1,串口3,發往串口1
void usart_recv_send(u8 type)
{
u8 i = 0;
u8 tmp_len = 0;
// 資料快取
static u8 buf[255] = {0};
// 資料長度
u8 buf_len = 0;
// 回傳快取區資料的個數
if(1 == type)
tmp_len = usart1_getdata_count();
else
tmp_len = usart3_getdata_count();
for(i=0; i<tmp_len; i++)
{
// 回傳快取區當前指標所指資料
if(1 == type)
buf[i] = usart1_receive_data();
else
buf[i] = usart3_receive_data();
buf_len++;
// 超過約定的上限長度
if(buf_len >= 250)
{
buf_len=0;
break;
}
}
// 資料不為空
if(0 != buf_len)
{
// 串口資料發送
if(1 == type)
usart3_send_bytes(buf, buf_len);
else
usart1_send_bytes(buf, buf_len);
}
}
usart1.c
#include "usart1.h"
#include "stdio.h"
static uint8_t usart1_buffer[255];
static uint8_t usart1_index;
static uint8_t usart1_count;
// USART1初始化 默認資料位8 停止位1 校驗位none
void usart1_init(u32 bound)
{
// GPIO埠設定
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能USART1,GPIOA時鐘
/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化GPIOA.9
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA.10
// Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//搶占優先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子優先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的引數初始化VIC暫存器
USART_InitStructure.USART_BaudRate = bound; // 串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 字長為8位資料格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一個停止位
USART_InitStructure.USART_Parity = USART_Parity_No; // 無奇偶校驗位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 無硬體資料流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收發模式
USART_Init(USART1, &USART_InitStructure); // 初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 開啟串口接受中斷
USART_Cmd(USART1, ENABLE); // 使能串口1
usart1_index=0;
usart1_count=0;
}
// 中斷服務函式
void USART1_IRQHandler(void)
{
// 接收中斷
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
if(usart1_count==usart1_index)
{
// 快取區無資料,回到起點開始存盤
usart1_count=0;
usart1_index=0;
}
usart1_buffer[usart1_count]=USART1->DR;
usart1_count++;
if(usart1_count>=250)
{
// 沒有及時取出資料,導致存盤位置到達末尾,回到起點
usart1_count=0;
usart1_index=0;
}
}
}
/*回傳快取區資料的個數*/
uint8_t usart1_getdata_count(void)
{
return usart1_count-usart1_index;
}
/*回傳快取區當前指標所指資料*/
uint8_t usart1_receive_data(void)
{
return usart1_buffer[usart1_index++];
}
/*串口資料發送函式
data_send:發送資料
*/
void usart1_send_byte(uint8_t data_send)
{
USART_SendData(USART1, data_send);
while (!(USART1->SR & USART_FLAG_TXE));
}
/*串口資料發送函式
data_buffer:發送資料串的首地址
length:發送資料的長度
*/
void usart1_send_bytes(uint8_t* data_buffer,uint8_t length)
{
uint8_t i;
for(i=0; i<length; i++)
{
usart1_send_byte(data_buffer[i]);
}
}
usart3.c
#include "usart3.h"
#include "stdio.h"
static uint8_t usart3_buffer[255];
static uint8_t usart3_index;
static uint8_t usart3_count;
// USART3初始化 默認 資料位8 停止位1 校驗位none
void usart3_init(u32 bound)
{
// GPIO埠設定
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* config USART3 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能USART3,GPIOB時鐘
/* USART3 GPIO config */
/* Configure USART3 Tx (PB.10) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure USART3 Rx (PB.11) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//搶占優先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子優先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的引數初始化VIC暫存器
USART_InitStructure.USART_BaudRate = bound; // 串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 字長為8位資料格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一個停止位
USART_InitStructure.USART_Parity = USART_Parity_No; // 無奇偶校驗位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 無硬體資料流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 收發模式
USART_Init(USART3, &USART_InitStructure); // 初始化串口3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // 開啟串口接受中斷
USART_Cmd(USART3, ENABLE); // 使能串口3
usart3_index=0;
usart3_count=0;
}
// 中斷服務函式
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
if(usart3_count==usart3_index)
{
//快取區無資料,回到起點開始存盤
usart3_count=0;
usart3_index=0;
}
usart3_buffer[usart3_count]=USART3->DR;
usart3_count++;
if(usart3_count>=250)
{
//沒有及時取出資料,導致存盤位置到達末尾,回到起點
usart3_count=0;
usart3_index=0;
}
}
}
/*回傳快取區資料的個數*/
uint8_t usart3_getdata_count(void)
{
return usart3_count-usart3_index;
}
/*回傳快取區當前指標所指資料*/
uint8_t usart3_receive_data(void)
{
return usart3_buffer[usart3_index++];
}
/*串口資料發送函式
data_send:發送資料
*/
void usart3_send_byte(uint8_t data_send)
{
USART_SendData(USART3, data_send);
while (!(USART3->SR & USART_FLAG_TXE));
}
/*串口資料發送函式
data_buffer:發送資料串的首地址
length:發送資料的長度
*/
void usart3_send_bytes(uint8_t* data_buffer,uint8_t length)
{
uint8_t i;
for(i=0; i<length; i++)
{
usart3_send_byte(data_buffer[i]);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/276686.html
標籤:其他
