更改

跳转至: 导航搜索

LED 闪烁控制软件模块

添加7,920字节2019年12月9日 (一) 16:25
led闪烁模块说明初版结束
|清空栈里所有led状态,并将led设置灭状态
|
|}<!-- bsp_status_led.c函数定义 --><syntaxhighlight lang="c" line="1">/*blink status*//*{ 1.period -> 闪烁周期 2.duty -> 引脚高电平占整个周期的百分比 3.times -> 闪烁次数}*/#define LED_BLINK1_PERIOD 200#define LED_BLINK1_DUTY 50#define LED_BLINK1_ON_MS 100#define LED_BLINK1_OFF_MS (LED_BLINK1_PERIOD - LED_BLINK1_ON_MS) #define LED_BLINK2_PERIOD 1000#define LED_BLINK2_DUTY 10#define LED_BLINK2_ON_MS 100#define LED_BLINK2_OFF_MS (LED_BLINK2_PERIOD - LED_BLINK2_ON_MS) #define LED_BLINK3_PERIOD 1000#define LED_BLINK3_DUTY 50#define LED_BLINK3_ON_MS 500#define LED_BLINK3_OFF_MS (LED_BLINK3_PERIOD - LED_BLINK3_ON_MS) #define LED_BLINK4_PERIOD 2000#define LED_BLINK4_DUTY 5#define LED_BLINK4_ON_MS 100#define LED_BLINK4_OFF_MS (LED_BLINK4_PERIOD - LED_BLINK4_ON_MS)  #define LED_BLINK_STOP 0#define LED_BLINK_FOREVER 0xFF #define LED_ON true#define LED_OFF false //******************************************************************************// typedef area///*定义当前led工作状态信息*/typedef struct{ bool ledLevelFlag; //true -> high,false -> low uint8_t ledPointer;}ledBlinkInfo_t; #ifdef LED_PRORITY_EN//定义LED状态的栈空间typedef struct{ uint8_t stack[LED_STATUS_END]; //栈的最大空间 uint8_t num; //栈当前含有元素个数 }ledStack_t;#endif //******************************************************************************// variable define// static uint8_t ledBlinkTimesTable[LED_STATUS_END] = {0x00}; static ledBlinkInfo_t ledInfo = {LED_OFF,0x00}; #ifdef LED_PRORITY_ENstatic ledStack_t ledStack = {{0x00},0x00}; #endif//******************************************************************************// function define///*定时次的超时回调函数*/static void ledTimeoutCb(bool level); /*获取led定时时间*/static uint16_t ledGetBlinkTime(bool level); //******************************************************************************// fn : LED_Init//// brief : initial led module//// param : none//// return : nonevoid LED_Init(void){ ledModule.fnTimerInit(ledTimeoutCb); ledModule.fnPinInit(); ledInfo.ledLevelFlag = LED_OFF; ledInfo.ledPointer = NULL; memset(ledBlinkTimesTable,0,LED_STATUS_END); #ifdef LED_PRORITY_EN memset(&ledStack,0,sizeof(ledStack_t)); LED_StackPush(LED_STATUS_ON,0xFF); LED_StackPush(LED_STATUS_BLINK3,0xFF);#else LED_Set(LED_STATUS_BLINK3,0xFF);#endif}//******************************************************************************// fn : LED_Init//// brief : set the led status//// param : ledStatus -> 设置led状态// times -> Led闪烁次数,0xFF 表示一直执行,直到下一次状态改变//// return : nonevoid LED_Set(eLEDStatus ledStatus, uint8_t times){ if(LED_STATUS_END <= ledStatus) { return ; }// if(times == LED_BLINK_STOP)// {// return;// } ledInfo.ledPointer = ledStatus; ledInfo.ledLevelFlag = LED_OFF; ledBlinkTimesTable[ledStatus] = times; ledModule.fnTimerStop(); if(LED_BLINK_STOP == times) { ledModule.fnPinLevelSet(ledInfo.ledLevelFlag); return; } switch(ledStatus) { case LED_STATUS_OFF: { ledModule.fnPinLevelSet(ledInfo.ledLevelFlag); } break; case LED_STATUS_ON: { ledInfo.ledLevelFlag = LED_ON; ledModule.fnPinLevelSet(ledInfo.ledLevelFlag); } break; case LED_STATUS_BLINK1: { ledInfo.ledLevelFlag = LED_ON; ledModule.fnPinLevelSet(ledInfo.ledLevelFlag); ledModule.fnTimerStart(LED_BLINK1_ON_MS,&ledInfo.ledLevelFlag); } break; case LED_STATUS_BLINK2: { ledInfo.ledLevelFlag = LED_ON; ledModule.fnPinLevelSet(ledInfo.ledLevelFlag); ledModule.fnTimerStart(LED_BLINK2_ON_MS,&ledInfo.ledLevelFlag); } break; case LED_STATUS_BLINK3: { ledInfo.ledLevelFlag = LED_ON; ledModule.fnPinLevelSet(ledInfo.ledLevelFlag); ledModule.fnTimerStart(LED_BLINK3_ON_MS,&ledInfo.ledLevelFlag); } break; case LED_STATUS_BLINK4: { ledInfo.ledLevelFlag = LED_ON; ledModule.fnPinLevelSet(ledInfo.ledLevelFlag); ledModule.fnTimerStart(LED_BLINK4_ON_MS,&ledInfo.ledLevelFlag); } break; default : return; } }#ifdef LED_PRORITY_EN//******************************************************************************// fn : LED_StackPush//// brief : push the led status to ledstack.it will be placed at the top//// param : ledStatus -> 设置led状态// times -> Led闪烁次数,0x00 表示一直执行,直到下一次状态改变//// return : nonebool LED_StackPush(eLEDStatus ledStatus,uint8_t times){ if(LED_STATUS_END <= ledStatus) { return false; } if(times == LED_BLINK_STOP) { return false; } if(ledStack.num > LED_STATUS_END) { return false; } ledStack.stack[ledStack.num] = ledStatus; ledStack.num++; LED_Set(ledStatus,times); return true;}//******************************************************************************// fn : LED_StackPop//// brief : pop the top node from ledstack//// param : none//// return : nonevoid LED_StackPop(void){ if(ledStack.num == 0) { return; } ledStack.num -= 1; //停止当前LED状态 ledInfo.ledLevelFlag = LED_OFF; ledModule.fnTimerStop(); ledModule.fnPinLevelSet(ledInfo.ledLevelFlag); if(ledStack.num) { eLEDStatus led_id = (eLEDStatus)ledStack.stack[ledStack.num - 1]; uint8_t times = ledBlinkTimesTable[led_id]; if(times == LED_BLINK_STOP) { LED_StackPop(); } else { LED_Set(led_id,times); } }} //******************************************************************************// fn : LED_StackClear//// brief : clear the stack of led//// param : none//// return : nonevoid LED_StackClear(void){ ledStack.num = 0; //停止当前LED状态 ledInfo.ledLevelFlag = LED_OFF; ledModule.fnTimerStop(); ledModule.fnPinLevelSet(ledInfo.ledLevelFlag);} #endif//******************************************************************************// static functions define area////******************************************************************************// fn : ledTimeoutCb//// brief : the timeout callback to the led//// param : level -> true:pin high;false:pin low//// return : nonestatic void ledTimeoutCb(bool level){ uint16_t ms = 0; if(level == LED_ON) { ledInfo.ledLevelFlag = LED_OFF; } else { ledInfo.ledLevelFlag = LED_ON; if(LED_BLINK_FOREVER != ledBlinkTimesTable[ledInfo.ledPointer]) { ledBlinkTimesTable[ledInfo.ledPointer] -= 1; if(ledBlinkTimesTable[ledInfo.ledPointer] == LED_BLINK_STOP) {#ifdef LED_PRORITY_EN LED_StackPop(); //弹出当前led状态 return;#else return;#endif } } } ms = ledGetBlinkTime(ledInfo.ledLevelFlag); ledModule.fnPinLevelSet(ledInfo.ledLevelFlag); ledModule.fnTimerStart(ms,&ledInfo.ledLevelFlag); }//******************************************************************************// fn : ledGetBlinkTime//// brief : Get the time with led status//// param : level -> true:pin high;false:pin low//// return : timestatic uint16_t ledGetBlinkTime(bool level){ uint16_t ms = 0; switch(ledInfo.ledPointer) { case LED_STATUS_BLINK1: ms = level ? LED_BLINK1_ON_MS : LED_BLINK1_OFF_MS; break; case LED_STATUS_BLINK2: ms = level ? LED_BLINK2_ON_MS : LED_BLINK2_OFF_MS; break; case LED_STATUS_BLINK3: ms = level ? LED_BLINK3_ON_MS : LED_BLINK3_OFF_MS; break; case LED_STATUS_BLINK4: ms = level ? LED_BLINK4_ON_MS : LED_BLINK4_OFF_MS; break; default: break; } return ms; }</syntaxhighlight>
119
个编辑

本PDF由谷雨文档中心自动生成,点击下方链接阅读最新内容。

取自“http://doc.iotxx.com/特殊:移动版差异/2508

导航菜单