#include "reg52.h"
sbit ADC_CS=P3^5;
sbit ADC_CLK=P2^1;
sbit ADC_DI=P3^6;
sbit ADC_DO=P3^7;
bit SecFlag;
bit F_ADC_CLK;
unsigned char Count4ms;
unsigned char disp_buff[6];
unsigned char disp_cnt; //0..5
unsigned char hour,min,sec;
unsigned char Beep_cnt;
unsigned char P2_buff;
unsigned char ADData;
// 0 1 2 3 4 5 6 7 8 9 a b c d e f blank
code unsigned char Tab1[17]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E,0xff};//;数码管显示字型表
code unsigned char Tab[6]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb};
void T0_service(void) interrupt 1
{
TH0=(65536-4000)>>8;
TL0=(unsigned char)(65536-4000);
Count4ms=Count4ms+1;
if (Count4ms==50)
{
Count4ms=0;
SecFlag=1;
}
P2_buff=Tab[disp_cnt];
if (!F_ADC_CLK)
P2_buff=P2_buff&0xfd;
//disp
P2=P2_buff;
P0=Tab1[disp_buff[disp_cnt]];
disp_cnt=disp_cnt+1;
if (disp_cnt==6)
disp_cnt=0;
}
void FillDispBuffer(void)
{
disp_buff[0]=16; //blank
disp_buff[1]=16;
disp_buff[2]=16;
disp_buff[3]=ADData/100;
disp_buff[4]=(ADData%100)/10;
disp_buff[5]=ADData%10;
}
void pulse(void)
{
unsigned char i;
F_ADC_CLK=1;
ADC_CLK=1;
i=2;
while(i-->0);
F_ADC_CLK=0;
ADC_CLK=0;
i=2;
while(i-->0);
}
void AD(void)
{
unsigned char ret_dat;
unsigned char i;
ret_dat=0;
ADC_CS=0;
i=5;
ADC_CS=1;
while(i-->0);
ADC_CS=0;
ADC_DI=1;
pulse();
pulse();
pulse();
for(i=0;i<8;i++)
{
ret_dat=ret_dat<<1;
pulse();
if(ADC_DO)
ret_dat=ret_dat|0x01;
}
ADData=ret_dat;
if ((ret_dat&0x01)==0x01)
ret_dat=0x80;
else
ret_dat=0x00;
for(i=0;i<7;i++)
{
ret_dat=ret_dat>>1;
pulse();
if (ADC_DO)
ret_dat=ret_dat|0x80;
}
ADC_CS=1;
ADC_CLK=0;
ADC_DO=1;
ADC_DI=1;
}
void main()
{
//initial
disp_cnt=0;
Count4ms=0;
hour=0;
min=0;
sec=0;
//
TMOD=0x01;
TH0=(65536-4000)>>8;
TL0=(unsigned char)(65536-4000);
TR0=1;
ET0=1;
EA=1;
while(1)
{
if (SecFlag)
{
SecFlag=0;
AD();
FillDispBuffer();
}
}
}