R130MG二维码stm32

#include "stm32f4xx.h"
#include "stdio.h"
#include "string.h"

 UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
void delay_ms(int ms){  
        for(int i = 0; i < ms; i++)   
        {        
              for(int j = 0; j < 2000; j++);   
         }
}

void r3013mg_send_cmd(char* cmd){         HAL_UART_Transmit(&huart2, (uint8_t*)cmd, strlen(cmd), 1000);}

 void r3013mg_recv_data(char* data, int len){    HAL_UART_Receive(&huart2, (uint8_t*)data, len, 1000);}

int main(void){   
          HAL_Init();   
          SystemClock_Config();  
          MX_GPIO_Init();    
          MX_USART2_UART_Init();   
          char cmd_init[] = "AT+INIT\r\n";                 r3013mg_send_cmd(cmd_init);   
           delay_ms(100);    
 char cmd_outputmode[] = "AT+OUTPUTMODE=1\r\n";    r3013mg_send_cmd(cmd_outputmode);    delay_ms(100);   
  char cmd_uart1tx[] = "AT+UART1TX=1\r\n";    r3013mg_send_cmd(cmd_uart1tx);    delay_ms(100);    
  char data[64] = {0};  
    while (1)    {        r3013mg_recv_data(data, 64);        
    printf("QR code: %s\n", data);        memset(data, 0, sizeof(data));        delay_ms(100);    }}
    void SystemClock_Config(void){    RCC_OscInitTypeDef RCC_OscInitStruct = {0};    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};    

/** Initializes the RCC Oscillators according to the specified parameters    * in the RCC_OscInitTypeDef structure.    */    

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;    RCC_OscInitStruct.HSIState = RCC_HSI_ON;    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;    

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)    {        Error_Handler();    } 

  /** Initializes the CPU, AHB and APB buses clocks    */    

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK                                |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;  

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;   

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)    {        Error_Handler();    }}

static void MX_USART2_UART_Init(void){    huart2.Instance = USART2;    
huart2.Init.BaudRate = 115200;    huart2.Init.WordLength = UART_WORDLENGTH_8B;    huart2.Init.StopBits = UART_STOPBITS_1;    huart2.Init.Parity = UART_PARITY_NONE;    huart2.Init.Mode = UART_MODE_TX_RX;    huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;    huart2.Init.OverSampling = UART_OVERSAMPLING_16;   
 if (HAL_UART_Init(&huart2) != HAL_OK)    {        Error_Handler();    }}

static void MX_GPIO_Init(void){         __HAL_RCC_GPIOA_CLK_ENABLE();    __HAL_RCC_GPIOB_CLK_ENABLE();    __HAL_RCC_GPIOC_CLK_ENABLE();}

R130MG二维码stm32_第1张图片

你可能感兴趣的:(经验分享)