📄 readtemp.c
字号:
/****************************************************************************
* 功能:使用硬件SPI接口读16通道温度。(硬件:CD4051,AD7505)
****************************************************************************/
#include "LPC21XX.h"
#define AD7505_nCS 0x20000 /* P1.17口为AD7505的片选 */
#define AD7505_nDRDY 0x2000000 //define p1.25
#define TEMP_cCSCH00 0x20000000 //define p0.29
#define TEMP_cCSCH10 0x40000000 //define p0.30
#define TEMP_CSA0 0x10000000 //define p0.28
#define TEMP_CSA1 0x8000000 //define p0.27
#define TEMP_CSA2 0x4000000 //define p0.26
/****************************************************************************
* 名称:DelayNS()
* 功能:长软件延时
* 入口参数:dly 延时参数,值越大,延时越久
* 出口参数:无
****************************************************************************/
void DelayNS(int dly)
{ int i;
for(; dly>0; dly--)
for(i=0; i<50000; i++);
}
/****************************************************************************
* 名称:MSpiIni()
* 功能:初始化SPI接口,设置为主机。
* 入口参数:无
* 出口参数:无
****************************************************************************/
void MSpiIni(void)
{ SPI_SPCCR = 0x52; // 设置SPI时钟分频
SPI_SPCR = 0x30; // 设置SPI接口模式,MSTR=1,CPOL=1,CPHA=0,LSBF=0
}
/****************************************************************************
* 名称:MSendData()
* 功能:向SPI总线发送数据。
* 入口参数:data 待发送的数据
* 出口参数:返回值为读取的数据
****************************************************************************/
int MSendData(int data)
{ //IO1CLR |= AD7505_nCS; // 片选
SPI_SPDR = data;
while( 0==(SPI_SPSR&0x80) ); // 等待SPIF置位,即等待数据发送完毕
// IO1SET |= AD7505_nCS;
return(SPI_SPDR);
}
/****************************************************************************
* 名称:
* 功能:初始化硬件
****************************************************************************/
void AD7505Init(void)
{
IO1CLR |= AD7505_nCS; // 片选
MSendData(0x00); //reset
MSendData(0x00); //reset
MSendData(0x00); //reset
MSendData(0x00); //reset
MSendData(0x00); //reset
MSendData(0x20); /* Active Channel is Ain1(+)/Ain1(-), next operation as write to the clock register */
MSendData(0x0C); /* master clock enabled, 4.9512MHz Clock, set output rate to 50Hz*/
MSendData(0x10); /* Active Channel is Ain1(+)/Ain1(-), next operation as write to the setup register */
MSendData(0x40); /* gain = 1, bipolar mode, buffer off, clear FSYNC and perform a Self Calibration*/
IO1SET |= AD7505_nCS;
}
void TempHardwareInit(void)
{
// int i,j;
PINSEL0 |= 0x00005500; // 设置SPI管脚连接
PINSEL1 |= 0x00000000;
IO1DIR|= AD7505_nCS; //select
// IO1DIR|= TEMP_cCSCH00|TEMP_cCSCH10|TEMP_CSA0|TEMP_CSA1|TEMP_CSA2; //select
MSpiIni(); // 初始化SPI接口
AD7505Init();
}
/****************************************************************************
* 名称:read temp ch1
* 功能: This program has read and write routines for the 68HC11 to interface to the AD7705
* and the sample program sets the various registers and then reads 1000 samples from one channel.
****************************************************************************/
unsigned int ReadCh1(void)
{
unsigned int i;
while(IO1PIN &AD7505_nDRDY);
IO1CLR |= AD7505_nCS; // 片选
MSendData(0x38); /*set the next operation for 16 bit read from the data register */
i=MSendData(0x00);
i<<=8;
i|=MSendData(0x00);
IO1SET |= AD7505_nCS;
return(i);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -