#include <reg52.h>
#include <intrins.h>
sbit ADC_CS = P3^4;
sbit ADC_CLK = P3^5;
sbit ADC_DO = P3^6;
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
unsigned char Read_ADC();
void delay1ms(int n);
unsigned char code TAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82
,0xf8,0x80,0x90};
unsigned char code DP_TAB[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02
,0x78,0x00,0x10};
unsigned char ledbuff[4]={0xc0,0x40,0xc0,0xc0};
void Timer0_Init()
{
EA = 1;
TMOD = 0x01;
TH0 = (65536 - 10000)/256;
TL0 = (65536 - 10000)%256;
TR0 = 1;
ET0 = 1;
}
void main()
{
float adc_vol = 0;
int adc_tmp = 0;
Timer0_Init();
while(1)
{
adc_vol = (5.0*Read_ADC())/255;
adc_tmp = (int)(adc_vol*1000);
// adc_tmp = Read_ADC();
ledbuff[3] = TAB[adc_tmp/1000]-0x80;
ledbuff[2] = TAB[adc_tmp/100%10];
ledbuff[1] = TAB[adc_tmp/10%10];
ledbuff[0] = TAB[adc_tmp%10];
delay1ms(100);
}
}
unsigned char Read_ADC()
{
unsigned char i,temp = 0;
EA = 0;
ADC_DO = 1;
_nop_();
_nop_();
ADC_CS = 0;
_nop_();
_nop_();
ADC_CLK = 1;
_nop_();
_nop_();
ADC_CLK = 0;
_nop_();
_nop_();
for(i=0;i<8;i++)
{
ADC_CLK = 1;
_nop_();
_nop_();
if(ADC_DO)
{
temp |=0x01;
}
temp <<= 1;
ADC_CLK = 0;
_nop_();
_nop_();
}
ADC_CS = 1;
_nop_();
_nop_();
EA = 1;
return temp;
}
void interrupt_time0() interrupt 1
{
static char i = 0;
TH0 = (65536 - 10000)/256;
TL0 = (65536 - 10000)%256;
P2 = 0xff;
switch(i)
{
case 0: P2_0=0;P2_1=1;P2_2=1;P2_3=1;P0 = ledbuff[3];i++;break;
case 1: P2_0=1;P2_1=0;P2_2=1;P2_3=1;P0 = ledbuff[2];i++;break;
case 2: P2_0=1;P2_1=1;P2_2=0;P2_3=1;P0 = ledbuff[1];i++;break;
case 3: P2_0=1;P2_1=1;P2_2=1;P2_3=0;P0 = ledbuff[0];i=0;break;
default:
break;
}
}
void delay1ms(int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}