资源简介
使用stm32f401 硬件spi ,驱动TI的AD转换芯片ads131A04
代码片段和文件信息
#include “main.h“
#include “stm32f4xx.h“
void ADS131_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC ENABLE);
//ADS131 RST脚
GPIO_InitStructure.GPIO_Pin = ADS131_RST_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(ADS131_RST_PORT &GPIO_InitStructure);
ADS131_RST_H;//RST 高电平
//ADS131 CS脚
GPIO_InitStructure.GPIO_Pin = ADS131_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(ADS131_CS_PORT &GPIO_InitStructure);
ADS131_CS_H;//CS 高电平,先不选中
//ADS131 DRDY脚
GPIO_InitStructure.GPIO_Pin = ADS131_DRDY_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100M
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(ADS131_DRDY_PORT &GPIO_InitStructure);
MX_SPI1_Init();
// SPI1_DMAInit();
}
void HardwareReset(void)
{
ADS131_RST_L;
Delay_1ms(3);
ADS131_RST_H;
Delay_1ms(10);
}
//SPI1写数据
uint8_t SPI1_WriteByte(uint8_t TxBuffer)
{
uint16_t i;
while( (SPI1->SR &SPI_SR_TXE) == 0 );
SPI1->DR=TxBuffer;
while((SPI1->SR&SPI_SR_RXNE)==0);
i=SPI1->DR;
return i;
}
//SPI1读数据
uint8_t SPI1_ReadByte(void)
{
while((SPI1->SR&SPI_SR_TXE)==0);
SPI1->DR=0x00;
while((SPI1->SR&SPI_SR_RXNE)==0);
return(SPI1->DR);
}
/* write a command to ADS131a04 */
uint16_t ADS131_WRITE_CMD(uint16_t command)
{
uint8_t i;
uint16_t i_rec = 0;
ADS131_CS_L;
for(i=0;i<20;i++);
SPI1_WriteByte((uint8_t)(command>>8));
SPI1_WriteByte((uint8_t)(command));
SPI1_WriteByte(0x00);
SPI1_WriteByte(0x00);
ADS131_CS_H;
ADS131_CS_L;
for(i=0;i<20;i++);
i_rec = SPI1_ReadByte();
i_rec <<= 8;
i_rec |= SPI1_ReadByte();
SPI1_WriteByte(0x00);
SPI1_WriteByte(0x00);
ADS131_CS_H;
for(i=0;i<20;i++);
/* Return the byte read from the SPI bus */
return i_rec;
//return SPI_I2S_ReceiveData(SPI1); //返回通过SPIx最近接收的数据
}
/* read a reg */
uint16_t ADS131_READ_REG(uint8_t Addr)
{
uint16_t regaddrregvalue;
regaddr = RREG|Addr;
regaddr <<= 8;
regaddr |= 0x00;
regvalue = ADS131_WRITE_CMD(regaddr);
return regvalue;
}
/* write a reg */
uint16_t ADS131_WRITE_REG(uint8_t Addruint8_t data)
{
uint16_t regaddrregvalue;
regad
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4326 2018-03-15 20:50 ADS131.c
文件 1535 2018-03-02 16:33 ADS131.h
----------- --------- ---------- ----- ----
5861 2
评论
共有 条评论