Android ve STM32F4 ü Bluetooth Yoluyla Haberleştirme


          Bu uygulamamızda STM32F4 Discovery Geliştirme Kitimiz ile herhangi bir Android işletim sistemine sahip (akıllı telefon ,tablet... vb.) cihaz arasında Bluetooth üzerinden haberlesmesini saglayıp, PA8 pinine bağlamıs oldugumuz led'i uzaktan kontrol edeceğiz. Buradaki mantık tabletten mikroişlemciye mesaj gönderip, daha sonra mikroişlemciden tablete mesaj gönderilmesidir.
Aşagıdaki resimde görmüş oldugunuz veri alıp göndermemizi saglayacak olan Bluetooth modülüdür.


Geliştirme kitimize aşagıdaki şemadaki gibi Bluetooth modülümüzü bağlıyoruz.
















STM32F4 Mikroişlemcimizin içine aşagıdaki kodu yazıp derliyoruz.
Aşagıdaki kodu yazarken ST elektronik şirketinin USART örneklerini temel alarak kolayca yazabilirsiniz.


#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "misc.h"

int i;

//ErrorStatus HSEStartUpStatus;

void NVIC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void USART1_IRQHandler(void);
void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount);

int main(void)
{
    usart_rxtx();
    while(1)
    {

    }
}

/******************************************************************************/
/*            STM32F10x Peripherals Interrupt Handlers                        */
/******************************************************************************/

/**
  * @brief  This function handles USARTx global interrupt request
  * @param  None
  * @retval None
  */
void USART1_IRQHandler(void)
{
    if ((USART1->SR & USART_FLAG_RXNE) != (u16)RESET)
    {
        i = USART_ReceiveData(USART1);
        if(i == '1'){
            GPIO_WriteBit(GPIOA,GPIO_Pin_8,Bit_SET);        // Set '1' on PA8
            UARTSend("LED ON\r\n",sizeof("LED ON\r\n"));    // Send message to UART1
        }
        else if(i == '0'){
            GPIO_WriteBit(GPIOA,GPIO_Pin_8,Bit_RESET);      // Set '0' on PA8
            UARTSend("LED OFF\r\n",sizeof("LED OFF\r\n"));
        }
    }
}

void usart_rxtx(void)
{
    const unsigned char welcome_str[] = " Welcome to Bluetooth!\r\n";

    /* Enable USART1 and GPIOA clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    /* NVIC Configuration */
    NVIC_Configuration();

    /* Configure the GPIOs */
    GPIO_Configuration();

    /* Configure the USART1 */
    USART_Configuration();

    /* Enable the USART1 Receive interrupt: this interrupt is generated when the
         USART1 receive data register is not empty */
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    /* print welcome information */
    UARTSend(welcome_str, sizeof(welcome_str));
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure (PA.8) as output */
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure); // Save

  /* 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);

  /* 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);
}

/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : Configures the USART1
*******************************************************************************/
void USART_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;

/* USART1 configuration ------------------------------------------------------*/
  USART_InitStructure.USART_BaudRate = 9600;        // Baud Rate
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  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);

  /* Enable USART1 */
  USART_Cmd(USART1, ENABLE);
}

/**
  * @brief  Configures the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the USARTx Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/*******************************************************************************
* Function Name  : UARTSend
* Description    : Send a string to the UART.
* Input          : - pucBuffer: buffers to be printed.
*                : - ulCount  : buffer's length
*******************************************************************************/
void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
    //
    // Loop while there are more characters to send.
    //
    while(ulCount--)
    {
        USART_SendData(USART1, (uint16_t) *pucBuffer++);
        /* Loop until the end of transmission */
        while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
        {
        }
    }
}

Kullanmıs oldugumuz Android APK dosyasını BURADAN indirebilirsiniz.
Android projesinin kaynak dosyalarını BURADAN indirebilirsiniz.











7 yorum:

  1. merhaba. apk kaynak kodlari linkini yeniler
    sen

    YanıtlaSil
    Yanıtlar
    1. Şuan tatildeyim. malesef laptopum yanımda degil..Döndüğüm zaman inş liinkleri yenilerim.İşin acil ise google play da güzel uygulamalar vardı.O apkları da kullanabilirsin.

      Sil
  2. kaynak kodlari lazım.cok acelesi yok.ben dönmeni bekleyim ozaman

    YanıtlaSil
  3. Bu yorum yazar tarafından silindi.

    YanıtlaSil
  4. android kod linklerini yenilersen iyi olur

    YanıtlaSil
    Yanıtlar
    1. pardon yaa ben seni unutmusum kusura bakma.Bu sayfada detaylı olarak anlatıyor bende zaten oradan almıstım kodları :)
      kolay gelsin...
      http://english.cxem.net/mcu/mcu1.php

      Sil
  5. ok.aldım kodları .Teşekkür ettim

    YanıtlaSil