C STM32单片机与SIM20模块与USART设备的通信
我试图让STM32f1微控制器与SIM20模块通信。 我希望所有的硬件设置都做得很好。 说到软件,我的C程序由以下组件组成:C STM32单片机与SIM20模块与USART设备的通信,c,embedded,stm32,C,Embedded,Stm32,我试图让STM32f1微控制器与SIM20模块通信。 我希望所有的硬件设置都做得很好。 说到软件,我的C程序由以下组件组成: RCC配置 GPIO配置 USART配置 发送字符串“AT+SRDFIRM” 将接收到的缓冲区存储在文件“test.txt”中 打开LED灯3 但是,没有收到来自SIM20的任何信息。文件中未存储任何内容,LED3未打开 我的C代码如下: /* Includes -------------------------------------------------------
/* 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;
}
/*包括------------------------------------------------------------------*/
#包括“stm32f10x.h”
#包括“stm32_eval.h”
#包括
#包括
/*私有类型定义-----------------------------------------------------------*/
typedef枚举{FAILED=0,PASSED=!FAILED}TestStatus;
/*私有定义------------------------------------------------------------*/
#定义USARTy USART1
#定义USARTy\U GPIO GPIOA/*端口名*/
#定义USARTy\u CLK RCC\u APB2Periph\u USART1
#定义USARTy\U GPIO\U CLK RCC\U APB2Periph\U GPIOA
#在GPIO中定义USARTy\U RxPin\U引脚\U 10/*引脚Rx名称*/
#定义USARTy\U TxPin GPIO\U引脚\U 9/*引脚发送名称*/
#定义USARTz USART2
#定义USARTz_GPIO GPIOA/*端口名*/
#定义USARTz_CLK RCC_APB1Periph_USART2
#定义USARTz_GPIO_CLK RCC_APB2Periph_GPIOA
#定义USARTz_RxPin GPIO_引脚_3/*引脚Rx名称*/
#定义USARTz_TxPin GPIO_引脚_2/*引脚发送名称*/
#定义TxBufferSize(计数(TxBuffer))
/*私有宏-------------------------------------------------------------*/
#定义(a)(sizeof(a)/sizeof(*(a))的计数
/*私有变量---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
uint8_t TxBuffer[]=“AT+SRDFIRM”;
uint8_t RxBuffer[TxBufferSize];
__IO uint8_t TXCONTER=0,RxCONTER=0;
uint8_t Bin[16];
/*私有函数原型-----------------------------------------------*/
无效RCC_配置(无效);
无效GPIO_配置(无效);
无效延迟(uuu IO uint32_ut);
TestStatus Buffercmp(uint8_t*pBuffer1、uint8_t*pBuffer2、uint16_t BufferLength);
__IO uint8_t索引=0;
volatile TestStatus TransferStatus=失败;
GPIO_InitTypeDef GPIO_InitStructure;
内部主(空)
{
STM_EVAL_LEDInit(LED1);
STM_EVAL_LEDInit(LED2);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
int i;
/*TxBuffer[0]=“B”;
RxBuffer[0]='\0'*/
/*系统时钟配置*/
RCC_配置();
/*配置GPIO端口*/
GPIO_配置();
USART_InitStructure.USART_波特率=115200;//配置权限
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//配置longueur mot
USART\u InitStructure.USART\u StopBits=USART\u StopBits\u 1;//位停止
USART_InitStructure.USART_奇偶校验=USART_奇偶校验号;//位对等
USART\u InitStructure.USART\u HardwareFlowControl=USART\u HardwareFlowControl\u None;//硬件控制
USART_InitStructure.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;//半双工
/*配置USARTy*/
USART_Init(USART1和USART_InitStructure);
/*启用USARTy*/
USART_Cmd(USART1,启用);
uint16_t reciv;
/*********************************************************/
FILE*fichier=NULL;
fichier=fopen(“test.txt”,“w”);
而(TXCONTER
@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++;
}
while(TXCONTER
一些需要注意的事项:
- UART是否处于重置状态