?? tlc2543.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 + -