?? untitled1.txt
字號:
間觸發合作式調度器在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 + -