SPI串行接口AD转换器TLC2543的驱动程序
扫描二维码
随时随地手机看文章
SPI串行接口AD转换器TLC2543的驱动程序
//-----------------------函数声明,变量定义--------------------------------------------------------
#include
#include
//--------------------------------------------------------------------------------------------------
//2543控制引脚宏定义
//--------------------------------------------------------------------------------------------------
sbitSS1=P1^0;//将p1.0口模拟片选
sbitSCK=P1^1;//将p1.1口模拟时钟输出
sbitMOSI=P1^2;//将p1.2口模拟主机输出
sbitMISO=P1^3;//将p1.3口模拟主机输入
//-----------------------预定义控制字--------------------------------------------------------
#defineCONTROL_WORD0x00//选择通道0,输出数据长度12bit,MSB,单极性二进制数
//--------------------------------------------------------------------------------------------------
//函数名称:delay
//入口参数:N
//函数功能:延时子程序,实现(16*N+24)us的延时
//系统采用11.0592MHz的时钟时,延时满足要求,其它情况需要改动
//--------------------------------------------------------------------------------------------------
voiddelay(unsignedintN)
{
inti;
for(i=0;i
//--------------------------------------------------------------------------------------------------
//名称:read2543
//功能:TLC2543驱动模块
//输入参数:CON_WORD控制字
//输出参数:ad转换值
//--------------------------------------------------------------------------------------------------
unsignedintread2543(unsignedcharCON_WORD)
{
unsignedintad=0,i;
SCK=0;
SS1=0;
for(i=0;i<12;i++)
{
if(MISO)ad|=0x01;
MOSI=(bit)(CON_WORD&0x80);
SCK=1;
delay(3);
SCK=0;
delay(3);
CON_WORD<<=1;
ad<<=1;
}
SS1=1;
ad>>=1;
return(ad);
}
//--------------------------------------------------------------------------------------------------
//名称:main
//功能:主函数
//输入参数:无
//输出参数:无
//--------------------------------------------------------------------------------------------------
voidmain()
{unsignedintad;
while(1)
{
ad=read2543(CONTROL_WORD);
}
}