首页 > 编程知识 正文

单片机自动门控制程序,简单的单片机设计实例

时间:2023-05-04 14:18:00 阅读:165897 作者:2247

我们在做项目的时候,有时会遇到对周期敏感的任务,比如给周期发消息。 因为对时间很敏感,所以我们需要把这个任务放在很高的优先级。 您可以使用此任务调度程序来简化任务管理。

任务来源* * * * * * * * * * * header file * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * header file //periodicintervalinticksunsignedintperiod; //runme flag (indicatingwhentaskisduetorun ) unsigned char RunMe; } sTask; //function prototypes-------------------function prototypes voidsch_start(void; //coreschedulerfunctionsvoidsch _ dispatch _ tasks ) void; unsignedcharsch _ add _ task (void (* ) void )、const unsigned int、const unsigned int ); unsignedcharsch _ delete _ task (constunsignedchar ); //maximumnumberoftasks//mustbeadjustedforeachnewproject # define sch _ max _ tasks (1) * * * * * * * * * * max 请参阅* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */----------------- is sch _ dispatch _ tasks (willrunit.thisfunctionmustbecalled (repeatedly ) from the main loop.- *------------------------------ - Index SCH_MAX_TASKS; Index () if ) ) sch_tasks_g[index].runme0) SCH_tasks_G[Index].pTask!=0) () sch_tasks_g[index].ptask ) ); //runthetasksch _ tasks _ g [ index ].runme-=1; //reset/reducerunmeflag//periodictaskswillautomaticallyrunagain//-ifthisisa ' one shot ' task,removeitfromthearray

he name of the function which is to be scheduled. NOTE: All scheduled functions must be 'void, void' - that is, they must take no parameters, and have a void return type. DELAY - The interval (TICKS) before the task is first executed PERIOD - If 'PERIOD' is 0, the function is only called once, at the time determined by 'DELAY'. If PERIOD is non-zero, then the function is called repeatedly at an interval determined by the value of PERIOD (see below for examples which should help clarify this). RETURN VALUE: Returns the position in the task array at which the task has been added. If the return value is SCH_MAX_TASKS then the task could not be added to the array (there was insufficient space). If the return value is < SCH_MAX_TASKS, then the task was added successfully. Note: this return value may be required, if a task is to be subsequently deleted - see SCH_Delete_Task(). EXAMPLES: Task_ID = SCH_Add_Task(Do_X,1000,0); Causes the function Do_X() to be executed once after 1000 sch ticks. Task_ID = SCH_Add_Task(Do_X,0,1000); Causes the function Do_X() to be executed regularly, every 1000 sch ticks. Task_ID = SCH_Add_Task(Do_X,300,1000); Causes the function Do_X() to be executed regularly, every 1000 ticks. Task will be first executed at T = 300 ticks, then 1300, 2300, etc. -*------------------------------------------------------------------*/unsigned char SCH_Add_Task(void (*pFunction)(), const unsigned int DELAY, const unsigned int PERIOD){ unsigned char Index = 0; // First find a gap in the array (if there is one) while((SCH_tasks_G[Index].pTask != 0) && (Index < SCH_MAX_TASKS)) { Index++; } // Have we reached the end of the list? if(Index == SCH_MAX_TASKS) { // Task list is full, return an error code return SCH_MAX_TASKS; } // If we're here, there is a space in the task array SCH_tasks_G[Index].pTask = pFunction; SCH_tasks_G[Index].Delay =DELAY; SCH_tasks_G[Index].Period = PERIOD; SCH_tasks_G[Index].RunMe = 0; // return position of task (to allow later deletion) return Index;}/*------------------------------------------------------------------*- SCH_Delete_Task() Removes a task from the scheduler. Note that this does *not* delete the associated function from memory: it simply means that it is no longer called by the scheduler. TASK_INDEX - The task index. Provided by SCH_Add_Task(). RETURN VALUE: RETURN_ERROR or RETURN_NORMAL-*------------------------------------------------------------------*/unsigned char SCH_Delete_Task(const unsigned char TASK_INDEX){ // Return_code can be used for error reporting, NOT USED HERE THOUGH! unsigned char Return_code = 0; SCH_tasks_G[TASK_INDEX].pTask = 0; SCH_tasks_G[TASK_INDEX].Delay = 0; SCH_tasks_G[TASK_INDEX].Period = 0; SCH_tasks_G[TASK_INDEX].RunMe = 0; return Return_code;}/*------------------------------------------------------------------*- SCH_Init_T1() Scheduler initialisation function. Prepares scheduler data structures and sets up timer interrupts at required rate. You must call this function before using the scheduler. -*------------------------------------------------------------------*/void SCH_Init_T1(void){ unsigned char i; for(i = 0; i < SCH_MAX_TASKS; i++) { SCH_Delete_Task(i); } // Set up Timer 1 // Values for 1ms and 10ms ticks are provided for various crystals OCR1A = 15000; // 10ms tick, Crystal 12 MHz //OCR1A = 20000; // 10ms tick, Crystal 16 MHz //OCR1A = 12500; // 10ms tick, Crystal 10 MHz //OCR1A = 10000; // 10ms tick, Crystal 8 MHz //OCR1A = 2000; // 1ms tick, Crystal 16 MHz //OCR1A = 1500; // 1ms tick, Crystal 12 MHz //OCR1A = 1250; // 1ms tick, Crystal 10 MHz //OCR1A = 1000; // 1ms tick, Crystal 8 MHz TCCR1B = (1 << CS11) | (1 << WGM12); // Timer clock = system clock/8 TIMSK |= 1 << OCIE1A; //Timer 1 Output Compare A Match Interrupt Enable}/*------------------------------------------------------------------*- SCH_Start() Starts the scheduler, by enabling interrupts. NOTE: Usually called after all regular tasks are added, to keep the tasks synchronised. NOTE: ONLY THE SCHEDULER INTERRUPT SHOULD BE ENABLED!!! -*------------------------------------------------------------------*/void SCH_Start(void){ sei();}/*------------------------------------------------------------------*- SCH_Update This is the scheduler ISR. It is called at a rate determined by the timer settings in SCH_Init_T1().-*------------------------------------------------------------------*/ISR(TIMER1_COMPA_vect){ unsigned char Index; for(Index = 0; Index < SCH_MAX_TASKS; Index++) { // Check if there is a task at this location if(SCH_tasks_G[Index].pTask) { if(SCH_tasks_G[Index].Delay == 0) { // The task is due to run, Inc. the 'RunMe' flag SCH_tasks_G[Index].RunMe += 1; if(SCH_tasks_G[Index].Period) { // Schedule periodic tasks to run again SCH_tasks_G[Index].Delay = SCH_tasks_G[Index].Period; SCH_tasks_G[Index].Delay -= 1; } } else { // Not yet ready to run: just decrement the delay SCH_tasks_G[Index].Delay -= 1; } } }}// ------------------------------------------------------------------************************C File*********************************** 引用

https://stackoverflow.com/questions/4065453/assign-delays-for-1-ms-or-2-ms-in-c

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。