前段时间画了块F40的板子,使用到ADS147这款芯片,和MCU SPI通讯,接pt100可以用来测温。现在我改成F103,同样使用ADS1247,接法和软件配置都差不多,但是这次读ADS1247寄存器的值是乱码,找不到原因,麻烦给位帮看看,给点建议。
这个是原先F407的:
[mw_shl_code=c,true]
[mw_shl_code=c,true]#include "stm32f4xx.h"
#include "ads1247.h"
#include "delay.h"
#include "stdio.h"
char q;
u8 ADH,ADM,ADL;
u32 k1,k2,k3;
/*******************************************************************************
* Function Name : SPI_Configuration
* Description : SPI接口设置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void ADS1247_SPI_Configuration()
{
SPI_InitTypeDef SPI_InitStruct;
GPIO_InitTypeDef GPIO_InitStructure,GPIO_InitStructure1,GPIO_InitStructure2;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD,ENABLE); //SPI的SCK、MISO、MOSI都是PB
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_SPI2); //SCK-PB10
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2); //MISO-PB14
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2); //MOSI-PB15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOB,&GPIO_InitStructure); //配置PB10 PB14 PB15
GPIO_InitStructure1.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStructure1.GPIO_Speed= GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure1); //PB12-DRDY
GPIO_InitStructure2.GPIO_Pin= GPIO_Pin_5|GPIO_Pin_6; //PD5-RESET PD6-START
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure2);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStructure2.GPIO_Pin=GPIO_Pin_13; //PC13-CS
GPIO_Init(GPIOC, &GPIO_InitStructure2);
//SPI_I2S_DeInit(SPI2);
SPI_Cmd(SPI2,DISABLE);
SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStruct);
SPI_Cmd(SPI2, ENABLE);
printf("SPI初始化OK
");
}
/*******************************************************************************
* Function Name : SPI_ADS1247_SendByte
* Description : SPI传输一个字符
* Input :
* Output :
*******************************************************************************/
u8 SPI_ADS1247_SendByte(u8 byte)
{
u8 retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
{
retry++;
printf("biea:%d
",1);
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI2, byte); //通过外设SPIx发送一个数据
retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) //检查指定的SPI标志位设置与否:接受缓存非空标志位
{
retry++;
printf("ca:%d
",1);
if(retry>200)return 0;
}
//GPIO_SetBits(GPIOB,GPIO_Pin_15); //DIN拉高
return SPI_I2S_ReceiveData(SPI2); //返回通过SPIx最近接收的数据
//GPIO_SetBits(GPIOB,GPIO_Pin_15); //DIN拉高
}
/*******************************************************************************
* Function Name : SPI_ADS1247_SendByte
* Description : 写ADS1247的寄存器
* Input :
* Output :
*******************************************************************************/
void ADS1247WREG(unsigned char regaddr,unsigned char databyte)
{
SPI_ADS1247_SendByte(ADS1247_CMD_WREG+(regaddr & 0xF));
SPI_ADS1247_SendByte(0);
SPI_ADS1247_SendByte(databyte);
}
/*******************************************************************************
* Function Name : SPI_ADS1247_SendByte
* Description : 读ADS1247的寄存器
* Input :
* Output :
*******************************************************************************/
unsigned char ADS1247RREG(unsigned char regaddr)
{
unsigned char r=0;
SPI_ADS1247_SendByte(ADS1247_CMD_RREG+(regaddr & 0xF));
SPI_ADS1247_SendByte(0);
r=SPI_ADS1247_SendByte(ADS1247_CMD_NOP);
return r;
}
/*******************************************************************************
* Function Name : ADS1247_ReadData
* Description : 读 ADS1247 转换值
* Input : addr:Regiter address
* Output : None
* Return : Register data
*******************************************************************************/
u32 ADS1247_ReadData()
{
u32 value = 0;
SPI_ADS1247_SendByte(ADS1247_CMD_RDATA);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12) ==1);
delay_ms(1);
value = SPI_ADS1247_SendByte(0xff)<<16;
value += SPI_ADS1247_SendByte(0xff)<<8;
value += SPI_ADS1247_SendByte(0xff);
return value;
}
/*******************************************************************************
* Function Name : ADS1247_Init (通道和电流输出端口没选择)
* Description : ADS1247 init
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void ADS1247_Init(void)
{
ADS1247_SPI_Configuration();
ADS_CS_0; //CS=0
ADS_RESET_1;
ADS_START_1;
while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) ==1);
SPI_ADS1247_SendByte(ADS1247_CMD_RESET); //RESET
delay_ms(1);
ADS1247WREG(ADS1247_REG_MUX1,0x38); // 00111000,bit7:启用内部晶振;bit6-5:内部参考电压一直打开;bit4-3:输入基准为片上基准,并且连接到REFP0;bit2-0:普通模式 (查看是否有1.5mA电流)
//MUX1赋值0x38以后REFP0和PEFN0两端会出现1.5V电压?????
delay_ms(1);
//ADS1247WREG(ADS1247_REG_SYS0,0x06); //增益1,320sps
ADS1247WREG(ADS1247_REG_SYS0,0x56); //增益32,320sps
delay_ms(1);
ADS1247WREG(ADS1247_REG_IDAC0,0x06); //DOUT/DRDY pin functions only as Data Out (default) 并且1mA
delay_ms(1);
ADS1247WREG(ADS1247_REG_VBIAS,0); //偏置电压关闭(默认)
delay_ms(1);
//量程和零点校正还没写
q=ADS1247RREG(ADS1247_REG_MUX1);
printf("测试:%x
",q);
//printf("测试:%d
",channelx);
ADS_CS_1;
}
/*******************************************************************************
* Function Name : void ADS1247_ReadData2(void)
* Description :
* Input : None
* Output : None
*******************************************************************************/
void ADS1247_ReadData2(void)
{
u8 i=0;
u32 data1,data2;
ADS_CS_0;
// ADS1247WREG(ADS1247_REG_MUX0,0x13); //AIN2+ AIN3-
// ADS1247WREG(ADS1247_REG_IDAC1,0x23); // 选择内部恒流源输出引脚 AIN2 AIN3
// SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
// data2=ADS1247_ReadData();
// printf("通道二:%x
",data2);
for(i=0;i<2;i++)
{
if(i==0)
{
ADS1247WREG(ADS1247_REG_MUX0,0x01); //AIN0+ AIN1-
ADS1247WREG(ADS1247_REG_IDAC1,0x01); // 选择内部恒流源输出引脚 AIN0 AIN1
SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
data1=ADS1247_ReadData();
printf("通道一:%x
",data1);
}
else
{
ADS1247WREG(ADS1247_REG_MUX0,0x13); //AIN2+ AIN3-
ADS1247WREG(ADS1247_REG_IDAC1,0x23); // 选择内部恒流源输出引脚 AIN2 AIN3
SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
data2=ADS1247_ReadData();
printf("通道二:%x
",data2);
}
}
ADS_CS_1;
}
float ADS1247_Readchannel1()
{
u32 data11;
float temp1;
ADS_CS_0;
ADS1247WREG(ADS1247_REG_MUX0,0x01); //AIN0+ AIN1-
ADS1247WREG(ADS1247_REG_IDAC1,0x01); // 选择内部恒流源输出引脚 AIN0 AIN1
SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
data11=ADS1247_ReadData();
// printf("通道一:%x
",data1);
ADS_CS_1;
if(data11<0x7fffff||data11==0x7fffff)
{
temp1=(2.018*data11/0x7fffff*1000/32+40)/0.37569; //外部参考电压为2V,0x7fffff=8366807,温度系数暂定为0.39,PGA=32,恒流源1mA
printf("通道一温度%.2f
", temp1);
return temp1;
}
if(data11>0x7fffff)
{
temp1=(-2.018*(0xffffff-data11+1)/0x7fffff*1000/32+39)/0.39; //补码形式
printf("通道一温度%.2f
", temp1);
return temp1;
}
}
float ADS1247_Readchannel2()
{
u32 data22;
float temp2;
ADS_CS_0;
ADS1247WREG(ADS1247_REG_MUX0,0x13); //AIN2+ AIN3-
ADS1247WREG(ADS1247_REG_IDAC1,0x23); // 选择内部恒流源输出引脚 AIN2 AIN3
SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
data22=ADS1247_ReadData();
//printf("通道二:%x
",data2);
ADS_CS_1;
if(data22<0x7fffff||data22==0x7fffff)
{
temp2=(2.018*data22/0x7fffff*1000/32+40)/0.37569; //外部参考电压为2V,0x7fffff=8366807,温度系数暂定为0.39,PGA=32,恒流源1mA
printf("通道二温度%.2f
", temp2);
return temp2;
}
if(data22>0x7fffff)
{
temp2=(-2.018*(0xffffff-data22+1)/0x7fffff*1000/32+39)/0.39; //补码形式
printf("通道二温度%.2f
", temp2);
return temp2;
}
}
[/mw_shl_code]
[/mw_shl_code]
下面这个是F103的,其实两个就是管脚配置不同:
[mw_shl_code=c,true]#include "ads1247.h"
#include "delay.h"
#include "stdio.h"
#include "stm32f10x_spi.h"
char q;
u8 ADH,ADM,ADL;
u32 k1,k2,k3;
/******************ads1247管脚与MCU对应关系*******************
SCK----------PB13
MISO---------PB14
MOSI---------PB15 (SPI)
CS-----------PC6
RESET--------PC8
START--------PC7
DRDY---------PB12
******************ads1247管脚与MCU对应关系*******************/
/*******************************************************************************
* Function Name : SPI_Configuration
* Description : SPI接口设置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void ADS1247_SPI_Configuration()
{
SPI_InitTypeDef SPI_InitStruct;
GPIO_InitTypeDef GPIO_InitStructure,GPIO_InitStructure1,GPIO_InitStructure2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE); //SPI的SCK、MISO、MOSI都是PB
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure); //配置PB13 PB14 PB15
GPIO_InitStructure1.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStructure1.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure1); //PB12-DRDY
GPIO_InitStructure2.GPIO_Pin= GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_6; //PC8-RESET PC7-START PC6-CS
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure2);
//SPI_I2S_DeInit(SPI2);
SPI_Cmd(SPI2,DISABLE);
SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; ////
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStruct);
SPI_Cmd(SPI2, ENABLE);
printf("SPI初始化OK
");
}
/*******************************************************************************
* Function Name : SPI_ADS1247_SendByte
* Description : SPI传输一个字符
* Input :
* Output :
*******************************************************************************/
u8 SPI_ADS1247_SendByte(u8 byte)
{
u8 retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
{
retry++;
printf("biea:%d
",1);
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI2, byte); //通过外设SPIx发送一个数据
retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) //检查指定的SPI标志位设置与否:接受缓存非空标志位
{
retry++;
printf("ca:%d
",1);
if(retry>200)return 0;
}
//GPIO_SetBits(GPIOB,GPIO_Pin_15); //DIN拉高
return SPI_I2S_ReceiveData(SPI2); //返回通过SPIx最近接收的数据
//GPIO_SetBits(GPIOB,GPIO_Pin_15); //DIN拉高
}
/*******************************************************************************
* Function Name : SPI_ADS1247_SendByte
* Description : 写ADS1247的寄存器
* Input :
* Output :
*******************************************************************************/
void ADS1247WREG(unsigned char regaddr,unsigned char databyte)
{
SPI_ADS1247_SendByte(ADS1247_CMD_WREG+(regaddr & 0xF));
SPI_ADS1247_SendByte(0);
SPI_ADS1247_SendByte(databyte);
}
/*******************************************************************************
* Function Name : SPI_ADS1247_SendByte
* Description : 读ADS1247的寄存器
* Input :
* Output :
*******************************************************************************/
unsigned char ADS1247RREG(unsigned char regaddr)
{
unsigned char r=0;
SPI_ADS1247_SendByte(ADS1247_CMD_RREG+(regaddr & 0xF));
SPI_ADS1247_SendByte(0);
r=SPI_ADS1247_SendByte(ADS1247_CMD_NOP);
return r;
}
/*******************************************************************************
* Function Name : ADS1247_ReadData
* Description : 读 ADS1247 转换值
* Input : addr:Regiter address
* Output : None
* Return : Register data
*******************************************************************************/
u32 ADS1247_ReadData()
{
u32 value = 0;
SPI_ADS1247_SendByte(ADS1247_CMD_RDATA);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12) ==1);
delay_ms(1);
value = SPI_ADS1247_SendByte(0xff)<<16;
value += SPI_ADS1247_SendByte(0xff)<<8;
value += SPI_ADS1247_SendByte(0xff);
return value;
}
/*******************************************************************************
* Function Name : ADS1247_Init (通道和电流输出端口没选择)
* Description : ADS1247 init
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void ADS1247_Init(void)
{
ADS1247_SPI_Configuration();
ADS_CS_0; //CS=0
ADS_RESET_1;
ADS_START_1;
while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) ==1);
SPI_ADS1247_SendByte(ADS1247_CMD_RESET); //RESET
delay_ms(1);
ADS1247WREG(ADS1247_REG_MUX1,0x38); // 00111000,bit7:启用内部晶振;bit6-5:内部参考电压一直打开;bit4-3:输入基准为片上基准,并且连接到REFP0;bit2-0:普通模式 (查看是否有1.5mA电流)
//MUX1赋值0x38以后REFP0和PEFN0两端会出现1.5V电压?????
delay_ms(1);
//ADS1247WREG(ADS1247_REG_SYS0,0x06); //增益1,320sps
ADS1247WREG(ADS1247_REG_SYS0,0x56); //增益32,320sps
delay_ms(1);
ADS1247WREG(ADS1247_REG_IDAC0,0x06); //DOUT/DRDY pin functions only as Data Out (default) 并且1mA
delay_ms(1);
ADS1247WREG(ADS1247_REG_VBIAS,0); //偏置电压关闭(默认)
delay_ms(1);
//量程和零点校正还没写
q=ADS1247RREG(ADS1247_REG_MUX1);
printf("测试:%x
",q);
//printf("测试:%d
",channelx);
ADS_CS_1;
}
/*******************************************************************************
* Function Name : void ADS1247_ReadData2(void)
* Description :
* Input : None
* Output : None
*******************************************************************************/
void ADS1247_ReadData2(void)
{
u8 i=0;
u32 data1,data2;
ADS_CS_0;
// ADS1247WREG(ADS1247_REG_MUX0,0x13); //AIN2+ AIN3-
// ADS1247WREG(ADS1247_REG_IDAC1,0x23); // 选择内部恒流源输出引脚 AIN2 AIN3
// SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
// data2=ADS1247_ReadData();
// printf("通道二:%x
",data2);
for(i=0;i<2;i++)
{
if(i==0)
{
ADS1247WREG(ADS1247_REG_MUX0,0x01); //AIN0+ AIN1-
ADS1247WREG(ADS1247_REG_IDAC1,0x01); // 选择内部恒流源输出引脚 AIN0 AIN1
SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
data1=ADS1247_ReadData();
printf("通道一:%x
",data1);
}
else
{
ADS1247WREG(ADS1247_REG_MUX0,0x13); //AIN2+ AIN3-
ADS1247WREG(ADS1247_REG_IDAC1,0x23); // 选择内部恒流源输出引脚 AIN2 AIN3
SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
data2=ADS1247_ReadData();
printf("通道二:%x
",data2);
}
}
ADS_CS_1;
}
float ADS1247_Readchannel1()
{
u32 data11;
float temp1;
ADS_CS_0;
ADS1247WREG(ADS1247_REG_MUX0,0x01); //AIN0+ AIN1-
ADS1247WREG(ADS1247_REG_IDAC1,0x01); // 选择内部恒流源输出引脚 AIN0 AIN1
SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
data11=ADS1247_ReadData();
printf("通道一:%x
",data11);
ADS_CS_1;
if(data11<0x7fffff||data11==0x7fffff)
{
temp1=(2.018*data11/0x7fffff*1000/32+40)/0.37569; //外部参考电压为2V,0x7fffff=8366807,温度系数暂定为0.39,PGA=32,恒流源1mA
printf("通道一温度%.2f摄氏度
", temp1);
return temp1;
}
if(data11>0x7fffff)
{
temp1=(-2.018*(0xffffff-data11+1)/0x7fffff*1000/32+39)/0.39; //补码形式
printf("通道一温度%.2f摄氏度
", temp1);
return temp1;
}
}
float ADS1247_Readchannel2()
{
u32 data22;
float temp2;
ADS_CS_0;
ADS1247WREG(ADS1247_REG_MUX0,0x13); //AIN2+ AIN3-
ADS1247WREG(ADS1247_REG_IDAC1,0x23); // 选择内部恒流源输出引脚 AIN2 AIN3
SPI_ADS1247_SendByte(ADS1247_CMD_SYNC); //同步AD转换
data22=ADS1247_ReadData();
printf("通道二:%x
",data22);
ADS_CS_1;
if(data22<0x7fffff||data22==0x7fffff)
{
temp2=(2.018*data22/0x7fffff*1000/32+40)/0.37569; //外部参考电压为2V,0x7fffff=8366807,温度系数暂定为0.39,PGA=32,恒流源1mA
printf("通道二温度%.2f摄氏度
", temp2);
return temp2;
}
if(data22>0x7fffff)
{
temp2=(-2.018*(0xffffff-data22+1)/0x7fffff*1000/32+39)/0.39; //补码形式
printf("通道二温度%.2f摄氏度
", temp2);
return temp2;
}
}
[/mw_shl_code]
同样,我在ADS1247_Init()函数中读取寄存器值时,发现前者可以,后者读出来是乱码。麻烦帮看看,下面是原理图:
一周热门 更多>