|
还是自己去编译吧,这是ICCAVR的。
#include <iom8v.h>
#include <macros.h>
#include "common.h"
#define SetPlus() PORTB |= (1<<PB0)
#define ClrPlus() PORTB &= ~(1<<PB0)
UCHAR GetPosition()
{
INT8 i,nRet;
UINT nSum;
UINT8 nADC,nMax,nMin,iMax,iMin,nVol[4];
//pVoltage = 0;
nMax = 0;
nMin = 255;
ADCSRA|=(1<<ADSC); //启动ADC
while(ADCSRA & (1<<ADSC)); //首次值抛弃
//连续四次取样
for(i=0;i<4;i++)
{ ADCSRA|=(1<<ADSC); //启动ADC
while(ADCSRA & (1<<ADSC));
nADC = ADCH;
if(nADC > nMax)
{ nMax = nADC;
iMax = i;
}
else if(nADC < nMin)
{ nMin = nADC;
iMin = i;
}
nVol = nADC;
}
nSum = 0;
//去掉最大最小值,取平均数
for(i=0;i<4;i++)
{ if(i!=iMax && i!=iMin)
nSum += nVol;
}
nSum >>= 2;
return nSum > 120 ? 120 : nSum;
}
void init_devices(void)
{
CLI();
PORTB = 0x00;
DDRB = 0x01;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
TCCR0 = 0x00;
TCNT0 = 0x83;
TCCR0 = 0x02;
ACSR = 0x80; //关闭模拟比较器
ADCSR = 0B10000000; //允许ADC
ADMUX = 0B00100000; //外部参考源,左对齐,0号通道
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00;
SEI();
}
void main(void)
{
INT8 nPos;
int i,nClr;
init_devices();
while(1)
{
SetPlus();
nPos = GetPosition(); //ADC过程,0.4ms
for(i=0;i<65;i++); //最少1 ms;
for(i=0;i<nPos;i++);
ClrPlus();
nClr = 2400 - nPos;
for(i=0;i<nClr;i++);
}
}
[ 本帖最后由 root_007 于 2009-6-24 17:52 编辑 ] |
|