⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tlc2543.c

📁 12位ADC转换器tlc2543I驱动程序
💻 C
字号:
/****************************************************************************
**************************** TLC2543 子程序 *********************************
*****************************************************************************/
#include <iom128v.h>
#include <macros.h>
#define uchar unsigned char
#define uint  unsigned int
#define	tlc_CLK  2        //数字串行时钟输入端
#define	tlc_DOUT 4        //2543串行数据输出端
#define	tlc_DIN  3        //2543串行数据输入端
#define	tlc_CS1  5        //片选端  低电平片选输入  高电平为掉电输入
#define tlc_CS2  6
#define tlc_CS3  7
#define tlc_CS4  3
#define tlc_CS5  4
 unsigned int val_adc[55];//各通道采集值
/*
#define	TLC2543_CLK_H 	  PORTB|=  BIT(tlc_CLK)
#define	TLC2543_CLK_L	    PORTB&= ~BIT(tlc_CLK)
#define	TLC2543_DOUT_H 	  PORTB|=  BIT(tlc_DOUT)
#define	TLC2543_DOUT_L 	  PORTB&= ~BIT(tlc_DOUT)
#define	TLC2543_DIN_H 	  PORTB|=  BIT(tlc_DIN)
#define	TLC2543_DIN_L     PORTB&= ~BIT(tlc_DIN)*/
#define	TLC2543_DOUT_READ DDRB &= ~BIT(tlc_DOUT)    // 设置为输入状态
/*
#define	TLC2543_CS1_H 	  PORTB|=  BIT(tlc_CS1)
#define	TLC2543_CS1_L 	  PORTB&= ~BIT(tlc_CS1)
#define	TLC2543_CS2_H 	  PORTB|=  BIT(tlc_CS2)
#define	TLC2543_CS2_L 	  PORTB&= ~BIT(tlc_CS2)
#define	TLC2543_CS3_H 	  PORTB|=  BIT(tlc_CS3)
#define	TLC2543_CS3_L 	  PORTB&= ~BIT(tlc_CS3)
#define	TLC2543_CS4_H 	  PORTG|=  BIT(tlc_CS4)
#define	TLC2543_CS4_L 	  PORTG&= ~BIT(tlc_CS4)
#define	TLC2543_CS5_H 	  PORTG|=  BIT(tlc_CS5)
#define	TLC2543_CS5_L 	  PORTG&= ~BIT(tlc_CS5)
uchar xs8[4];*/
extern void delay (uint us);

unsigned int tlc2543(uchar senddata)
{ 
  unsigned int  recdata = 0;
  unsigned int  i = 0;
  //senddata = 0B11010000;//控制字
  senddata<<=4;
  recdata = 0;//接收到的转换结果数据
  TLC2543_DOUT_READ;
  //delay(7);
  //TLC2543_CS1_L;
  //delay(7);
  //TLC2543_CLK_L; 
  //delay(7);
  //  senddata|=0x08;
    for(i=0;i<12;i++)
   {
   	delay(1);
   	
	  if(senddata & 0x80)
	    PORTB |= BIT(3);//PORTB |= 0B00100000;
	  else  
	    PORTB &=~BIT(3);//PORTB &= 0B11011111;//准备发送控制字 
	  senddata <<= 1;	
   if(PINB & BIT(4))
	  recdata |= 0x0001;
    else  
	  recdata &= 0xFFFE;
   recdata <<= 1;//接收上次转换结果 
	PORTB |= BIT(2);//PORTB |= 0B00000100;//时钟上升沿
	delay(1);
	PORTB &=~BIT(2);//PORTB &= 0B11111011;//时钟下降沿
  }
  recdata >>= 1;
  recdata &= 0x0FFF;//截取低12位
  return(recdata);
}


unsigned int adc(unsigned char tongdao)
{
 unsigned int temp,temphe;
 unsigned char flag;
 temphe=0;
 if(tongdao<11)
 {
		delay(4);
		PORTB&=~BIT(5);//PB5-#CS1 下降沿
		delay(8);
		temp=tlc2543(tongdao);		
		PORTB|=BIT(5);//PB5-#CS1	上升沿			  		
		
			delay(4);
			PORTB&=~BIT(5);//PB5-#CS1	下降沿
			delay(8);
			temp=tlc2543(tongdao);		
			PORTB|=BIT(5);//PB5-#CS1	上升沿
 }
 else if(tongdao<22)
 {
 		delay(4);
		PORTB&=~BIT(6);//PB6-#CS2
		delay(8);
		temp=tlc2543(tongdao-11);		
		PORTB|=BIT(6);//PB6-#CS2				  		
		
		
			delay(4);
			PORTB&=~BIT(6);//PB6-#CS2
			delay(8);
			temp=tlc2543(tongdao-11);		
			PORTB|=BIT(6);//PB6-#CS2
 }
 else if(tongdao<33)
 {
  		delay(4);
		PORTB&=~BIT(7);//PB7-#CS3
		delay(8);
		temp=tlc2543(tongdao-22);		
		PORTB|=BIT(7);//PB7-#CS3				  		
		
		
			delay(4);
			PORTB&=~BIT(7);//PB7-#CS3
			delay(8);
			temp=tlc2543(tongdao-22);		
			PORTB|=BIT(7);//PB7-#CS3		
 }
 else if(tongdao<44)
 {
  		delay(4);
		PORTG&=~BIT(3);//PG3-#CS4
		delay(8);
		temp=tlc2543(tongdao-33);		
		PORTG|=BIT(3);//PG3-#CS4				  		
		
		
			delay(4);
			PORTG&=~BIT(3);//PG3-#CS4
			delay(8);
			temp=tlc2543(tongdao-33);		
			PORTG|=BIT(3);//PG3-#CS4		
 }
 
 else 
 {
  		delay(4);
		PORTG&=~BIT(4);//PG4-#CS5
		delay(8);
		temp=tlc2543(tongdao-44);		
		PORTG|=BIT(4);//PG4-#CS5				  		
		
		
			delay(4);
			PORTG&=~BIT(4);//PG4-#CS5
			delay(8);
			temp=tlc2543(tongdao-44);		
			PORTG|=BIT(4);//PG4-#CS5		
 }
 
 
 return (temp);
 
}

void shujucaiji(void)
{
   unsigned char i,j;	
	for(j=0;j<55;j++)
  {
   		val_adc[j]=0;
  }
  for(i=0;i<8;i++)
  {
   		for(j=0;j<55;j++)
		{
		 	val_adc[j]+=adc(j);
		}   
  }  
  for(j=0;j<55;j++)
  {
   		val_adc[j]=val_adc[j]>>3;
  }  
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -