gaoyang9992006 发表于 2022-3-7 21:32

【N32G45XVL-STB_V1.1】开发板评测之驱动蓝牙遥控小车

本帖最后由 gaoyang9992006 于 2022-4-12 17:24 编辑

@21小跑堂

使用开发板的USART1,PA9---TX/PA10---RX
使用GPIOE(2/4;3/5;/10/12;11/13),因为使用这几个方便我插杜邦线
串口的使用与IO的使用,请参考我前面的帖子
串口使用方法与注意事项
https://bbs.21ic.com/icview-3203688-1-1.html
IO点灯与解决烧录不了的方法
https://bbs.21ic.com/icview-3202554-1-1.html
最后我们上最终的代码
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/

/**
* @file main.c
* @author Nations
* @version v1.0.0
*
* @CopyRight Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include <stdio.h>
#include "main.h"

/** @addtogroup N32G45X_StdPeriph_Examples
* @{
*/

/** @addtogroup USART_Printf
* @{
*/

typedef enum
{
    FAILED = 0,
    PASSED = !FAILED
} TestStatus;

#define countof(a) (sizeof(a) / sizeof(*(a)))

USART_InitType USART_InitStructure;

void RCC_Configuration(void);
void GPIO_Configuration(void);
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);

uint8_t get_1byte(void);


void stop(void)
{
      GPIOE->PBC = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_5;      
}
void up(void)
{
      GPIOE->PBSC = GPIO_PIN_10;
      GPIOE->PBC= GPIO_PIN_12;
      
      GPIOE->PBSC = GPIO_PIN_11;
      GPIOE->PBC= GPIO_PIN_13;

      GPIOE->PBSC = GPIO_PIN_2;
      GPIOE->PBC= GPIO_PIN_4;

      GPIOE->PBSC = GPIO_PIN_3;
      GPIOE->PBC= GPIO_PIN_5;      
}

void down(void)
{
      GPIOE->PBC= GPIO_PIN_10;
      GPIOE->PBSC = GPIO_PIN_12;
      
      GPIOE->PBC= GPIO_PIN_11;
      GPIOE->PBSC = GPIO_PIN_13;

      GPIOE->PBC= GPIO_PIN_2;
      GPIOE->PBSC = GPIO_PIN_4;

      GPIOE->PBC= GPIO_PIN_3;
      GPIOE->PBSC = GPIO_PIN_5;      
}

void left(void)
{
      GPIOE->PBSC = GPIO_PIN_10;
      GPIOE->PBC= GPIO_PIN_12;
      
      GPIOE->PBC = GPIO_PIN_11;
      GPIOE->PBC= GPIO_PIN_13;
}

void right(void)
{
      GPIOE->PBC = GPIO_PIN_10;
      GPIOE->PBC= GPIO_PIN_12;
      
      GPIOE->PBSC = GPIO_PIN_11;
      GPIOE->PBC= GPIO_PIN_13;
}


/**
* @briefMain program
*/
int main(void)
{
    uint8_t x;
    uint8_t cmd;
    unsigned int i=0;

      /* System Clocks Configuration */
    RCC_Configuration();

    /* Configure the GPIO ports */
    GPIO_Configuration();

    /* USARTy and USARTz configuration ------------------------------------------------------*/
    USART_InitStructure.BaudRate            = 9600;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity            = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

    /* Configure USARTx */
    USART_Init(USARTx, &USART_InitStructure);
    /* Enable the USARTx */
    USART_Enable(USARTx, ENABLE);
                printf("Hello MyCar\n");
               

               
               

      

    while (1)
    {
                              while(get_1byte()=='{')
      {
            cmd = '{';
            for(i=1;i<15;i++)
                cmd = get_1byte();
            if(cmd=='}')
            {
                x=cmd;
                switch(x)
                {
                case 'U':
                  up();
                  printf("UP\n");
                  break;
                case 'D':
                  down();
                  printf("DOWN\n");
                  break;
                case 'L':
                  left();
                  printf("LEFT\n");
                  break;
                case 'R':
                  right();
                  printf("RIGHT\n");
                  break;
                case 'S':
                  stop();
                  printf("STOP\n");
                  break;
                }
            }
      }
    }
}

/**
* @briefConfigures the different system clocks.
*/
void RCC_Configuration(void)
{
    /* Enable GPIO clock */
    GPIO_APBxClkCmd(USARTx_GPIO_CLK | RCC_APB2_PERIPH_AFIO, ENABLE);
    /* Enable USARTy and USARTz Clock */
    USART_APBxClkCmd(USARTx_CLK, ENABLE);
}

/**
* @briefConfigures the different GPIO ports.
*/
void GPIO_Configuration(void)
{
    GPIO_InitType GPIO_InitStructure;

    /* Configure USARTx Tx as alternate function push-pull */
    GPIO_InitStructure.Pin      = USARTx_TxPin;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
    GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure);

    /* Configure USARTx Rx as input floating */
    GPIO_InitStructure.Pin       = USARTx_RxPin;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//            GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
    GPIO_InitPeripheral(USARTx_GPIO, &GPIO_InitStructure);
      
/*
      对使用的IO进行初始化
*/
                RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
                GPIO_InitStructure.Pin      = GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_11|GPIO_PIN_13|GPIO_PIN_2|GPIO_PIN_4|GPIO_PIN_3|GPIO_PIN_5;
                GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure);      
}

/**
* @briefCompares two buffers.
* @parampBuffer1, pBuffer2: buffers to be compared.
* @param BufferLength buffer's length
* @return PASSED: pBuffer1 identical to pBuffer2
*         FAILED: pBuffer1 differs from pBuffer2
*/
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
    while (BufferLength--)
    {
      if (*pBuffer1 != *pBuffer2)
      {
            return FAILED;
      }

      pBuffer1++;
      pBuffer2++;
    }

    return PASSED;
}

uint8_t get_1byte(void)
{
      uint8_t ch;
      while (USART_GetFlagStatus(USARTx, USART_FLAG_RXDNE) == RESET)
      {
      }
      ch = (USART_ReceiveData(USARTx)&0xFF);
//      printf("%c",ch);
      return ch;
}


/* retarget the C library printf function to the USART */
int fputc(int ch, FILE* f)
{
    USART_SendData(USARTx, (uint8_t)ch);
    while (USART_GetFlagStatus(USARTx, USART_FLAG_TXDE) == RESET)
      ;
    return (ch);
}

#ifdef USE_FULL_ASSERT

/**
* @briefReports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @param file pointer to the source file name
* @param line assert_param error line source number
*/
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}

#endif

/**
* @}
*/

/**
* @}
*/

B站视频链接,因为手机又要拍照,又要遥控,所以比较尴尬。
https://www.bilibili.com/video/BV1i44y1M7LR/
手机端软件是我自己编写的,已经开源,请到上位机板块查找,这里提供编译好的


四驱纸糊车,蓝牙遥控。

huquanz711 发表于 2022-3-8 07:58

车车呢?之前发过?

gaoyang9992006 发表于 2022-3-8 08:58

huquanz711 发表于 2022-3-8 07:58
车车呢?之前发过?

https://bbs.21ic.com/icview-3173112-1-1.html
这是手机APP的开发教程贴。

海滨消消 发表于 2022-3-8 16:46

感谢楼主分享

zhuotuzi 发表于 2022-5-19 10:26

学习一下这个解读指令的方法。

lzbf 发表于 2022-7-8 09:44

这个做的不错。

sdCAD 发表于 2022-7-8 10:16

经典蓝牙吗   
页: [1]
查看完整版本: 【N32G45XVL-STB_V1.1】开发板评测之驱动蓝牙遥控小车