1

我正在尝试让智能手机应用程序与 MCU (STM32F030R8) 进行通信。我目前正在进行的步骤是让 WiFi 模块与 MCU 通信。在使用 Arduino Uno/PC 终端时,我可以使用 AT 固件来配置 ESP,但基本上想通过 STM 板做同样的事情。

当我将代码加载到 STM 并使用 Tera Term 向 STM 发送数据时,它应该能够向 ESP 发送 AT 命令,但它不起作用。现在我不知道为什么这不起作用。我正在寻找关于我可能错过的代码或我忽略/不知道的其他原则的指针。

过去几天我在互联网上进行了搜索,但没有找到太多有用的信息。这实际上是我对 C 的经验的总结。感谢您的帮助!

硬件设置如下...

STM32     ESP
PA10      TX
PA9       RX
3.3V      VCC
3.3V      CH_PD
GND       GND

UNO       ESP
TX        TX
RX        RX
3.3V      VCC
3.3V      CH_PD
GND       GND

我一起弗兰肯斯坦的代码如下。我正在使用 Keil uVision 5。

#include "stm32f0xx.h"
#include <stdio.h>
#include <string.h>

void USART1_Init(void);
void USART2_Init(void);
void USART1_Write(char ch[]);
char USART1_Read(void);

int main(void)
{
    // disable the interrupt detection
    __disable_irq();

    RCC->AHBENR |= 0x00020000;                      // enable port A clock
    GPIOA->MODER |= 0x00000400;                     // set mode for PA5

    USART1_Init();
    USART2_Init();

    char strCommand[50];

    NVIC_EnableIRQ(USART1_IRQn);
    __enable_irq();

    printf("\r\nInterface for ESP8266 AT commands.\r\n");
    printf("Please enter the AT command: ");
    gets(strCommand);
    USART1_Write(strCommand);
    printf("\r\n");

    while (1);
}

// write data to the peripheral via USART1
void USART1_Write(char ch[])
{
    // wait while the tx buffer is empty
    while (!(USART1->ISR & 0x00000080));
    for (int i = 0; i < strlen(ch); i++)
    {
        USART1->TDR = (ch[i] & 0xFF);
    }
    USART1->TDR = 0x0000000D;
    USART1->TDR = 0x0000000A;
}

// read data from the peripheral via USART1
char USART1_Read(void)
{
    // wait while the rx buffer is empty
    while (USART1->ISR & 0x00000020);
    return USART1->RDR;
}

// write data to the PC via USART2
int USART2_Write(int ch)
{
    // wait while the tx buffer is empty
    while (!(USART2->ISR & 0x00000080));
    USART2->TDR = (ch & 0xFF);
    return ch;
}

// read data from the PC via USART2
int USART2_Read(void)
{
    // wait while the rx buffer is empty
    while (!(USART2->ISR & 0x00000020));
    return USART2->RDR;
}

// initialize USART1
void USART1_Init(void)
{
    RCC->APB2ENR |= 0x00004000;                 // enable USART1 clock

    GPIOA->AFR[1] |= 0x00000110;                // set AF1 to PA9, PA10

    GPIOA->MODER |= 0x00280000;                 // define GPIO modes to alternate function for PA9, PA10

    USART1->BRR = 0x00000341;                   // set the baud rate, 9600 @ 8MHz

    USART1->CR1 = 0x0000002C;                   // enable te, re, and rx interrupt
    USART1->CR1 |= 0x00000001;                  // enable ue
}

// initialize USART2
void USART2_Init(void)
{
    RCC->APB1ENR |= 0x00020000;                 // enable USART2 clock

    GPIOA->AFR[0] |= 0x00001100;                // set AF1 to PA2, PA3

    GPIOA->MODER |= 0x000000A0;                 // define GPIO modes to alternate function for PA2, PA3

    USART2->BRR = 0x00000341;                   // set the baud rate, 9600 @ 8MHz

    USART2->CR1 = 0x0000000C;                   // enable te and re
    USART2->CR1 |= 0x00000001;                  // enable ue
}

// handle the USART1 interrupt event
void USART1_IRQHandler(void)
{
    char c;
    if (USART1->ISR & 0x00000020)
    {
        c = USART1->RDR;
        //GPIOA->ODR ^= 0x00000020;             // toggle the PA5 state
        printf("%c", c);
    }
}

// implement stdin/stdout/stderr functionality
struct __FILE{int handle;};
FILE __stdin = {0};
FILE __stdout = {1};
FILE __stderr = {2};

int fgetc(FILE *f)
{
    int c;

    c = USART2_Read();

    if (c == '\r')
    {
        USART2_Write(c);
        c = '\n';
    }
    USART2_Write(c);

    return c;
}

int fputc(int c, FILE *f)
{
    return USART2_Write(c);
}

编辑:

我稍微修改了代码并继续尝试。当尝试更改广播的网络名称/密码时(它不起作用),它现在始终以命令中的前两个键入字符进行回复,过了一会儿提出了乱码,看起来应该是用“AT”召唤出来的+GMR”命令。以下代码是上传到设备的代码。关于这怎么可能发生的任何想法?谢谢!

#include "stm32f0xx.h"
#include <stdio.h>
#include <string.h>

void USART1_Init(void);
void USART2_Init(void);
void USART1_Write(char ch[]);
char USART1_Read(void);

int main(void)
{
    // disable the interrupt detection
    __disable_irq();

    RCC->AHBENR |= 0x00020000;                      // enable port A clock
    GPIOA->MODER |= 0x00000400;                     // set mode for PA5

    USART1_Init();
    USART2_Init();

    char strCommand[50];

    NVIC_EnableIRQ(USART1_IRQn);
    __enable_irq();

    printf("\r\nInterface for ESP8266 AT commands.\r\n");
    printf("Please enter the AT command: ");
    gets(strCommand);
    USART1_Write(strCommand);
    printf("\r\n");

    while (1);
}

// write data to the peripheral via USART1
void USART1_Write(char ch[])
{
    // wait for the tx buffer to be empty
    while (!(USART1->ISR & 0x00000080));
    for (int i = 0; i < strlen(ch); i++)
    {
        USART1->TDR = (ch[i] & 0xFF);
    }
//  USART1->TDR = '\r';
//  USART1->TDR = '\n';
}

// read data from the peripheral via USART1
char USART1_Read(void)
{
    // wait while the rx buffer is empty
    while (USART1->ISR & 0x00000020);
    return USART1->RDR;
}

// write data to the PC via USART2
int USART2_Write(int ch)
{
    // wait while the tx buffer is empty
    while (!(USART2->ISR & 0x00000080));
    USART2->TDR = (ch & 0xFF);
    return ch;
}

// read data from the PC via USART2
int USART2_Read(void)
{
    // wait while the rx buffer is empty
    while (!(USART2->ISR & 0x00000020));
    return USART2->RDR;
}

// initialize USART1
void USART1_Init(void)
{
    RCC->APB2ENR |= 0x00004000;                 // enable USART1 clock

    GPIOA->AFR[1] |= 0x00000110;                // set AF1 to PA9, PA10

    GPIOA->MODER |= 0x00280000;                 // define GPIO modes to alternate function for PA9, PA10

    USART1->BRR = 0x00000341;                       // set the baud rate, 9600 @ 8MHz

    USART1->CR1 = 0x0000002C;                       // enable te, re, and rx interrupt
    USART1->CR1 |= 0x00000001;                  // enable ue
}

// initialize USART2
void USART2_Init(void)
{
    RCC->APB1ENR |= 0x00020000;                 // enable USART2 clock

    GPIOA->AFR[0] |= 0x00001100;                // set AF1 to PA2, PA3

    GPIOA->MODER |= 0x000000A0;                 // define GPIO modes to alternate function for PA2, PA3

    USART2->BRR = 0x00000341;                       // set the baud rate, 9600 @ 8MHz

    USART2->CR1 = 0x0000000C;                       // enable te and re
    USART2->CR1 |= 0x00000001;                  // enable ue
}

// handle the USART1 interrupt event
void USART1_IRQHandler(void)
{
    char c;
    while (1)
    {
        if (USART1->ISR & 0x00000020)
        {
            c = USART1->RDR;
        }
        else
        {
            break;
        }
        printf("%c", c);
    }
}

// implement stdin/stdout/stderr functionality
struct __FILE{int handle;};
FILE __stdin = {0};
FILE __stdout = {1};
FILE __stderr = {2};

int fgetc(FILE *f)
{
    int c;

    c = USART2_Read();

    if (c == '\r')
    {
        USART2_Write(c);
        c = '\n';
    }
    USART2_Write(c);

    return c;
}

int fputc(int c, FILE *f)
{
    return USART2_Write(c);
}

Tera Term 输出窗口

4

1 回答 1

0

我能够让 ESP 使用以下代码响应 AT 命令。有些部分被注释掉了,因为我只是在用我的方式来“工作”代码。我能够通过硬编码字符串更改 SSID 和密码。我只是通过按“Enter”发送它来让代码继续。

事实证明,问题在于向 ESP 发送数据(而不是接收,这是我最初的理论)。现在我需要获取uint8_t[]从终端接收的代码,但这是与原始问题不同的问题,因此我将其标记为已回答。

感谢@hamboy75、@Juraj 和@KamilCuk 的帮助!

有用

#include "stm32f0xx.h"
#include <stdio.h>
#include <string.h>

void USART1_Init(void);
void USART2_Init(void);
void USART1_Write(char ch[]);
char USART1_Read(void);
void USART1_Get(unsigned char *string);

void USART1_PutChar(uint8_t ch);
void USART1_Write2(uint8_t *str);

int main(void)
{
    // disable the interrupt detection
    __disable_irq();

    RCC->AHBENR |= 0x00020000;                      // enable port A clock
    GPIOA->MODER |= 0x00000400;                     // set mode for PA5

    USART1_Init();
    USART2_Init();

    char strCommand[50];
    uint8_t str[] = "AT+CWSAP_CUR=\"tempNetwork\",\"tempPassword\",1,2,1,0\r\n";

    NVIC_EnableIRQ(USART1_IRQn);
    __enable_irq();

    printf("\r\nInterface for ESP8266 AT commands.\r\n");
    printf("Please enter the AT command: ");
    gets(strCommand);
    //USART1_Write(strCommand);
    USART1_Write2(str);
    printf("\r\n");

    while (1);
}

// write data to the peripheral via USART1
void USART1_Write(char ch[])
{
    // wait for the tx buffer to be empty
    while (!(USART1->ISR & 0x00000080));
    for (int i = 0; i < strlen(ch); i++)
    {
        //USART1->TDR = (ch[i] & 0xFF);
    }
    //USART1->TDR = '\r';
    //USART1->TDR = '\n';

    USART1->TDR = ('T' & 0xFF);
    USART1->TDR = ('T' & 0xFF);
    USART1->TDR = '\r';
    USART1->TDR = '\n';
}

// write a string via USART1
void USART1_Write2(uint8_t *str)
{
    while (*str != 0)
    {
        USART1_PutChar(*str);
        str++;
    }
}

// write one character via USART1
void USART1_PutChar(uint8_t ch)
{
    while (!(USART1->ISR & 0x00000080));
    USART1->TDR = ch;
}

// read data from the peripheral via USART1
char USART1_Read(void)
{
    // wait while the rx buffer is empty
    while (USART1->ISR & 0x00000020);
    return USART1->RDR;
}

// write data to the PC via USART2
int USART2_Write(int ch)
{
    // wait while the tx buffer is empty
    while (!(USART2->ISR & 0x00000080));
    USART2->TDR = (ch & 0xFF);
    return ch;
}

// read data from the PC via USART2
int USART2_Read(void)
{
    // wait while the rx buffer is empty
    while (!(USART2->ISR & 0x00000020));
    return USART2->RDR;
}

// initialize USART1
void USART1_Init(void)
{
    RCC->APB2ENR |= 0x00004000;                 // enable USART1 clock

    GPIOA->AFR[1] |= 0x00000110;                // set AF1 to PA9, PA10

    GPIOA->MODER |= 0x00280000;                 // define GPIO modes to alternate function for PA9, PA10

    USART1->BRR = 0x00000341;                       // set the baud rate, 9600 @ 8MHz

    USART1->CR1 = 0x0000002C;                       // enable te, re, and rx interrupt
    USART1->CR1 |= 0x00000001;                  // enable ue
}

// initialize USART2
void USART2_Init(void)
{
    RCC->APB1ENR |= 0x00020000;                 // enable USART2 clock

    GPIOA->AFR[0] |= 0x00001100;                // set AF1 to PA2, PA3

    GPIOA->MODER |= 0x000000A0;                 // define GPIO modes to alternate function for PA2, PA3

    USART2->BRR = 0x00000341;                       // set the baud rate, 9600 @ 8MHz

    USART2->CR1 = 0x0000000C;                       // enable te and re
    USART2->CR1 |= 0x00000001;                  // enable ue
}

// handle the USART1 interrupt event
void USART1_IRQHandler(void)
{
    char c;
    while (1)
    {
        if (USART1->ISR & 0x00000020)
        {
            c = USART1->RDR;
            printf("%c", c);
        }
        else
        {
            break;
        }
    }
}

// implement stdin/stdout/stderr functionality
struct __FILE{int handle;};
FILE __stdin = {0};
FILE __stdout = {1};
FILE __stderr = {2};

int fgetc(FILE *f)
{
    int c;

    c = USART2_Read();

    if (c == '\r')
    {
        USART2_Write(c);
        c = '\n';
    }
    USART2_Write(c);

    return c;
}

int fputc(int c, FILE *f)
{
    return USART2_Write(c);
}
于 2020-02-05T19:33:03.037 回答