spi设置
void spi_init(void)
{
SpiaRegs.SPICCR.all =0x000F; // Reset on, rising edge, 16-bit char bits
//0x000F对应Rising Edge,0x004F对应Falling Edge
SpiaRegs.SPICTL.all =0x000E; // Enable master mode, normal phase,talk
SpiaRegs.SPISTS.all=0x0000; // enable talk, and SPI int disabled.
SpiaRegs.SPIBRR =0x000F;
SpiaRegs.SPICCR.all =0x008F; // Relinquish SPI from Reset
SpiaRegs.SPIPRI.bit.FREE = 1; // Set so breakpoints don't disturb xmission
}
1256 函数
unsigned char SPI_SendByte(unsigned char a)
{ unsigned char b;
SpiaRegs.SPISTS.bit.INT_FLAG=1;
SpiaRegs.SPITXBUF=a&0x00FF;
while(!SpiaRegs.SPISTS.bit.INT_FLAG);
b=SpiaRegs.SPIRXBUF&0x00FF;
return b;
}
// 功 能:ADS1256 写数据,向ADS1256中地址为regaddr的寄存器写入一个字节databyte
void ADS1256WREG(unsigned char regaddr,unsigned char databyte)
{
ADS_CS = 0;
while(ADS_DRDY);//当ADS1256_DRDY为低时才能写寄存器
SPI_SendByte(ADS1256_CMD_WREG|(regaddr & 0xF)); //向寄存器写入数据地址
SPI_SendByte(0); //写入数据的个数n-1
SPI_SendByte(databyte); //向regaddr地址指向的寄存器写入数据databyte
ADS_CS = 1;
}
// 功 能:读24位数据
unsigned long ADS1256ReadData(void)
{
unsigned char i=0;
unsigned long sum=0;
unsigned long r=0;
ADS_CS = 0;
while(ADS_DRDY); //当ADS1256_DRDY为低时才能写寄存器
SPI_SendByte(ADS1256_CMD_SYNC);
SPI_SendByte(ADS1256_CMD_WAKEUP);
SPI_SendByte(ADS1256_CMD_RDATA);
//ADS_SCLK = 0;
for(i=0;i<3;i++)
{
sum = sum << 8;
r = SPI_SendByte(0);
sum |= r;
}
ADS_CS = 1;
return sum;
}
// 功 能:ADS1256 读寄存器数据,备 注: 从ADS1256中地址为regaddr的寄存器读出一个字节databyte
unsigned char ADS1256RREG(unsigned char regaddr)
{
unsigned char r=0;
ADS_CS = 0;
while(ADS_DRDY);//当ADS1256_DRDY为低时才能 读寄存器
SPI_SendByte(ADS1256_CMD_RREG|(regaddr & 0xF));//写入地址
SPI_SendByte(0);//写入读取数据的个数n-1
delay(10000);
r=SPI_SendByte(0); //读出regaddr地址指向的寄存器的数据
ADS_CS = 1;
return r;//返回数据
}
// 功 能:ADS1256初始化子程序
void ADS1256_Init(void)
{
ADS_CS = 0;
ADS_REST = 1; //
//ADS1256WREG(ADS1256_STATUS,0x06); // 高位在前、校准、使用缓冲
//ADS1256WREG(ADS1256_MUX,0x12); // 初始化端口A0为‘+’,AINCOM位‘-’
ADS1256WREG(ADS1256_ADCON,0x60);
delay(100); // 放大倍数1
ADS1256WREG(ADS1256_DRATE,ADS1256_DRATE_5SPS); // 数据5sps
delay(100);
ADS1256WREG(ADS1256_IO,0x00); // IO状态输入
delay(100);
ADS_CS = 1;
}
主函数
void main()
{
InitSysCtrl();
Init_Gpio();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
spi_fifo_init();
spi_init();
ADS1256_Init();
delay(10000);
EINT;
ERTM;
//ADS_sum(ADS1256_MUXP_AIN0 | ADS1256_MUXN_AINCOM);
//SPI_SendByte(ADS1256_CMD_SELFCAL); //偏移和增益自动校准
//SPI_SendByte(ADS1256_CMD_SYSOCAL); //系统失调校准
//SPI_SendByte(ADS1256_CMD_SYSGCAL); //系统增益校准
//ADS1256WREG(ADS1256_STATUS,0x06);
//delay(10000);
k=ADS1256RREG(ADS1256_ADCON);
if(k==0x06)
flag1=1;
while(1)
{
}
}
|