- UID
- 373967
- 帖子
- 9008
- 主題
- 2609
- 精華
- 0
- 積分
- 1003
- 楓幣
- 0
- 威望
- 980
- 存款
- 35891
- 贊助金額
- 0
- 推廣
- 0
- GP
- 1205
- 閱讀權限
- 70
- 在線時間
- 460 小時
- 註冊時間
- 2023-1-12
- 最後登入
- 2024-12-22
|
製作手機SIM網路分享器需要以下零件:
STM32F723RET6 微控制器
SIM卡插座
4G/LTE模組(例如SIM7600E-H)
USB端口(可供手機使用)
聚酯電容器和陶瓷電容器
積層陶瓷電容器
結晶振盪器
電感
訊號二極體
電晶體
關於連結腳位,可以參考STM32F723RET6的datasheet。
以下是製作手機SIM網路分享器的參考程式碼
基於Keil MDK-ARM開發環境:
#include "stm32f7xx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define USART_RX_BUF_LEN 512
#define MAX_CONNECT_CNT 5
static uint8_t usart_rx_buf[USART_RX_BUF_LEN];
static uint32_t usart_rx_len;
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Enable USART clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Configure USART Tx and Rx as alternate function push//pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
/* Configure USART */
USART_InitStructure.USART_BaudRate = 115200;
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);
/* Enable USART */
USART_Cmd(USART1, ENABLE);
/* Enable USART RX interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Configure USART RX interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint8_t ch = USART_ReceiveData(USART1);
if (usart_rx_len < USART_RX_BUF_LEN)
{
usart_rx_buf[usart_rx_len] = ch;
usart_rx_len++;
}
}
}
int main(void)
{
char* cmd_str = "AT+CGACT=1,1\r\n"; // Activate PDP context
char* apn_str = "APN name"; // Enter your APN name here
char* user_str = "APN username"; // Enter your APN username here
char* pass_str = "APN password"; // Enter your APN password here
char* conn_str = "AT+NETOPEN\r\n"; // Open network connection
char* ip_str = "AT+CIPOPEN=0,"TCP","example.com",80\r\n"; // Connect to server
/* Initialize USART */
USART1_Init();
/* Configure SIM7600E-H */
printf("Configuring SIM7600E-H...\r\n");
printf("AT+CGDCONT=1,"IP","%s"\r\n", apn_str);
printf("AT+CGAUTH=1,1,"%s","%s"\r\n", user_str, pass_str);
/* Wait for response /
while (usart_rx_len == 0)
{
/ Do nothing */
}
/* Check response /
if (strstr((const char)usart_rx_buf, "OK") == NULL)
{
printf("Failed to configure SIM7600E-H\r\n");
return -1;
}
else
{
printf("SIM7600E-H configured successfully\r\n");
}
/* Clear USART buffer */
usart_rx_len = 0;
memset(usart_rx_buf, 0, USART_RX_BUF_LEN);
/* Connect to network */
printf("Connecting to network...\rprintf("%s", cmd_str);
/* Wait for response /
while (usart_rx_len == 0)
{
/ Do nothing */
}
/* Check response /
if (strstr((const char)usart_rx_buf, "OK") == NULL)
{
printf("Failed to connect to network\r\n");
return -1;
}
else
{
printf("Connected to network successfully\r\n");
}
/* Clear USART buffer */
usart_rx_len = 0;
memset(usart_rx_buf, 0, USART_RX_BUF_LEN);
/* Open network connection */
printf("Opening network connection...\r\n");
printf("%s", conn_str);
/* Wait for response /
while (usart_rx_len == 0)
{
/ Do nothing */
}
/* Check response /
if (strstr((const char)usart_rx_buf, "CONNECT OK") == NULL)
{
printf("Failed to open network connection\r\n");
return -1;
}
else
{
printf("Network connection opened successfully\r\n");
}
/* Clear USART buffer */
usart_rx_len = 0;
memset(usart_rx_buf, 0, USART_RX_BUF_LEN);
/* Connect to server */
printf("Connecting to server...\r\n");
printf("%s", ip_str);
/* Wait for response /
while (usart_rx_len == 0)
{
/ Do nothing */
}
/* Check response /
if (strstr((const char)usart_rx_buf, "CONNECT OK") == NULL)
{
printf("Failed to connect to server\r\n");
return -1;
}
else
{
printf("Connected to server successfully\r\n");
}
/* Clear USART buffer */
usart_rx_len = 0;
memset(usart_rx_buf, 0, USART_RX_BUF_LEN);
/* Send HTTP request */
printf("Sending HTTP request...\r\n");
printf("GET / HTTP/1.1\r\n");
printf("Host: example.com\r\n\r\n");
/* Wait for response /
while (usart_rx_len == 0)
{
/ Do nothing */
}
/* Check response /
if (strstr((const char)usart_rx_buf, "HTTP/1.1 200 OK") == NULL)
{
printf("Failed to send HTTP request\r\n");
return -1;
}
else
{
printf("HTTP request sent successfully\r\n");
}
/* Clear USART buffer */
usart_rx_len = 0;
memset(usart_rx_buf, 0, USART_RX_BUF_LEN);
/* Close network connection */
printf("Closing network connection...\r\n");
printf("AT+CIPCLOSE=0\r\n");
/* Wait for response /
while (usart_rx_len == 0)
{
/ Do nothing */
}
/* Check response /
if (strstr((const char)usart_rx_buf, "CLOSE OK") == NULL)
{
printf("Failed to close network connection\r\n");
return -1;
}
else
{
printf("Network connection closed successfully\r\n");
}
/* Clear USART buffer */
usart_rx_len = 0;
memset(usart_rx_buf, 0, USART_RX_BUF_LEN);
/* Deactivate PDP context */
printf("Deactivating PDP context...\r\n");
printf("AT+CGACT=0,1\r\n");
/* Wait for response */
while (usart_rx_len == 0)
{
/* Do nothing */
}
/* Check response /
if (strstr((const char)usart_rx_buf, "OK") == NULL)
{
printf("Failed to deactivate PDP context\r\n");
return -1;
}
else
{
printf("PDP context deactivated successfully\r\n");
}
/* Clear USART buffer */
usart_rx_len = 0;
memset(usart_rx_buf, 0, USART_RX_BUF_LEN);
/* Disable GPRS */
printf("Disabling GPRS...\r\n");
printf("AT+CGATT=0\r\n");
/* Wait for response /
while (usart_rx_len == 0)
{
/ Do nothing */
}
/* Check response /
if (strstr((const char)usart_rx_buf, "OK") == NULL)
{
printf("Failed to disable GPRS\r\n");
return -1;
}
else
{
printf("GPRS disabled successfully\r\n");
}
/* Clear USART buffer */
usart_rx_len = 0;
memset(usart_rx_buf, 0, USART_RX_BUF_LEN);
/* Power off module */
printf("Powering off module...\r\n");
printf("AT+CPOWD=1\r\n");
/* Wait for response */
HAL_Delay(5000);
/* Check response */
printf("Module powered off successfully\r\n");
return 0;
}
int main(void)
{
/* Initialize HAL */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize USART */
MX_USART1_UART_Init();
/* Initialize SIM800C */
if (sim800c_init() != 0)
{
Error_Handler();
}
/* Test HTTP connection */
if (http_test() != 0)
{
Error_Handler();
}
/* Infinite loop /
while (1)
{
/ Do nothing */
}
}
在上述程式碼中,sim800c_init() 函式用於初始化 SIM800C 模組,其中包含了建立 USART 連線、設定模組參數、檢查模組狀態等步驟。
http_test() 函式用於測試 HTTP 連線,其中包含了建立 GPRS 連線、設定 APN、建立 PDP context、發送 HTTP 請求、接收 HTTP 回應等步驟。
在 http_test() 函式中,我們使用了許多 AT 指令與 USART 串口通訊,並對回應進行解析和檢查,確保每個步驟都能夠成功執行。
在主函式 main() 中,我們首先初始化 HAL,然後配置系統時鐘和 USART。
接著,我們初始化 SIM800C 模組,並測試 HTTP 連線。
最後,我們進入無窮迴圈,等待事件的發生。 |
|