下面是MAX1241和51單片機連接的電路圖:
本程序的完整工程文件下載: http://www.zg4o1577.cn/f/1241.rar
以下是源碼:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define High 1
#define Low 0
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() //定義為可以返回的參數
{
uint voltage_temp=0;
uchar ucloop=12;
cs=High;
sclk=Low;
cs=Low;
while(dout==0);//EOC信號為高表示轉換結束
sclk=High;
sclk=Low;
while(ucloop--)
{
sclk=High;//上升沿數據穩定并讀出
voltage_temp<<=1;
if(dout==1)
voltage_temp+=1;
sclk=Low;
}
cs=High;
return voltage_temp; //返回數據
}
void shuxiang(void )
{
char i, j;
t=dushuju(); //調用數據
b[0]=(t/10000)%10; //取得整數值到數組
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;
}