Stm32F103与WiFi模块ESP8266 Station模式控制LED灯程序
#include "stm32f10x.h"
#include "string.h"
#include "stdio.h"
unsigned char UARTbuff[100];
unsigned char AT1[32]="AT\r\n";
unsigned char AT2[32]="AT+CWMODE=1\r\n";
unsigned char AT3[32]="AT+RST\r\n";
unsigned char AT4[64]="AT+CWJAP=\"12345\",\"12345678\" \r\n";
unsigned char AT5[32]="AT+CIPMUX=1\r\n";
unsigned char AT6[64]="AT+CIPSTART=\"TCP\",\"192.168.43.10\",8080\r\n";
unsigned char AT7[32]="AT+CIPMODE=1\r\n";
unsigned char AT8[32]="AT+CIPSEND\r\n";
unsigned char AT9[32]="+++\r";
int k=0;
static unsigned char count=0;
void delayms(unsigned int t)
{
unsigned x,y;
for(x=t;x>0;x--)
for(y=12400;y>0;y--);
}
void My_USART1_Init(unsigned long int bound)
{
GPIO_InitTypeDef GPIO_InitStrue;
USART_InitTypeDef USART_InitStrue;
NVIC_InitTypeDef NVIC_InitStrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);
USART_InitStrue.USART_BaudRate=bound;
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
USART_InitStrue.USART_Parity=USART_Parity_No;
USART_InitStrue.USART_StopBits=USART_StopBits_1;
USART_InitStrue.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStrue);
USART_Cmd(USART1,ENABLE);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStrue);
}
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
}
void sendchars(unsigned char *UARTbuff)
{
unsigned char i = 0;
while(UARTbuff[i]!= '\0')
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1,UARTbuff[i]);
USART_ClearFlag(USART1,USART_FLAG_TC);
i++;
}
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE))
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
UARTbuff[count]= USART_ReceiveData(USART1);
count++;
if(count>8)
{
count=0;
USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
k=1;
}
}
}
int main(void)
{
char a[15];
int i=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
LED_Init();
My_USART1_Init(115200);
delayms(3000);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
sendchars(AT1);
delayms(2000);
sendchars(AT2);
delayms(2000);
sendchars(AT3);
delayms(2000);
delayms(2000);
sendchars(AT4);
delayms(2000);
sendchars(AT6);
delayms(2000);
sendchars(AT7);
delayms(2000);
sendchars(AT8);
delayms(2000);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
while( UARTbuff[i++]!='\0')
UARTbuff[i-1]='\0';
while(1)
{
if(k==1)
{
k=0;
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
sprintf(a,"%s",UARTbuff);
sendchars(UARTbuff);
if(strstr((const char*)a,"oonled"))
{ GPIO_ResetBits(GPIOE,GPIO_Pin_5);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
while( UARTbuff[i++]!='\0')
UARTbuff[i-1]='\0';
}
if(strstr((const char*)a,"offled"))
{ GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
while( UARTbuff[i++]!='\0')
UARTbuff[i-1]='\0';
}
}
}
}
|