stm32系列--ads1256
while(1) { if( myusart.reflag>0) { Com_Handle(); myusart.recount=0; myusart.reflag=0; } ADS_sum_plus(i<<4|(i+1),j); //如何取三次的平均值呢,完成 if(++g>=10) //一个通道测算十次,四个通道 { g=0; adc_vel[j].min=ads.min; adc_vel[j].max=ads.max; //负电压的数据处理 if( adc_vel[j].aver & 0x800000 ) { adc_vel[j].aver = ~(unsigned long)adc_vel[j].aver; adc_vel[j].aver &= 0x7fffff; adc_vel[j].aver += 1; adc_vel[j].aver = -adc_vel[j].aver; } if( adc_vel[j].min & 0x800000 ) { adc_vel[j].min = ~(unsigned long)adc_vel[j].min; adc_vel[j].min &= 0x7fffff; adc_vel[j].min += 1; adc_vel[j].min = -adc_vel[j].min; } if( adc_vel[j].max & 0x800000 ) { adc_vel[j].max = ~(unsigned long)adc_vel[j].max; adc_vel[j].max &= 0x7fffff; adc_vel[j].max += 1; adc_vel[j].max = -adc_vel[j].max; } i+=2; j++; } if(j>=4) // 四个通道测试完毕,保存上次的结果;开始新一轮测算 { i=0; j=0; for(k=0;k<5;k++) //差分 { adc_temp[k].aver=adc_vel[k].aver; adc_temp[k].min=adc_vel[k].min; adc_temp[k].max=adc_vel[k].max; } } }
#include "ADS1256.h" #include "usart.h" #include "delay.h" #include "main.h" ///*************************** 宏定义***********************************************/ /******************************** 变量定义 ------------------------------------*/ void SPI2_GPIO_init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* Configure SPI2 pins: SCK(PB13),and MOSI(PB15) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); /*Configure PB.12(CS)--------------------------------------------*/ GPIO_InitStructure.GPIO_Pin =GPIO_Pin_12; //PB12--CS PIN GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //MISO GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//输入模式 GPIO_Init(GPIOB, &GPIO_InitStructure); } /******************************************************************************* * * * 发送字节程 * //在SCLK的下降沿,系统通过DIN向ADSl256发送数据; //而在SCLK的上升沿,系统则通过DOUT从ADSl256读取数 * *******************************************************************************/ #define US_NUM 10 #define US_TIMES 10 void spi_sendbyte(unsigned char va) { unsigned char i=8; SCLK_LOW(); while(i--) { if(va&0x80) MOSI_HIGH(); else MOSI_LOW(); delay_us(5); SCLK_HIGH(); delay_us(US_NUM); // delay_us(500); SCLK_LOW(); delay_us(US_NUM); va<<=1; } } /******************************************************************************* * * * 接收字节程序 * * *******************************************************************************/ unsigned char spi_recvbyte(void) { unsigned char data=0,i=8; SCLK_LOW(); while(i--) { data<<=1; SCLK_HIGH(); delay_us(US_NUM); SCLK_LOW(); delay_us(US_NUM); if(MISO_GET) data |=0x01; else data&=0xfe; } return data; } /******************************************************************************* * Function Name : GPIO_Configuration * Description : Configures the different GPIO ports. * Input : None * Output : None * Return : None *******************************************************************************/ void ADS1256_GPIO_init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10; //PB10--RST 2024-2-20 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //DRDY(PB11) 2024-2-20 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //输入模式 GPIO_Init(GPIOB, &GPIO_InitStructure); SPI2_GPIO_init(); } /*******************************************/ void ADS1256_int(void) { CS_HIGH(); SCLK_HIGH();//CS SCLK 初始化为高 SCLK_LOW(); delay_us(2); CS_LOW(); REST_HIGH();//reset while(NDRDY_GET); //NDRDY_GET,芯片正常工作,没有正在转换数据; spi_sendbyte(CMD_RESET);//发送指令 while(NDRDY_GET); ADS1256_Write_Reg(REG_STATUS,0x04); //STATUS REGISTER:Auto-Calibration Enabled,Analog Input Buffer Disabled while(NDRDY_GET); ADS1256_Write_Reg(REG_MUX,0x08); //AIN0 is Positive,single-ended measurements while(NDRDY_GET); ADS1256_Write_Reg(REG_ADCON,0x00); //ADCON=00h;// 放大倍数1 ADS1256_Write_Reg(REG_DRATE,0xd0); //data rate 15k SPS while(NDRDY_GET); spi_sendbyte(CMD_SELFCAL); delay_us(20); // USART2_SendStr("initOK"); } /******************************************************************************* * *读AD转换结果 *******************************************************************************/ unsigned long readad(void) { unsigned char result1; unsigned char result2; unsigned char result3; unsigned long result; CS_LOW(); while(NDRDY_GET==0); delay_us(10); spi_sendbyte(CMD_RDATA); while(NDRDY_GET); delay_us(10); //min=50*(1/fCLKIN)=50*(1/7.68MHZ)=6500ns(6.5us);max=whatever delay_us(100); result1 = spi_recvbyte(); result2 = spi_recvbyte(); result3 = spi_recvbyte(); result=result1*65536+result2*256+result3; return result; } unsigned long readad_nTimes(unsigned char nTimes) { unsigned long result = 0; unsigned char i=0; for(i=0;i<nTimes;i++) { result += readad(); } result = result/nTimes; return result; } unsigned long ADS_sum(unsigned char road) { ADS1256_Write_Reg(REG_MUX,road);//设置通道 // return readad(); return readad_nTimes(3); } ADC_RES ads; void readad_nTimes_new(unsigned char nTimes) { // unsigned long result[2] = {0}; unsigned long temp=0; unsigned long temp_min=0; unsigned long temp_max=0; unsigned char i=0; ads.aver=0; for(i=0;i<nTimes;i++) { //result[0]= readad(); temp = readad(); ads.aver += temp; if(i==0) { temp_min = temp; temp_max = temp; } if(temp<temp_min) { temp_min = temp; } if(temp>temp_max) { temp_max = temp; } } ads.aver = ads.aver/nTimes; ads.min = temp_min; ads.max = temp_max; } void readad_nTimes_plus(unsigned char nTimes,unsigned char road_num) { // unsigned long result[2] = {0}; unsigned long temp=0; unsigned long temp_min=0; unsigned long temp_max=0; unsigned char i=0; static unsigned char com_i=0; if(com_i==0) { ads.aver=0; } temp = readad(); ads.aver += temp; if(i==0) { temp_min = temp; temp_max = temp; } if(temp<temp_min) { temp_min = temp; } if(temp>temp_max) { temp_max = temp; } ads.min = temp_min; ads.max = temp_max; if(++com_i>=nTimes) { ads.aver = ads.aver/nTimes; adc_vel[road_num].aver=ads.aver; ads.aver=0; com_i=0; } } void ADS_sum_new(unsigned char road) { ADS1256_Write_Reg(REG_MUX,road);//设置通道 // return readad(); readad_nTimes_new(US_TIMES); } void ADS_sum_plus(unsigned char road,unsigned char road_num) { ADS1256_Write_Reg(REG_MUX,road);//设置通道 // return readad(); readad_nTimes_plus(US_TIMES,road_num); } /******************************************************************************* * * * 写寄存器 * * *******************************************************************************/ void ADS1256_Write_Reg(unsigned char reg_name, unsigned char reg_data) { while(NDRDY_GET); spi_sendbyte(CMD_WREG|reg_name); spi_sendbyte(0x00); spi_sendbyte(reg_data); } //#endif
#ifndef ADS1256_H #define ADS1256_H /*************************** 宏定义***********************************************/ #define NDRDY_GET GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) //PB11 #define CS_LOW() GPIO_ResetBits(GPIOB,GPIO_Pin_12) #define CS_HIGH() GPIO_SetBits(GPIOB,GPIO_Pin_12) //PB12 #define MOSI_HIGH() GPIO_SetBits(GPIOB,GPIO_Pin_15) #define MOSI_LOW() GPIO_ResetBits(GPIOB,GPIO_Pin_15) //PB15 #define SCLK_HIGH() GPIO_SetBits(GPIOB,GPIO_Pin_13) #define SCLK_LOW() GPIO_ResetBits(GPIOB,GPIO_Pin_13) //PB13 #define MISO_GET GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) //PB14 #define REST_HIGH() GPIO_SetBits(GPIOB,GPIO_Pin_10) #define REST_LOW() GPIO_ResetBits(GPIOB,GPIO_Pin_10) //PB10 #define ADCLK RCC_APB2Periph_GPIOA //定义AD芯片所使用的I/O端口的时钟。 /*Registers' Address*/ #define REG_STATUS 0 #define REG_MUX 1 #define REG_ADCON 2 #define REG_DRATE 3 #define REG_IO 4 #define REG_OFC0 5 #define REG_OFC1 6 #define REG_OPC2 7 #define REG_FSC0 8 #define REG_FSC1 9 #define REG_FSC2 10 /*Operation Command*/ //#define CMD_WAKEUP 0x00 #define CMD_RDATA 0x01 #define CMD_RDATAC 0x03 #define CMD_SDATAC 0x0F #define CMD_RREG 0x10 #define CMD_WREG 0x50 #define CMD_SELFCAL 0xf0 #define CMD_SELFOCAL 0xf1 #define CMD_SELFGCAL 0xf2 #define CMD_SYSOCAL 0xf3 #define CMD_SYSGCAL 0xf4 #define CMD_SYNC 0xfc #define CMD_STANDBY 0xfd #define CMD_RESET 0xfe #define CMD_WAKEUP 0xFF extern unsigned long result; typedef struct { unsigned long min; unsigned long aver; unsigned long max; }ADC_RES; extern ADC_RES ads; /*********************************声明函数 ------------------------------------*/ void ADS1256_GPIO_init(void); void NVIC_Configuration(void); void SPI2_Init(void); void ADS1256_int(void); unsigned long readad(void); unsigned long readad_nTimes(unsigned char nTimes); void ADS1256_Write_Reg(unsigned char reg_name, unsigned char reg_data); unsigned long ADS_sum(unsigned char road); void ADS_sum_new(unsigned char road); void spi_sendbyte(unsigned char va); unsigned char spi_recvbyte(void); void readad_nTimes_plus(unsigned char nTimes,unsigned char road_num); void ADS_sum_plus(unsigned char road,unsigned char road_num); #endif