main.c文件
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd.h"
#include "wifi.h"
#include "key.h"
#include "timer.h"
#include "beep.h"
#include "lcd_init.h"
//技术支持:
int main(void)
{
u16 rlen=0;
int add;
u8 key;
u16 led0pwmval=0; //PWM用变量
u8 dir=1; //PWM初始递增
u8 revbuf[200] = {0}; //接收buffer
u8 heartbeat[5]={0x55,0x00,0x00,0x00,0xaa}; //心跳包格式
u8 ledflag = 1; //LED处理变量
u8 beepflag = 0;//蜂鸣器处理变量
u8 picflag = 0;//图片处理变量
u8 pwmflag = 1;//PWM处理变量
u8 beepfirst=1;//定义此变量来解决蜂鸣器上电后默认响的问题
u8 picjif=0;//定义此变量解决PWM时刷新图片导致PWM观察不明显的问题
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
delay_init(); //延时函数初始化
uart_init(115200); //串口1初始化为115200
wifi_usart2_init(115200);
LED_Init(); //初始化LED
LCD_Init(); //初始化LCD
KEY_Init(); //初始化KEY
BEEP_Init(); //初始化BEEP
TIM1_PWM_Init(899,0);//初始化TIM1
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);//进行清屏
LCD_ShowString(0,0,(u8*)"地址: LED: ",RED,WHITE);
LCD_ShowString(0,16,(u8*)"蜂鸣器: PWM: ",RED,WHITE);
add=esp8266_connect_ap_tcp("TZH-WiFi","88888888","192.168.0.25","8234");
if(add)
{
heartbeat[1] = add&0xff;
esp8266_send_serverdata(heartbeat,5);
}
LCD_ShowPicture(0,32,128,128,gImage_ta);
while(1)
{
key=KEY_Scan(0); //进行按键扫描 不检测连按
if( key == KEYLEFT_PRES ) //KEYLEFT按下,连接服务器
{
//连接wifi ap ssid ="TZHXXKJ" password = "1105_tzh" tcp server ip="192.168.1.147" port="8234"
add=esp8266_connect_ap_tcp((u8*)"TZH-WiFi","88888888","192.168.0.25","8234");
if(add)
{
heartbeat[1] = add&0xff;
esp8266_send_serverdata(heartbeat,5);
}
}
//-------------------------------------------------------------------------------------------------------
if(key==KEYRIGHT_PRES) //KEYRIGHT按下,往服务器发送数据
{
esp8266_send_serverdata((u8*)"123456789",9);
}
//-------------------------------------------------------------------------------------------------------
if(!ledflag) //LED 相关处理
{
LED1=0;
LCD_ShowString(112,0,(u8*)"开",RED,WHITE);
}
else
{
LED1=1;
LCD_ShowString(112,0,(u8*)"关",RED,WHITE);
}
if(!beepflag) //蜂鸣器 相关处理
{
if(beepfirst) //第一次上电时蜂鸣器不响
{
BEEP=0;
LCD_ShowString(56,16,(u8*)"关",RED,WHITE);
}
else
{
BEEP=1;
LCD_ShowString(56,15,(u8*)"开",RED,WHITE);
}
}
else
{
BEEP=0;
LCD_ShowString(56,16,(u8*)"关",RED,WHITE);
}
//-------------------------------------------------------------------------------------------------------
if(picflag) //动态图片相关处理
{
LCD_ShowPicture(0,32,128,128,gImage_gif1);
LCD_ShowPicture(0,32,128,128,gImage_gif2);
}
//-------------------------------------------------------------------------------------------------------
if(!pwmflag)//PWM 相关处理
{
if(picflag)//如果动态图片刷新为真
{
picjif=1;//记录该状态
picflag=0;//先禁止动态图片刷新 否则影响PWM观察
}
if(dir)led0pwmval += 15;
else led0pwmval -= 15;
if(led0pwmval>800)dir=0;
if(led0pwmval==0)dir=1;
TIM_SetCompare1(TIM1,led0pwmval);
LCD_ShowString(112,16,(u8*)"开",RED,WHITE);
}
else//PWM关闭
{
if(picjif)//如果记录状态为真,说明动态图片刷新被禁止
{
picjif=0;//清除该状态变量
picflag=1;//恢复动态图片刷新
}
TIM_SetCompare1(TIM1,0);
LCD_ShowString(112,16,(u8*)"关",RED,WHITE);
}
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
delay_ms(10);
rlen=esp8266_rev_serverdata(revbuf); //接收服务器发过来的数据
if(rlen)
{
if(revbuf[2] == 1) //LED 相关处理
{
ledflag=revbuf[3];
}
if(revbuf[2] == 2)//蜂鸣器 相关处理
{
beepfirst=0;
beepflag=revbuf[3];
}
if(revbuf[2] == 3)//图片显示 相关处理
{
switch(revbuf[3])
{
case 0x01:picflag=0;picjif=0;LCD_ShowPicture(0,32,128,128,gImage_ta);break;
case 0x02:picflag=0;picjif=0;LCD_ShowPicture(0,32,128,128,gImage_yun);break;
case 0x03:picflag=1;picjif=0;break;
default:
LCD_ShowPicture(0,32,128,128,gImage_ta);
}
}
if(revbuf[2] == 4) //PWM 相关处理
{
pwmflag=revbuf[3];
}
}
} //end while(1)
}
|