//ADC0832 数字电压表(量程0-5V)
#include
#include < intrins.h >
#define uchar unsigned char
#define uint unsigned int
#include
sbit spi_cs = P3^4;
sbit spi_clk= P3^5;
sbit spi_io = P1^1;
#define LED7 XBYTE[0x9000]
#define LED6 XBYTE[0x9001]
#define LED5 XBYTE[0x9f02]
#define LED4 XBYTE[0x9f03]
#define LED3 XBYTE[0x9f04]
#define LED2 XBYTE[0x9f05]
#define LED1 XBYTE[0x9f06]
#define LED0 XBYTE[0x9f07]
uchar adval;
uchar tt[]={ 0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f
};
uchar tt1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
sbit CS=P3^4;
//sbit CLK=P1^0;
//sbit CLK=P3^5;
//sbit DIO=P1^1;
/*
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
*/
void spiStart()
{
spi_clk = 0;
spi_io = 1;
_nop_();
spi_cs = 0;
_nop_(); _nop_();
}
void spiEnd()
{
spi_cs = 1;
spi_io = 1;
spi_clk = 1;
_nop_(); _nop_();
}
void spiSend(uchar op,uchar n)
{
uchar i;
op = op<<(8-n);
for( i=0 ; i {
spi_io = op&0x80;
spi_clk = 1;
_nop_();
spi_clk = 0;
op = op<<1;
}
spi_io = 1;
}
uint adc0832(uchar channel)
{
uchar i,ndat=0;
uint dat=0;
spiStart();
spiSend(0x06+channel,0x03);
_nop_();
if( spi_io )
return 0;
spi_io = 1;
for( i=0 ; i<8 ; i++ )
{
spi_clk = 1; _nop_();
spi_clk = 0; _nop_();
dat = dat<<1;
if( spi_io )
dat |= 0x0100;
}
for( i=0 ; i<8 ; i++ )
{
spi_clk = 1; _nop_();
spi_clk = 0; _nop_();
ndat = ndat>>1;
if( spi_io )
ndat |= 0x80;
}
spiEnd();
dat += ndat;
return dat;
}
void delay1()
{
int k;
for(k=0;k<20000;k++);
}
/*
void display(int k)
{
P2=0xfe;
P0=tt[k/1000];
delay1();
P2=0xfd;
P0=tt[k%1000/100];
delay1();
P3=0xfb;
P0=tt[k%100/10];
delay1();
P2=0xf7;
P0=tt[k%10];
delay1();
P2=0xff;
}
*/
void display(float z)
{
float k;
//int m;
k=(float)(z/256)*5;
k=2*k;
LED7=tt1[((int)k)%10];
delay1();
LED6=tt[((int)(10*k))%10];
delay1();
LED5=tt[((int)(k*100))%10];
delay1();
LED4=tt[((int)(k*1000))%10];
delay1();
}
void init()
{
LED0=0;
LED1=0;
LED2=0;
LED3=0;
LED4=0;
LED5=0;
LED6=0;
LED7=0;
}
void main(void)
{
//P2=0xff;
// P0=0xff;
init();
while(1)
{
//adc0832(1);
display(adc0832(1));
|
文件: |
ADC0832pcb图.rar |
大小: |
158KB |
下载: |
下载 | |
}
}
阅读(739) | 评论(0) | 转发(0) |