⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 untitled1.txt

📁 一个多任务调度器
💻 TXT
📖 第 1 页 / 共 2 页
字号:
间触发合作式调度器在avr中的移植(cvavr编译器) 

/*****************************************************  
This program was produced by the  
CodeWizardAVR V1.24.7d Standard  
Automatic Program Generator  
?Copyright 1998-2005 Pavel Haiduc, HP InfoTech s.r.l.  
http://www.hpinfotech.com  
e-mail:office@hpinfotech.com  

Project : os_mega16  
Version :   
Date    : 2006-2-12  
Author  : SHAOZH                            
Company : SZH                               
Comments:   


Chip type           : ATmega16L  
Program type        : Application  
Clock frequency     : 8.000000 MHz  
Memory model        : Small  
External SRAM size  : 0  
Data Stack size     : 256  
*****************************************************/  

#include <mega16.h>  

#define uchar unsigned char  
#define uint  unsigned int  
#define ulong unsigned long  

void LED1_Flash(void);  
void LED2_Flash(void);  
void LED3_Flash(void);   
                              
/********************************************************  
    ××××××××××××××公用数据类型声明××××××××××××××××××××  
    每个任务的存储器总和为7个字节  
********************************************************/  
typedef struct {  
                void (* pTask)(void);  
                uint Delay;  
                uint Period;  
                uchar RunMe;   
               }sTask;          

/********************************************************  
    ××××××××××××××公用的常数××××××××××××××××××××  
    任务的最大数目  
    注意:每个新建项目必须调整    
********************************************************/    
#define OS_MAX_TASKS   (4)           //每个新建项目必须调整   
//*******************************************************  
#define ERROR_OS_MANY_TASKS    1      //任务满  
#define ERROR_OS_DELETE_TASK   2      //任务删除  
#define RETURN_ERROR           1      //任务删除   
#define RETURN_OK              0           
                  
/********************************************************  
    ××××××××××××××公用变量定义××××××××××××××××××××  
/********************************************************/  
sTask OS_tasks_G[OS_MAX_TASKS];   //任务队列    
uchar Error_code_G=0;             //显示错误代码    
uchar old_Error_code_G=0;         //保存显示错误代码   
//*****************私有函数原型**************************  
static void OS_Go_To_Sleep(void);  
//*****************私有变量******************************  
static uint Error_tick_count_G;   //跟踪自从上一次记录错误以来的时间  
static uchar Last_error_code_G;   //上次错误代码(在一分钟后复位)  

#define OS_REPORT_ERRORS  1       //1:显示错误代码  0:不显示错误代码函数    
#define OS_SLEEP_MODE     1       //1:进入空闲  0: 不进入空闲         

/********************************************************  
    ××××××××××××××公用的函数原型××××××××××××××××××××  
    调度器内核函数  
********************************************************/                 
void OS_Dispatch_Tasks(void);  
uchar OS_Add_Task(void(*)(void),const uint ,const uint );  
uchar OS_Delete_Task(const uchar);  
void OS_Report_Status(void);    

/********************************************************  
      ×××××××××××××××调度函数×××××××××××××××××  
********************************************************/  
void OS_Dispatch_Tasks(void)  
     {uchar Index;  
      for (Index=0;Index<OS_MAX_TASKS;Index++)  
           {
           if(OS_tasks_G[Index].RunMe>0)      
              { 
              (*OS_tasks_G[Index].pTask)();  
                OS_tasks_G[Index].RunMe-=1;  
                if(OS_tasks_G[Index].Period==0)  
                  {
                  OS_Delete_Task(Index);  
                  }  
              }  
           }                             
     // OS_Report_Status();  //报告系统状态  
     // OS_Go_To_Sleep();    //进入空闲模式       
     }  
/********************************************************  
      ×××××××××××××××加任务函数×××××××××××××××××  
      Fn_P    任务名  (必须是无传人参数,无返回值的任务)  
      DELAY   在任务第一次被运行之前的间隔(时标)  
      PERIOD  如果为 0 :该函数将在DELAY确定的时间被调用一次  
              如果为非0 : 该函数将按DELAY的值在确定的时间被重复调用  
********************************************************/   
uchar OS_ADD_Task(void (*pFunction)(),const uint DELAY,const uint PERIOD)  
      {
       uchar Index=0;  
       while ((OS_tasks_G[Index].pTask!=0)&&(Index<OS_MAX_TASKS))  
             {
             Index++;  
             }          
       if(Index==OS_MAX_TASKS)  
         {Error_code_G=ERROR_OS_MANY_TASKS;   //设置全局错误变量  
          return OS_MAX_TASKS;                //返回错误代码  
         }       
       OS_tasks_G[Index].pTask=pFunction;  
       OS_tasks_G[Index].Delay=DELAY;  
       OS_tasks_G[Index].Period=PERIOD;  
       OS_tasks_G[Index].RunMe=0;  
       return Index;     
      }  
/********************************************************  
      ×××××××××××××××删除任务函数×××××××××××××××××  
********************************************************/   
uchar OS_Delete_Task(const uchar TASK_INDEX)  
    {bit Return_code;  
     if(OS_tasks_G[TASK_INDEX].pTask==0)  
        {Error_code_G=ERROR_OS_DELETE_TASK;  //设置全局错误变量  
         Return_code=RETURN_ERROR;           //返回错误代码  
        }  
     else  
        {Return_code=RETURN_OK;  
        }   
     OS_tasks_G[TASK_INDEX].pTask=0x0000;  
     OS_tasks_G[TASK_INDEX].Delay=0;  
     OS_tasks_G[TASK_INDEX].Period=0;  
     OS_tasks_G[TASK_INDEX].RunMe=0;  
     return Return_code;                   //返回状态  
    }                                                  
/********************************************************  
      ×××××××××××××××显示错误代码函数×××××××××××××××××  
********************************************************/   
#if OS_REPORT_ERRORS  
void OS_Report_Status()  
     {  
             if(Error_code_G!=old_Error_code_G)  
               {   
                //在次处编写错误显示程序  
                old_Error_code_G=Error_code_G;                    
                if(Error_code_G!=0)  
                  {Error_tick_count_G=60000;  
                  }         
                else  
                  {Error_tick_count_G=0;         
                  }                                           
               }                                              
             else  
               {if(Error_tick_count_G!=0)  
                  {if(--Error_tick_count_G==0)  
                     {Error_code_G=0;        //复位错误代码  
                     }  
                  }  
               }                                
     }                                 
#endif  
/********************************************************  
      ×××××××××××××××进入空闲函数×××××××××××××××××  
********************************************************/   
#if OS_SLEEP_MODE     
#include <SLEEP.h>  
void OS_Go_To_Sleep(void)  
  { sleep_enable();  //使能进入空闲  
    idle();          //进入空闲        
  }                              
#endif     
/********************************************************  
      ×××××××××××××××调度器初始化函数×××××××××××××××××  
********************************************************/  
void OS_Init_T0(void)  
     {
      uchar i;  
      for(i=0;i<OS_MAX_TASKS;i++)  
         {
         OS_Delete_Task(i);  
         }                    
      Error_code_G=0;  
      // Timer/Counter 0 initialization  
// Clock source: System Clock  
// Clock value: 125.000 kHz  
      TCCR0=0x03;  
      TCNT0=0x7D;  
// Timer(s)/Counter(s) Interrupt(s) initialization  
      TIMSK=0x01;     
     }                                              
/********************************************************  
      ×××××××××××××××启动调度器函数×××××××××××××××××  
********************************************************/       
OS_Start(void)  
     {#asm("sei")  
     }      
       
/********************************************************  
      ×××××××××××××××调度中断函数×××××××××××××××××  
********************************************************/  
interrupt [TIM0_OVF] void timer0_ovf_isr(void)  
{
uchar Index;  
TCNT0=0x7D;  
for(Index=0;Index<OS_MAX_TASKS;Index++)  
    {
    if(OS_tasks_G[Index].pTask)  
       {
       if(OS_tasks_G[Index].Delay==0)  
           {
           OS_tasks_G[Index].RunMe+=1;  
           if(OS_tasks_G[Index].Period)  
              {
              OS_tasks_G[Index].Delay=OS_tasks_G[Index].Period;  
              }  
           }                                                      
        else  
          {OS_tasks_G[Index].Delay-=1;  
          }     
       }  
    }  

}     


void main(void)  
{  
// Declare your local variables here  

// Input/Output Ports initialization  
// Port A initialization  
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out   
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0   
PORTA=0x00;  
DDRA=0xFF;  

// Port B initialization  
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out   
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0   
PORTB=0x00;  
DDRB=0xFF;  

// Port C initialization  
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out   
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0   
PORTC=0x00;  
DDRC=0xFF;  

// Port D initialization  
// Func7=In Func6=Out Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In   
// State7=T State6=0 State5=T State4=T State3=T State2=T State1=T State0=T   
PORTD=0x00;  
DDRD=0x40;  

// Timer/Counter 0 initialization  
// Clock source: System Clock  
// Clock value: 125.000 kHz  
// Mode: Normal top=FFh  
// OC0 output: Disconnected  
TCCR0=0x03;  
TCNT0=0x7D;  
OCR0=0x00;  

// Timer/Counter 1 initialization  
// Clock source: System Clock  
// Clock value: Timer 1 Stopped  
// Mode: Normal top=FFFFh  
// OC1A output: Discon.  
// OC1B output: Discon.  
// Noise Canceler: Off  
// Input Capture on Falling Edge  
// Timer 1 Overflow Interrupt: Off  
// Input Capture Interrupt: Off  
// Compare A Match Interrupt: Off  
// Compare B Match Interrupt: Off  
TCCR1A=0x00;  
TCCR1B=0x00;  
TCNT1H=0x00;  
TCNT1L=0x00;  
ICR1H=0x00;  
ICR1L=0x00;  
OCR1AH=0x00;  
OCR1AL=0x00;  
OCR1BH=0x00;  
OCR1BL=0x00;  

// Timer/Counter 2 initialization  
// Clock source: System Clock  
// Clock value: Timer 2 Stopped  
// Mode: Normal top=FFh  
// OC2 output: Disconnected  
ASSR=0x00;  
TCCR2=0x00;  
TCNT2=0x00;  
OCR2=0x00;  

// External Interrupt(s) initialization  
// INT0: Off  
// INT1: Off  
// INT2: Off  
MCUCR=0x00;  
MCUCSR=0x00;  

// Timer(s)/Counter(s) Interrupt(s) initialization  
TIMSK=0x01;  

// Analog Comparator initialization  
// Analog Comparator: Off  
// Analog Comparator Input Capture by Timer/Counter 1: Off  
ACSR=0x80;  
SFIOR=0x00;  
OS_ADD_Task(LED1_Flash,0,250);    //led1闪烁频率2Hz  
OS_ADD_Task(LED2_Flash,3,500);    //led2闪烁频率1Hz  
OS_ADD_Task(LED3_Flash,7,1000);   //led3闪烁频率0.5Hz  
OS_Start();  
// Global enable interrupts  
//#asm("sei")  

while (1)  
      {  
       OS_Dispatch_Tasks();  
      };  
}  

/************************************************  
my_mega16_dome.c  
************************************************/  
#define LED1   PORTD.6  
#define LED2   PORTC  
#define LED3   PORTA  
void LED1_Flash(void)  
{ LED1^=1;  
}  
void LED2_Flash(void)  
{ LED2^=0xff;  
}  
void LED3_Flash(void)  
{ LED3^=0xff;  
}  
以上程序硬件实验通过 

----------项目实例----------- 
#include "Main.H"  


#define  CU_AU     P3_0  
#define  KW1       P3_1  
#define  KW3       P3_2  
#define  LED_3KW   P3_3  
#define  LED_1KW   P3_4  
#define  LED_CUO   P3_5  
#define  LED_AUO   P3_7  
#define  OUT_1KW   P1_1  
#define  OUT_3KW   P1_2  
#define  COM1      P1_7  
#define  COM2      P1_6  
#define  COM3      P1_5  
#define  DATA      P1_4  
#define  CLK       P1_3  
                          //  0     1    2    3    4    5    6    
const unsigned char Tab[13]={0xf9,0xc0,0xa4,0xb0,0x99,0x92,0x82,  
                     //  7     8    9    A    C    U        
                        0xf8,0x80,0x90,0x88,0xc6,0xc1};  


bit CU_AU_fg=0;       //  0  手动 ;1   自动;  
bit Work_fg=0;        //  0  停止 ;1   工作;  
bit AU_start_fg=0;    //  0  自动状态不工作;1 自动状态工作;  
bit KW1_fg=0;         //  0  停止 ;1   工作;   
bit KW3_fg=0;         //  0  停止 ;1   工作;  
bit CU_AU_down_fg=0;  //键按下标志;  
bit Sec_500ms_fg=0;   //500ms标志;  
bit start_one=0;      //开机有无键按下标志;  
unsigned char led_disp_index=0; //数码管扫描指针;  
unsigned int  CU_AU_count=0;    //按键延迟计数器  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -