实例:STM32f412+ADS1256 (8 通道带PGA 的24 位ADC ) ADS1256精度很高,适合做5位半万用表,称重系统。 1 程序分析: 1.1 主程序 int main(void) { int32_tadc[8]; int32_tvolt[8]; bsp_Init(); PrintfLogo(); /* 串口输出 */ bsp_DelayMS(100); /* 延时 */ bsp_InitADS1256(); /*初始化ADS1256. PGA=1, DRATE=30KSPS,BUFEN=1,输入电压正负5V */ { uint8_tid; id= ADS1256_ReadChipID(); //读取芯片id if(id != 3) { printf("Error,ASD1256 Chip ID = 0x%Xrn", id); } else { printf("Ok,ASD1256 Chip ID = 0x%Xrn", id); } } ADS1256_CfgADC(ADS1256_GAIN_1,ADS1256_1000SPS); /*配置adc参数,增益1:1 输出速率 1KHz */ ADS1256_StartScan(); /* 启动中断扫描模式,轮流采集8通道数据 */ while(1) { bsp_Idle(); /* 空闲函数,看门狗*/ for(i = 0; i < 8; i++) { /*采样结果*/ adc= ADS1256_GetAdc(i); /*4194303对应2.5V */ volt= ((int64_t)adc * 2500000) / 4194303; /*计算实际电压 */ } { int32_tiTemp; for(i = 0; i < 8; i++) { iTemp= volt; if(iTemp < 0) { iTemp= -iTemp; printf("%d=%6d,(-%d.%03d%03d V) ", i, adc, iTemp /1000000, (iTemp%1000000)/1000, iTemp%1000); } else { printf("%d=%6d,(%d.%03d %03d V) ", i, adc, iTemp/1000000, (iTemp%1000000)/1000,iTemp%1000); } } printf("rn"); } bsp_DelayMS(300); /*延时 */ } } 1.2配置系统时钟 #if defined (STM32F40_41xxx) uint32_t SystemCoreClock = 100000000; #endif /* STM32F40_41xxx */ SystemInit() 该函数被启动文件调用。 void SystemInit(void) { /*FPU settings ------------------------------------------------------------*/ #if(__FPU_PRESENT == 1) && (__FPU_USED == 1) SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ #endif /*Reset the RCC clock configuration to the default reset state ------------*/ /*Set HSION bit */ RCC->CR |= (uint32_t)0x00000001; /*Reset CFGR register */ RCC->CFGR = 0x00000000; /*Reset HSEON, CSSON and PLLON bits */ RCC->CR &= (uint32_t)0xFEF6FFFF; /*Reset PLLCFGR register */ RCC->PLLCFGR = 0x24003010; /*Reset HSEBYP bit */ RCC->CR &= (uint32_t)0xFFFBFFFF; /*Disable all interrupts */ RCC->CIR = 0x00000000; #if defined (DATA_IN_ExtSRAM) || defined(DATA_IN_ExtSDRAM) SystemInit_ExtMemCtl(); #endif /* DATA_IN_ExtSRAM ||DATA_IN_ExtSDRAM */ /*Configure the System clock source, PLL Multiplier and Divider factors, AHB/APBx prescalers and Flash settings----------------------------------*/ SetSysClock(); /*Configure the Vector Table location add offset address ------------------*/ #ifdef VECT_TAB_SRAM SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocationin Internal SRAM */ #else SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocationin Internal FLASH */ #endif } 1.3 硬件配置 void SystemInit_ExtMemCtl(void) { register uint32_t tmpreg = 0, timeout = 0xFFFF; register uint32_t index; /*Enable GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH and GPIOI interface clock */ RCC->AHB1ENR |= 0x000001FC; /*Connect PCx pins to FMC Alternate function */ GPIOC->AFR[0] = 0x0000000c; GPIOC->AFR[1] = 0x00007700; /*Configure PCx pins in Alternate function mode */ GPIOC->MODER = 0x00a00002; /*Configure PCx pins speed to 50 MHz */ GPIOC->OSPEEDR = 0x00a00002; /*Configure PCx pins Output type to push-pull */ GPIOC->OTYPER = 0x00000000; /*No pull-up, pull-down for PCx pins */ GPIOC->PUPDR = 0x00500000; /*Connect PDx pins to FMC Alternate function */ GPIOD->AFR[0] = 0x000000CC; GPIOD->AFR[1] = 0xCC000CCC; /*Configure PDx pins in Alternate function mode */ GPIOD->MODER = 0xA02A000A; /*Configure PDx pins speed to 50 MHz */ GPIOD->OSPEEDR = 0xA02A000A; /*Configure PDx pins Output type to push-pull */ GPIOD->OTYPER = 0x00000000; /*No pull-up, pull-down for PDx pins */ GPIOD->PUPDR = 0x00000000; /*Connect PEx pins to FMC Alternate function */ GPIOE->AFR[0] = 0xC00000CC; GPIOE->AFR[1] = 0xCCCCCCCC; /*Configure PEx pins in Alternate function mode */ GPIOE->MODER = 0xAAAA800A; /*Configure PEx pins speed to 50 MHz */ GPIOE->OSPEEDR = 0xAAAA800A; /*Configure PEx pins Output type to push-pull */ GPIOE->OTYPER = 0x00000000; /*No pull-up, pull-down for PEx pins */ GPIOE->PUPDR = 0x00000000; /*Connect PFx pins to FMC Alternate function */ GPIOF->AFR[0] = 0xcccccccc; GPIOF->AFR[1] = 0xcccccccc; /*Configure PFx pins in Alternate function mode */ GPIOF->MODER = 0xAA800AAA; /*Configure PFx pins speed to 50 MHz */ GPIOF->OSPEEDR = 0xAA800AAA; /*Configure PFx pins Output type to push-pull */ GPIOF->OTYPER = 0x00000000; /*No pull-up, pull-down for PFx pins */ GPIOF->PUPDR = 0x00000000; /*Connect PGx pins to FMC Alternate function */ GPIOG->AFR[0] = 0xcccccccc; GPIOG->AFR[1] = 0xcccccccc; /* ConfigurePGx pins in Alternate function mode */ GPIOG->MODER = 0xaaaaaaaa; /*Configure PGx pins speed to 50 MHz */ GPIOG->OSPEEDR = 0xaaaaaaaa; /*Configure PGx pins Output type to push-pull */ GPIOG->OTYPER = 0x00000000; /*No pull-up, pull-down for PGx pins */ GPIOG->PUPDR = 0x00000000; /*Connect PHx pins to FMC Alternate function */ GPIOH->AFR[0] = 0x00C0CC00; GPIOH->AFR[1] = 0xCCCCCCCC; /*Configure PHx pins in Alternate function mode */ GPIOH->MODER = 0xAAAA08A0; /*Configure PHx pins speed to 50 MHz */ GPIOH->OSPEEDR = 0xAAAA08A0; /*Configure PHx pins Output type to push-pull */ GPIOH->OTYPER = 0x00000000; /*No pull-up, pull-down for PHx pins */ GPIOH->PUPDR = 0x00000000; /*Connect PIx pins to FMC Alternate function */ GPIOI->AFR[0] = 0xCCCCCCCC; GPIOI->AFR[1] = 0x00000CC0; /*Configure PIx pins in Alternate function mode */ GPIOI->MODER = 0x0028AAAA; /*Configure PIx pins speed to 50 MHz */ GPIOI->OSPEEDR = 0x0028AAAA; /*Configure PIx pins Output type to push-pull */ GPIOI->OTYPER = 0x00000000; /*No pull-up, pull-down for PIx pins */ GPIOI->PUPDR = 0x00000000; /*-- FMC Configuration------------------------------------------------------*/ /*Enable the FMC interface clock */ RCC->AHB3ENR |= 0x00000001; /*Configure and enable SDRAM bank1 */ FMC_Bank5_6->SDCR[0] = 0x000039D0; FMC_Bank5_6->SDTR[0] = 0x01115351; /*SDRAM initialization sequence */ /*Clock enable command */ FMC_Bank5_6->SDCMR = 0x00000011; tmpreg = FMC_Bank5_6->SDSR & 0x00000020; while((tmpreg != 0) & (timeout-- > 0)) { tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } /*Delay */ for(index = 0; index<1000; index++); /*PALL command */ FMC_Bank5_6->SDCMR = 0x00000012; timeout = 0xFFFF; while((tmpreg != 0) & (timeout-- > 0)) { tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } /*Auto refresh command */ FMC_Bank5_6->SDCMR = 0x00000073; timeout = 0xFFFF; while((tmpreg!= 0) & (timeout-- > 0)) { tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } /*MRD register program */ FMC_Bank5_6->SDCMR = 0x00046014; timeout = 0xFFFF; while((tmpreg != 0) & (timeout-- > 0)) { tmpreg = FMC_Bank5_6->SDSR & 0x00000020; } /*Set refresh count */ tmpreg = FMC_Bank5_6->SDRTR; FMC_Bank5_6->SDRTR = (tmpreg | (0x0000027C<<1)); /*Disable write protection */ tmpreg = FMC_Bank5_6->SDCR[0]; FMC_Bank5_6->SDCR[0] = (tmpreg & 0xFFFFFDFF); }
|