#include"reg52.h"
code unsigned char code_tab[17]={0xc0,0xf9,0xa4,0xb0,\
0x99,0x92,0x82,0xf8,\
0x80,0x90,0x88,0x83,\
0xc6,0xa1,0x86,0x8e,
0x7f}; //数值码表
#define uchar unsigned char
#define uint unsigned int
unsigned int dat=0;
float ft;
/***********************/
sbit SCK = P1^0; //LTC1402时钟脚
sbit CONV = P1^1; //LTC1402转换控制脚
sbit DOUT = P1^2; //LTC1402数据输入脚
void delay(uchar temp) //延时
{
uchar i,j;
i=temp;
for(;i>0;i--)
for(j=200;j>0;j--);
}
//数码管显示
void display_led( float num)
{
uint temp;
uchar a,b,c,d;
temp=num*100;
a=temp/1000;
b=(temp%1000)/100;
c=(temp%100)/10;
d=(temp%10);
{
P0=code_tab[a];
P2|=0X0F;
P2&=~(0X01);
delay(5);
P0=code_tab[b];
P2|=0X0F;
P2&=~(0X02);
delay(5);
P0=code_tab[c];
P2|=0X0F;
P2&=~(0X04);
delay(5);
P0=code_tab[d];
P2|=0X0F;
P2&=~(0X08);;
delay(5);
P0=code_tab[16];
P2|=0X0F;
P2&=~(0X02);
delay(5);
}
}
//等待子程序
void nop(void)
{
}
//初始化IO口
void init_pio(void)
{
P0=0XFF;
P1=0XFF;
P2=0XFF;
P3=0XFF;
}
/****************/
//LTC1402转换子程序转换
unsigned int ad_change(void)
{
unsigned int temp=0;
uchar i;
SCK=0;
CONV=1;
nop();
SCK=1;
CONV=0;
SCK=0;
SCK=1;
SCK=0;
for(i=0;i<12;i++)
{
SCK=1;
SCK=0;
temp=(temp<<1)|DOUT;
}
SCK=1;
SCK=0;
SCK=1;
SCK=0;
return temp;
}
/**********************/
//主程序
void main(void)
{
unsigned int result ;
float ft;
uchar i;
init_pio();
for(;;)
{
result = ad_change();
nop();
ft=(4.9215*result*4.096)/4095;
for(i=0;i<100;i++)
{
display_led(ft);
}
}
}
|