没有试 30000sps
采样率低可能还不能用, 需要改变 SPI 速率等
使用 ADS1256DRDY 引脚作中断触发进行数据采集
main.c 中 (死循环前只执行一次):
- if (state)
- {
- 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_RDATAC);
- while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
- TIM_Cmd(TIM4, ENABLE); //ʹÄÜTIMxÍâÉè
- delay_us(8);
- state = 0;
- }
外部中断:
- 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);
- }
来源: http://www.bubuko.com/infodetail-3159668.html