2019 8 9 STM32F407ADS1526连续转换模式相关配置(采样率达到15000SPS)

没有试30000sps

采样率低可能还不能用,需要改变SPI速率等

使用ADS1256DRDY引脚作中断触发进行数据采集

main.c中(死循环前只执行一次):

 1         if (state)
 2         {
 3             ADS1256WREG(ADS1256_MUX,(0 << 4) | ADS1256_MUXN_AINCOM);        //ÉèÖÃͨµÀ
 4                 
 5             GPIO_ResetBits(GPIOB, GPIO_Pin_6);;
 6             while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY)); //µ±ADS1256_DRDYΪµÍʱ²ÅÄÜд¼Ä´æÆ÷ 
 7         //    ADS1256WREG(ADS1256_MUX,channel);        //ÉèÖÃͨµÀ
 8             SPI_WriteByte(ADS1256_CMD_SYNC);
 9         //    while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
10             SPI_WriteByte(ADS1256_CMD_WAKEUP);                   
11             while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
12             SPI_WriteByte(ADS1256_CMD_RDATAC);
13             while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
14             TIM_Cmd(TIM4, ENABLE);  //ʹÄÜTIMxÍâÉè
15             delay_us(8);
16             state = 0;
17         }

外部中断:

u8 state=1;

long double getval[15010];
static u32 i;
//ÍⲿÖжÏ0·þÎñ³ÌÐò
void EXTI9_5_IRQHandler(void)
{
    long ulResult;
    long double ldVolutage;
    
    if(EXTI_GetITStatus(EXTI_Line8)!=RESET)     
    {

//        ADS1256WREG(ADS1256_MUX,(0 << 4) | ADS1256_MUXN_AINCOM);        //ÉèÖÃͨµÀ
//            
//        GPIO_ResetBits(GPIOB, GPIO_Pin_6);;
//        while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY)); //µ±ADS1256_DRDYΪµÍʱ²ÅÄÜд¼Ä´æÆ÷ 
//    //    ADS1256WREG(ADS1256_MUX,channel);        //ÉèÖÃͨµÀ
//        SPI_WriteByte(ADS1256_CMD_SYNC);
//    //    while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
//        SPI_WriteByte(ADS1256_CMD_WAKEUP);                   
//        while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
//        SPI_WriteByte(ADS1256_CMD_RDATA);
//        while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
//        delay_us(8);
//    
        
        if(i<15000)
        {
            ulResult |= (SPI_WriteByte(0xff) << 16);
            ulResult |= (SPI_WriteByte(0xff) << 8);
            ulResult |= SPI_WriteByte(0xff);

            state = 1;

            if( ulResult & 0x800000 )
            {
                ulResult = ~(unsigned long)ulResult;
                ulResult &= 0x7fffff;
                ulResult += 1;
                ulResult = -ulResult;
            }

            getval[i++] = (long double)ulResult*5/8388607;        
//            i++;        
//            printf("i = %d\r\n",i);
//            printf("%lf",ldVolutage);     //double
//            printf("V\r\n");                
        }
        else 
        {        
//            for(int t = 0;t<i;t++)
//            {
//                printf("t = %d\r\n",t);
//                printf("%lf",getval[t]);     //double
//                printf("V\r\n");    
//            }
            i=0;
        }

    }         


    EXTI_ClearITPendingBit(EXTI_Line8); //Çå³ýLINE0ÉϵÄÖжϱê־λ 
}    

       
//ÍⲿÖжϳõʼ»¯³ÌÐò
//³õʼ»¯PE2~4,PA0ΪÖжÏÊäÈë.
void EXTIX_Init(void)
{
    NVIC_InitTypeDef   NVIC_InitStructure;
    EXTI_InitTypeDef   EXTI_InitStructure;
    
//    KEY_Init(); //°´¼ü¶ÔÓ¦µÄIO¿Ú³õʼ»¯
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//ʹÄÜSYSCFGʱÖÓ    
 
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource8);//PB8 Á¬½Óµ½ÖжÏÏß8

    /* ÅäÖÃEXTI_Line2,3,4 */
    EXTI_InitStructure.EXTI_Line = EXTI_Line8;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//ÖжÏʼþ
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;//ÖжÏÏßʹÄÜ
  EXTI_Init(&EXTI_InitStructure);//ÅäÖÃ
 
        
    NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//ÍⲿÖжÏ0
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//ÇÀÕ¼ÓÅÏȼ¶0
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//×ÓÓÅÏȼ¶2
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//ʹÄÜÍⲿÖжÏͨµÀ
  NVIC_Init(&NVIC_InitStructure);//ÅäÖÃ
    
       
}

 

ADS1256配置

void SPI1_Init(void)
{     
  GPIO_InitTypeDef  GPIO_InitStructure;
  SPI_InitTypeDef  SPI_InitStructure;
    
//  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//ʹÄÜGPIOBʱÖÓ
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//ʹÄÜSPI1ʱÖÓ
 
  //GPIOFB3,4,5³õʼ»¯ÉèÖÃ
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;//PB3~5¸´Óù¦ÄÜÊä³ö    
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜ
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­
  GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯
    
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_SPI1); //PB3¸´ÓÃΪ SPI1
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource4,GPIO_AF_SPI1); //PB4¸´ÓÃΪ SPI1
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1); //PB5¸´ÓÃΪ SPI1
 
    //ÕâÀïÖ»Õë¶ÔSPI¿Ú³õʼ»¯
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//¸´Î»SPI1
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);//Í£Ö¹¸´Î»SPI1

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;        //ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPI
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;        //ÉèÖÃSPIµÄÊý¾Ý´óС:SPI·¢ËͽÓÊÕ8λ֡½á¹¹
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;        //´®ÐÐͬ²½Ê±ÖӵĿÕÏÐ״̬ΪµÍµçƽ
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;    //´®ÐÐͬ²½Ê±Öӵĵڶþ¸öÌø±äÑØ£¨ÉÏÉý»òϽµ£©Êý¾Ý±»²ÉÑù
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;        //NSSÐźÅÓÉÓ²¼þ£¨NSS¹Ü½Å£©»¹ÊÇÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí:ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;        //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ256
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;    //Ö¸¶¨Êý¾Ý´«Êä´ÓMSBλ»¹ÊÇLSBλ¿ªÊ¼:Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
    SPI_InitStructure.SPI_CRCPolynomial = 7;    //CRCÖµ¼ÆËãµÄ¶àÏîʽ
    SPI_Init(SPI1, &SPI_InitStructure);  //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷
 
    SPI_Cmd(SPI1, ENABLE); //ʹÄÜSPIÍâÉè
}

//³õʼ»¯ADS1256 GPIO
void Init_ADS1256_GPIO(void)
{
  GPIO_InitTypeDef GPIO_InitStructure; 

    RCC -> AHB1ENR |= (1<<1);    //PB
    
     GPIO_InitStructure.GPIO_Pin = GPIO_RCC_ADS1256Reset|GPIO_Pin_6; 
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
     GPIO_Init(GPIO_RCC_ADS1256Reset_PORT, &GPIO_InitStructure);  
    
     GPIO_ResetBits(GPIO_RCC_ADS1256Reset_PORT, GPIO_RCC_ADS1256Reset );

    GPIO_InitStructure.GPIO_Pin = GPIO_ADS1256DRDY; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_Init(GPIO_ADS1256DRDY_PORT, &GPIO_InitStructure);  

    SPI1_Init();
}


//-----------------------------------------------------------------//
//    ¹¦    ÄÜ£º  Ä£ÄâSPIͨÐÅ
//    Èë¿Ú²ÎÊý: /    ·¢Ë͵ÄSPIÊý¾Ý
//    ³ö¿Ú²ÎÊý: /    ½ÓÊÕµÄSPIÊý¾Ý
//    È«¾Ö±äÁ¿: /
//    ±¸    ×¢:     ·¢ËͽÓÊÕº¯Êý
//-----------------------------------------------------------------//
unsigned char SPI_WriteByte(unsigned char TxData)
{     
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}//µÈ´ý·¢ËÍÇø¿Õ  
    
    SPI_I2S_SendData(SPI1, TxData); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öbyte  Êý¾Ý
        
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){} //µÈ´ý½ÓÊÕÍêÒ»¸öbyte  
 
    return SPI_I2S_ReceiveData(SPI1); //·µ»Øͨ¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý    
             
} 

//-----------------------------------------------------------------//
//    ¹¦    ÄÜ£ºADS1256 дÊý¾Ý
//    Èë¿Ú²ÎÊý: /
//    ³ö¿Ú²ÎÊý: /
//    È«¾Ö±äÁ¿: /
//    ±¸    ×¢: ÏòADS1256ÖеØַΪregaddrµÄ¼Ä´æÆ÷дÈëÒ»¸ö×Ö½Údatabyte
//-----------------------------------------------------------------//
void ADS1256WREG(unsigned char regaddr,unsigned char databyte)
{
    GPIO_ResetBits(GPIOB, GPIO_Pin_6);
    while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));//µ±ADS1256_DRDYΪµÍʱ²ÅÄÜд¼Ä´æÆ÷
    //Ïò¼Ä´æÆ÷дÈëÊý¾ÝµØÖ·
    SPI_WriteByte(ADS1256_CMD_WREG | (regaddr & 0x0F));
    //дÈëÊý¾ÝµÄ¸öÊýn-1
    SPI_WriteByte(0x00);
    //ÏòregaddrµØÖ·Ö¸ÏòµÄ¼Ä´æÆ÷дÈëÊý¾Ýdatabyte
    SPI_WriteByte(databyte);
    delay_us(1);
    GPIO_SetBits(GPIOB, GPIO_Pin_6);
}


//³õʼ»¯ADS1256
void ADS1256_Init(void)
{
    Init_ADS1256_GPIO();
    GPIO_ResetBits(GPIOB, GPIO_Pin_7);  
    delay_us(50);
    GPIO_SetBits(GPIOB, GPIO_Pin_7);//RESET
    delay_us(50);    
    GPIO_ResetBits(GPIOB, GPIO_Pin_6);//CS
    ADS1256WREG(ADS1256_STATUS,0x06);  // ¸ßλÔÚÇ°¡¢Ð£×¼¡¢Ê¹Óûº³å
    while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
    SPI_WriteByte(ADS1256_CMD_SELFCAL);//Æ«ÒÆÔöÒæ×ÔУ׼
//    ADS1256WREG(ADS1256_MUX,0x08);                  // ³õʼ»¯¶Ë¿ÚA0Ϊ¡®+¡¯£¬AINCOMλ¡®-¡¯
//    while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
    ADS1256WREG(ADS1256_ADCON,0);   // ·Å´ó±¶Êý1
    ADS1256WREG(ADS1256_DRATE,ADS1256_DRATE_15000SPS);  // Êý¾Ý5sps
    ADS1256WREG(ADS1256_IO,0x00); 
    GPIO_SetBits(GPIOB, GPIO_Pin_6);    
}

 

转载于:https://www.cnblogs.com/calm-monkey/p/11369858.html

STM32单片机(STM32F429)读写(8通道带PGA的24位ADC)ADS1256软件例程源码,可以做为你的学习设计参考。 int main(void) { uint8_t i; bsp_Init(); PrintfLogo(); /* 打印例程Logo到串口1 */ bsp_DelayMS(100); /* 等待上电稳定,等基准电压电路稳定, bsp_InitADS1256() 内部会进行自校准 */ bsp_InitADS1256(); /* 初始化配置ADS1256. PGA=1, DRATE=30KSPS, BUFEN=1, 输入正负5V */ /* 打印芯片ID (通过读ID可以判断硬件接口是否正常) , 正常时状态寄存器的高4bit = 3 */ #if 0 { uint8_t id; id = ADS1256_ReadChipID(); if (id != 3) { printf("Error, ASD1256 Chip ID = 0x%X\r\n", id); } else { printf("Ok, ASD1256 Chip ID = 0x%X\r\n", id); } } #endif ADS1256_CfgADC(ADS1256_GAIN_1, ADS1256_30SPS); /* 配置ADC参数: 增益1:1, 数据输出速率 1KHz */ ADS1256_StartScan(); /* 启动中断扫描模式, 轮流采集8个通道的ADC数据. 通过 ADS1256_GetAdc() 函数来读取这些数据 */ while (1) { bsp_Idle(); /* 空闲时执行的函数,比如喂狗. 在bsp.c中 */ /* 打印采集数据 */ for (i = 0; i < 8; i++) { int32_t iTemp; iTemp = ((int64_t)g_tADS1256.AdcNow[i] * 2500000) / 4194303; /* 计算实际电压值(近似估算的),如需准确,请进行校准 */ if (iTemp < 0) { iTemp = -iTemp; printf("%d=%6d,(-%d.%03d %03d V) ", i, g_tADS1256.AdcNow[i], iTemp /1000000, (iTemp%1000000)/1000, iTemp%1000); } else { printf("%d=%6d,( %d.%03d %03d V) ", i, g_tADS1256.AdcNow[i], iTemp/1000000, (iTemp%1000000)/1000, iTemp%1000); } } printf("\r\n"); bsp_DelayMS(500); /* 每隔500ms 输出一次数据 */ } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值