#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 RCC_APB2Periph_ADS_PORT RCC_APB2Periph_GPIOC
//#define ADS_PORT GPIOC
#define ADS_RESET GPIO_Pin_1
#define ADS_START GPIO_Pin_2
//#define ADS_DRDY GPIO_Pin_3
#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);
//#define SPI_ADS1248_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
//#define SPI_ADS1248_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)
/******************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);
求助大家,小弟实在没搞定,4d30 0.00000 每次的读数都是这个 没电压,4d是我在指令后面写的可以准确读出是30,但是数据始终读不出来,查了很多资料说在时序上可能存在问题,但是真的是不知道怎么修改,请大家帮忙,究竟是哪里有问题。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>