这个地方有:http://user.qzone.qq.com/34704436
#define zhu
#include "dispatch.h"
#define KeyConstant 0b00000110
#define ADC_VREF_TYPE 0x00
//*****************任务*****************
void pulseOUT(void);
void GetWidth(void);
void key(void);
void aging(void);
//*****************任务*****************
unsigned int width;
unsigned char KeyVal;
unsigned char count_T0;
extern TSdata Tbase[];
//ADC
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
//波形产生
void WaveMaker(unsigned short int val)
{
PORTB.0=1;
while(val!=0)
{
val--;
#asm("nop\nop\nop\nop");
}
PORTB.0=0;
}
void key_bothkey()
{
static bit ModeFlag;
ModeFlag=!ModeFlag;
Tbase[0].pTask=0;
if(ModeFlag)
{
AddTask(aging,0,0,MPT);
Tbase[1].pTask=0;
}
else
{
AddTask(pulseOUT,0,0,MPT);
AddTask(GetWidth,0,0,MPT);
}
}
void main(void)
{
//端口初始化
DDRB= 0B00000001;
PORTB=0B00000110;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value:
//TCCR0A=0x00;
TIMSK0=0x02;
TCCR0B=0x05; //1024分频,
// ADC initialization
// ADC Clock frequency: 250.000 kHz
//DIDR0=0x00;
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x85;
count_T0=68; //设置初值,为20MS
AddTask(pulseOUT,0,0,MPT);
AddTask(GetWidth,0,0,MPT);
AddTask(key,0,0,MPT);
DispatchStart();
while (1)
{
DispatchTask();
}
}
void pulseOUT()
{
WaveMaker(width+250); //500us~2500us,足可覆盖舵机范围
}
void aging()
{
static bit flag; //+、-标记
WaveMaker(width+250); //500us~2500us,足可覆盖舵机范围
//自动测试舵机
if(!flag)
{
if(width<989)
{
width=width+35;
}
else
{
flag=1;
}
}
else
{
if(width>35)
{
width=width-35;
}
else
{
flag=0;
}
}
}
void GetWidth()
{
width=(read_adc(2)+read_adc(2))/2;
}
void key()
{
static unsigned char KeyNow,KeyBack,debounce;
unsigned char temp,temp2;
temp=PINB&KeyConstant;
if(temp!=KeyNow)
{
KeyNow=temp;
debounce=0;
return;
}
if(debounce!=0)
{
debounce--;
return;
}
temp=PINB&KeyConstant;
if(temp!=KeyNow)
{
KeyNow=temp;
return;
}
temp=KeyNow^KeyBack;
if(temp==0)
{
return;
}
KeyBack=KeyNow;
if(temp&KeyBack)
{
return;
}
for(temp2=1;temp2<3;temp2++)
{
temp>>=1;
if(temp&1)
{
KeyVal=temp2;
//return;
break;
}
}
switch(KeyVal)
{
case 1:
if(!(PINB&0b00000110))
{
key_bothkey();
break;
}
if(count_T0>22)
{
count_T0=count_T0-4;
}
break;
case 2:
if(!(PINB&0b00000110))
{
key_bothkey();
break;
}
if(count_T0<116)
{
count_T0=count_T0+4;
}
break;
default:
}
KeyVal=0;
}
长叹永恒…
引用删除2楼 朱逸
评论时间:2006年12月20日 23时51分
"dispatch.c"
#define zhu
#ifdef zhu
#include "dispatch.h"
/*============================相关声明============================*/
/*============================相关定义============================*/
TSdata Tbase[MaxTasks]; //任务数据数组
unsigned char ID;
volatile unsigned char message;
/**************************************************************************
系统刷新(在定时器中)
入口=无
出口=无
Author:zhuyi
Date:2006-11-08
**************************************************************************/
interrupt[TIM0_OVF] void ClockUpdate(void) //定时器时钟
{
unsigned char index;
TCNT0=count_T0;
for(index=0;index<MaxTasks;index++)
{
if(Tbase[index].pTask)
{
if(Tbase[index].Delay==0)
{
if(Tbase[index].Priority==MPT)
{
Tbase[index].RunMe+=1;
message=0;
}
else
{
ID=index; //记录任务
(*Tbase[index].pTask)();
}
if(Tbase[index].Period==255)
{
Tbase[index].pTask=0;
}
else
{
Tbase[index].Delay=Tbase[index].Period;
}
}
else
{
Tbase[index].Delay--;
}
}
}
}
/**************************************************************************
任务调度函数
(不调用高优先级,并且低优先级的任务最后总会运行,因为它对周期不敏感)
入口=无
出口=无
Author:zhuyi
Date:2006-11-08
**************************************************************************/
void DispatchTask(void)
{
unsigned char index;
for(index=0;index<MaxTasks;index++)
{
if(Tbase[index].pTask)
{
if((Tbase[index].Priority==MPT) && (Tbase[index].RunMe>0))
{
ID=index;
(*Tbase[index].pTask) ();
Tbase[index].RunMe-=1;
}
}
}
message=1;
while(message);
}
/**************************************************************************
添加任务函数
入口=4,任务名;延时;周期;级别{HPT(抢占),MPT(常规)}
出口=添加任务成功与否
Author:zhuyi
Date:2006-11-08
**************************************************************************/
unsigned char AddTask(void ( * pFunction)(), const unsigned char DELAY,
const unsigned char PERIOD,const unsigned char PRIORITY)
{
unsigned char Index = 0;
// First find a gap in the array
while ((Tbase[Index].pTask !=0 )&& (Index < MaxTasks))
{
Index++;
}
if (Index == MaxTasks)
{
return MaxTasks;
}
Tbase[Index].pTask = pFunction;
Tbase[Index].Delay = DELAY;
Tbase[Index].Period = PERIOD;
Tbase[Index].Priority=PRIORITY;
Tbase[Index].RunMe = 0;
return Index;
}
#endif
长叹永恒…
引用删除3楼 朱逸
评论时间:2006年12月20日 23时52分
"dispatch.h"
#include <tiny13.h>
#ifdef zhu
typedef struct
{
void(*pTask)(void);
unsigned char Delay;
unsigned char Period;
unsigned char RunMe;
unsigned char Priority;
} TSdata;
void DispatchTask(void);
unsigned char AddTask(void ( * pFunction)(), const unsigned char DELAY, const unsigned char PERIOD,const unsigned char PRIORITY);
#define HPT 0
#define MPT 127
#define DispatchStart() #asm("sei")
#define MaxTasks (3)
#endif |