三通道12位模數(shù)轉(zhuǎn)換MAX1241串口傳輸單片機(jī)C程序
#include
#define uchar unsigned char
#define uint unsigned int
#define High 1
#define Low0
int t1,t2,t3,t4,t5,t6,t7,t11,t12,t21,t22,t31,t32;
int t;
sbit cs=P2^7;
sbit sclk=P2^6;
sbit dout=P2^5;
sbit cs2=P2^4;
sbit sclk2=P2^3;
sbit dout2=P2^2;
sbit cs3=P2^1;
sbit sclk3=P2^0;
sbit dout3=P0^7;
uint dushuju() //定義為可以返回的參數(shù)
{
uint voltage_temp=0;
uchar ucloop=12;
cs=High;
sclk=Low;
cs=Low;
while(dout==0);//EOC信號為高表示轉(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ù)
}
uint dushuju2() //定義為可以返回的參數(shù)
{
uint voltage_temp2=0;
uchar ucloop=12;
cs2=High;
sclk2=Low;
cs2=Low;
while(dout2==0);//EOC信號為高表示轉(zhuǎn)換結(jié)束
sclk2=High;
sclk2=Low;
while(ucloop--)
{
sclk2=High;//上升沿?cái)?shù)據(jù)穩(wěn)定并讀出
voltage_temp2<<=1;
if(dout2==1)
voltage_temp2+=1;
sclk2=Low;
}
cs2=High;
return voltage_temp2; //返回?cái)?shù)據(jù)
}
uint dushuju3() //定義為可以返回的參數(shù)
{
uint voltage_temp3=0;
uchar ucloop=12;
cs3=High;
sclk3=Low;
cs3=Low;
while(dout3==0);//EOC信號為高表示轉(zhuǎn)換結(jié)束
sclk3=High;
sclk3=Low;
while(ucloop--)
{
sclk3=High;//上升沿?cái)?shù)據(jù)穩(wěn)定并讀出
voltage_temp3<<=1;
if(dout3==1)
voltage_temp3+=1;
sclk3=Low;
}
cs3=High;
return voltage_temp3; //返回?cái)?shù)據(jù)
}
uint dushuju4() //定義為可以返回的參數(shù)
{
uint voltage_temp4=0;
uchar ucloop=12;
cs=High;
sclk=Low;
cs=Low;
while(dout==0);//EOC信號為高表示轉(zhuǎn)換結(jié)束
sclk=High;
sclk=Low;
while(ucloop--)
{
sclk=High;//上升沿?cái)?shù)據(jù)穩(wěn)定并讀出
voltage_temp4<<=1;
if(dout==1)
voltage_temp4+=1;
sclk=Low;
}
cs=High;
return voltage_temp4; //返回?cái)?shù)據(jù)
}
void shuju(void)
{
t4=dushuju();
t5=dushuju2();
t6=dushuju3();
t12=t4%256;//將數(shù)據(jù)轉(zhuǎn)換為高8位和低8位
t11=t4/256%256;
t22=t5%256;
t21=t5/256%256;
t32=t6%256;
t31=t6/256%256;
}
void shujul(void)
{
t7=dushuju4();
}
void csh(void)
{
SM0=0;
SM1=1;
REN=1;
TI=0;
RI=0;
PCON=0;
TH1=0xF3;
TL1=0XF3;
TMOD=0X20;
EA=1;
ET1=0;
ES=1;
TR1=1;
TI=0;
SBUF=1;
while(!TI);
TI=0;
SBUF=t11;
while(!TI);
TI=0;
SBUF=t12;
while(!TI);
TI=0;
SBUF=2;
while(!TI);
TI=0;
SBUF=t21;
while(!TI);
TI=0;
SBUF=t22;
while(!TI);
TI=0;
SBUF=3;
while(!TI);
TI=0;
SBUF=t31;
while(!TI);
TI=0;
SBUF=t32;
while(!TI);
TI=0;
}
void main(void)
{
shuju();
csh();
loop:
shujul();
if ((t7-t4)>=10)
{
shuju();
csh();
goto loop ;
}
else
goto loop ;
}
評論