|
SCLK PA5
DIN PA7
DOUT PA6
DRDY PA2
CS PA3
RESET PA4
電源接5v
數(shù)字地和模擬地可以用光耦或者0歐姆電阻隔離開(kāi)
單片機(jī)源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "adc.h"
- #include "ads1256.h"
-
- #define MEDIAN_LEN 5 //中直濾波的總長(zhǎng)度,一般選取奇數(shù)
- #define MEDIAN 2 //中值在濾波數(shù)組中的位置
- unsigned long AD_buffer[MEDIAN_LEN]; //ad采集數(shù)組緩存
- //unsigned long MED_buffer[MEDIAN_LEN]; //中值濾波數(shù)組緩存
- unsigned char medleng = 0; //存入緩存的數(shù)據(jù)個(gè)數(shù)
- unsigned long medina_filter(unsigned long *MED_buffer);
-
-
- int main(void)
- {
- delay_init(); //延時(shí)函數(shù)初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置中斷優(yōu)先級(jí)分組為組2:2位搶占優(yōu)先級(jí),2位響應(yīng)優(yōu)先級(jí)
- uart_init(115200); //串口初始化為115200
- LED_Init(); //LED端口初始化
-
-
- SPI_ADS1256_Init(); //SPI初始化
- ADS1256_GPIO_init(); //端口初始化
- ADS1256_Init();
- while(1)
- {
- /*
- ADS_sum(a,b):函數(shù)功能,測(cè)量電壓a-b
- 解說(shuō):ADS1256_MUXP_AIN(0~7)代表通道0~7,差分正 P:positive
- ADS1256_MUXN_AIN(0~7)代表通道0~7,差分負(fù) N:negative
-
- 例:差分測(cè)量通道0和1:ADS_sum(ADS1256_MUXP_AIN0 | ADS1256_MUXN_AIN1); 返回通道0-通道1的電壓值
- 單端測(cè)量通道0: ADS_sum(ADS1256_MUXP_AIN0 | ADS1256_MUXN_AINCOM); 返回通道0對(duì)地的電壓值
- */
- ADS_sum(ADS1256_MUXP_AIN0 | ADS1256_MUXN_AINCOM); //通道0對(duì)地的電壓值
- // ADS_sum(ADS1256_MUXP_AIN1 | ADS1256_MUXN_AINCOM); //通道1對(duì)地的電壓值
- // sendChar(0x0d); sendChar(0x0a);
- // ADS_sum(ADS1256_MUXP_AIN2 | ADS1256_MUXN_AINCOM); //通道2對(duì)地的電壓值
- // sendChar(0x0d); sendChar(0x0a);
- // ADS_sum(ADS1256_MUXP_AIN3 | ADS1256_MUXN_AINCOM); //通道3對(duì)地的電壓值
- // sendChar(0x0d); sendChar(0x0a);
- // ADS_sum(ADS1256_MUXP_AIN4 | ADS1256_MUXN_AINCOM); //通道4對(duì)地的電壓值
- // sendChar(0x0d); sendChar(0x0a);
- // ADS_sum(ADS1256_MUXP_AIN5 | ADS1256_MUXN_AINCOM); //通道5對(duì)地的電壓值
- // sendChar(0x0d); sendChar(0x0a);
- // ADS_sum(ADS1256_MUXP_AIN6 | ADS1256_MUXN_AINCOM); //通道6對(duì)地的電壓值
- // sendChar(0x0d); sendChar(0x0a);
- // ADS_sum(ADS1256_MUXP_AIN7 | ADS1256_MUXN_AINCOM); //通道7對(duì)地的電壓值
- //
- delay_ms(500);
- }
- }
- /**********************************************************/
- // 函數(shù)名:中值濾波函數(shù)
- // 描述 :提取前9次采集的數(shù)據(jù),去掉高3個(gè),去掉低3個(gè),然后中間的
- // 描述 :3個(gè)數(shù)據(jù)求平均值,該算法可盡可能的濾掉干擾數(shù)據(jù),并不影響采集速度。
- // 輸入 :9個(gè)數(shù)據(jù)的數(shù)組
- // 輸出 :中間3個(gè)數(shù)據(jù)的平均值
- /*********************************************************/
- unsigned long medina_filter(unsigned long *MED_buffer) //xad - ADC轉(zhuǎn)換值
- {
-
- unsigned char i,j;
- unsigned long xd;
- u32 xxd;
-
- for(i = 0; i < MEDIAN_LEN; i ++)
- {
- for(j = 0; j < MEDIAN_LEN - i; j ++)
- {
-
- if( MED_buffer[i] > MED_buffer[i + 1]) // 輪詢到的當(dāng)前元素>AD值,則交換它們的值
- { xd = MED_buffer[i]; MED_buffer[i] = MED_buffer[i + 1]; MED_buffer[i + 1] = xd;}
- }
- }
- xxd = MED_buffer[MEDIAN - 1] + MED_buffer[MEDIAN] + MED_buffer[MEDIAN + 1];
- xd = xxd/3;
- return xd; //中值
- }
- /******************* (C) COPYRIGHT 2014 三峰電子開(kāi)發(fā)工作室 *****END OF FILE****/
復(fù)制代碼- #include "ads1256.h"
- #include "delay.h"
- #include "usart.h"
- u8 results1,results2,results3;
- /*******************************************************************************
- * Function Name : SPI_FLASH_Init
- * Description : Initializes the peripherals used by the SPI FLASH driver.
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void SPI_ADS1256_Init(void)
- {
- SPI_InitTypeDef SPI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Enable SPI1 and GPIO clocks */
- /*!< SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO,
- SPI_FLASH_SPI_MISO_GPIO, SPI_FLASH_SPI_DETECT_GPIO
- and SPI_FLASH_SPI_SCK_GPIO Periph clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE);
- /*!< SPI_FLASH_SPI Periph clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
- /*!< AFIO Periph clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
-
-
- /*!< Configure SPI_FLASH_SPI pins: RESET */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推免輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /*!< Configure SPI_FLASH_SPI pins:SCK | DIN */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7; //
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 復(fù)用推免輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /*!< Configure SPI_FLASH_SPI pins: DOUT */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Deselect the FLASH: Chip Select high */
- /* SPI1 configuration */
- // 在SCLK下降沿,系統(tǒng)通過(guò)DIN向1256發(fā)送數(shù)據(jù)
- // 在SCLK上升沿,系統(tǒng)通過(guò)DOUT 讀取1256數(shù)據(jù)
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //兩線全雙工
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主機(jī)模式
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8位模式
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //SCLK空閑時(shí)位低電平
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //SCLK的下降沿采集數(shù)據(jù)
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //從機(jī)選擇信號(hào),軟件模式,就是用GPIO選擇從機(jī)
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //APB2或者APB1總線頻率的1/2~1/256分頻
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //數(shù)據(jù)傳輸從MSB位開(kāi)始
- SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值計(jì)算的多項(xiàng)式
- SPI_Init(SPI1, &SPI_InitStructure);
- /* Enable SPI1 */
- SPI_Cmd(SPI1, ENABLE);
- }
- /*******************************************************************************
- * Function Name : SPI_FLASH_SendByte
- * Description : Sends a byte through the SPI interface and return the byte
- * received from the SPI bus.
- * Input : byte : byte to send.
- * Output : None
- * Return : The value of the received byte.
- *******************************************************************************/
- u8 SPI_SendByte(u8 byte)
- {
- /* Loop while DR register in not emplty */
- while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
- /* Send byte through the SPI1 peripheral */
- SPI_I2S_SendData(SPI1, byte);
- /* Wait to receive a byte */
- while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
- /* Return the byte read from the SPI bus */
- return SPI_I2S_ReceiveData(SPI1);
- }
- /*******************************************************************
- 函數(shù)名:初始化DAC接口
- 函數(shù)原型:void ADC_gpio_init()
- 參數(shù)說(shuō)明:無(wú)
- 返回: 無(wú)
- ********************************************************************/
- void ADS1256_GPIO_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /////////CS reset 引腳設(shè)置
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_4 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP; //--推免輸出模式
- GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz; //--GPIO翻轉(zhuǎn)速度為50MHz
- GPIO_Init(GPIOA , &GPIO_InitStructure); //--將配置寫(xiě)入對(duì)應(yīng)的GPIO寄存器中
- // GPIO_InitTypeDef GPIO_InitStructure;
- ///////// DRDY引腳設(shè)置
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Pin= GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING; //--浮點(diǎn)輸入模式
- GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz; //--GPIO翻轉(zhuǎn)速度為50MHz
- GPIO_Init(GPIOA , &GPIO_InitStructure); //--將配置寫(xiě)入對(duì)應(yīng)的GPIO寄存器中
- }
- //-----------------------------------------------------------------//
- // 功 能:ADS1256 寫(xiě)數(shù)據(jù)
- // 入口參數(shù): /
- // 出口參數(shù): /
- // 全局變量: /
- // 備 注: 向ADS1256中地址為regaddr的寄存器寫(xiě)入一個(gè)字節(jié)databyte
- //-----------------------------------------------------------------//
- void ADS1256WREG(unsigned char regaddr,unsigned char databyte)
- {
- ADS_CS_LOW();
- while(ADS_DRDY);//當(dāng)ADS1256_DRDY為低時(shí)才能寫(xiě)寄存器
- //向寄存器寫(xiě)入數(shù)據(jù)地址
- SPI_SendByte(ADS1256_CMD_WREG | (regaddr & 0xF));
- //寫(xiě)入數(shù)據(jù)的個(gè)數(shù)n-1
- SPI_SendByte(0);
- //向regaddr地址指向的寄存器寫(xiě)入數(shù)據(jù)databyte
- delay_us(5);
- SPI_SendByte(databyte);
- ADS_CS_HIGH();
- }
- //-----------------------------------------------------------------//
- // 功 能:ADS1256 讀寄存器數(shù)據(jù)
- // 入口參數(shù): /
- // 出口參數(shù): /
- // 全局變量: /
- // 備 注: 從ADS1256中地址為regaddr的寄存器讀出一個(gè)字節(jié)databyte
- //-----------------------------------------------------------------//
- unsigned char ADS1256RREG(unsigned char regaddr)
- {
- //從ADS1256中地址為regaddr的寄存器讀出一個(gè)字節(jié)
- unsigned char r=0;
- ADS_CS_LOW();
- while(ADS_DRDY);//當(dāng)ADS1256_DRDY為低時(shí)才能寫(xiě)寄存器
- //寫(xiě)入地址
- SPI_SendByte(ADS1256_CMD_RREG | (regaddr & 0xF));
- //寫(xiě)入讀取數(shù)據(jù)的個(gè)數(shù)n-1
- SPI_SendByte(0);
- delay_us(10); //min=50*(1/fCLKIN)=50*(1/7.68MHZ)=6500ns;max=whatever
- //讀出regaddr地址指向的寄存器的數(shù)據(jù)
- r=SPI_SendByte(0);
- ADS_CS_HIGH();
- return r;//返回?cái)?shù)據(jù)
- }
- //-----------------------------------------------------------------//
- // 功 能:ADS1256初始化子程序
- // 入口參數(shù): /
- // 出口參數(shù): /
- // 全局變量: /
- // 備 注: /
- //-----------------------------------------------------------------//
- void ADS1256_Init(void)
- {
- ADS_CS_LOW();
- RESET_HIGH();
- delay_us(100);
- SPI_SendByte(ADS1256_CMD_REST);
- delay_ms(10);
- while(ADS_DRDY); //當(dāng)ADS1256_DRDY為低時(shí)才能寫(xiě)寄存器
- SPI_SendByte(ADS1256_CMD_SYNC); //同步命令
- SPI_SendByte(ADS1256_CMD_WAKEUP); //同步喚醒
-
- while(ADS_DRDY);
- SPI_SendByte(ADS1256_CMD_WREG | ADS1256_STATUS);//連續(xù)寫(xiě)入4個(gè)寄存器
- SPI_SendByte(3);
- SPI_SendByte(0x04); //高位在前,使用內(nèi)部校準(zhǔn),不使用緩存
- SPI_SendByte(ADS1256_MUXP_AIN2 | ADS1256_MUXN_AIN3);//端口輸入A2為正,A3位負(fù)
- SPI_SendByte(ADS1256_GAIN_1); //放大倍數(shù)設(shè)置
- SPI_SendByte(ADS1256_DRATE_2_5SPS); //采集速度設(shè)置
- delay_us(100);
-
- while(ADS_DRDY); //當(dāng)ADS1256_DRDY為低時(shí)才能寫(xiě)寄存器
- SPI_SendByte(ADS1256_CMD_SELFCAL); //偏移和增益自動(dòng)校準(zhǔn)
-
- ADS_CS_HIGH();
- delay_ms(100);
-
- }
- //-----------------------------------------------------------------//
- // 功 能:
- // 入口參數(shù): /
- // 出口參數(shù): /
- // 全局變量: /
- // 備 注:
- //-----------------------------------------------------------------//
- unsigned long ADS1256ReadData(void)
- {
- unsigned char i=0;
- unsigned long sum=0;
- unsigned long r=0;
- ADS_CS_LOW();
- while(ADS_DRDY); //當(dāng)ADS1256_DRDY為低時(shí)才能寫(xiě)寄存器
- SPI_SendByte(ADS1256_CMD_SYNC);
- SPI_SendByte(ADS1256_CMD_WAKEUP);
- SPI_SendByte(ADS1256_CMD_RDATA);
- delay_us(10); //min=50*(1/fCLKIN)=50*(1/7.68MHZ)=6500ns;max=whatever
- for(i=0;i<3;i++)
- {
- sum = sum << 8;
- r = SPI_SendByte(0);
- sum |= r;
- }
- ADS_CS_HIGH();
- return sum;
- }
- //-----------------------------------------------------------------//
- // 功 能:讀取ADS1256單路數(shù)據(jù)
- // 入口參數(shù): /
- // 出口參數(shù): /
- // 全局變量: /
- // 備 注: /
- //-----------------------------------------------------------------//
- void sendChar(unsigned char ch)
- {
- USART_SendData(USART1, (unsigned char) ch);
- while (!(USART1->SR & USART_FLAG_TXE));
- }
- unsigned char i=0;
- long ulResult;
- long double ldVolutage;
- unsigned long ADS_sum(unsigned char road)
- {
- unsigned long results=0;
- unsigned long Result_sum=0;
- unsigned long fVoltage;
- unsigned char buf[10],i,rod;
- ADS1256WREG(ADS1256_MUX,road); //設(shè)置通道
- SPI_SendByte(ADS1256_CMD_SELFCAL); //偏移和增益自動(dòng)校準(zhǔn)
-
- rod = (road >> 4) ;
- // printf("第");
- // sendChar(rod);
- // printf("路:");
- Result_sum = ADS1256ReadData();//讀取AD值,返回24位數(shù)據(jù)。
- ulResult= ADS1256ReadData();//讀取AD值,返回24位數(shù)據(jù)。
- // return results;
- if(Result_sum<0x800000) //測(cè)得電壓值為正的情況
- {
- // fVoltage=Result_sum*985; //954=2*2.5/2^19*100000000 2.5為基準(zhǔn)電壓 100000000為為了計(jì)算的準(zhǔn)確性轉(zhuǎn)換為整形計(jì)算。理論為954,實(shí)際修正為985
- // buf[0]=(unsigned char)(fVoltage/100000000);
- // buf[0]=buf[0]+0x30;
- // buf[1]='.';
- // buf[2]=(unsigned char)((fVoltage%100000000)/10000000);
- // buf[2]=buf[2]+0x30;
- // buf[3]=(unsigned char)((fVoltage%10000000)/1000000);
- // buf[3]=buf[3]+0x30;
- // buf[4]=(unsigned char)(fVoltage%1000000/100000);
- // buf[4]=buf[4]+0x30;
- // buf[5]=(unsigned char)(fVoltage%100000/10000);
- // buf[5]=buf[5]+0x30;
- // buf[6]=(unsigned char)(fVoltage%10000/1000);
- // buf[6]=buf[6]+0x30;
- // buf[7]=(unsigned char)(fVoltage%1000/100);
- // buf[7]=buf[7]+0x30;
- // buf[8]='V'; //V
- // buf[9]=0x20;
-
- if( ulResult & 0x800000 )
- {
- ulResult = ~(unsigned long)ulResult;
- ulResult &= 0x7fffff;
- ulResult += 1;
- ulResult = -ulResult;
- }
-
- ldVolutage = (long double)ulResult*0.59604644775390625;
- printf("第%d通道:",rod);
- printf("%lf",ldVolutage); //double
- printf("uV\r\n");
- }
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
代碼可能有錯(cuò)誤,請(qǐng)各位大神指導(dǎo):
24位ADS1256.7z
(291.4 KB, 下載次數(shù): 200)
2019-7-20 03:17 上傳
點(diǎn)擊文件名下載附件
|
評(píng)分
-
查看全部評(píng)分
|