51单片机:TLC549测量电压,并将测量值显示在数码管上

51单片机:TLC549测量电压,并将测量值显示在数码管上

要求:

在51单片机上利用TLC549这个A/D转换器测量电压,并将测量值显示在数码管上。电源范围是0-5V,可以实时测量出电压大小并显示出来。

仿真电路图:

51单片机:TLC549测量电压,并将测量值显示在数码管上_第1张图片

代码如下:

//TLC549测量电压,并将测量值显示在数码管上
#include
sbit SDO  = P1^0;
sbit CS   = P1^1;
sbit SCLK = P1^2;
sbit qian = P2^4;	  //数码管位选
sbit bai  = P2^5;
sbit shi  = P2^6;
sbit ge   = P2^7;
unsigned char date[4];//数码管位选
unsigned char code tb[18]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
				          0x7f,0x6f,0x77,0x7c,0x39,0x5e,
						  0x79,0x71,0x00,0X80};//共阴极七段数码
//主函数
void main()			
{  
   void ET0_init();
   void RT1_init();
   ET0  = 1;
   TR0  = 1;
   ET1  = 1;
   TR1  = 1;
   EA   = 1;  //打开总中断
   SCLK = 0; //TLC549初始化
   CS   = 1;   
   while(1);

}

//定时器0初始化
void ET0_init()             
{
	TMOD &= 0xf0;			//定时器0
    TMOD |= 0x01;
    TH0  = (65536-2000)/256; //2ms溢出
    TL0  = (65536-2000)%256; 
}

//定时器1初始化
void RT1_init()   
{
   TMOD &= 0x0f;			 
   TMOD |= 0x10;
   TH1  = (65536-2000)/256; //2ms溢出
   TL1  = (65536-2000)%256; 
}

//定时器0中断
void ET0_INT() interrupt 1   
{	
    static unsigned char i;
    
	TH0  = (65536-2000)/256; // 重装初值
    TL0  = (65536-2000)%256;

	P2 |= 0xf0;  //消影
	switch(i)	 //位选,选择点亮的数码管,
	{
	    case(0):
		  P0 = date[0];qian = 0;break;//显示千位
	    case(1):
		  P0 = date[1];bai  = 0;break;//显示百位
	    case(2):
		  P0 = date[2];shi  = 0;break;//显示十位
		case(3):
		  P0 = date[3];ge   = 0;break;//显示个位		
  }

	i++;
    if(i==4)
	   {
		  i=0;
	   }

}

//定时器T1中断服务程序
void ET1_INT() interrupt 3   
{
  unsigned int vt;
  unsigned char i;
  unsigned char AD;

  TH1  = (65536-2000)/256; // 重装初值
  TL1  = (65536-2000)%256;

  CS = 0;
  for(i=0;i<8;i++)
  {
     SCLK = 1;
	 AD = ( AD << 1 )|SDO;
	 SCLK = 0;

  }
  CS = 1;
  vt = (500.0/256.0)*AD; //测量电压值

  date[0] =tb[vt/1000];
  date[1] =tb[vt/100%10]|0x80; //点亮小数点
  date[2] =tb[vt/10%10];
  date[3] =tb[vt%10];
}

仿真结果:

51单片机:TLC549测量电压,并将测量值显示在数码管上_第2张图片

你可能感兴趣的:(51单片机)