使用ADS1248和STM32F4相连,能够读出自己写的指令,却不能读出数据
用的SPI2与ADS1248相连
ADS1248WREG(ADS1248_REG_MUX0,0x01); //00100101,Bit7-6:传感器电流源检测不使用,Bit5-3:正输入为AIN0,Bit2-0:负输入为AIN1
delay_ms(20);
ADS1248WREG(ADS1248_REG_MUX1,0x30); //00110000,Bit7:启用内部晶振,Bit6-5:内部基准一直打开,Bit4-3:输入基准为片上基准,Bit2-0:普通模式
delay_ms(20);
ADS1248WREG(ADS1248_REG_VBIAS,0); //00000000,Bit7-0:偏置电压选择关闭(默认)
delay_ms(20);
ADS1248WREG(ADS1248_REG_IDAC1,0xFF);//11111111,Bit7-4:断开内部第一恒流源的连接,Bit3-0:断开内部第二恒流源的连接 0X8F打开第一内部恒流源
delay_ms(20);
ADS1248WREG(ADS1248_REG_IDAC0,0x00); //00001000,Bit7-4:只读,Bit3:DRDY/DOUT功能复用Dout线 ,Bit2-0:关闭内部恒流源
delay_ms(20);
q=ADS1248RREG(ADS1248_REG_IDAC1);
printf("4d%x",q);
ADS1248WREG(ADS1248_REG_SYS0,0x09); //00000000,Bit7:必须设置为0,Bit6-41:选择128倍增益,Bit3-0:选择2000SPS测量速度
delay_ms(20);
4d 后面回的指令都是对的
4dff 0.00000 为什么就是没有数据值出来
你试试内部的测试信号能否正常读出,原理图能不能看看
内部测试信号怎么测试?额,原理图这个我是直接接的实物,用的是F4的SPI2的四个脚和ADS1248连接,START和REST接的是另外两个脚,之前将DSRY做中断,后来直接在while里面直接读AD值
System Monitors里的测试信号,可以配置。
#include "stm32f4xx.h"
#include "ADS1248.h"
#include "delay.h"
u8 results1,results2,results3;
u32 D;
char q;
void SPI_ADS1248_Init(void)
{
SPI_InitTypeDef SPI_InitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(Open_SPIx_SCK_GPIO_CLK | Open_SPIx_MISO_GPIO_CLK | Open_SPIx_MOSI_GPIO_CLK,ENABLE);
Open_SPIx_CLK_INIT(RCC_APB1Periph_SPI2,ENABLE);
GPIO_PinAFConfig(Open_SPIx_SCK_GPIO_PORT, Open_SPIx_SCK_SOURCE, Open_SPIx_MOSI_AF);
GPIO_PinAFConfig(Open_SPIx_MISO_GPIO_PORT, Open_SPIx_MISO_SOURCE, Open_SPIx_MOSI_AF);
GPIO_PinAFConfig(Open_SPIx_MOSI_GPIO_PORT, Open_SPIx_MOSI_SOURCE, Open_SPIx_MOSI_AF);
GPIO_InitStructure.GPIO_Pin = Open_SPIx_SCK_PIN;
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(Open_SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Open_SPIx_MISO_PIN;
GPIO_Init(Open_SPIx_MISO_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Open_SPIx_MOSI_PIN;
GPIO_Init(Open_SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);
SPI_I2S_DeInit(Open_SPIx);
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(Open_SPIx, &SPI_InitStruct);
SPI_Cmd(Open_SPIx, ENABLE);
}
/*******************************************************************************
* Function Name : SPI_FLASH_SendByte
* Description : Sends a byte through the SPI interface and return the byte
* received from the SPI bus.
* Input : byte : byte to send.
* Output : None
* Return : The value of the received byte.
*******************************************************************************/
u8 SPI_ADS1248_SendByte(u8 byte)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI2, byte);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI2);
}
/*******************************************************************
函数名:初始化DAC接口
函数原型:void ADC_gpio_init()
参数说明:无
返回: 无
********************************************************************/
void ADS1248_GPIO_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Pin= ADS_RESET ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //--推免输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //--GPIO翻转速度为50MHz
GPIO_Init(GPIOA , &GPIO_InitStructure); //--将配置写入对应的GPIO寄存器中
RCC_AHBxPeriphClockCmd(RCC_GPIO_CS,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIO_CS_PORT, &GPIO_InitStructure);
}
//-----------------------------------------------------------------//
// 功 能:ADS1248 写数据
// 入口参数: /
// 出口参数: /
// 全局变量: /
// 备 注: 向ADS1248中地址为regaddr的寄存器写入一个字节databyte
//-----------------------------------------------------------------//
void ADS1248WREG(unsigned char regaddr,unsigned char databyte)
{
SPI_ADS1248_SendByte(ADS1248_CMD_WREG+(regaddr & 0xF));
SPI_ADS1248_SendByte(0);
SPI_ADS1248_SendByte(databyte);
}
//-----------------------------------------------------------------//
// 功 能:ADS1248 读数据
// 入口参数: /
// 出口参数: /
// 全局变量: /
// 备 注: 从ADS1248中地址为regaddr的寄存器读出一个字节databyte
//-----------------------------------------------------------------//
unsigned char ADS1248RREG(unsigned char regaddr)
{
unsigned char r=0;
SPI_ADS1248_SendByte(ADS1248_CMD_RREG+(regaddr & 0xF));
SPI_ADS1248_SendByte(0);
r=SPI_ADS1248_SendByte(ADS1248_CMD_NOP);
return r;
}
//-----------------------------------------------------------------//
// 功 能:ADS1248初始化子程序
// 入口参数: /
// 出口参数: /
// 全局变量: /
// 备 注: /
//-----------------------------------------------------------------//
void InitADS1248(void)
{
START_0;//发送复位命令
RESET_0;
delay_ms(10);
RESET_1;
delay_ms(20);
ADS1248WREG(ADS1248_REG_MUX0,0x01);
delay_ms(20);
ADS1248WREG(ADS1248_REG_MUX1,0x30);
delay_ms(20);
ADS1248WREG(ADS1248_REG_VBIAS,0);
delay_ms(20);
ADS1248WREG(ADS1248_REG_IDAC1,0xFF);
delay_ms(20);
ADS1248WREG(ADS1248_REG_IDAC0,0x00);
delay_ms(20);
q=ADS1248RREG(ADS1248_REG_MUX1);
printf("4d%x",q);
ADS1248WREG(ADS1248_REG_SYS0,0x01);
delay_ms(20);
}
void Ads1248(void)
{
D=0;
SPI_ADS1248_SendByte(ADS1248_CMD_RDATAC);
results1=SPI_ADS1248_SendByte(0xFF);
results2=SPI_ADS1248_SendByte(0xFF);
results3=SPI_ADS1248_SendByte(0xFF);
D|=results1<<16|results2<<8|results3;
SPI_ADS1248_SendByte(ADS1248_CMD_NOP);
}
void START_test(void)
{
START_1;
delay_us(800);
// delay_ms(7);
START_0;
}
void NVIC_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOE,&GPIO_InitStructure);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource4);
EXTI_InitStructure.EXTI_Line = EXTI_Line4; //--外部中断线,使用第1根
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //--中断模式
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //--上升沿触发模式
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //--打开中断
EXTI_Init(&EXTI_InitStructure); //--写入对应寄存器中
// 使能外部中断,并配置优先级// EXTI_Trigger_Falling EXTI_Trigger_Rising
NVIC_InitStructure.NVIC_IRQChannel =EXTI4_IRQn; //--第1个引脚,所以使用1通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//--设置中断优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //--设置子优先级为1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //--启用通道
NVIC_Init(&NVIC_InitStructure);
}
#ifndef _ADS1248_H
#define _ADS1248_H
#include <stdio.h>
#include "stm32f4xx.h"
/**
* @brief Definition for connected to I2C1
*/
/* SPIx Communication boards Interface */
// SPI1 MISO (PA6) MOSI(PA7) CLK(PA5) NSS(PA4)
#define Open_SPIx SPI2
#define Open_SPIx_CLK RCC_APB1Periph_SPI2
#define Open_SPIx_CLK_INIT RCC_APB1PeriphClockCmd
#define Open_SPIx_IRQn SPI2_IRQn
#define Open_SPIx_IRQHANDLER SPI2_IRQHandler
#define Open_SPIx_SCK_PIN GPIO_Pin_13
#define Open_SPIx_SCK_GPIO_PORT GPIOB
#define Open_SPIx_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB
#define Open_SPIx_SCK_SOURCE GPIO_PinSource13
#define Open_SPIx_SCK_AF GPIO_AF_SPI2
#define Open_SPIx_MISO_PIN GPIO_Pin_14
#define Open_SPIx_MISO_GPIO_PORT GPIOB
#define Open_SPIx_MISO_GPIO_CLK RCC_AHB1Periph_GPIOB
#define Open_SPIx_MISO_SOURCE GPIO_PinSource14
#define Open_SPIx_MISO_AF GPIO_AF_SPI2
#define Open_SPIx_MOSI_PIN GPIO_Pin_15
#define Open_SPIx_MOSI_GPIO_PORT GPIOB
#define Open_SPIx_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB
#define Open_SPIx_MOSI_SOURCE GPIO_PinSource15
#define Open_SPIx_MOSI_AF GPIO_AF_SPI2
/******************ads1248引脚设置*******************/
#define ADS_RESET GPIO_Pin_1
#define ADS_START GPIO_Pin_2
#define ADS_DRDY GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4)
#define RESET_1 GPIO_SetBits(GPIOA, ADS_RESET)
#define RESET_0 GPIO_ResetBits(GPIOA, ADS_RESET)
#define START_1 GPIO_SetBits(GPIOA, ADS_START)
#define START_0 GPIO_ResetBits(GPIOA, ADS_START)
//#define SPI_ADS1248_CS_PIN GPIO_Pin_4 /* PC.05 */
#define RCC_GPIO_CS RCC_AHB1Periph_GPIOB
#define RCC_AHBxPeriphClockCmd RCC_AHB1PeriphClockCmd
#define GPIO_PIN_CS GPIO_Pin_12
#define GPIO_CS_PORT GPIOB
#define SPI_ADS1248_Enable GPIO_ResetBits(GPIO_CS_PORT,GPIO_PIN_CS);
#define SPI_ADS1248_Disable GPIO_SetBits(GPIO_CS_PORT,GPIO_PIN_CS);
/******************ads1248寄存器地址*******************/
#define ADS1248_CMD_WAKEUP 0x00
#define ADS1248_CMD_SLEEP 0x02
#define ADS1248_CMD_SYNC 0x04
#define ADS1248_CMD_RESET 0x06
#define ADS1248_CMD_NOP 0xff
#define ADS1248_CMD_RDATA 0x12
#define ADS1248_CMD_RDATAC 0x14
#define ADS1248_CMD_SDATAC 0x16
#define ADS1248_CMD_RREG 0x20
#define ADS1248_CMD_WREG 0x40
#define ADS1248_REG_MUX0 0x00
#define ADS1248_REG_VBIAS 0x01
#define ADS1248_REG_MUX1 0x02
#define ADS1248_REG_SYS0 0x03
#define ADS1248_REG_CFC0 0x04
#define ADS1248_REG_CFC1 0x05
#define ADS1248_REG_CFC2 0x06
#define ADS1248_REG_FSC0 0x07
#define ADS1248_REG_FSC1 0x08
#define ADS1248_REG_FSC2 0x09
#define ADS1248_REG_IDAC0 0x0a
#define ADS1248_REG_IDAC1 0x0b
#define ADS1248_REG_GPIOCFG 0x0c
#define ADS1248_REG_GPIODIR 0x0d
#define ADS1248_REG_GPIODAT 0x0e
/******************ads1248测量通道选择*******************/
#define Ads_1 0x07
#define Ads_2 0x0f
#define Ads_3 0x17
#define Ads_4 0x1f
#define Ads_5 0x27
#define Ads_6 0x2f
#define Ads_7 0x37
extern u8 results1;
extern u8 results2;
extern u8 results3;
extern u32 D;
extern char q;
void ADS1248_GPIO_init(void);
void ADS1248WREG(unsigned char regaddr,unsigned char databyte);
void InitADS1248(void);
void Ads1248(void);
void START_test(void);
void SPI_ADS1248_Init(void);
u8 SPI_ADS1248_SendByte(u8 byte);
u8 SPI_ADS1248_Receivebyte(void);
那个你说的那个应该怎么配置?这是我的ADS1248的配置代码