奇怪的ADS1247,关于在STM32F407和STM32F103上的应用
时间:10-02
整理:3721RD
点击:
新年到了,先祝大家新年快乐!
前段时间做一个STM32F407的项目,里面用到了ADS1247采集铂电阻温度,虽然遇到点问题,但是后面也调通了。现在这个项目用的是STM32F103,同样用的是ADS1247采集铂电阻温度,但是读ADS1247寄存器死活不对,我是焊了两块PCB,一块读出来一直是0Xff,另一块就一直在变化了。
下面是我的原理图,两个原理图一样,和MCU是SPI通讯:
下面是我的程序:
首先是STM32F407的:
#include "stm32f4xx.h" #include "ads1247.h" #include "delay.h" #include "stdio.h" char q; u8 ADH,ADM,ADL; u32 k1,k2,k3; /******************ads1247管脚与MCU对应关系******************* SCK----------PB10 MISO---------PB14 MOSI---------PB15 (SPI) CS-----------PC13 RESET--------PD5 START--------PD6 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_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\r\n"); } /******************************************************************************* * 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\r\n",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\r\n",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\r\n",q); //printf("测试:%d\r\n",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\r\n",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\r\n",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\r\n",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\r\n",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\r\n", temp1); return temp1; } if(data11>0x7fffff) { temp1=(-2.018*(0xffffff-data11+1)/0x7fffff*1000/32+39)/0.39; //补码形式 printf("通道一温度%.2f\r\n", 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\r\n",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,恒流源1mAprintf("通道二温度%.2f\r\n", temp2); return temp2; } if(data22>0x7fffff) {temp2=(-2.018*(0xffffff-data22+1)/0x7fffff*1000/32+39)/0.39; //补码形式 printf("通道二温度%.2f\r\n", temp2); return temp2; } }
接下来是STM32F103的:
#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\r\n"); } /******************************************************************************* * 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\r\n",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\r\n",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\r\n",q); //printf("测试:%d\r\n",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\r\n",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\r\n",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\r\n",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\r\n",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摄氏度\r\n", temp1); return temp1; } if(data11>0x7fffff) { temp1=(-2.018*(0xffffff-data11+1)/0x7fffff*1000/32+39)/0.39; //补码形式 printf("通道一温度%.2f摄氏度\r\n", 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\r\n",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,恒流源1mAprintf("通道二温度%.2f摄氏度\r\n", temp2); return temp2; } if(data22>0x7fffff) {temp2=(-2.018*(0xffffff-data22+1)/0x7fffff*1000/32+39)/0.39; //补码形式 printf("通道二温度%.2f摄氏度\r\n", temp2); return temp2; } } 代码可能比较多,但是主要不同的就是在SPI的GPIO初始化,其他部分都是一样的,让我不明白的就是为什么F407没问题,但是F103读寄存器不行了。我也在F103中用示波器测量SPI_SCK和SPI_MOSI的波形,都是可以出来正确时序的。
自己先顶一下,贴这么多代码挺不好意思的,实在是自己找不出原因了,拜托大家有空帮看下,谢谢!
你的第35行
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
修改成
RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
就行了。你试试吧。
你好,您可以留个邮箱或者什么联系方式吗?我最近也在用1247,有问题想跟您请教下
STM32F103的程序里的35行修改过没 楼上有人回复改了试试看