51單片機(jī)驅(qū)動(dòng)12位AD轉(zhuǎn)換MAX1241程序及原理圖
本程序的完整工程文件下載:http://www.51hei.com/f/1241.rar
以下是源碼:
#include
#defineuchar unsigned char
#define uint unsigned int
#define High 1
#define Low0
int t;
sbit cs=P2^7;
sbit sclk=P2^6;
sbit dout=P2^5;
unsigned char b[5];
uchar code tv[]={0x08,0x10,0x20,0x40,0X80};//位選
uchar code dc[]={0xC0, //"0"
0xF9, //"1"
0xA4, //"2"
0xB0, //"3"
0x99, //"4"
0x92, //"5"
0x82, //"6"
0xF8, //"7"
0x80, //"8"
0x90, //"9"
};
uint dushuju() //定義為可以返回的參數(shù)
{
uint voltage_temp=0;
uchar ucloop=12;
cs=High;
sclk=Low;
cs=Low;
while(dout==0);//EOC信號(hào)為高表示轉(zhuǎn)換結(jié)束
sclk=High;
sclk=Low;
while(ucloop--)
{
sclk=High;//上升沿?cái)?shù)據(jù)穩(wěn)定并讀出
voltage_temp<<=1;
if(dout==1)
voltage_temp+=1;
sclk=Low;
}
cs=High;
return voltage_temp; //返回?cái)?shù)據(jù)
}
void shuxiang(void )
{
char i, j;
t=dushuju(); //調(diào)用數(shù)據(jù)
b[0]=(t/10000)%10; //取得整數(shù)值到數(shù)組
b[1]=(t/1000)%10;
b[2]=(t/100)%10;
b[3]=(t/10)%10;
b[4]=(t/1)%10;
b[5]=0;
for(i=0;i<5;i++)
{
P3=tv[i];
j=b[i];
P1=dc[j];
Delay();
}
}
void delay(void)
{
uint i;
for(i=0;i<100;i++);
}
main()
{
loop:
dushuju();
shuxiang();
goto loop;
}
評(píng)論