单片机:数字电压表TLC2543 C程序代码

#include"TLC2543.H"
unsigned char TLC2543_DATA[7];
void TLC2543_Delay10us(void)   
{
     
    unsigned char a,b;
    for(b=1;b>0;b--)
        for(a=10;a>0;a--);
}
/************************************
* 函数名称:TLC2543_ReadData()
* 函数功能:读取ADC0804数据
* 输入参数:无
* 返回参数:电压原始数据值
************************************/
uint16 TLC2543_ReadData(uint8 AX)
{
     
  
  uint32 Vol=0;
  uint8 i=0;
  TLC2543_CS_L;
  TLC2543_Delay;
  AX=AX<<4;
  for(i=0;i<8;i++)
  {
      
  
    TLC2543_CLK_L;
    TLC2543_DIN=AX&0x80;
    AX=AX<<1;
    TLC2543_CLK_H;
    TLC2543_Delay;
    Vol=Vol<<1;
  	Vol=Vol|TLC2543_DOUT;	
  }
  for(i=0;i<4;i++)
  {
     
    TLC2543_CLK_L;
    TLC2543_Delay;
    TLC2543_CLK_H;
    TLC2543_Delay;   
    Vol=Vol<<1;
  	Vol=Vol|TLC2543_DOUT; 
  }
  TLC2543_CLK_L;
  TLC2543_CS_H; 
  return(Vol);
}
/************************************
* 函数名称:TLC2543_GetVoltage(uchar V)
* 函数功能:获取ADC0804的采集的电压
* 输入参数:参考电压
* 返回参数:电压值*10
************************************/
uint32 TLC2543_GetVoltage(uint8 AX,uint8 Vref)
{
     
  uint8 i=0;
  uint16 dat=0;
  uint32 temp_V=0,temp_Data;
  dat=TLC2543_ReadData(AX);
  dat=TLC2543_ReadData(AX);
  dat=dat&0x0fff;

  temp_V=((dat*100000)/4096)*Vref;
  temp_Data=temp_V;
  TLC2543_DATA[0]=(unsigned char)(temp_V/100000);
  temp_V=temp_V%100000;
  TLC2543_DATA[1]=(unsigned char)(temp_V/10000);
  temp_V=temp_V%10000;
  TLC2543_DATA[2]=(unsigned char)(temp_V/1000);
  temp_V=temp_V%1000;
  TLC2543_DATA[3]=(unsigned char)(temp_V/100);
  temp_V=temp_V%100;
  TLC2543_DATA[4]=(unsigned char)(temp_V/10);
  temp_V=temp_V%10;
  TLC2543_DATA[5]=(unsigned char)(temp_V);
  return(temp_Data);	

}





 #include
 sbit TLC2543_DIN=P1^2;
 sbit TLC2543_DOUT=P1^3;
 sbit TLC2543_CS=P1^4;
 sbit TLC2543_CLK=P1^1;
 sbit TLC2543_EOC=P1^0;
 extern unsigned char TLC2543_DATA[7];
 #define TLC2543_Delay TLC2543_Delay10us()
 #define uint8 unsigned char
 #define uint16 unsigned int
 #define uint32 unsigned long
 #define TLC2543_CS_L   TLC2543_CS=0
 #define TLC2543_CS_H   TLC2543_CS=1
 #define TLC2543_CLK_H  TLC2543_CLK=1
 #define TLC2543_CLK_L  TLC2543_CLK=0
 #define TLC2543_DOUT_H TLC2543_DOUT=1
 #define TLC2543_DOUT_L TLC2543_DOUT=0
 #define TLC2543_EOC_H  TLC2543_EOC=1
 #define TLC2543_A0     0X00
 #define TLC2543_A1     0X10
 #define TLC2543_A2     0X20
 #define TLC2543_A3     0X30
 #define TLC2543_A4     0X40
 #define TLC2543_A5     0X50
 #define TLC2543_A6     0X60
 #define TLC2543_A7     0X70
 #define TLC2543_A8     0X80
 #define TLC2543_A9     0X90
 #define TLC2543_A10    0XA0
 #define TLC2543_TEST1   0XB0
 #define TLC2543_TEST2   0XC0
 #define TLC2543_TEST3   0XD0
 #define TLC2543_SLEEP   0XE0
 #define TLC2543_8BIT    0x04
 #define TLC2543_12BIT   0x08
 #define TLC2543_16BIT   0x0C
 #define TLC2543_SinPolar   0x00
 #define TLC2543_DouPolar   0x01
 uint16 TLC2543_ReadData(uint8 AX);
 uint32 TLC2543_GetVoltage(uint8 AX,uint8 Vref);

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