ESP01s station-client模式 代码

一,目录

二,代码

main.c

#include "stm32f10x.h"
#include "bsp_uart.h"
#include "bsp_esp01s.h"
#include "bsp_SysTick.h"

 
 
int main(void)
{
	char* ESP3201S_PORT_REV;
	//设置NVIC组
	NVIC_SetPriorityGrouping(NVIC_PriorityGroup_2);
	//初始化USART1和USART2
	SysTick_Init();
	ESP01S_Init();
	while(1)
	{
		if(getESP3201S_PORT_REV_flag()==1)
		{
			ESP3201S_PORT_REV = getESP3201S_PORT_REV();
			//把ESP01S来的数据发送给PC
			Serial_SendString(DEBUG_PORT,ESP3201S_PORT_REV);
			ESP_CLEAR_REV_BUFF();
		}
	}
}
 

bsp_uart.h

#ifndef __BSP_UART_H__
#define __BSP_UART_H__
#include "stm32f10x.h"
 


usart1数据缓冲
//extern uint8_t USART1_REV[100];
usart2数据缓冲
//extern uint8_t USART2_REV[100];
usart1数据个数
//extern uint8_t usart1_index;
usart2数据个数
//extern uint8_t usart2_index;
usart1数据接受完成标志位
//extern uint8_t usart1_flag;
usart2数据接受完成标志位
//extern uint8_t usart2_flag;



/**
	USARTx	(x......1.2.3.4.5)
	USART_BaudRate
	USART_WordLength	USART_WordLength_8b/USART_WordLength_9b
	USART_StopBits		USART_StopBits_1/USART_StopBits_0_5/USART_StopBits_2/USART_StopBits_1_5
	USART_Parity		USART_Parity_No/USART_Parity_Even/USART_Parity_Odd
	USART_IT
  *   This parameter can be one of the following values:
  *     @arg USART_IT_CTS:  CTS change interrupt (not available for UART4 and UART5)
  *     @arg USART_IT_LBD:  LIN Break detection interrupt
  *     @arg USART_IT_TXE:  Tansmit Data Register empty interrupt
  *     @arg USART_IT_TC:   Transmission complete interrupt
  *     @arg USART_IT_RXNE: Receive Data register not empty interrupt
  *     @arg USART_IT_IDLE: Idle line detection interrupt
  *     @arg USART_IT_ORE:  OverRun Error interrupt
  *     @arg USART_IT_NE:   Noise Error interrupt
  *     @arg USART_IT_FE:   Framing Error interrupt
  *     @arg USART_IT_PE:   Parity Error interrupt
	
	
*/
 
 
void BSP_UART_Init(USART_TypeDef* USARTx,
				uint8_t NVIC_PRE_Priority,
				uint8_t NVIC_SUB_Priority,
				uint32_t USART_BaudRate,
				uint16_t USART_WordLength,
				uint16_t USART_StopBits,
				uint16_t USART_Parity);
 
void Serial_SendByte(USART_TypeDef* USARTx,uint8_t Byte);
void Serial_SendArray(USART_TypeDef* USARTx,uint8_t *Array, uint16_t Length);
void Serial_SendString(USART_TypeDef* USARTx,char *String);
void DEBUG_USART12_TO_USART21(void);
void Clear_Buff(char* BUFF_DATA,uint8_t index);
#endif /**		__BSP_UART_H__	*/

bsp_uart.c

#include "bsp_uart.h"




/**
	USARTx	(x......1.2.3.4.5)
	USART_BaudRate
	USART_WordLength	USART_WordLength_8b/USART_WordLength_9b
	USART_StopBits		USART_StopBits_1/USART_StopBits_0_5/USART_StopBits_2/USART_StopBits_1_5
	USART_Parity		USART_Parity_No/USART_Parity_Even/USART_Parity_Odd
*/
void BSP_UART_Init(USART_TypeDef* USARTx,
				uint8_t NVIC_PRE_Priority,
				uint8_t NVIC_SUB_Priority,
				uint32_t USART_BaudRate,
				uint16_t USART_WordLength,
				uint16_t USART_StopBits,
				uint16_t USART_Parity)
{
	GPIO_TypeDef* USART_GPIOx;
	uint32_t RCC_UARTx_PORT_CLK;
	uint32_t RCC_UARTx_CLK;
	uint16_t USARTx_PIN_TX;
	uint16_t USARTx_PIN_RX;
 
	IRQn_Type NVIC_USARTx_IRQn;
	
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	if(USARTx==USART1)
	{
		USART_GPIOx = GPIOA;
		USARTx_PIN_TX = GPIO_Pin_9;
		USARTx_PIN_RX = GPIO_Pin_10;
		NVIC_USARTx_IRQn = USART1_IRQn;
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
 
	}else if(USARTx==USART2)
	{
		USART_GPIOx = GPIOA;
		RCC_UARTx_PORT_CLK = RCC_APB2Periph_GPIOA;
		RCC_UARTx_CLK = RCC_APB1Periph_USART2;
		USARTx_PIN_TX = GPIO_Pin_2;
		USARTx_PIN_RX = GPIO_Pin_3;
		NVIC_USARTx_IRQn = USART2_IRQn;
	}else if(USARTx==USART3)
	{
		USART_GPIOx = GPIOB;
		RCC_UARTx_PORT_CLK = RCC_APB2Periph_GPIOB;
		RCC_UARTx_CLK = RCC_APB1Periph_USART3;
		USARTx_PIN_TX = GPIO_Pin_10;
		USARTx_PIN_RX = GPIO_Pin_11;
		NVIC_USARTx_IRQn = USART3_IRQn;
	}else if(USARTx==UART4)
	{
		USART_GPIOx = GPIOC;
		RCC_UARTx_PORT_CLK = RCC_APB2Periph_GPIOC;
		RCC_UARTx_CLK = RCC_APB1Periph_USART3;
		USARTx_PIN_TX = GPIO_Pin_10;
		USARTx_PIN_RX = GPIO_Pin_11;
		NVIC_USARTx_IRQn = UART4_IRQn;
	}else if(USARTx==UART5)
	{
		USARTx_PIN_TX = GPIO_Pin_12;
		USARTx_PIN_RX = GPIO_Pin_2;
		NVIC_USARTx_IRQn = UART5_IRQn;
	}
	if(USARTx!=USART1&&USARTx!=UART5)
	{
		RCC_APB1PeriphClockCmd(RCC_UARTx_CLK,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_UARTx_PORT_CLK,ENABLE);
	}else if(USARTx==UART5)
	{
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
		
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Pin = USARTx_PIN_TX;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOC,&GPIO_InitStructure);
	
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_InitStructure.GPIO_Pin = USARTx_PIN_RX;
		GPIO_Init(GPIOD,&GPIO_InitStructure);
	}
 
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = USARTx_PIN_TX;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(USART_GPIOx,&GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = USARTx_PIN_RX;
	GPIO_Init(USART_GPIOx,&GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = USART_BaudRate;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
	USART_InitStructure.USART_Parity = USART_Parity;
	USART_InitStructure.USART_StopBits = USART_StopBits;
	USART_InitStructure.USART_WordLength = USART_WordLength;
	USART_Init(USARTx,&USART_InitStructure);
	
	//开启接收非空中断和空闲中断
	USART_ITConfig(USARTx,USART_IT_RXNE,ENABLE);
	USART_ITConfig(USARTx,USART_IT_IDLE,ENABLE);
	
	NVIC_InitStructure.NVIC_IRQChannel = NVIC_USARTx_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PRE_Priority;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_SUB_Priority;
	NVIC_Init(&NVIC_InitStructure);
	
	//使能串口
	USART_Cmd(USARTx, ENABLE);
 
}
 
 
/**
  * 函    数:串口发送一个字节
  * 参    数:Byte 要发送的一个字节
  * 返 回 值:无
  */
void Serial_SendByte(USART_TypeDef* USARTx,uint8_t Byte)
{
	USART_SendData(USARTx, Byte);		//将字节数据写入数据寄存器,写入后USART自动生成时序波形
	while (USART_GetFlagStatus(USARTx,USART_FLAG_TXE)==RESET);	//等待发送完成
	/*下次写入数据寄存器会自动清除发送完成标志位,故此循环后,无需清除标志位*/
}
 
/**
  * 函    数:串口发送一个数组
  * 参    数:Array 要发送数组的首地址
  * 参    数:Length 要发送数组的长度
  * 返 回 值:无
  */
void Serial_SendArray(USART_TypeDef* USARTx,uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)		//遍历数组
	{
		Serial_SendByte(USARTx,Array[i]);		//依次调用Serial_SendByte发送每个字节数据
	}
	
}
 
/**
  * 函    数:串口发送一个字符串
  * 参    数:String 要发送字符串的首地址
  * 返 回 值:无
  */
void Serial_SendString(USART_TypeDef* USARTx,char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)//遍历字符数组(字符串),遇到字符串结束标志位后停止
	{
		Serial_SendByte(USARTx,String[i]);		//依次调用Serial_SendByte发送每个字节数据
	}
	
}


 
void Clear_Buff(char* BUFF_DATA,uint8_t index)
{
	uint8_t i=0;
	while(i<(index))
	{
		BUFF_DATA[i]='\0';
		i++;
	}
 
}

bsp_SysTick.h

#ifndef __SYSTICK_H
#define __SYSTICK_H

#include "stm32f10x.h"

void SysTick_Init(void);
void Delay_us(__IO u32 nTime);
#define Delay_ms(x) Delay_us(100*x)	 //单位ms

void SysTick_Delay_Us( __IO uint32_t us);
void SysTick_Delay_Ms( __IO uint32_t ms);


#endif /* __SYSTICK_H */

bsp_SysTick.c

  
#include "bsp_SysTick.h"
#include "core_cm3.h"
#include "misc.h"

static __IO u32 TimingDelay;
 
/**
  * @brief  启动系统滴答定时器 SysTick
  * @param  无
  * @retval 无
  */
void SysTick_Init(void)
{
	/* SystemFrequency / 1000    1ms中断一次
	 * SystemFrequency / 100000	 10us中断一次
	 * SystemFrequency / 1000000 1us中断一次
	 */
//	if (SysTick_Config(SystemFrequency / 100000))	// ST3.0.0库版本
	if (SysTick_Config(SystemCoreClock / 100000))	// ST3.5.0库版本
	{ 
		/* Capture error */ 
		while (1);
	}
}

/**
  * @brief   us延时程序,10us为一个单位
  * @param  
  *		@arg nTime: Delay_us( 1 ) 则实现的延时为 1 * 10us = 10us
  * @retval  无
  */
void Delay_us(__IO u32 nTime)
{ 
	TimingDelay = nTime;	

	// 使能滴答定时器  
	SysTick->CTRL |=  SysTick_CTRL_ENABLE_Msk;

	while(TimingDelay != 0);
}

/**
  * @brief  获取节拍程序
  * @param  无
  * @retval 无
  * @attention  在 SysTick 中断函数 SysTick_Handler()调用
  */
void TimingDelay_Decrement(void)
{
	if (TimingDelay != 0x00)
	{ 
		TimingDelay--;
	}
}

#if 0
// 这个 固件库函数 在 core_cm3.h中
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{ 
  // reload 寄存器为24bit,最大值为2^24
	if (ticks > SysTick_LOAD_RELOAD_Msk)  return (1);
  
  // 配置 reload 寄存器的初始值	
  SysTick->LOAD  = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;
	
	// 配置中断优先级为 1<<4-1 = 15,优先级为最低
  NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); 
	
	// 配置 counter 计数器的值
  SysTick->VAL   = 0;
	
	// 配置systick 的时钟为 72M
	// 使能中断
	// 使能systick
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk | 
                   SysTick_CTRL_TICKINT_Msk   | 
                   SysTick_CTRL_ENABLE_Msk;                    
  return (0); 
}
#endif

// couter 减1的时间 等于 1/systick_clk
// 当counter 从 reload 的值减小到0的时候,为一个循环,如果开启了中断则执行中断服务程序,
// 同时 CTRL 的 countflag 位会置1
// 这一个循环的时间为 reload * (1/systick_clk)

void SysTick_Delay_Us( __IO uint32_t us)
{
	uint32_t i;
	SysTick_Config(SystemCoreClock/1000000);
	
	for(i=0;i<us;i++)
	{
		// 当计数器的值减小到0的时候,CRTL寄存器的位16会置1	
		while( !((SysTick->CTRL)&(1<<16)) );
	}
	// 关闭SysTick定时器
	SysTick->CTRL &=~SysTick_CTRL_ENABLE_Msk;
}

void SysTick_Delay_Ms( __IO uint32_t ms)
{
	uint32_t i;	
	SysTick_Config(SystemCoreClock/1000);
	
	for(i=0;i<ms;i++)
	{
		// 当计数器的值减小到0的时候,CRTL寄存器的位16会置1
		// 当置1时,读取该位会清0
		while( !((SysTick->CTRL)&(1<<16)) );
	}
	// 关闭SysTick定时器
	SysTick->CTRL &=~ SysTick_CTRL_ENABLE_Msk;
}


/*********************************************END OF FILE**********************/

bsp_esp01s.h

#ifndef __BSP_ESP01S_H__
#define __BSP_ESP01S_H__
#include "stm32f10x.h"



/**
uart1	tx:PA9	RX:PA10
uart2	tx:PA2	RX:PA3
uart2连接wifi作为通讯端口
esp01s						stm32f103vct6
3.3v							3.3v
tx								PA3
rx								PA2
gnd								gnd
uart1作为调试端口
*/

#define ESP3201S_PORT	USART2
#define DEBUG_PORT		USART1

typedef enum{
	CWMODE_ERROR=1,
	RST_ERROR = 2,
	CWJAP_ERROR=3,
	CIPSTART_ERROR=4,
	CIPMUX_ERROR=5
	
}CMD_ERROR_TypeDef;

void ESP01S_Init(void);
void DEBUG_EPS01S(void);
uint8_t getESP3201S_PORT_REV_index(void);
uint8_t getESP3201S_PORT_REV_flag(void);
char* getESP3201S_PORT_REV(void);
void setESP3201S_PORT_REV_index(uint8_t ESP3201S_PORT_REV_index);
void setESP3201S_PORT_REV_flag(uint8_t ESP3201S_PORT_REV_flag);
void ESP_CLEAR_REV_BUFF(void);


#endif /**		__BSP_ESP01S_H__	*/

bsp_esp01s.c

#include "bsp_esp01s.h"
#include "bsp_uart.h"
#include "string.h"
#include "bsp_SysTick.h"
//#define CIPSTART_MAKER(PROTOCOL,SERVER_HOST,PORT)	("AT+CIPSTART="##PROTOCOL##","##"SERVER_HOST"##","##PORT##"\r\n")

//usart1数据缓冲
char DEBUG_PORT_REV[100];
//usart2数据缓冲
char ESP3201S_PORT_REV[100];
//usart1数据个数
uint8_t DEBUG_PORT_REV_index = 0;
//usart2数据个数
uint8_t ESP3201S_PORT_REV_index = 0;
//usart1数据接受完成标志位
uint8_t DEBUG_PORT_REV_flag = 0;
//usart2数据接受完成标志位
uint8_t ESP3201S_PORT_REV_flag = 0;
//ESP01s初始化步骤控制标识符 0进入错误处理函数,1继续下一步
uint8_t NEXT_STEP_FLAG = 1;
//ERROR_CODE为0表示没有错误,其他表示有错
uint8_t ERROR_CODE;
uint8_t ESP_INIT_COMPLETE_FLAG=0;

//查询到的信息缓冲区
char* GET_INFO;
char* temp;
char* CMD_CWMODE = "AT+CWMODE=1\r\n";

char* CMD_RST = "AT+RST\r\n";
char* CMD_CWJAP = "AT+CWJAP=\"lemontreexxxx\",\"xxxxxxxxxxxx\"\r\n";
char* CMD_CWJAP1 = "WIFI CONNECTED";
char* CMD_CWJAP2="AT+CWJAP?\r\n";
char* CMD_CIFSR = "AT+CIFSR\r\n";
char* CMD_CIPMUX = "AT+CIPMUX=0\r\n";
char* CMD_CIPSTART = "AT+CIPSTART=\"TCP\",\"192.168.2.2\",8080\r\n";
char* CMD_CIPMODE = "AT+CIPMODE=0\r\n";
char* CMD_CIPSTATUS = "AT+CIPSTATUS\r\n";





char* itoa(int num,char* str,int radix);
uint8_t ESP3201S_Cmd(char* cmd,uint8_t (*VerifyFunction)(uint32_t),uint32_t ms);
uint8_t ESP3201S_ONE_ANSWER_VerifyFunction(uint32_t ms);
char* ESP3201S_getInformation(char* cmd,char* (*getFunction)(void));
char* getCWJAP(void);
char* getCIFSR(void);
char* getCIPSTATUS(void);
void ESP_CLEAR_REV_BUFF(void);

void ESP01S_Init(void)
{
	BSP_UART_Init(DEBUG_PORT,2,2,115200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No);
	BSP_UART_Init(ESP3201S_PORT,2,2,115200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No);
	SysTick_Delay_Ms(3000);
	if(NEXT_STEP_FLAG)
	{
		//设置station模式 AT+CWMODE=
		ESP3201S_Cmd(CMD_CWMODE,ESP3201S_ONE_ANSWER_VerifyFunction,50);
		if(ERROR_CODE==0)
			Serial_SendString(DEBUG_PORT,"ESP设置station模式成功\r\n");
		
	}
	
	if(NEXT_STEP_FLAG)
	{
		//重启eps模块 AT+RST
		ESP3201S_Cmd(CMD_RST,ESP3201S_ONE_ANSWER_VerifyFunction,6000);
		if(ERROR_CODE==0)
			Serial_SendString(DEBUG_PORT,"ESP_REST_SUCCESS\r\n");
	}


	
	if(NEXT_STEP_FLAG)
	{
		//连接wifi AT+CWJAP="xxxxxxxxxxxxx","xxxxxxxxxxxxx"
		ESP3201S_Cmd(CMD_CWJAP,ESP3201S_ONE_ANSWER_VerifyFunction,10000);
	}
	
		
	if(NEXT_STEP_FLAG)
	{
		//测试是否获取到IP,以验证连接wifi是否正常
		ERROR_CODE=ESP3201S_Cmd(CMD_CIFSR,ESP3201S_ONE_ANSWER_VerifyFunction,1000);
	}
	
	if(NEXT_STEP_FLAG)
	{
		//设置单链接模式
		ERROR_CODE=ESP3201S_Cmd(CMD_CIPMUX,ESP3201S_ONE_ANSWER_VerifyFunction,1000);
	}
	
	if(NEXT_STEP_FLAG)
	{
		//连接服务器
		ERROR_CODE=ESP3201S_Cmd(CMD_CIPSTART,ESP3201S_ONE_ANSWER_VerifyFunction,6000);
		ESP_INIT_COMPLETE_FLAG=1;
	}
	
	if((ERROR_CODE==0)&&ESP_INIT_COMPLETE_FLAG)
	{
		Serial_SendString(DEBUG_PORT,"ESP3201s初始化完毕\r\nCWJAP:");
		Serial_SendString(DEBUG_PORT,ESP3201S_getInformation(CMD_CWJAP2,getCWJAP));
		Serial_SendString(DEBUG_PORT,ESP3201S_getInformation(CMD_CIFSR,getCIFSR));
		Serial_SendString(DEBUG_PORT,ESP3201S_getInformation(CMD_CIPSTATUS,getCIPSTATUS));
	
	}else
	{
		Serial_SendString(DEBUG_PORT,"ESP3201s_init_error  \r\n");
	}

}

void ESP_CLEAR_REV_BUFF(void)
{
		Clear_Buff(ESP3201S_PORT_REV,100);
		ESP3201S_PORT_REV_index = 0;
		ESP3201S_PORT_REV_flag = 0;

}


char* getCWJAP(void)
{
	
	strtok(ESP3201S_PORT_REV,":");
	GET_INFO=strtok(NULL,",");
	return GET_INFO;
}
char* getCIFSR(void)
{
	GET_INFO = ESP3201S_PORT_REV;
	return ESP3201S_PORT_REV;
}

char* getCIPSTATUS(void)
{
	GET_INFO = ESP3201S_PORT_REV;
	return ESP3201S_PORT_REV;
}

uint8_t ESP3201S_Cmd(char* cmd,uint8_t (*VerifyFunction)(uint32_t),uint32_t ms)
{
	ESP_CLEAR_REV_BUFF();
	if(strlen(cmd)!=0)
	{
		Serial_SendString(ESP3201S_PORT,cmd);
	}
	ERROR_CODE = VerifyFunction(ms);

	return ERROR_CODE;
}

//ESP只进行一次应答的命令校验
uint8_t ESP3201S_ONE_ANSWER_VerifyFunction(uint32_t ms)
{
	char* cmd_toke;
	SysTick_Delay_Ms(ms);
	while(ESP3201S_PORT_REV_flag!=1);

	if(strstr(ESP3201S_PORT_REV,"OK"))
	{

		cmd_toke = strtok(ESP3201S_PORT_REV,"\r\n");
		NEXT_STEP_FLAG = 1;

		Serial_SendString(DEBUG_PORT,strcat(cmd_toke," execute success\r\n"));
		return 0;
	}else
	{
		
		NEXT_STEP_FLAG = 0;
		cmd_toke = strtok(ESP3201S_PORT_REV,"\r\n");
		Serial_SendString(DEBUG_PORT,strcat(cmd_toke," execute fail\r\n"));
		return 100;
	}
	

}



char* ESP3201S_getInformation(char* cmd,char* (*getFunction)(void))
{
	ESP_CLEAR_REV_BUFF();
	if(strlen(cmd)!=0)
	{
		Serial_SendString(ESP3201S_PORT,cmd);
	}
	while(ESP3201S_PORT_REV_flag!=1);
	if(strstr(ESP3201S_PORT_REV,"OK"))
	{
		NEXT_STEP_FLAG = 1;
		getFunction();
		
		return GET_INFO;
	}else
	{
		NEXT_STEP_FLAG = 0;
		return 0;
	}
	
}


char* itoa(int num,char* str,int radix)
{
	char temp;//临时变量,交换两个值时用到
    char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//索引表
    unsigned unum;//存放要转换的整数的绝对值,转换的整数可能是负数
    int i=0,j,k;//i用来指示设置字符串相应位,转换之后i其实就是字符串的长度;转换后顺序是逆序的,有正负的情况,k用来指示调整顺序的开始位置;j用来指示调整顺序时的交换。
 
    //获取要转换的整数的绝对值
    if(radix==10&&num<0)//要转换成十进制数并且是负数
    {
        unum=(unsigned)-num;//将num的绝对值赋给unum
        str[i++]='-';//在字符串最前面设置为'-'号,并且索引加1
    }
    else unum=(unsigned)num;//若是num为正,直接赋值给unum
 
    //转换部分,注意转换后是逆序的
    do
    {
        str[i++]=index[unum%(unsigned)radix];//取unum的最后一位,并设置为str对应位,指示索引加1
        unum/=radix;//unum去掉最后一位
 
    }while(unum);//直至unum为0退出循环
 
    str[i]='\0';//在字符串最后添加'\0'字符,c语言字符串以'\0'结束。
 
    //将顺序调整过来
    if(str[0]=='-') k=1;//如果是负数,符号不用调整,从符号后面开始调整
    else k=0;//不是负数,全部都要调整
 

    for(j=k;j<=(i-1)/2;j++)//头尾一一对称交换,i其实就是字符串的长度,索引最大值比长度少1
    {
        temp=str[j];//头部赋值给临时变量
        str[j]=str[i-1+k-j];//尾部赋值给头部
        str[i-1+k-j]=temp;//将临时变量的值(其实就是之前的头部值)赋给尾部
    }
 
    return str;//返回转换后的字符串
 
}



void USART1_IRQHandler(void)
{
	
	if(USART_GetITStatus(DEBUG_PORT,USART_IT_RXNE)!=RESET)
	{
		DEBUG_PORT_REV[DEBUG_PORT_REV_index++]=USART_ReceiveData(DEBUG_PORT);
	}
	if(DEBUG_PORT_REV_index>99)
	{
		DEBUG_PORT_REV_index=99;
	}
	if(USART_GetITStatus(DEBUG_PORT,USART_IT_IDLE)!=RESET)
	{
		DEBUG_PORT->SR;	 //读取SR寄存器
		DEBUG_PORT->DR;    //读取DR寄存器 (先读USART_SR,然后读USART_DR可以清除空闲中断标志位IDLE)
		DEBUG_PORT_REV_flag=1;
	}
	
 
}
 
 
void USART2_IRQHandler(void)
{
	
	if(USART_GetITStatus(ESP3201S_PORT,USART_IT_RXNE)!=RESET)
	{
		ESP3201S_PORT_REV[ESP3201S_PORT_REV_index++]=USART_ReceiveData(ESP3201S_PORT);
	}
	if(ESP3201S_PORT_REV_index>99)
	{
		ESP3201S_PORT_REV_index=99;
	}
	if(USART_GetITStatus(ESP3201S_PORT,USART_IT_IDLE)!=RESET)
	{
		ESP3201S_PORT->SR;	 	//读取SR寄存器
		ESP3201S_PORT->DR;		//读取DR寄存器 (先读USART_SR,然后读USART_DR可以清除空闲中断标志位IDLE)
		ESP3201S_PORT_REV_flag=1;	//置1数据接收完成标志位
	}
}


void DEBUG_USART12_TO_USART21(void)
{
	if(DEBUG_PORT_REV_flag==1)
	{
		//把PC发来的数据发送到ESP01S
		Serial_SendString(ESP3201S_PORT,DEBUG_PORT_REV);
		//清除DEBUG_PORT_REV数据缓冲,方便下次使用
		Clear_Buff(DEBUG_PORT_REV,DEBUG_PORT_REV_index);
		DEBUG_PORT_REV_index=0;
		//清除接收完成标志位
		DEBUG_PORT_REV_flag=0;
	}
	if(ESP3201S_PORT_REV_flag==1)
	{
		//把ESP01S来的数据发送给PC
		Serial_SendString(DEBUG_PORT,ESP3201S_PORT_REV);
		Clear_Buff(ESP3201S_PORT_REV,ESP3201S_PORT_REV_index);
		ESP3201S_PORT_REV_index=0;
		ESP3201S_PORT_REV_flag=0;
	}

}


uint8_t getDEBUG_PORT_REV_index(void)
{
	if(DEBUG_PORT_REV_flag==1)
	{
		return DEBUG_PORT_REV_index;
	}else
	{
		return 0;
	}
}

uint8_t getESP3201S_PORT_REV_index(void)
{
	if(ESP3201S_PORT_REV_flag==1)
	{
		return ESP3201S_PORT_REV_index;
	}else
	{
		return 0;
	}
}

uint8_t getDEBUG_PORT_REV_flag(void)
{
	return DEBUG_PORT_REV_flag;
}

uint8_t getESP3201S_PORT_REV_flag(void)
{
	return ESP3201S_PORT_REV_flag;
}

void setESP3201S_PORT_REV_flag(uint8_t ESP3201S_PORT_REV_flag)
{
	ESP3201S_PORT_REV_flag=ESP3201S_PORT_REV_flag;
}

void setESP3201S_PORT_REV_index(uint8_t ESP3201S_PORT_REV_index)
{

	ESP3201S_PORT_REV_index = ESP3201S_PORT_REV_index;
}

char* getESP3201S_PORT_REV(void)
{
	
	if(ESP3201S_PORT_REV_flag)
	{
		strcpy(temp,ESP3201S_PORT_REV);
		return ESP3201S_PORT_REV;
	}else
	{
		return "";
	}

}



三,结果

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值