查看: 359|回复: 0

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

[复制链接]
  • TA的每日心情

    2024-11-15 16:19
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    292

    主题

    26

    回帖

    2978

    积分

    管理员

    积分
    2978
    发表于 2024-11-12 16:44:29 | 显示全部楼层 |阅读模式
    使用开发板的USART1,PA9---TX/PA10---RX
    使用GPIOE(2/4;3/5;/10/12;11/13),因为使用这几个方便我插杜邦线
    [url=]复制[/url]

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


    /**
    * @brief  Main program
    */
    int main(void)
    {
        uint8_t x;
        uint8_t cmd[15];
        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[0] = '{';
                for(i=1;i<15;i++)
                    cmd = get_1byte();
                if(cmd[14]=='}')
                {
                    x=cmd[12];
                    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;
                    }
                }
            }
        }
    }

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

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

    /**
    * @brief  Compares two buffers.
    * @param  pBuffer1, 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

    /**
    * @brief  Reports 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/
    手机端软件是我自己编写的,已经开源,请到上位机板块查找,这里提供编译好的


    四驱纸糊车,蓝牙遥控。

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    友情链接:

    返回顶部 返回列表