#include "main.h"
void ADS1120_RESET(void)
{
while((SPI2->SR &SPI_I2S_FLAG_TXE)==RESET);//发送缓冲器为空
SPI_I2S_SendData(SPI2, 0x06); //复位
}
void ADS1120_WREG(void)
{
u16 time = 1000;
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x40); //在00寄存器中写入1个字节
while(time)
{
time--;
}
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x30); //AINP = AIN1, AINN = AIN2;Gain = 1 (default)GA enabled (default)
time = 1000;
while(time)
{
time--;
}
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x44); //在01寄存器中写入一个字节; )
time = 1000;
while(time)
{
time--;
}
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
//20 SPS (default);Normal mode (256-kHz modulator clock) (default); 单次; Disables temperature sensor (default);Current sources off (default)
SPI_I2S_SendData(SPI2, 0x00);
time = 1000;
while(time)
{
time--;
}
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x48); //在02寄存器中写入一个字节
time = 1000;
while(time)
{
time--;
}
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x00); //无特殊配置都是默认值
time = 1000;
while(time)
{
time--;
}
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x4C);//在03寄存器中写入一个字节
time = 1000;
while(time)
{
time--;
}
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x00); //无特殊配置都是默认值Only the dedicated DRDY pin is used to indicate when data are ready (default)
}
void ADS1120_RREG(void)
{
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x20);//读寄存器
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==SET);//接受缓冲器为非空
SPI_I2S_ReceiveData(SPI2);
}
void ADS1120_START(void)
{
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x08);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
}
void ADS1120_RDATA(void)
{
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x10);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
SPI_I2S_ReceiveData(SPI2);
}
void RCC_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);//使能或者失能APB1外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能或者失能APB2外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能复用功能时钟
}
void SPI_Config()
{
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;//时钟悬空低
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//第二个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;//内部NSS信号由SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;//预分频值为16
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7;//用于CRC值计算的多项式
SPI_Init(SPI2,&SPI_InitStructure);//初始化配置
SPI_Cmd(SPI2,ENABLE);//使能或者失能SPI外设
}
void Hardware_GPIO_Init(void)
{
//初始化miso,mosi,sclk引脚/cs外部接地
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15|GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, 12);
*/
// 中断引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource6);
}
void DATA_EXTI_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line6;//中断线
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断请求
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿为中断请求
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能
EXTI_Init(&EXTI_InitStructure);//检查指定的EXTI线路触发请求发生与否
}
/*void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
*/
int main()
{
SystemInit(); //配置系统时钟
RCC_Init(); //配置外部时钟
Hardware_GPIO_Init(); //配置spi引脚;中断引脚
SPI_Config(); //配置spi
DATA_EXTI_Init(); //配置/drdy中断
ADS1120_RESET(); //初始化芯片
ADS1120_WREG(); //配置芯片
ADS1120_START(); //开始转换
//ADS1120_RREG();
ADS1120_RDATA();
while (1)
{ while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x00); //在00寄存器中写入1个字节
temp2 = SPI_I2S_ReceiveData(SPI2);
ADS1120_START(); //开始转换
ADS1120_RDATA();
/*if(AD_EXTI_FLAG == 1)
{
AD_EXTI_FLAG = 0;
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x00);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
temp1 = SPI_I2S_ReceiveData(SPI2);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, 0x00);
temp= (((temp1 << 8) &0xFF00) | temp2);
return temp;
}
*/
}
} |
|