一.硬件
1.HC-05模块
2.数码管
3.连接硬件
二.在keil5中的代码
main.c代码:
#include "stm32f10x.h"
#include "buletooth.h"
#include "led.h"
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "led.h"
#include "exti.h"void SystemClock_Config(void);
void GPIO_Init(void);
void UART_Init(void);int main(void)
{
Stm32_Clock_Init(9); //³õʼ»¯ÏµÍ³Ê±ÖÓΪ72MHZ
delay_init(72); // ³õʼ»¯ÑÓʱº¯Êý
uart_init(72, 9600); // ³õʼ»¯´®¿Ú£¬²¨ÌØÂÊΪ9600
SystemClock_Config();
GPIO_Init();
UART_Init();
LED_Init();
EXTI_Init();
while (1)
{
BlueTooth_Process();
}
}void SystemClock_Config(void)
{
RCC->CR |= RCC_CR_HSEON;
while (!(RCC->CR & RCC_CR_HSERDY));
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY_2;
RCC->CFGR |= RCC_CFGR_HPRE_DIV1 | RCC_CFGR_PPRE2_DIV1 | RCC_CFGR_PPRE1_DIV2;
RCC->CFGR |= RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL9;
RCC->CR |= RCC_CR_PLLON;
while (!(RCC->CR & RCC_CR_PLLRDY));
RCC->CFGR |= RCC_CFGR_SW_PLL;
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);
}void GPIO_Init(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN;
uint32_t shiftPA9 = 9 * 4;
uint32_t shiftPA10 = 10 * 4;
GPIOA->CRH &= ~((0xF << shiftPA9) | (0xF << shiftPA10));
GPIOA->CRH |= (0xB << shiftPA9);
GPIOA->CRH |= (0x4 << shiftPA10);
}
void UART_Init(void)
{
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
USART1->BRR = 0x1D4C; // 9600 baud @ 72 MHz
USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
}
led.c代码:
#include "sys.h"
#include "led.h"//³õʼ»¯PA8ºÍPD2µÄÊä³ö¿Ú£¬²¢ÊÇʹÄÜÕâÁ½¸ö¿ÚµÄʱÖÓ
//LED IO³õʼ»¯void LED_Init(void)
{
RCC->APB2ENR|=1<<2; //ʹÄÜPORTAʱÖÓ
GPIOA->CRL&=0X00000000;
GPIOA->CRL |= 0x33333333;
JTAG_Set(SWD_ENABLE); //¹Ø±ÕJTAG,¿ªÆôSWD
GPIOA->ODR&=0X00000000;//È«²¿Êä³öµÍµçƽ
}//ÉèÖÃÿλÊý×ÖÊä³öµÄ¶þ¼«¹Ü״̬
void LED_1(void)
{
PAout(1)=1;
PAout(2)=1;
PAout(0)=1;
PAout(5)=0;
PAout(6)=1;
PAout(7)=1;
PAout(3)=1;
PAout(4)=0;
}
void LED_2(void)
{
PAout(1)=0;
PAout(2)=1;
PAout(0)=0;
PAout(5)=0;
PAout(6)=0;
PAout(7)=1;
PAout(3)=0;
PAout(4)=1;
}
void LED_3(void)
{
PAout(1)=0;
PAout(0)=0;
PAout(2)=0;
PAout(5)=1;
PAout(6)=0;
PAout(7)=1;
PAout(3)=0;
PAout(4)=1;
}
void LED_4(void)
{
PAout(1)=0;
PAout(0)=1;
PAout(5)=1;
PAout(6)=0;
PAout(7)=1;
PAout(2)=0;
PAout(3)=1;
PAout(4)=0;
}
void LED_5(void)
{
PAout(1)=1;
PAout(0)=0;
PAout(5)=1;
PAout(6)=0;
PAout(7)=1;
PAout(3)=0;
PAout(2)=0;
PAout(4)=0;
}
void LED_6(void)
{
PAout(1)=1;
PAout(0)=0;
PAout(5)=0;
PAout(6)=0;
PAout(7)=1;
PAout(3)=0;
PAout(4)=0;
PAout(2)=0;
}
void LED_7(void)
{
PAout(1)=0;
PAout(0)=0;
PAout(5)=1;
PAout(6)=1;
PAout(7)=1;
PAout(2)=0;
PAout(3)=1;
PAout(4)=1;
}
void LED_8(void)
{
PAout(1)=0;
PAout(0)=0;
PAout(2)=0;
PAout(5)=0;
PAout(6)=0;
PAout(7)=1;
PAout(3)=0;
PAout(4)=0;
}
void LED_9(void)
{
PAout(1)=0;
PAout(0)=0;
PAout(5)=1;
PAout(6)=0;
PAout(7)=1;
PAout(3)=0;
PAout(4)=0;
PAout(2)=0;
}
buletooth.c
#include "stm32f10x.h"
#include "buleTooth.h"
#include "led.h"
void BlueTooth_Process(void)
{
if (USART1->SR & USART_SR_RXNE)
{
uint8_t data = USART1->DR;
USART_SendData(data);
switch (data)
{
case 1:
LED_1();
break;
case 2:
LED_2();
break;
case 3:
LED_3();
break;
case 4:
LED_4();
break;
case 5:
LED_5();
break;
case 6:
LED_6();
break;
case 7:
LED_7();
break;
case 8:
LED_8();
break;
case 9:
LED_9();
break;
default:
break;
}
}
}void USART_SendData(uint8_t data)
{
while (!(USART1->SR & USART_SR_TXE));
USART1->DR = data;
}
exti.c代码:
#include "stm32f10x.h"
#include "led.h"
#include "usart.h"
#include "buletooth.h"void EXTI_Init(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN;
GPIOA->CRH &= ~(GPIO_CRH_CNF9 | GPIO_CRH_CNF10);
GPIOA->CRH |= GPIO_CRH_CNF9_1 | GPIO_CRH_MODE9_0;
GPIOA->CRH |= GPIO_CRH_CNF10_1;
AFIO->EXTICR[2] &= ~(AFIO_EXTICR3_EXTI9 | AFIO_EXTICR3_EXTI10);
AFIO->EXTICR[2] |= AFIO_EXTICR3_EXTI9_PA | AFIO_EXTICR3_EXTI10_PA;
EXTI->IMR |= EXTI_IMR_MR9 | EXTI_IMR_MR10;
EXTI->FTSR |= EXTI_FTSR_TR9 | EXTI_FTSR_TR10;
NVIC_SetPriority(EXTI9_5_IRQn, 2);
NVIC_EnableIRQ(EXTI9_5_IRQn);
NVIC_SetPriority(EXTI15_10_IRQn, 2);
NVIC_EnableIRQ(EXTI15_10_IRQn);
}
void EXTI9_5_IRQHandler(void)
{
if (EXTI->PR & EXTI_PR_PR9)
{
EXTI->PR |= EXTI_PR_PR9;
if (USART1->SR & USART_SR_RXNE)
{
uint8_t data = USART1->DR;
switch (data)
{
case 1:
LED_1();
break;
case 2:
LED_2();
break;
case 3:
LED_3();
break;
case 4:
LED_4();
break;
case 5:
LED_5();
break;
case 6:
LED_6();
break;
case 7:
LED_7();
break;
case 8:
LED_8();
break;
case 9:
LED_9();
break;
default:
break;
}
}
}
}
void EXTI15_10_IRQHandler(void)
{
if (EXTI->PR & EXTI_PR_PR10)
{
EXTI->PR |= EXTI_PR_PR10;
if (USART1->SR & USART_SR_RXNE)
{
uint8_t data = USART1->DR;switch (data)
{
case 1:
LED_1();
break;
case 2:
LED_2();
break;
case 3:
LED_3();
break;
case 4:
LED_4();
break;
case 5:
LED_5();
break;
case 6:
LED_6();
break;
case 7:
LED_7();
break;
case 8:
LED_8();
break;
case 9:
LED_9();
break;
default:
break;
}
}
}
}
三.注意事项:
1.在写好程序后,用串口调试助手(XCOM)来进行程序展示,在上传过程中使用PA9和PA10的两个跳线帽来实现蓝牙控制数码管的数字,但这里要将switch……case中的要用'1','2'等来进行控制才能实现相关功能。
2.在蓝牙调试器app(手机上的)中的对话模式中要把两个跳线帽去掉,将引脚PA9来连接HC-05上的RX引脚,PA10来连接HC-05上的TX引脚,这里也要将switch……case中的要用'1','2'等来进行控制才能实现相关功能。
3.在蓝牙调试器app(手机上的)中的专业模式中,则用上述代码即可,用switch……case中的要用1,2等。
图片配置如下: