用51写tlc2543低位先传
时间: 2023-06-02 16:01:35 浏览: 102
51单片机控制TLC2543
以下是51单片机通过SPI协议驱动TLC2543进行低位先传输的示例代码:
```c
#include <reg51.h>
#define SCK P3_5
#define SDI P3_6
#define SDO P3_7
#define CS P3_4
unsigned char SPI_Read() {
unsigned char i, dat = 0;
for (i = 0; i < 8; i++) {
SCK = 0;
dat <<= 1;
if (SDO) dat |= 0x01;
SCK = 1;
}
return dat;
}
void SPI_Write(unsigned char dat) {
unsigned char i;
for (i = 0; i < 8; i++) {
SCK = 0;
if (dat & 0x80) SDI = 1;
else SDI = 0;
dat <<= 1;
SCK = 1;
}
}
void TLC2543_Init() {
CS = 1;
SCK = 1;
}
unsigned int TLC2543_Read(unsigned char channel) {
unsigned char i;
unsigned int result = 0;
CS = 0; // 使能片选
SPI_Write(0x80 | (channel << 4)); // 发送指令字节
for (i = 0; i < 2; i++) {
result <<= 8;
result |= SPI_Read(); // 读取数据字节
}
CS = 1; // 禁止片选
return result;
}
void main() {
unsigned int adc_value;
TLC2543_Init();
while (1) {
adc_value = TLC2543_Read(0); // 读取通道0的ADC值
// 处理ADC值
}
}
```
阅读全文