atmega8535 Ad轉(zhuǎn)換的c程序
atmega8535 Ad轉(zhuǎn)換的c程序
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharTable[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
ucharData[4]={0,0,0,0};
voidDelayMs(uinti)//0.25ms
{ucharj;
for(;i!=0;i--)
{for(j=250;j!=0;j--){;}}
}
voidDisplay(uchar*p)//間隔5ms顯示
{uchari,sel=0x01;
for(i=0;i<4;i++)
{PORTD=sel;
PORTB=0xff-Table[p[i]];
DelayMs(20);
sel=sel<<1;
}
}
uintADC_Convert(void)
{uinttemp1,temp2;
temp1=(uint)ADCL;
temp2=(uint)ADCH;
temp2=(temp2<<8)+temp1;
return(temp2);
}
voidProcess(uinti,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;
}
voidmain(void)
{uinti;
DDRA=0x00;//設(shè)置A口為不帶上拉輸入;
PORTA=0x00;
DDRB=0xff;//設(shè)置B口為推挽1輸出;
PORTB=0xff;
DDRD=0xff;//設(shè)置C口為推挽1輸出;
PORTD=0xff;
PORTB=0x00;//B口初始化輸出1;C口初始化輸出0;點亮全部數(shù)碼管;
PORTD=0xff;
ADMUX=0x01;//選擇第二通道ADC1;
ADCSR=0xe6;//125K轉(zhuǎn)換速率,自由轉(zhuǎn)換模式;啟動AD轉(zhuǎn)換器;
DelayMs(3000);//延時待系統(tǒng)穩(wěn)定;
while(1)
{i=ADC_Convert();
Process(i,Data);
Display(Data);
}
}
#include
#define uchar unsigned char
#define uint unsigned int
void DelayMs(uint i)
{uchar j;
for(;i!=0;i--)
{for(j=255;j!=0;j--) {;}}
}
main()
{DDRA=0xff; //A口1輸出
PORTA=0xff;
DDRB=0xff; //B口1輸出
PORTB=0xff;
while(1)
{DelayMs(0);
PORTA=0xff;
PORTB=0x00;}
}