本帖最后由 qyihan 于 2013-6-28 16:06 编辑
公司用ADS1242芯片,现在做了一个实验板,ADS1242芯片5V供电,参考电压是2.5V,遇到一个问题,AD输出值一直是65535,不会变,不知道是怎么回事,请大家帮忙指导一下!!!
下面是我写的驱动程序:
ADS1242.h
#ifndef __ADS1242_H__
#define __ADS1242_H__
#include <reg52.h> //头文件的包含
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define _Nop() _nop_() //定义空指令
/*定义ADS1242的寄存器地址 */
#define ADS1242_SETUP 0x00
#define ADS1242_MUX 0x01
#define ADS1242_ACR 0x02
#define ADS1242_ODAC 0x03
#define ADS1242_DIO 0x04
#define ADS1242_DIR 0x05
#define ADS1242_IOCON 0x06
#define ADS1242_OCR0 0x07
#define ADS1242_OCR1 0x08
#define ADS1242_OCR2 0x09
#define ADS1242_FSR0 0x0a
#define ADS1242_FSR1 0x0b
#define ADS1242_FSR2 0x0c
#define ADS1242_DOR2 0x0d
#define ADS1242_DOR1 0x0e
#define ADS1242_DOR0 0x0f
/*定义ADS1242的寄存器地址 */
#define ADS1242_CMD_RDATA 0x01
#define ADS1242_CMD_RDATAC 0x03
#define ADS1242_CMD_STOPC 0x0f
#define ADS1242_CMD_RREG 0x10
#define ADS1242_CMD_WREG 0x50
#define ADS1242_CMD_SELFCAL 0xf0
#define ADS1242_CMD_SELFOCAL 0xf1
#define ADS1242_CMD_SELFGCAL 0xf2
#define ADS1242_CMD_SYSOCAL 0xf3
#define ADS1242_CMD_SYSGCAL 0xf4
#define ADS1242_CMD_WAKEUP 0xfb
#define ADS1242_CMD_DSYNC 0xfc
#define ADS1242_CMD_SLEEP 0xfd
#define ADS1242_CMD_RESET 0xfe
/*配置寄存器位*/
#define BUFFER_ON (1<<4) /*ACR寄存器BUFFER启用*/
#define BUFFER_OFF (0<<4) /*ACR寄存器BUFFER不用*/
#define RANGE_0 (0<<2) /*配置ACR寄存器的RANGE位为0*/
#define RANGE_1 (1<<2) /*配置ACR寄存器的RANGE位为1*/
#define SPEED_0 (0<<5) /**/
#define SPEED_1 (1<<5)
#define DATARATE_3_75 (0x10) /*配置ACR寄存器的Data Rate为3.75HZ*/
#define DATARATE_7_5 (0x01) /*配置ACR寄存器的Data Rate为7.5HZ*/
#define DATARATE_15 (0x00) /*配置ACR寄存器的Data Rate为15HZ*/
#define PGA_1 (0x00) /*配置SETUP寄存器的PGA为1 */
#define PGA_2 (0x01) /*配置SETUP寄存器的PGA为2 */
#define PGA_4 (0x02) /*配置SETUP寄存器的PGA为4 */
#define PGA_8 (0x03) /*配置SETUP寄存器的PGA为8 */
#define PGA_16 (0x04) /*配置SETUP寄存器的PGA为16 */
#define PGA_32 (0x05) /*配置SETUP寄存器的PGA为32 */
#define PGA_64 (0x06) /*配置SETUP寄存器的PGA为64 */
#define PGA_128 (0x07) /*配置SETUP寄存器的PGA为128 */
/*定义IO口*/
sbit IO_PDWN= P1^0;
sbit IO_DRDY= P1^1;
sbit IO_SCLK= P1^2;
sbit IO_DOUT= P1^3;
sbit IO_DIN= P1^4;
sbit IO_CS= P1^5;
void ADS1242Init();
/*写一个字节的数据 */
void WriteByte(uchar Dat);
/*读一个字节的数据 */
uchar ReadByte();
/*写寄存器 */
void WriteReg(uchar RegAdd,uchar RegDat);
/*读寄存器 */
uchar ReadReg(uchar RegAdd);
/*选择AD通道 */
void SelectChannel(uchar PosChannel,uchar NegChannel);
/*等待AD转换完成 */
void WaitADConComplete();
/*从DOR寄存器读取转换结果 */
ulong ReadDataFromDOR(uchar PosChannel,uchar NegChannel);
/*上电自校正*/
void SelfCalibrate();
/*供主程序调用*/
ulong ADS1242Convert(uchar PosChannel,uchar NegChannel);
#endif
ADS1242.C
#include "ads1242.h"
#include "delay.h"
uchar errcounter = 0;
void ADS1242Init()
{
IO_PDWN = 1;
IO_DOUT = 0;
}
/*写一个字节的数据 */
void WriteByte(uchar Dat)
{
uchar i;
IO_CS = 0;
IO_SCLK = 0;
_nop_();
for(i=0;i<8;i++)
{
Dat = Dat<<1;
IO_DIN = CY;
IO_SCLK = 1;
_nop_();
_nop_();
IO_SCLK = 0;
_nop_();
}
IO_CS = 1;
}
/*读一个字节的数据 */
uchar ReadByte()
{
uchar i,RDat;
RDat = 0;
IO_CS = 0;
IO_SCLK = 0;
_nop_();
for(i=0;i<8;i++)
{
IO_SCLK = 1;
_nop_();
RDat = (RDat<<1)|IO_DOUT;
IO_SCLK = 0;
_nop_();
_nop_();
// DelayUs2x(2);
}
IO_CS = 1;
return RDat;
}
/*写寄存器 */
void WriteReg(uchar RegAdd,uchar RegDat) /*寄存器地址,写入数据*/
{
WriteByte(ADS1242_CMD_WREG + RegAdd); /*写寄存器命令+寄存器地址*/
WriteByte(0x00);
WriteByte(RegDat);
}
/*读寄存器 */
uchar ReadReg(uchar RegAdd) /*寄存器地址 */
{
WriteByte(ADS1242_CMD_RREG + RegAdd); /*读寄存器命令+寄存器地址*/
WriteByte(0x00);
return (ReadByte());
}
/*选择AD通道 */
void SelectChannel(uchar PosChannel,uchar NegChannel) /*正向通道,负向通道*/
{
WriteReg(ADS1242_MUX,((PosChannel<<4)+NegChannel));
}
/*等待AD转换完成 */
void WaitADConComplete()
{
while(!IO_DRDY); /*DRDY为0表示转换完成*/
while(IO_DRDY);
}
/*从DOR寄存器读取转换结果 */
ulong ReadDataFromDOR(uchar PosChannel,uchar NegChannel) /*正负通道号*/
{
ulong temp;
uchar DataMSB,DataMid,DataLSB;
IO_CS = 0;
SelectChannel(PosChannel,NegChannel); /*选择通道*/
WaitADConComplete();
WriteByte(0x01);
IO_CS = 0;
DelayMs(1); /*发送读数据命令*/
DataMSB = ReadReg(ADS1242_DOR2);
DataMid = ReadReg(ADS1242_DOR1);
DataLSB = ReadReg(ADS1242_DOR1);
IO_CS = 1;
temp = (DataMSB<<16) + (DataMid<<8) + DataLSB;
temp = DataLSB;
return temp;
}
/*上电自校正*/
void SelfCalibrate()
{
IO_PDWN = 1;
WriteByte(ADS1242_CMD_RESET);
WriteReg(ADS1242_SETUP,PGA_1);
WriteReg(ADS1242_ACR,BUFFER_OFF + RANGE_0 + DATARATE_3_75); /*配置ACR寄存器*/
WriteReg(ADS1242_SETUP,PGA_1);
WriteReg(ADS1242_ODAC,0x00); /*上电自校正*/
IO_CS = 0;
while(IO_DRDY); /*等待校正完成*/
IO_CS = 1;
WriteReg(ADS1242_ACR,BUFFER_ON + RANGE_0 + DATARATE_3_75); /*配置ACR寄存器*/
}
/*供主程序调用*/
ulong ADS1242Convert(uchar PosChannel,uchar NegChannel)
{
ulong temp;
ADS1242Init();
SelfCalibrate();
temp = ReadDataFromDOR(PosChannel,NegChannel);
temp = ReadDataFromDOR(PosChannel,NegChannel); /*取第二个数据*/
return temp;
}
|