物联网小demo

发布于:2024-05-06 ⋅ 阅读:(22) ⋅ 点赞:(0)

机智云生成代码

具体参考之前的文章
在这里插入图片描述
在这里插入图片描述

初始化

ADC用来使用光敏电阻
在这里插入图片描述
在这里插入图片描述
连续采样开启 采样的周期调高
定时器
在这里插入图片描述
定时器1用来实现延时 为了只用温湿度模块DHT11
在这里插入图片描述

定时器4用来和51进行交互 实现定时的发送和检测心跳信号
IIC
在这里插入图片描述
用来使用oled屏幕
USART
在这里插入图片描述
串口1和串口2是机智云自己生成的 串口一用来打印esp8266工作状态
串口2用来与esp8266进行通信
串口三是用来与51进行通信
GPIO
在这里插入图片描述
PA4是用来使用烟雾传感器 使用烟雾传感器的数字信号输出 DO
PB5用来模拟客厅灯

代码编写

以下代码都是在在这里插入图片描述
中编写

oled模块实现
找到oled.c和相关的头文件在这里插入图片描述
头文件
在这里插入图片描述
源文件
添加到这个项目的Inc和Src中
在keil中添加源文件到User 注意这里添加源文件后编译一下可能会有报错 因为进来的不一定默认为c文件的形式 需要手动设置在这里插入图片描述
在这里插入图片描述
把初始化代码丢到user_Init函数中去 在这里插入图片描述
一个字占用16个像素点 如果需要让字体居中就第一行留16个像素点 最后一行也留16个像素点 所以第一个字的坐标是从16开始的 第二个参数0是第一行 注意第一行是0 第二行的行标是2 第三行是4 依次往下
取字模软件在最后一个参数是第几个字
在这里插入图片描述
在这里插入图片描述
在这个h文件中最后一个数组粘贴字模
DHT11
同样找到.c和.H文件 添加
在这里插入图片描述
调用DHT_Read()函数 注意这里的的Read函数是放在handle中的 在while循环中这个User_handle是一直在被调用的 如果要持续读取温湿度需要放在while()循环中 结构体赋值是为了上传到机智云平台 sprintf函数是将 数据转化为字符串 所以字符串的形式打印在oled上 (注意需要先定义一个长度为30的缓冲区 OLED_BUFF)
H表示湿度 T表示温度 百分号使用两个百分号来表示
35表示从两个字之后开始显示(一个字的占用的像素点是16 两个是32 )2表示第二行 oled-buff是缓冲区 字符的大小是16
烟雾传感器
在这里插入图片描述
因为使用传感器的数字信号输出 直接读取高低电平获取烟雾 烟雾传感器后面有一个旋钮 用来设置阈值 一旦烟雾浓度超过阈值 数字信号输出就会从高电平变为低电平 oled就会由Y变为N
光敏电阻
ADC 首先在user_Init初始化函数中调用一下这个函数 HAL_ADCEx_Calibration_Start(&hadc1);
在这里插入图片描述
因为我们只用到一个通道 所以定义一个32位的变量ad_value
在这里插入图片描述
如果想了解ADC建议移步ADC使用

我们使用的是查询方式 HAL_OK是函数执行OK就是函数执行正常 可以不用加

在这里插入图片描述
同样将数据转化为字符串打印在oled上
在这里插入图片描述
数据上传到机智云平台 12位ADC数字最大为4095 显示光强百分数
LED在这里插入图片描述
找到时间led1 机智云自己生成
在这里插入图片描述
当连接wifi的手机在机智云平台上按下led的开关 单片机就会进入这个if语句执行读写操作 关闭就进入else
但记住 开关只有一次 if else 也只会进入一次

32和51的交互

32和51通过32的串口三进行交互 找到32对应的串口三的Tx和Rx 与51的Tx和Rx连接 32比喻为客厅 51比喻为卧室 当在手机上打开卧室开关32通过串口发送给到51 51执行相应的命令
同时51会有一个应答信号 告诉32自己是否收到
代码实现 在这里插入图片描述
手机上按下led2开关 拉高标志位
在这里插入图片描述
当标志位为1时候32不停的发送命令给到51 (要在while函数中) 51通过串口接受命令在这里插入图片描述
做出对应的动作
这里发送的数据是有格式的 一帧数据分为帧头 数据 CRC 帧尾 帧头为0x55 帧尾为0xAA 数据就是0x1 表示这个命令是让卧室打开灯 CRC=帧头加上数据
在这里插入图片描述
调用函数发送
51收到后判断是不是有效指令

在这里插入图片描述
接收到指令 回复应答信号在这里插入图片描述
注意32发送分51的指令是四个位 而51给32的应答信号是5个位
32通过串口中断接收51的应答信号
首先要实现32串口中断的功能 在这里插入图片描述
然后再串口初始化中打开串口中断在这里插入图片描述
注意这里由于机智云 将串口和定时器的初始化全部打包放在在这里插入图片描述
所以对应的 串口的接收中断回调函数和定时器中断回调函数也在这个文件中
之后要是工程中用到了外设的初始化 一定要注意寻找是否将回调函数和初始化打包放在了一起 不然自己重新写会导致工程错误!!!!
在这里插入图片描述
判断数据是否正确 然后根据应答信号的数据位 判断是之前那个指令的应答信号 如这个0x5 就是之前打开卧室灯 0x1+4 =0x5指令的应答信号 然后拉低标志位 使得32不再一直发送命令指令到51 从而完成交互
其他的也是如此 不过多赘述
心跳信号
32通过定时器4实现每隔一段时间发送一帧心跳信号
在这里插入图片描述
自动重装数值
在这里插入图片描述
通过zig_bee 这个变量来实现定时发送
在这里插入图片描述

在这里插入图片描述
当这个变量大于80一段时间没有接收到51发来的心跳信号 oled显示err 在这里插入图片描述

如果收到这个变量会自动清零
32代码

/**
************************************************************
* @file         gizwits_product.c
* @brief        Gizwits control protocol processing, and platform-related       hardware initialization 
* @author       Gizwits
* @date         2017-07-19
* @version      V03030000
* @copyright    Gizwits
* 
* @note         机智云.只为智能硬件而生
*               Gizwits Smart Cloud  for Smart Products
*               链接|增值ֵ|开放|中立|安全|自有|自由|生态
*               www.gizwits.com
*
***********************************************************/

#include <stdio.h>
#include <string.h>
#include "hal_key.h"
#include "gizwits_product.h"
#include "common.h"
#include "oled.h"
#include "dht11.h"
#include "adc.h"

static uint32_t timerMsCount;
uint8_t aRxBuffer;
uint8_t OLED_BUFF[30];
uint32_t ad_value;
uint8_t uart3_send_buff[4];
uint8_t uart3_recv_buff[5];
uint8_t curtain_on_cmd = 0,curtain_off_cmd = 0,led_on_cmd = 0,led_off_cmd = 0;
uint8_t zig_count = 81;
/** User area the current device state structure*/
dataPoint_t currentDataPoint;

extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim4;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;

/**@} */
/**@name Gizwits User Interface
* @{
*/

/**
* @brief Event handling interface

* Description:

* 1. Users can customize the changes in WiFi module status

* 2. Users can add data points in the function of event processing logic, such as calling the relevant hardware peripherals operating interface

* @param [in] info: event queue
* @param [in] data: protocol data
* @param [in] len: protocol data length
* @return NULL
* @ref gizwits_protocol.h
*/
int8_t gizwitsEventProcess(eventInfo_t *info, uint8_t *gizdata, uint32_t len)
{
    uint8_t i = 0;
    dataPoint_t *dataPointPtr = (dataPoint_t *)gizdata;
    moduleStatusInfo_t *wifiData = (moduleStatusInfo_t *)gizdata;
    protocolTime_t *ptime = (protocolTime_t *)gizdata;
    
#if MODULE_TYPE
    gprsInfo_t *gprsInfoData = (gprsInfo_t *)gizdata;
#else
    moduleInfo_t *ptModuleInfo = (moduleInfo_t *)gizdata;
#endif

    if((NULL == info) || (NULL == gizdata))
    {
        return -1;
    }

    for(i=0; i<info->num; i++)
    {
        switch(info->event[i])
        {
        case EVENT_led1:
            currentDataPoint.valueled1 = dataPointPtr->valueled1;
            GIZWITS_LOG("Evt: EVENT_led1 %d \n", currentDataPoint.valueled1);
            if(0x01 == currentDataPoint.valueled1)
            {
							HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,0);
							OLED_ShowString(0,6,"LED:ON ",16);
            //user handle
            }
            else
            {
							HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,1);
							OLED_ShowString(0,6,"LED:Off",16);
            //user handle    
            }
            break;
        case EVENT_led2:
            currentDataPoint.valueled2 = dataPointPtr->valueled2;
            GIZWITS_LOG("Evt: EVENT_led2 %d \n", currentDataPoint.valueled2);
            if(0x01 == currentDataPoint.valueled2)
            {
							led_on_cmd = 1;
            //user handle
            }
            else
            {
							led_off_cmd = 1;
            //user handle    
            }
            break;
        case EVENT_curtain:
            currentDataPoint.valuecurtain = dataPointPtr->valuecurtain;
            GIZWITS_LOG("Evt: EVENT_curtain %d \n", currentDataPoint.valuecurtain);
            if(0x01 == currentDataPoint.valuecurtain)
            {
							curtain_on_cmd = 1;
            //user handle
            }
            else
            {
							curtain_off_cmd = 1;
            //user handle    
            }
            break;




        case WIFI_SOFTAP:
            break;
        case WIFI_AIRLINK:
            break;
        case WIFI_STATION:
            break;
        case WIFI_CON_ROUTER:
 
            break;
        case WIFI_DISCON_ROUTER:
 
            break;
        case WIFI_CON_M2M:
 
            break;
        case WIFI_DISCON_M2M:
            break;
        case WIFI_RSSI:
            GIZWITS_LOG("RSSI %d\n", wifiData->rssi);
            break;
        case TRANSPARENT_DATA:
            GIZWITS_LOG("TRANSPARENT_DATA \n");
            //user handle , Fetch data from [data] , size is [len]
            break;
        case WIFI_NTP:
            GIZWITS_LOG("WIFI_NTP : [%d-%d-%d %02d:%02d:%02d][%d] \n",ptime->year,ptime->month,ptime->day,ptime->hour,ptime->minute,ptime->second,ptime->ntp);
            break;
        case MODULE_INFO:
            GIZWITS_LOG("MODULE INFO ...\n");
#if MODULE_TYPE
            GIZWITS_LOG("GPRS MODULE ...\n");
            //Format By gprsInfo_t
            GIZWITS_LOG("moduleType : [%d] \n",gprsInfoData->Type);
#else
            GIZWITS_LOG("WIF MODULE ...\n");
            //Format By moduleInfo_t
            GIZWITS_LOG("moduleType : [%d] \n",ptModuleInfo->moduleType);
#endif
        break;
        default:
            break;
        }
    }

    return 0;
}

/**
* User data acquisition

* Here users need to achieve in addition to data points other than the collection of data collection, can be self-defined acquisition frequency and design data filtering algorithm

* @param none
* @return none
*/
void userHandle(void)
{
	DHT_Read();
	
	HAL_ADC_Start(&hadc1);
	HAL_ADC_PollForConversion(&hadc1,10);
	ad_value=HAL_ADC_GetValue(&hadc1);

	currentDataPoint.valuesmoke = HAL_GPIO_ReadPin(SOMKE_GPIO_Port, SOMKE_Pin);
	currentDataPoint.valuetemperature1 = 	Data[0];
	currentDataPoint.valuewet1 = Data[2];
	currentDataPoint.valuesun1 = 100-(ad_value/4095.0*100);
	
	sprintf(OLED_BUFF,"H:%2d%% T:%2dC",currentDataPoint.valuetemperature1,currentDataPoint.valuewet1);
	OLED_ShowString(35,2,OLED_BUFF,16);
			if(HAL_GPIO_ReadPin(SOMKE_GPIO_Port, SOMKE_Pin))
		{
			OLED_ShowString(0,4,"smog:Y",16);
		}
		else
		{
			OLED_ShowString(0,4,"smog:N",16);
		}
		
		sprintf(OLED_BUFF,"sun:%2d%%",currentDataPoint.valuesun1);
		OLED_ShowString(64,4,OLED_BUFF,16);
		if(zig_count>80)//没收到心跳信号
			{
			OLED_ShowString(64,6,"Zig:ERR",16);
			}
		else
		{
			OLED_ShowString(64,6,"Zig:OK ",16);	
		}
	
 /*
    currentDataPoint.valuesmoke = ;//Add Sensor Data Collection
    currentDataPoint.valuetemperature1 = ;//Add Sensor Data Collection
    currentDataPoint.valuewet1 = ;//Add Sensor Data Collection
    currentDataPoint.valuesun1 = ;//Add Sensor Data Collection
    currentDataPoint.valuetemperature2 = ;//Add Sensor Data Collection
    currentDataPoint.valuewet2 = ;//Add Sensor Data Collection

    */
		if(led_on_cmd)//打开卧室灯
		{
			uart3_send_buff[0]= 0x55;//帧头
			uart3_send_buff[1]= 0x1;//数据指令
			uart3_send_buff[2]= uart3_send_buff[0]+uart3_send_buff[1];//CRC
			uart3_send_buff[3]= 0xAA;//帧尾
			HAL_UART_Transmit(&huart3, uart3_send_buff, 4, 100);
		}
		
		if(led_off_cmd)//关闭卧室灯
		{
			uart3_send_buff[0]= 0x55;
			uart3_send_buff[1]= 0x2;
			uart3_send_buff[2]= uart3_send_buff[0]+uart3_send_buff[1];
			uart3_send_buff[3]= 0xAA;
			HAL_UART_Transmit(&huart3, uart3_send_buff, 4, 100);
		}
		
		if(curtain_on_cmd)//打开窗帘
		{
			uart3_send_buff[0]= 0x55;
			uart3_send_buff[1]= 0x3;
			uart3_send_buff[2]= uart3_send_buff[0]+uart3_send_buff[1];
			uart3_send_buff[3]= 0xAA;
			HAL_UART_Transmit(&huart3, uart3_send_buff, 4, 100);
		}
		
		if(curtain_off_cmd)//关闭窗帘
		{
			uart3_send_buff[0]= 0x55;
			uart3_send_buff[1]= 0x4;
			uart3_send_buff[2]= uart3_send_buff[0]+uart3_send_buff[1];
			uart3_send_buff[3]= 0xAA;
			HAL_UART_Transmit(&huart3, uart3_send_buff, 4, 100);
		}
		

	}
    


/**
* Data point initialization function

* In the function to complete the initial user-related data
* @param none
* @return none
* @note The developer can add a data point state initialization value within this function
*/
void userInit(void)
{
    memset((uint8_t*)&currentDataPoint, 0, sizeof(dataPoint_t));
    
		OLED_Init();
		OLED_Clear();
		OLED_ShowCHinese(16,0,0);//一个字16个像素点 从中间开始 两头剩余32
		OLED_ShowCHinese(32,0,1);
		OLED_ShowCHinese(48,0,2);
		OLED_ShowCHinese(64,0,3);
		OLED_ShowCHinese(80,0,4);
		OLED_ShowCHinese(96,0,5);
		OLED_ShowCHinese(0,2,6);//2代表第二行
		OLED_ShowCHinese(16,2,7);

	
		OLED_ShowString(0,6,"LED:Off",16);
		HAL_ADCEx_Calibration_Start(&hadc1);//adc的采集函数 需要事先使能
	

    /** Warning !!! DataPoint Variables Init , Must Within The Data Range **/ 
    /*
    currentDataPoint.valuesmoke = ;
    currentDataPoint.valueled1 = ;
    currentDataPoint.valueled2 = ;
    currentDataPoint.valuecurtain = ;
    currentDataPoint.valuetemperature1 = ;
    currentDataPoint.valuewet1 = ;
    currentDataPoint.valuesun1 = ;
    currentDataPoint.valuetemperature2 = ;
    currentDataPoint.valuewet2 = ;
    */

}


/**
* @brief Millisecond timing maintenance function, milliseconds increment, overflow to zero

* @param none
* @return none
*/
void gizTimerMs(void)
{
    timerMsCount++;
}

/**
* @brief Read millisecond count

* @param none
* @return millisecond count
*/
uint32_t gizGetTimerCount(void)
{
    return timerMsCount;
}

/**
* @brief MCU reset function

* @param none
* @return none
*/
void mcuRestart(void)
{
    __set_FAULTMASK(1);
    HAL_NVIC_SystemReset();
}

/**@} */

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
 
  return ch;
}

/**
  * @brief  Period elapsed callback in non blocking mode 
  * @param  htim : TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//定时器回调函数
{
	if(htim==&htim2)
	{
			keyHandle();
			gizTimerMs();
	}
			if(htim==&htim4)
	{
			zig_count++;
			if(zig_count>100)
				zig_count=90;
	}
}

/**
* @brief Timer TIM3 init function

* @param none
* @return none
*/
void timerInit(void)//定时器初始化
{
	HAL_TIM_Base_Start_IT(&htim2);
	HAL_TIM_Base_Start_IT(&htim4);


}

/**
  * @brief  This function handles USART IDLE interrupt.
  */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef*UartHandle)//串口回调函数
{  
    if(UartHandle->Instance == USART2)  
    {  
				gizPutData((uint8_t *)&aRxBuffer, 1);

        HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer, 1);//开启下一次接收中断  
    }  
		
		
		 if(UartHandle->Instance == USART3)//判断是否是串口3收到的数据
    {  
			if(uart3_recv_buff[0] == 0x55&&uart3_recv_buff[0]+uart3_recv_buff[1]+uart3_recv_buff[2] == uart3_recv_buff[3]&&uart3_recv_buff[4] == 0xAA)
			{
				switch(uart3_recv_buff[1])
				{
						case 0x5:
							led_on_cmd=0;
							break;
						case 0x6:
							led_off_cmd=0;
							break;
						case 0x7:
							curtain_on_cmd=0;
							break;
						case 0x8:
							curtain_off_cmd=0;
							break;
						case 0x9:
							 currentDataPoint.valuetemperature2 = uart3_recv_buff[2];//Add Sensor Data Collection 卧室温度
							break;
						case 0xa:
							currentDataPoint.valuewet2 = uart3_recv_buff[2];//Add Sensor Data Collection 卧室湿度
							break;
						case 0xb:
							zig_count=0;
							break;
						default:break;
					
				}
						
			}	
				HAL_UART_Receive_IT(&huart3,uart3_recv_buff, 5);
    }  
}  

/**
* @brief USART init function

* Serial communication between WiFi modules and device MCU
* @param none
* @return none
*/
void uartInit(void)
{
	HAL_UART_Receive_IT(&huart2, (uint8_t *)&aRxBuffer, 1);//开启下一次接收中断  
	HAL_UART_Receive_IT(&huart3,uart3_recv_buff,5);
}

/**
* @brief Serial port write operation, send data to WiFi module
*
* @param buf      : buf address
* @param len      : buf length
*
* @return : Return effective data length;-1,return failure
*/
int32_t uartWrite(uint8_t *buf, uint32_t len)
{
		uint8_t crc[1] = {0x55};
    uint32_t i = 0;
	
    if(NULL == buf)
    {
        return -1;
    }

    for(i=0; i<len; i++)
    {
        HAL_UART_Transmit_IT(&huart2, (uint8_t *)&buf[i], 1);
				while (huart2.gState != HAL_UART_STATE_READY);//Loop until the end of transmission

        if(i >=2 && buf[i] == 0xFF)
        {
						HAL_UART_Transmit_IT(&huart2, (uint8_t *)&crc, 1);
						while (huart2.gState != HAL_UART_STATE_READY);//Loop until the end of transmission
        }
    }

#ifdef PROTOCOL_DEBUG
    GIZWITS_LOG("MCU2WiFi[%4d:%4d]: ", gizGetTimerCount(), len);
    for(i=0; i<len; i++)
    {
        GIZWITS_LOG("%02x ", buf[i]);

        if(i >=2 && buf[i] == 0xFF)
        {
            GIZWITS_LOG("%02x ", 0x55);
        }
    }
    GIZWITS_LOG("\n");
#endif
		
		return len;
}  

51代码

#include "reg52.h"
#include <intrins.h>
#include "delay.h"
#include "uart.h"
#include "main.h"
#include "oled.h"
//#include "bmp.h"
#include "dht11.h"
#include <string.h>
#include <stdio.h>

uchar recv;
uchar Recv_Buf[4];
uchar Send_buf[5];
uchar OLED_Buf[15];
char step_cw_flag=0,step_ccw_flag=0;
int H,T;

uchar phasecw[4] ={0x08,0x04,0x02,0x01};
uchar phaseccw[4]={0x01,0x02,0x04,0x08};
void main()
{
	int i=0,y;
	UART_Init();
	OLED_Init();			//初始化OLED  
	OLED_Clear(); 
	OLED_ShowCHinese(16,0,0);
	OLED_ShowCHinese(32,0,1);
	OLED_ShowCHinese(48,0,2);
	OLED_ShowCHinese(64,0,3);
	OLED_ShowCHinese(80,0,4);
	OLED_ShowCHinese(96,0,5);
	OLED_ShowCHinese(0,2,6);
	OLED_ShowCHinese(16,2,7);
	sprintf(OLED_Buf,"Curtain:OFF",H,T);
	OLED_ShowString(0,6,OLED_Buf,16);	
	while(1)
  {
		DHT11_Read_Byte(&H, &T);	//卧室通过dht11获得温度 串口发送给32上传到云平台
		sprintf(OLED_Buf,"H:%2d%% T:%2dC",H,T);
		OLED_ShowString(0,4,OLED_Buf,16);
		Send_buf[0]=0x55;
		Send_buf[1]=0x9;
		Send_buf[2]=T;
		Send_buf[3]=Send_buf[0]+Send_buf[1]+Send_buf[2];
		Send_buf[4]=0xAA;				 
		for(i=0;i<5;i++)
		UART_Send_Byte(Send_buf[i]);
		Delay350ms();
		
		Send_buf[0]=0x55;//卧室通过dht11获得湿度 串口发送给32上传到云平台
		Send_buf[1]=0xA;
		Send_buf[2]=H;
		Send_buf[3]=Send_buf[0]+Send_buf[1]+Send_buf[2];
		Send_buf[4]=0xAA;				 
		for(i=0;i<5;i++)
		UART_Send_Byte(Send_buf[i]);		
		Delay350ms();
		
		Send_buf[0]=0x55;//心跳信号 51发送给32
		Send_buf[1]=0xB;
		Send_buf[2]=0xB;
		Send_buf[3]=Send_buf[0]+Send_buf[1]+Send_buf[2];
		Send_buf[4]=0xAA;				 
		for(i=0;i<5;i++)
		UART_Send_Byte(Send_buf[i]);
		Delay350ms();		
		
		if(step_cw_flag)//标志位为高 收到开窗帘指令
		{
			sprintf(OLED_Buf,"Curtain:ON ",H,T);
			OLED_ShowString(0,6,OLED_Buf,16);
			for(y=0;y<200;y++) //步进电机的正转一次
			{
				for(i=0;i<4;i++)
				{
					P2 = phasecw[i];
					Delay5ms();	
				}
			}
			step_cw_flag=0;//拉低 正转一次
		}
		
		if(step_ccw_flag)//标志位为高 收到关窗帘指令
		{
			sprintf(OLED_Buf,"Curtain:OFF",H,T);
			OLED_ShowString(0,6,OLED_Buf,16);
			for(y=0;y<200;y++)//反转一次
			{
				for(i=0;i<4;i++)
				{
					P2 = phaseccw[i];
					Delay5ms();	
				}
			}
			step_ccw_flag=0;//拉低
		}
  }
}

UART_Routine(void) interrupt 4
{
	static int i = 0;  //static 
	int x=0;
	char Recv;
	if(1 == RI) //if(RI)
	{
		RI=0;
		Recv=SBUF;
		if(Recv == 0X55)
		{
			i = 0;
			Recv_Buf[i] = Recv;	
		}else{
			i++;
			Recv_Buf[i] = Recv;		
		}

		if(0x55==Recv_Buf[0]&&Recv_Buf[0]+Recv_Buf[1]==Recv_Buf[2]&&Recv_Buf[3]==0xAA)//判断是否是有效指令
		{
			switch(Recv_Buf[1])//根据数据指令做出对应动作
			{
				 case 0x1:  //开灯
						LED1=0;LED2=0;LED3=0;LED4=0;
						Send_buf[0]=0x55;
						Send_buf[1]=Recv_Buf[1]+0x4;
				 		Send_buf[2]=Recv_Buf[1]+0x4;
						Send_buf[3]=Send_buf[0]+Send_buf[1]+Send_buf[2];
						Send_buf[4]=0xAA;				 
				    for(x=0;x<5;x++)
				 		UART_Send_Byte(Send_buf[x]);								 
				   break;   
				 case 0x2: //关灯
					  P1|=0x0F;
						Send_buf[0]=0x55;
						Send_buf[1]=Recv_Buf[1]+0x4;
				 		Send_buf[2]=Recv_Buf[1]+0x4;
						Send_buf[3]=Send_buf[0]+Send_buf[1]+Send_buf[2];
						Send_buf[4]=0xAA;				 
				    for(x=0;x<5;x++)
				 		UART_Send_Byte(Send_buf[x]);
				    break;
				 case 0x3: //开窗帘
					  step_cw_flag=1;//拉高标志位
						Send_buf[0]=0x55;
						Send_buf[1]=Recv_Buf[1]+0x4;
				 		Send_buf[2]=Recv_Buf[1]+0x4;
						Send_buf[3]=Send_buf[0]+Send_buf[1]+Send_buf[2];
						Send_buf[4]=0xAA;				 
				    for(x=0;x<5;x++)
				 		UART_Send_Byte(Send_buf[x]);
				    break;
				 case 0x4: //关窗帘
						step_ccw_flag=1;
						Send_buf[0]=0x55;
						Send_buf[1]=Recv_Buf[1]+0x4;
				 		Send_buf[2]=Recv_Buf[1]+0x4;
						Send_buf[3]=Send_buf[0]+Send_buf[1]+Send_buf[2];
						Send_buf[4]=0xAA;				 
				    for(x=0;x<5;x++)
				 		UART_Send_Byte(Send_buf[x]);
				    break;
				 default:break;
			}	
			for(x=0;x<4;x++)
			Recv_Buf[x]=0;			
		}	
		if(i>4)
			i=0;
	}
}