函数总结如下:只需要修改SampleApp.c下面的文件,格式如下。
NV 非易失性的
串口是以ascal码进行传输的 ‘0’=0x30
HAL_KEY_SW_1 对应第二个按键
HAL_KEY_SW_6 对应第一个按键
0x1000到0xFFFF这段flash是留给用户用的空间
当进行SampleApp_ProcessEvent( uint8 task_id, uint16 events )函数时候,系统自动肥培了一个ID放在task_id,自己定义时间的ID从task_id是一样的。
#include "OSAL.h"
#include "ZDApp.h"
#include "SampleApp.h"/* HAL */
#include "hal_led.h"
#include "hal_key.h"
#include "MT_UART.h"
#include "dht11.h"
#include "hal_adc.h"
#include "OSAL_Nv.h"
/* 包括串口头文件 */
#include "hal_uart.h"
/* 串口基本定义 */
#define MY_DEFINE_UART_PORT 0 //自定义串口号(0,1);
#define RX_MAX_LENGTH 20 //接收缓冲区最大值: 20个字节;
uint8 RX_BUFFER[RX_MAX_LENGTH]; //接收缓冲区;
void UartCallBackFunction(uint8 port , uint8 event); //回调函数声明,定义在最后面;
/* 配置串口 */
halUARTCfg_t uartConfig; //定义串口配置结构体变量;
void Uart_Config(void); //函数声明;
void Uart_Config(void) //函数定义;
{
uartConfig.configured = TRUE; //允许配置;
uartConfig.baudRate = HAL_UART_BR_115200;//波特率;
uartConfig.flowControl = FALSE;
uartConfig.flowControlThreshold = 64; //don't care - see uart driver.
uartConfig.rx.maxBufSize = 128; //串口接收缓冲区大小
uartConfig.tx.maxBufSize = 128; //串口发送缓冲区大小
uartConfig.idleTimeout = 6; //don't care - see uart driver.
uartConfig.intEnable = TRUE; //使能中断
uartConfig.callBackFunc = UartCallBackFunction; //指定回调函数名;
}
//按键事件处理函数声明
void SampleApp_HandleKeys( uint8 shift, uint8 keys );
uint8 sampleapp_id;
void SampleApp_Init( uint8 task_id )
{
unsigned char adc_val = 0;
sampleapp_id = task_id;
Uart_Config();
HalUARTOpen(MY_DEFINE_UART_PORT , &uartConfig); //打开串口
HalAdcInit();
adc_val = HalAdcRead(HAL_ADC_CHANNEL_0 , HAL_ADC_RESOLUTION_8);
RegisterForKeys( task_id ); // 登记所有的按键事件
}
uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )//应用层任务处理函数
{
afIncomingMSGPacket_t *MSGpkt;
(void)task_id; // Intentionally unreferenced parameter
unsigned char temp[2],humi[2];
char r_val;
if( events & DHT11_EVT )
{
//获取温湿度并且发送给电脑;
//获取温湿度
r_val = dht11_value(temp , humi , DHT11_STRING);
if(r_val == 0)
{
HalUARTWrite(MY_DEFINE_UART_PORT , "temp: " , 6);
HalUARTWrite(MY_DEFINE_UART_PORT , temp , 2);
HalUARTWrite(MY_DEFINE_UART_PORT , "C\r\n" , 3);
HalUARTWrite(MY_DEFINE_UART_PORT , "humi: " , 6);
HalUARTWrite(MY_DEFINE_UART_PORT , humi , 2);
HalUARTWrite(MY_DEFINE_UART_PORT , "%\r\n" , 3);
}
osal_start_timerEx( sampleapp_id , DHT11_EVT , 1000 ); //1000ms;
return (events ^ DHT11_EVT);
}
if ( events & SYS_EVENT_MSG ) //接收系统消息再进行判断
{
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( task_id );
while ( MSGpkt )
{
switch ( MSGpkt->hdr.event )
{
case KEY_CHANGE://按键事件
SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
break;
}
// Release the memory
osal_msg_deallocate( (uint8 *)MSGpkt );
// Next - if one is available
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( task_id );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
return 0;
}
unsigned char flag = 0;
void SampleApp_HandleKeys( uint8 shift, uint8 keys ) //按键事件处理函数
{
(void)shift; // Intentionally unreferenced parameter
if ( keys & HAL_KEY_SW_6 ) //S1
{
if(flag == 0)
{
flag = 1;
//启动事件;
osal_start_timerEx( sampleapp_id , DHT11_EVT , 1000 ); //1000ms启动事件;
}
else
{
flag = 0;
//关闭事件;
osal_stop_timerEx( sampleapp_id , DHT11_EVT );
}
}
}
static void UartCallBackFunction(uint8 port , uint8 event)//串口回调函数,接收到数据时会调用到该函数;
{
uint8 RX_Length = 0; //接收到字符串大小;
uint8 r_val;
uint8 w_val = 24;
uint8 r_s[3];
RX_Length = Hal_UART_RxBufLen(MY_DEFINE_UART_PORT); //读取接收字符串大小;
if(RX_Length != 0) //有数据存在;
{
//读取串口数据;
HalUARTRead(MY_DEFINE_UART_PORT , RX_BUFFER , RX_Length);
if(osal_memcmp(RX_BUFFER,"nvinit",6))
{
//初始化nv指定位置;
osal_nv_item_init( 0x1200 , 1 , NULL );
}
else if(osal_memcmp(RX_BUFFER,"nvread",6))//判断接受到的数据是否是"nvread",如果是,函数返回TURE
{
//读出nv中指定位置的数据;
osal_nv_read( 0x1200 , 0 , 1 , &r_val );
r_s[0] = r_val/10+'0';
r_s[1] = r_val%10+'0';
r_s[2] = '\n';
HalUARTWrite(MY_DEFINE_UART_PORT , r_s , 3);
}
else if(osal_memcmp(RX_BUFFER,"nvwrite",7))//判断接受到的数据是否是"nvwrite",如果是,函数返回TURE
{
osal_nv_write( 0x1200 , 0 , 1 , &w_val );
//往指定的NV位置写入一个数据;
}
}
}