AVR AD轉換的C語言編程
#define uchar unsigned char
#define uint unsigned int
uchar Table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar Da
void DelayMs(uint i) //0.25ms
{uchar j;
for(;i!=0;i--)
{for(j=250;j!=0;j--) {;}}
}
void Display(uchar *p) //間隔5ms顯示
{uchar i,sel=0x01;
for(i=0;i<4;i++)
{PORTB=sel;
PORTC=0xff-Table[p[i]];
DelayMs(20);
sel=sel<<1;
}
}
uint ADC_Convert(void)
{uint temp1,temp2;
temp1=(uint)ADCL;
temp2=(uint)ADCH;
temp2=(temp2<<8)+temp1;
return(temp2);
}
void Process(uint i,uchar *p)
{p[0]=i/1000;
i=i%1000;
p[1]=i/100;
i=i%100;
p[2]=i/10;
i=i%10;
p[3]=i;
}
/*
void Timer0_Init(void)
{SREG=SREG|0x80; //全局中斷開;
TIMSK=TIMSK|0x01;//T/C0溢出中斷使能;
TCCR0=0x05;//CLK/1024;128uS
TIFR=0x01;//T/C0中斷標志位寫1清0;
TCNT0=256-125;//125*128us=16ms
}*/
void main(void)
{uint i;
//設置A口為不帶上拉輸入;
DDRA=0x00;
PORTA=0x00;
//設置B口為推挽1輸出;
DDRB=0xff;
PORTB=0xff;
//設置C口為推挽1輸出;
DDRC=0xff;
PORTC=0xff;
//B口初始化輸出1;C口初始化輸出0;點亮全部數(shù)碼管;
PORTB=0xff;
PORTC=0x00;
//ADC初始化;
ADMUX=0x02;//選擇第三通道;
ADCSR=0xe6;//125K轉換速率,自由轉換模式;啟動AD轉換器;
//延時待系統(tǒng)穩(wěn)定;
DelayMs(3000);
while(1)
{i=ADC_Convert();
Process(i,Da
Display(Da
}
}
/*#pragma interrupt_handler Timer0:10
void Timer0()
{Int_Cnt+=1;
// Timer0_Init();
}*/
評論