www.pudn.com > TLC549_89C52.rar > TLC549_Volt.c, change:2009-08-04,size:2281b


#include<reg51.h> 
#include<stdio.h> 
#include<intrins.h> 
#include<math.h> 
#define uint unsigned int 
#define uchar unsigned char 
#define ulong unsigned long 
//uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};  共阴极 
uchar tab[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //0 1 2 3 4 5 6 7 8 9含小数点,共阳极 
uchar a[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0 1 2 3 4 5 6 7 8 9 不含小数点 
uchar disb[]={0x08,0x04,0x02,0x01};   //段选      
//uchar disdat[6]; 
 
uchar ConvertValue=0; 
ulong da0,da1,da2,da3; 
 
uchar da,i; 
float xs; 
ulong bb; 
 
sbit Clock=P1^2; 
sbit DataOut=P1^1; 
sbit CS=P1^0; 
 
/*延时*/ 
#define Wait1us        {_nop_();} 
#define Wait2us       {_nop_();_nop_();} 
#define Wait4us       {Wait2us;Wait2us;} 
#define Wait8us       {Wait4us;Wait4us;} 
#define Wait10us      {Wait8us;Wait2us;} 
#define Wait20us      {Wait10us;Wait10us;} 
#define Wait30us      {Wait10us;Wait10us;} 
void delay(uint t) 
{ 
  uint i; 
  while(t--) 
  { for(i=0;i<=125;i++);} 
 } 
 
/*显示*/ 
void display(void) 
{ 
  uchar j; 
 
 for(j=0;j<10;j++) 
 { 
  P2=disb[0];     //个位 
  P0=a[da3]; 
  delay(3); 
  P2=disb[1]; 
  P0=a[da2]; 
  delay(3); 
  P2=disb[2]; 
  P0=a[da1]; 
  delay(3); 
  P2=disb[3]; 
  P0=tab[da0]; 
  delay(3); 
 } 
} 
 
/*采集转换函数*/ 
unsigned char ADCSelChannel(void) 
{ 
  CS=1; 
  Clock=0; 
  CS=0; 
  Wait4us; 
  for(i=0;i<8;i++)     //输入采样转换时钟 
  { 
    Clock=1; 
	Clock=0; 
   } 
  CS=1; 
  Wait10us;  //等待转换结束 
  CS=0; 
  Wait4us; 
  for(i=0;i<=7;i++) 
  { 
   Clock=1; 
   Clock=0; 
   ConvertValue<<=1; 
   if(DataOut) 
   ConvertValue+=1; 
  } 
  Clock=1; 
  Clock=0; 
  CS=1; 
  da=ConvertValue; 
  Wait30us; 
  xs=(da/256.00)*5.00; 
  return(xs); 
} 
 
/*主函数*/ 
void main(void) 
{ 
  SCON=0x40;         //串口方式1 
  PCON=0;            //SMOD=0 
  REN=1;            //允许接收 
  TMOD=0x20;        //定时器1,定时方式2 
  TH1=0xe6;         //12MHz   1200波特率 
  TL1=0xe6; 
  TR1=1; 
  while(1) 
  { 
   //da=0x03; 
   ADCSelChannel(); 
   //xs=(da/256.00)*5.00; 
   bb=xs*1000.00; 
   da0=bb/1000; 
   bb=bb%1000; 
   da1=bb/100; 
   bb=bb%100; 
   da2=bb/10; 
   da3=bb%10; 
   display(); 
   SBUF=da; 
   while(TI==0); 
   TI=0; 
   delay(10); 
   } 
 }