void SIM900A_Init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); // 配置USART1的GPIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置USART1 USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); // 等待SIM900A模块启动 delay_ms(5000); } void SIM900A_SendCommand(char* command) { while(*command) { USART_SendData(SIM900A_USART, *command++); while(USART_GetFlagStatus(SIM900A_USART, USART_FLAG_TC) == RESET); } } void SIM900A_SendSMS(char* phoneNumber, char* message) { SIM900A_SendCommand("AT+CMGF=1"); // 设置短信格式为文本模式 SIM900A_SendCommand("AT+CMGS=\"+86"); // 设置手机号码 SIM900A_SendCommand(phoneNumber); SIM900A_SendCommand("\""); SIM900A_SendCommand(message); // 发送短信内容 USART_SendData(SIM900A_USART, 0x1A); // 发送Ctrl+Z结束短信 while(USART_GetFlagStatus(SIM900A_USART, USART_FLAG_TC) == RESET); } void SIM900A_SendSMS_Chinese(char* phoneNumber, char* message) { char ucs2Message[200]; u8 i; u8 j; SIM900A_SendCommand("AT+CMGF=0"); // 设置短信格式为PDU模式 SIM900A_SendCommand("AT+CSCS=\"UCS2\""); // 设置字符集为UCS2 SIM900A_SendCommand("AT+CSMP=17,167,0,8"); // 设置短信参数 SIM900A_SendCommand("AT+CMGS=\"+86"); // 设置手机号码 SIM900A_SendCommand(phoneNumber); SIM900A_SendCommand("\""); for(i = 0, j = 0; i < strlen(message); i += 2, j += 4) { sprintf(&ucs2Message[j], "%02X%02X", message[i+1], message[i]); } ucs2Message[j] = '\0'; SIM900A_SendCommand(ucs2Message); // 发送短信内容 USART_SendData(SIM900A_USART, 0x1A); // 发送Ctrl+Z结束短信 while(USART_GetFlagStatus(SIM900A_USART, USART_FLAG_TC) == RESET); }
时间: 2024-03-26 13:37:31 浏览: 61
这段代码是用来控制 SIM900A 模块发送短信的,包括初始化、发送 ASCII 码短信、发送中文短信。其中 SIM900A_Init 函数用来初始化 USART1 和 SIM900A 模块,SIM900A_SendCommand 函数用来发送 AT 指令,SIM900A_SendSMS 函数用来发送 ASCII 码短信,SIM900A_SendSMS_Chinese 函数用来发送中文短信。
阅读全文