本帖最后由 昱枫 于 2020-11-28 17:08 编辑
最近学习关于一种对象的思维进行MCU的开发
写程序就有点像写系统那种
目前写了一个输出的控制的API
以LED控制为实例
1.定义输出对象功能如下
<blockquote>#ifndef __SIG_OUTPUT_H__
#include "sig_output.h"
#define SIG_OUTPUT_NUM 100
SIG_OUTPUT_MGR *sigOutputMgr[SIG_OUTPUT_NUM]={NULL};
uint32_t u32tick=0;
void runTick(void)
{
u32tick++;
}
uint32_t getTickNowMs(void)
{
return u32tick;
}
uint32_t getTickRunMs(uint32_t u32StartTick)
{
uint32_t u32RunTick=0;
if(u32StartTick > getTickNowMs())
{
u32RunTick = (0xFFFFFFFF - u32StartTick) + getTickNowMs();
}
else
{
u32RunTick = getTickNowMs()- u32StartTick;
}
return u32RunTick;
}
void sigOutputInit(SIG_OUTPUT_MGR *sigOutput,
pFunIsRunState pIsRun,
pFunSetState pSetHigh,
pFunSetState pSetLow)
{
uint8_t i = 0;
for(i=0;i<SIG_OUTPUT_NUM;i++)
{
if(NULL == sigOutputMgr[i])
{
sigOutputMgr[i] = sigOutput;
sigOutput->event = SIG_OUT_INIT;
sigOutput->pIsRun = pIsRun;
sigOutput->pSetHigh = pSetHigh;
sigOutput->pSetLow = pSetLow;
return;
}
}
}
void sigOutputHdl(SIG_OUTPUT_MGR *sigOutput)
{
//printf("sigOutputHdl enent %d ! \r\n",sigOutput->event);
switch(sigOutput->event)
{
case SIG_OUT_INIT:
sigOutput->state = SIG_OUT_IDLE_STATE;
break;
case SIG_OUT_LOW:
(sigOutput->pSetLow)();
sigOutput->state = SIG_OUT_LOW_STATE;
break;
case SIG_OUT_HIGH:
(sigOutput->pSetHigh)();
sigOutput->state = SIG_OUT_HIGH_STATE;
break;
case SIG_OUT_BLIINK:
{
//printf(" SIG_OUT_BLIINK state %d ! \r\n",sigOutput->state);
switch(sigOutput->state)
{
case SIG_OUT_LOW_STATE:
if( getTickRunMs(sigOutput->u32TimeVal) >= sigOutput->u32SetLowCount )
{
(sigOutput->pSetHigh)();
sigOutput->state = SIG_OUT_HIGH_STATE;
sigOutput->u32TimeVal = getTickNowMs();
//printf(" TO SIG_OUT_HIGH_STATE ! \r\n");
}
break;
case SIG_OUT_HIGH_STATE:
if( getTickRunMs(sigOutput->u32TimeVal) >= sigOutput->u32SetHighCount )
{
sigOutput->state = SIG_OUT_LOW_STATE;
sigOutput->u32TimeVal = getTickNowMs();
//printf(" TO SIG_OUT_LOW_STATE! \r\n");
sigOutput->u32Count--;
if(sigOutput->u32Count==0)
{
sigOutput->state = SIG_OUT_IDLE_STATE;
//printf(" TO SIG_OUT_IDLE_STATE! \r\n");
}
(sigOutput->pSetLow)();
}
break;
case SIG_OUT_IDLE_STATE:
if(sigOutput->u32Count > 0)
{
(sigOutput->pSetLow)();
sigOutput->state = SIG_OUT_LOW_STATE;
sigOutput->u32TimeVal = getTickNowMs();
//printf(" TO SIG_OUT_LOW_STATE! \r\n");
}
break;
default:
sigOutput->state = SIG_OUT_IDLE_STATE;
//printf("sigOutputHdl default ! \r\n");
break;
}
}
break;
default:
sigOutput->state = SIG_OUT_IDLE_STATE;
break;
}
}
void sigOutputTask(void)
{
uint8_t i = 0;
runTick();
for(i=0;i<SIG_OUTPUT_NUM;i++)
{
if(NULL != sigOutputMgr[i])
{
if(sigOutputMgr[i]->pIsRun())
{
sigOutputHdl(sigOutputMgr[i]);
//printf("sigOutputTask %d tick %d! \r\n",i, getTickNowMs());
}
}
}
}
//high low
void sigOutputReq(SIG_OUTPUT_MGR *sigOutput,SIG_OUT_EVENT event)
{
sigOutput->event = event;
}
//blink
void sigOutputBlinkReq(SIG_OUTPUT_MGR *sigOutput,SIG_OUT_EVENT event,
uint32_t u32Count,
uint32_t u32SetHighCount,
uint32_t u32SetLowCount)
{
sigOutput->event = event;
sigOutput->u32Count = u32Count;
sigOutput->u32SetHighCount = u32SetHighCount;
sigOutput->u32SetLowCount = u32SetLowCount;
}
实例应用函数
<blockquote>#include "app.h"
主函数
int main(void)
{
uint32_t i=0;
uint32_t x =0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Delay_Init();
USART_Printf_Init(115200);
ledOutputInit();
printf("SystemClk:%d\r\n",SystemCoreClock);
//printf("EXTI0 Test\r\n");
//EXTI0_INT_INIT();
//57.459 - 41.886 = 15.6s SystemCoreClock/100
//34.574- 33.014 =1.56s SystemCoreClock/1000 = 72000
//34.574- 33.014 =1.56s SystemCoreClock/1000 = 72000
//1ms 720/8/2 = 180/4 = 45
ledBlink(10,COUNT_X_MS(200), COUNT_X_MS(200));
while(1)
{
sigOutputTask(); //信号输出任务
if(getLedIsCountZero())
{
i += 1;
if(i>100)
{
i = 1;
x++;
}
//printf("COUNT_X_MS i = %d\r\n",i);
if(1 == x%2)
{
ledBlink(1,COUNT_X_MS(i), COUNT_X_MS(100 - i));
}
else
{
ledBlink(1,COUNT_X_MS(100 - i), COUNT_X_MS(i));
}
}
}
}
逻辑分析仪获取信号状态
可以看见
占空比由 1% - 100% - 1% 这样不但循环
LED 效果
|