STM32中關於HC-05的學習筆記

使用單片機與手機進行通信

接線問題:

HC-05的RX,TX與單片機的PA9,PA10對應相接。至於連接在ATK MODULE 端口中下RX,TX相連無法接受,有待與進一步研究

待續...

下面帶上可行的程序:

#include "stm32f10x.h"    
#include "stm32f10x_rcc.h"    
#include "stm32f10x_gpio.h"    
#include "stm32f10x_usart.h"   
#include "stm32f10x_crc.h"  
#include "system_stm32f10x.h"   
#include "stdio.h" 
#include "delay.h"


void RCC_Configuration(void);    
void GPIO_Configuration(void);  

void USART_Configuration(void);   
void delay_ms(u16 time);  
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data);  
void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str);
int Putchar(int c);


 int main(void)
 {	 

	  SystemInit();  //這個函數在我們系統啓動的時候都會調用,用來設置系統的整個時鐘系統。
	  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|
	  RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE);     //GPIO 端口時鐘使能
  
    GPIO_Configuration();  //GPIO 端口初始化   
    USART_Configuration(); //USART 串口初始化,我們使用串口1
	  delay_init();
	 
	 while(1)
	 {
	  delay_ms(500);
    UART_PutStr(USART1,"hello2 world!\n"); 
	 
	 }
 }

void GPIO_Configuration(void)      
{      
  GPIO_InitTypeDef GPIO_InitStructure;      

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    //傳輸速度爲50Mhz
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;  //使用端口爲9號位              
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //複用推輓輸出         
  GPIO_Init(GPIOA, &GPIO_InitStructure);    //使用PA系列端口            

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //使用端口爲10號位            
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;   //上拉輸入     
  GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化GPIOA.10                
} 

void USART_Configuration(void)
{    

    USART_InitTypeDef USART_InitStructure;                   
    USART_InitStructure.USART_BaudRate = 9600;    //串口波特率                
    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);                    
    USART_Cmd(USART1,ENABLE);    //使能串口1 
}  

  
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data)  
{  
    USART_SendData(USARTx, Data);  
    while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET){}  
}  

void UART_PutStr (USART_TypeDef* USARTx, uint8_t *str)    
{    
    while (0 != *str)    
    {    
        UART_PutChar(USARTx, *str);    
        str++;      
    }    
} 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章