Air780E展示
合宙最近新出的cat1通讯模块
项目说明
本测试项目主要使用到GD32F427的UART串口通讯功能,使用串口发送AT指令,驱动Air780E实现相应的功能。
由于开发板某些串口被占用,本次使用串口1发送AT指令,使用串口2作为调试信息输出(将串口1和模块之间的通讯信息,通过串口2回显到PC端,方便调试)。下面是部分代码:
串口的定义及初始化
#define UART1_Clock RCU_USART1
#define UART1_IO_Clock RCU_GPIOD
#define UART1_IO_Port GPIOD
#define UART1_TXD_Pin GPIO_PIN_5
#define UART1_RXD_Pin GPIO_PIN_6
#define UART2_Clock RCU_USART2
#define UART2_IO_Clock RCU_GPIOC
#define UART2_IO_Port GPIOC
#define UART2_TXD_Pin GPIO_PIN_10
#define UART2_RXD_Pin GPIO_PIN_11
void UART1_Init(void)
{
rcu_periph_clock_enable( UART1_Clock);
rcu_periph_clock_enable(UART1_IO_Clock);
gpio_af_set(UART1_IO_Port, GPIO_AF_7, UART1_TXD_Pin);
gpio_af_set(UART1_IO_Port, GPIO_AF_7, UART1_RXD_Pin);
gpio_mode_set(UART1_IO_Port, GPIO_MODE_AF, GPIO_PUPD_PULLUP,UART1_TXD_Pin);
gpio_output_options_set(UART1_IO_Port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,UART1_TXD_Pin);
gpio_mode_set(UART1_IO_Port, GPIO_MODE_AF, GPIO_PUPD_PULLUP,UART1_RXD_Pin);
gpio_output_options_set(UART1_IO_Port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,UART1_RXD_Pin);
usart_deinit(USART1);
usart_baudrate_set(USART1,115200);
usart_receive_config(USART1, USART_RECEIVE_ENABLE);
usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
usart_enable(USART1);
nvic_irq_enable(USART1_IRQn, 0, 0);
usart_interrupt_enable(USART1, USART_INT_RBNE);
}
void UART2_Init(void)
{
rcu_periph_clock_enable( UART2_Clock);
rcu_periph_clock_enable(UART2_IO_Clock);
gpio_af_set(UART2_IO_Port, GPIO_AF_7, UART2_TXD_Pin);
gpio_af_set(UART2_IO_Port, GPIO_AF_7, UART2_RXD_Pin);
gpio_mode_set(UART2_IO_Port, GPIO_MODE_AF, GPIO_PUPD_PULLUP,UART2_TXD_Pin);
gpio_output_options_set(UART2_IO_Port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,UART2_TXD_Pin);
gpio_mode_set(UART2_IO_Port, GPIO_MODE_AF, GPIO_PUPD_PULLUP,UART2_RXD_Pin);
gpio_output_options_set(UART2_IO_Port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,UART2_RXD_Pin);
usart_deinit(USART2);
usart_baudrate_set(USART2,115200);
usart_receive_config(USART2, USART_RECEIVE_ENABLE);
usart_transmit_config(USART2, USART_TRANSMIT_ENABLE);
usart_enable(USART2);
nvic_irq_enable(USART2_IRQn, 0, 0);
usart_interrupt_enable(USART2, USART_INT_RBNE);
}
串口的接收和发送处理
unsigned char Data=0;
void USART1_IRQHandler(void)
{
while((RESET != usart_interrupt_flag_get(USART1, USART_INT_FLAG_RBNE)) &&(RESET != usart_flag_get(USART1, USART_FLAG_RBNE)))
{
Data=usart_data_receive(USART1);
usart_data_transmit(USART2,Data);
}
}
void UART1_Transmit(unsigned char *Data,unsigned int Lenth)
{
int i=0;
unsigned char U2_Data[256]={0};
strcpy((char*)U2_Data,"Send:");
strcat((char*)U2_Data, (char*)Data);
for(i=0;i<Lenth;i++)
{
usart_data_transmit(USART1,Data[i]);
while(RESET == usart_flag_get(USART1, USART_FLAG_TC));
}
for(i=0;i<strlen((char*)U2_Data);i++)
{
usart_data_transmit(USART2,U2_Data[i]);
while(RESET == usart_flag_get(USART2, USART_FLAG_TC));
}
}
主函数
int main(void)
{
systick_config();
rcu_periph_clock_enable(RCU_GPIOC);
gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_bit_reset(GPIOC, GPIO_PIN_6);
rcu_periph_clock_enable(RCU_GPIOA);
gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0);
UART1_Init();
UART2_Init();
while(1) {
if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_0)) {
delay_1ms(100);
if(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_0)) {
gpio_bit_write(GPIOC, GPIO_PIN_6, (bit_status)(1 - gpio_input_bit_get(GPIOC, GPIO_PIN_6)));
}
while(RESET == gpio_input_bit_get(GPIOA, GPIO_PIN_0)) {
}
UART1_Transmit((unsigned char *)"AT\r\n",strlen("AT\r\n"));
delay_1ms(1000);
UART1_Transmit((unsigned char *)"AT+CPIN?\r\n",strlen("AT+CPIN?\r\n"));
delay_1ms(1000);
UART1_Transmit((unsigned char *)"AT+CSQ\r\n",strlen("AT+CSQ\r\n"));
delay_1ms(1000);
UART1_Transmit((unsigned char *)"AT+CGATT?\r\n",strlen("AT+CGATT?\r\n"));
delay_1ms(1000);
UART1_Transmit((unsigned char *)"AT+CSTT\r\n",strlen("AT+CSTT\r\n"));
delay_1ms(1000);
UART1_Transmit((unsigned char *)"AT+CIICR\r\n",strlen("AT+CIICR\r\n"));
delay_1ms(1000);
UART1_Transmit((unsigned char *)"AT+CIFSR\r\n",strlen("AT+CIFSR\r\n"));
delay_1ms(1000);
UART1_Transmit((unsigned char *)"AT+CIPSTART=\"TCP\",\"112.125.89.8\",\"36146\"\r\n",strlen("AT+CIPSTART=\"TCP\",\"112.125.89.8\",\"36146\"\r\n"));
delay_1ms(1000);
UART1_Transmit((unsigned char *)"AT+CIPSEND=10\r\n",strlen("AT+CIPSEND=10\r\n"));
delay_1ms(1000);
UART1_Transmit((unsigned char *)"GD32F427V!\r\n",strlen("GD32F427V!\r\n"));
delay_1ms(1000);
}
}
}
测试结果
下图是接线以及使用TCP通讯的测试结果,可以看到,开发板可以正常的发送和接收到数据,但是中文会显示乱码。
原作者:消逝雨迹