我正在尝试让 STM32f1 微 Controller 与 SIM20 模块通信。 我希望所有的硬件设置都做得很好。 说到软件,我的 C 程序由以下组件组成:
- RCC 配置
- GPIO配置
- USART 配置
- 发送字符串“AT+SRDFIRM”
- 将接收到的缓冲区存储在文件“test.txt”中
- 打开 LED3
但是没有收到来自 SIM20 的任何信息。文件中未存储任何内容,LED3 未亮起。
我的 C 代码如下:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
#include <stdlib.h>
/* Private typedef -----------------------------------------------------------*/
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
#define USARTy USART1
#define USARTy_GPIO GPIOA /* PORT name*/
#define USARTy_CLK RCC_APB2Periph_USART1
#define USARTy_GPIO_CLK RCC_APB2Periph_GPIOA
#define USARTy_RxPin GPIO_Pin_10/* pin Rx name*/
#define USARTy_TxPin GPIO_Pin_9 /* pin Tx name*/
#define USARTz USART2
#define USARTz_GPIO GPIOA/* PORT name*/
#define USARTz_CLK RCC_APB1Periph_USART2
#define USARTz_GPIO_CLK RCC_APB2Periph_GPIOA
#define USARTz_RxPin GPIO_Pin_3/* pin Rx name*/
#define USARTz_TxPin GPIO_Pin_2/* pin Tx name*/
#define TxBufferSize (countof(TxBuffer))
/* Private macro -------------------------------------------------------------*/
#define countof(a) (sizeof(a) / sizeof(*(a)))
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
uint8_t TxBuffer[] = "AT+SRDFIRM";
uint8_t RxBuffer[TxBufferSize];
__IO uint8_t TxConteur = 0, RxConteur = 0;
uint8_t Bin[16];
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void Delay(__IO uint32_t);
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
__IO uint8_t index = 0;
volatile TestStatus TransferStatus = FAILED;
GPIO_InitTypeDef GPIO_InitStructure;
int main(void)
{
STM_EVAL_LEDInit(LED1);
STM_EVAL_LEDInit(LED2);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
int i;
/*TxBuffer[0] = 'B';
RxBuffer[0] ='\0';*/
/* System Clocks Configuration */
RCC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
USART_InitStructure.USART_BaudRate = 115200; // configuration vitesse
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // configuration longueur mot
USART_InitStructure.USART_StopBits = USART_StopBits_1; // bit de stop
USART_InitStructure.USART_Parity = USART_Parity_No; // bit de parite
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // hardware control
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // half duplex
/* Configure USARTy */
USART_Init(USART1,&USART_InitStructure);
/* Enable the USARTy */
USART_Cmd(USART1,ENABLE);
uint16_t reciv;
/*********************************************************/
FILE* fichier = NULL;
fichier = fopen("test.txt", "w");
while(TxConteur < TxBufferSize)
{
/* Send one byte from USARTy to USARTz */
USART_SendData(USARTy, TxBuffer[TxConteur++]);
}
/* Loop until USARTy DR register is empty */
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
while(TxConteur < TxBufferSize)
{
RxBuffer[RxConteur] = USART_ReceiveData(USARTy) & 0xFF;
RxConteur++;
}
fprintf(fichier,"%s","RxBuffer");
fclose(fichier); // On ferme le fichier qui a été ouvert
TransferStatus = Buffercmp(TxBuffer, RxBuffer, TxBufferSize);
STM_EVAL_LEDOn(LED3);
while (1)
{
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure1,GPIO_InitStructure2;
/* Configure USARTy Rx as input floating */
GPIO_InitStructure1.GPIO_Pin =GPIO_Pin_10;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure1);
/* Configure USARTy Tx as alternate function push-pull */
GPIO_InitStructure2.GPIO_Pin =GPIO_Pin_9;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure2);
}
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
while(BufferLength--)
{
if(*pBuffer1 != *pBuffer2)
{
return FAILED;
}
pBuffer1++;
pBuffer2++;
}
return PASSED;
}
@H2CO3:这是包含问题的程序部分:
while(TxConteur < TxBufferSize-1)
{
/* Send one byte from USARTy to USARTz */
USART_SendData(USARTy, TxBuffer[TxConteur++]);
while(USART_GetFlagStatus(USART1, USART_FLAG_IDLE) == RESET);
}
RxConteur=0;
/* Store the received byte in RxBuffer */
while(RxConteur < TxBufferSize-1)
{
RxBuffer[RxConteur] = USART_ReceiveData(USARTy) & 0xFF;
RxConteur++;
}
最佳答案
一些要看的东西:
- UART 是否已重置?
如果 STM32 在默认情况下将 UART 保持在重置状态,我不记得副手。您启用了时钟,但没有明确将其取消复位。如果它仍然处于重置状态,那么当您旋转等待 RXNE 标志设置时,它可能总是读取为重置。这将停止执行,您将无法达到 LED 启用状态。
USART_SendData
和USART_ReceiveData
是否检查数据寄存器的状态?
如果这些函数不检查数据寄存器的状态,那么您的传输可能无法正常输出。在 115200 传输一个字符需要 80 微秒。第一次写入 DR 将很快加载到移位寄存器,第二次写入 DR 将被保持,但除非在 USART_SendData< 中检查 DR 状态
进一步尝试发送会破坏之前加载的字节。传输的最终结果可能会以 AM
的形式出现在串行线上。
类似地,USART_ReceiveData
可能会用相同字节的重复填充接收缓冲区,直到下一个字节进入(尽管 STM32 可能会在读取值后清除 DR)。
- 文件。您使用的是什么编译器和运行时?
因为这是您的 main()
函数,我们可以看到在启动时初始化了什么。我没有看到文件系统的任何初始化。它可能发生在 main 之前,具体取决于运行时。您使用什么工具来构建它,它是否支持文件访问?我知道 IAR 的运行时支持标准文件调用,但默认情况下它们将返回失败,除非您实现低级函数。您没有检查文件是否已成功打开,使用 fprintf
写入可能会崩溃。
确保您的运行时支持文件访问并对调用进行有意义的操作,并为您的文件调用添加错误检查。
关于STM32微 Controller 与SIM20模块与UART设备的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14113686/