diff --git a/source/app/app_config.c b/source/app/app_config.c index befdc14..0bf77ee 100644 --- a/source/app/app_config.c +++ b/source/app/app_config.c @@ -18,6 +18,8 @@ void L3_param_init(void) G.tail[0] = TAIL0; G.tail[1] = TAIL1; + G.bat_percent = 0; + G.volume = 0x15; //U8 i; G.debug = 1; diff --git a/source/app/app_config.h b/source/app/app_config.h index 22489b3..6a536bf 100644 --- a/source/app/app_config.h +++ b/source/app/app_config.h @@ -107,6 +107,8 @@ typedef struct global_param U8 head[2]; U8 tail[2]; + // 电量百分比 + U8 bat_percent; // 喇叭音量 U8 volume; //调试模式 diff --git a/source/app/main.c b/source/app/main.c index ad7e48c..c57f6f9 100644 --- a/source/app/main.c +++ b/source/app/main.c @@ -58,6 +58,9 @@ void L0_TASK_init(void) //游戏任务初始化 L3_task_game_init(); + //adc任务初始化 + L3_task_Adc_init(); + // //游戏任务初始化 // L3_task_game_init(); // //寄存器监听任务初始化 @@ -126,12 +129,19 @@ void main(void) // L0_uart0_0d0a(); // L0_uart0_sendstr("oid_p"); // } + // 系统状态任务 L3_task_appstatus_handler(&_s_task_appstatus); + // 系统状态测试任务 // L3_task_AppTest_handler(&_s_task_apptest); + // 按键状态任务 L3_task_keystatus_handler(&_s_task_keystatus); + + // adc任务 + L3_task_Adc_handler(&_s_task_adc); + // OID和WIFI任务 if (R.app_status != POW_OFF) { diff --git a/source/app/main.h b/source/app/main.h index 458a269..51b7ad5 100644 --- a/source/app/main.h +++ b/source/app/main.h @@ -75,6 +75,7 @@ #include "../app/task_w600.h" #include "../app/task_SmartConfig.h" #include "../app/task_game.h" +#include "../app/task_adc.h" #include "../bsp/bsp_led.h" diff --git a/source/app/task_adc.c b/source/app/task_adc.c new file mode 100644 index 0000000..83be702 --- /dev/null +++ b/source/app/task_adc.c @@ -0,0 +1,62 @@ +#include "../app/task_adc.h" +#include "../msp/msp_adc.h" +#include "../app/app_config.h" +#include "../msp/uart0.h" + + + +S_TASK_ADC _s_task_adc; + + +void L3_task_Adc_init(void) +{ + L1_task_init(&_s_task_adc.task); + _s_task_adc.index = 0; + _s_task_adc.SUM = 0; + L3_task_s_go(_s_task_adc,D_task_init); +} + +#define D_task_ADC_READ 0x50 +#define D_task_ADC_COUNT 0x51 + + +void L3_task_Adc_handler(S_TASK_ADC *s) +{ + TTSS_Task_init() + L2_task_go_Tdelay(D_task_ADC_READ,D_Tdelay_300ms); + + TTSS_Task_step(D_task_ADC_READ) + s->adc_val[_s_task_adc.index] = L1_ADC_Read(12); + s->SUM += s->adc_val[_s_task_adc.index++]; + + if (_s_task_adc.index >= 10) + { + _s_task_adc.index = 0; + L2_task_go_Tdelay(D_task_ADC_COUNT,D_Tdelay_300ms); + } + else + { + L2_task_go_Tdelay(D_task_ADC_READ,D_Tdelay_300ms); + } + TTSS_Task_step(D_task_ADC_COUNT) + // 计算平均值 + U16 Aver = s->SUM / ADC_NUM; + // 计算方差 + U8 i; + FP32 Variance = 0; + for(i = 0; i < ADC_NUM; i++) + { + Variance += (s->adc_val[i] - Aver) * (s->adc_val[i] - Aver); + } + Variance /= ADC_NUM; + + // // 方差合格,计算电量,放到G,不合格丢弃 + // if ((U16)(Variance*10) < 2) + // { + // Vin = (Aver / 0x1000) * 3.3; + // Percent = Vin / 4.2 + // } + Aver = 0; + L2_task_go_Tdelay(D_task_ADC_READ,D_Tdelay_300ms); + TTSS_Task_end(); +} \ No newline at end of file diff --git a/source/app/task_adc.h b/source/app/task_adc.h new file mode 100644 index 0000000..1c9e289 --- /dev/null +++ b/source/app/task_adc.h @@ -0,0 +1,34 @@ +//////////////////////////////////////////////////////////////////////////// +///@copyright Copyright (c) 2018, 传控科技 All rights reserved. +///------------------------------------------------------------------------- +/// @file bsp_drv.h +/// @brief bsp @ driver config +///------------------------------------------------------------------------- +/// @version 1.0 +/// @author CC +/// @date 20180331 +/// @note cc_AS_stc02 + +////////////////////////////////////////////////////////////////////////////// + +#ifndef _TASK_ADC_H +#define _TASK_ADC_H + +#include "../ctask/task.h" + +#define ADC_NUM 10 + +typedef struct _s_task_adc +{ + TS_task task; + U16 adc_val[ADC_NUM]; + U8 index; + U16 SUM; +}S_TASK_ADC; + +extern S_TASK_ADC _s_task_adc; + +extern void L3_task_Adc_init(void); +extern void L3_task_Adc_handler(S_TASK_ADC *s); + +#endif // #ifndef