函数总结如下:只需要修改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位置写入一个数据;
}