因為項目需要所以把AD7705程序移植到野火stm32挑戰者開發板上,需要的朋友可以下載
0.png (7.95 KB, 下載次數: 40)
下載附件
2019-7-17 18:59 上傳
單片機源程序如下:
- /*
- *********************************************************************************************************
- 需要注意的是模塊使用3.3V供電較穩定,使用5V供電可能回出問題,讀出來的數據有時候是錯的
- 還有就是AIN1-和AIN2-引腳的電壓最高為1V,AIN1+,AIN2+的最高電壓為2.5V,詳情請見數據手冊說明,
- 用來檢測開發板3.3V和5V的的電壓也是可以的,一般不會出現問題。
- 本例程是單極性輸入,增益為1,帶輸入緩沖。帶緩沖可以解決ADC采樣端,由于未進行電壓跟隨而造成的
- ADC檢測錯誤。
- 測試條件:串接3個1K的電阻和一個10K的電阻進行采樣,采樣的結果和萬用表測量的結果基本一致
- AD7705的供電電壓盡量 不要為5V這點很重要!!!!! 不要為5V這點很重要!!!!!
- 不要為5V這點很重要!!!!!不要為5V這點很重要!!!!!不要為5V這點很重要!!!!!
- *********************************************************************************************************
- */
- #include "AD7705.h"
- #include "systick.h"
- #define SOFT_SPI /* 定義此行表示使用GPIO模擬SPI接口 */
- //#define HARD_SPI /* 定義此行表示使用CPU的硬件SPI接口 */
- /* 通道1和通道2的增益,輸入緩沖,單極性 */
- #define __CH1_GAIN_BIPOLAR_BUF (GAIN_1 | UNIPOLAR | BUF_EN)
- #define __CH2_GAIN_BIPOLAR_BUF (GAIN_1 | UNIPOLAR | BUF_EN)
- /*
- TM7705模塊 野火STM32挑戰者 STM32F429開發板
- SCK ------ PE2
- DOUT ------ PE4
- DIN ------ PE3
- CS ------ PE5
- DRDY ------ PE6
- RST ------ PI8
- */
- #if !defined(SOFT_SPI) && !defined(HARD_SPI)
- #error "Please define SPI Interface mode : SOFT_SPI or HARD_SPI"
- #endif
- #ifdef SOFT_SPI /* 軟件SPI */
- /* 定義GPIO端口 */
- #define RCC_SCK RCC_AHB1Periph_GPIOE
- #define PORT_SCK GPIOE
- #define PIN_SCK GPIO_Pin_2
- #define RCC_DIN RCC_AHB1Periph_GPIOE
- #define PORT_DIN GPIOE
- #define PIN_DIN GPIO_Pin_3
- #define RCC_DOUT RCC_AHB1Periph_GPIOE
- #define PORT_DOUT GPIOE
- #define PIN_DOUT GPIO_Pin_4
- #define RCC_CS RCC_AHB1Periph_GPIOE
- #define PORT_CS GPIOE
- #define PIN_CS GPIO_Pin_5
- #define RCC_DRDY RCC_AHB1Periph_GPIOE
- #define PORT_DRDY GPIOE
- #define PIN_DRDY GPIO_Pin_6
- #define RCC_RESET RCC_AHB1Periph_GPIOI
- #define PORT_RESET GPIOI
- #define PIN_RESET GPIO_Pin_8
- /* 定義口線置0和置1的宏 */
- #define RESET_0() GPIO_ResetBits(PORT_RESET, PIN_RESET)
- #define RESET_1() GPIO_SetBits(PORT_RESET, PIN_RESET)
- #define CS_0() GPIO_ResetBits(PORT_CS, PIN_CS)
- #define CS_1() GPIO_SetBits(PORT_CS, PIN_CS)
- #define SCK_0() GPIO_ResetBits(PORT_SCK, PIN_SCK)
- #define SCK_1() GPIO_SetBits(PORT_SCK, PIN_SCK)
- #define DI_0() GPIO_ResetBits(PORT_DIN, PIN_DIN)
- #define DI_1() GPIO_SetBits(PORT_DIN, PIN_DIN)
- #define DO_IS_HIGH() (GPIO_ReadInputDataBit(PORT_DOUT, PIN_DOUT) == Bit_SET)
- #define DRDY_IS_LOW() (GPIO_ReadInputDataBit(PORT_DRDY, PIN_DRDY) == Bit_RESET)
- #endif
- #ifdef HARD_SPI /* 硬件SPI */
- ;
- #endif
- /* 通信寄存器bit定義 */
- enum
- {
- /* 寄存器選擇 RS2 RS1 RS0 */
- REG_COMM = 0x00, /* 通信寄存器 */
- REG_SETUP = 0x10, /* 設置寄存器 */
- REG_CLOCK = 0x20, /* 時鐘寄存器 */
- REG_DATA = 0x30, /* 數據寄存器 */
- REG_ZERO_CH1 = 0x60, /* CH1 偏移寄存器 */
- REG_FULL_CH1 = 0x70, /* CH1 滿量程寄存器 */
- REG_ZERO_CH2 = 0x61, /* CH2 偏移寄存器 */
- REG_FULL_CH2 = 0x71, /* CH2 滿量程寄存器 */
- /* 讀寫操作 */
- WRITE = 0x00, /* 寫操作 */
- READ = 0x08, /* 讀操作 */
- /* 通道 */
- CH_1 = 0, /* AIN1+ AIN1- */
- CH_2 = 1, /* AIN2+ AIN2- */
- CH_3 = 2, /* AIN1- AIN1- */
- CH_4 = 3 /* AIN1- AIN2- */
- };
- /* 設置寄存器bit定義 */
- enum
- {
- MD_NORMAL = (0 << 6), /* 正常模式 */
- MD_CAL_SELF = (1 << 6), /* 自校準模式 */
- MD_CAL_ZERO = (2 << 6), /* 校準0刻度模式 */
- MD_CAL_FULL = (3 << 6), /* 校準滿刻度模式 */
- GAIN_1 = (0 << 3), /* 增益 */
- GAIN_2 = (1 << 3), /* 增益 */
- GAIN_4 = (2 << 3), /* 增益 */
- GAIN_8 = (3 << 3), /* 增益 */
- GAIN_16 = (4 << 3), /* 增益 */
- GAIN_32 = (5 << 3), /* 增益 */
- GAIN_64 = (6 << 3), /* 增益 */
- GAIN_128 = (7 << 3), /* 增益 */
- /* 無論雙極性還是單極性都不改變任何輸入信號的狀態,它只改變輸出數據的代碼和轉換函數上的校準點 */
- BIPOLAR = (0 << 2), /* 雙極性輸入 */
- UNIPOLAR = (1 << 2), /* 單極性輸入 */
- BUF_NO = (0 << 1), /* 輸入無緩沖(內部緩沖器不啟用) */
- BUF_EN = (1 << 1), /* 輸入有緩沖 (啟用內部緩沖器) */
- FSYNC_0 = 0,
- FSYNC_1 = 1 /* 不啟用 */
- };
- /* 時鐘寄存器bit定義 */
- enum
- {
- CLKDIS_0 = 0x00, /* 時鐘輸出使能 (當外接晶振時,必須使能才能振蕩) */
- CLKDIS_1 = 0x10, /* 時鐘禁止 (當外部提供時鐘時,設置該位可以禁止MCK_OUT引腳輸出時鐘以省電 */
- /*
- 2.4576MHz(CLKDIV=0 )或為 4.9152MHz (CLKDIV=1 ),CLK 應置 “0”。
- 1MHz (CLKDIV=0 )或 2MHz (CLKDIV=1 ),CLK 該位應置 “1”
- */
- CLK_4_9152M = 0x08,
- CLK_2_4576M = 0x00,
- CLK_1M = 0x04,
- CLK_2M = 0x0C,
- FS_50HZ = 0x00,
- FS_60HZ = 0x01,
- FS_250HZ = 0x02,
- FS_500HZ = 0x04,
- // FS_50HZ = 0x04,
- // FS_60HZ = 0x05,
- // FS_250HZ = 0x06,
- // FS_500HZ = 0x07,
- /*
- 四十九、電子秤應用中提高TM7705 精度的方法
- 當使用主時鐘為 2.4576MHz 時,強烈建議將時鐘寄存器設為 84H,此時數據輸出更新率為10Hz,即每0.1S 輸出一個新數據。
- 當使用主時鐘為 1MHz 時,強烈建議將時鐘寄存器設為80H, 此時數據輸出更新率為4Hz, 即每0.25S 輸出一個新數據
- */
- ZERO_0 = 0x00,
- ZERO_1 = 0x80
- };
- static void TM7705_SyncSPI(void);
- static void TM7705_Send8Bit(uint8_t _data);
- static uint8_t TM7705_Recive8Bit(void);
- static void TM7705_WriteByte(uint8_t _data);
- static void TM7705_Write3Byte(uint32_t _data);
- static uint8_t TM7705_ReadByte(void);
- static uint16_t TM7705_Read2Byte(void);
- static uint32_t TM7705_Read3Byte(void);
- static void TM7705_WaitDRDY(void);
- void TM7705_ResetHard(void);
- static void TM7705_Delay(void);
- /*
- *********************************************************************************************************
- * 函 數 名: bsp_DelayMS
- * 功能說明: ms級延遲,延遲精度為正負1ms
- * 形 參: n : 延遲長度,單位1 ms。 n 應大于2
- * 返 回 值: 無
- * 這2個全局變量轉用于 bsp_DelayMS() 函數
- */
- extern volatile uint32_t s_uiDelayCount;
- extern volatile uint8_t s_ucTimeOutFlag;
- /* 開關全局中斷的宏 */
- #define ENABLE_INT() __set_PRIMASK(0) /* 使能全局中斷 core_cmFunc.h 的頭文件中*/
- #define DISABLE_INT() __set_PRIMASK(1) /* 禁止全局中斷 */
- void bsp_DelayMS(uint32_t n)
- {
- if (n == 0)
- {
- return;
- }
- else if (n == 1)
- {
- n = 2;
- }
-
- DISABLE_INT(); /* 關中斷 */
-
- s_uiDelayCount = n;
- s_ucTimeOutFlag = 0;
-
- ENABLE_INT(); /* 開中斷 */
-
- while (1)
- {
- /*
- 等待延遲時間到
- 注意:編譯器認為 s_ucTimeOutFlag = 0,所以可能優化錯誤,因此 s_ucTimeOutFlag 變量必須申明為 volatile
- */
- if (s_ucTimeOutFlag == 1)
- {
- break;
- }
- }
- }
- /*
- *********************************************************************************************************
- * 函 數 名: bsp_InitTM7705
- * 功能說明: 配置STM32的GPIO和SPI接口,用于連接 TM7705
- * 形 參: 無
- * 返 回 值: 無
- *********************************************************************************************************
- */
- void bsp_InitTM7705(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- #ifdef SOFT_SPI /* 軟件SPI */
- CS_1();
- SCK_1();
- DI_1();
-
- /* 打開GPIO時鐘 */
- RCC_AHB1PeriphClockCmd(RCC_SCK | RCC_DIN | RCC_DOUT | RCC_CS | RCC_DRDY | RCC_RESET, ENABLE);
-
- /* 配置幾個推完輸出IO */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; /* 設為輸出口 */
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /* 設為推挽模式 */
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; /* 上下拉電阻不使能 */
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; /* IO口最大速度 */
- GPIO_InitStructure.GPIO_Pin = PIN_SCK;
- GPIO_Init(PORT_SCK, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = PIN_DIN;
- GPIO_Init(PORT_DIN, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = PIN_CS;
- GPIO_Init(PORT_CS, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = PIN_RESET;
- GPIO_Init(PORT_RESET, &GPIO_InitStructure);
- /* 配置GPIO為浮動輸入模式(實際上CPU復位后就是輸入狀態) */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; /* 設為輸入口 */
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /* 設為推挽模式 */
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; /* 無需上下拉電阻 */
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* IO口最大速度 */
- GPIO_InitStructure.GPIO_Pin = PIN_DOUT;
- GPIO_Init(PORT_DOUT, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = PIN_DRDY;
- GPIO_Init(PORT_DRDY, &GPIO_InitStructure);
-
- #endif
- bsp_DelayMS(10);
-
- TM7705_ResetHard(); /* 硬件復位 */
-
- /*
- 在接口序列丟失的情況下,如果在DIN 高電平的寫操作持續了足夠長的時間(至少 32個串行時鐘周期),
- TM7705 將會回到默認狀態。
- */
- bsp_DelayMS(5);
- TM7705_SyncSPI(); /* 同步SPI接口時序 */
- bsp_DelayMS(5);
- /* 配置時鐘寄存器 通道1*/
- TM7705_WriteByte(REG_CLOCK | WRITE | CH_1); /* 先寫通信寄存器,下一步是寫時鐘寄存器 */
- TM7705_WriteByte(CLKDIS_0 | CLK_4_9152M | FS_50HZ); /* 刷新速率50Hz */
- // TM7705_WriteByte(CLKDIS_0 | CLK_4_9152M | FS_500HZ); /* 刷新速率500Hz */
- /* 配置時鐘寄存器 通道2*/
- TM7705_WriteByte(REG_CLOCK | WRITE | CH_2); /* 先寫通信寄存器,下一步是寫時鐘寄存器 */
- TM7705_WriteByte(CLKDIS_0 | CLK_4_9152M | FS_500HZ); /* 刷新速率50Hz */
-
- /* 每次上電進行一次自校準 通道2 */
- TM7705_CalibSelf(1); /* 內部自校準 CH1 */
- bsp_DelayMS(5);
-
- /* 每次上電進行一次自校準 */
- TM7705_CalibSelf(2); /* 內部自校準 CH2 */
- bsp_DelayMS(5);
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_Delay
- * 功能說明: CLK之間的延遲,時序延遲. 用于STM32F407 168M主頻
- * 形 參: 無
- * 返 回 值: 無
- *********************************************************************************************************
- */
- static void TM7705_Delay(void)
- {
- uint16_t i;
- for (i = 0; i < 5; i++);
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_ResetHard
- * 功能說明: 硬件復位 TM7705芯片
- * 形 參: 無
- * 返 回 值: 無
- *********************************************************************************************************
- */
- void TM7705_ResetHard(void)
- {
- RESET_1();
- // bsp_DelayMS(1); ///增大延時時間,從407 移植到429上
- bsp_DelayMS(5);
- RESET_0();
- // bsp_DelayMS(2);
- bsp_DelayMS(10);
- RESET_1();
- // bsp_DelayMS(1);
- bsp_DelayMS(5);
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_SyncSPI
- * 功能說明: 同步TM7705芯片SPI接口時序
- * 形 參: 無
- * 返 回 值: 無
- *********************************************************************************************************
- */
- static void TM7705_SyncSPI(void)
- {
- /* AD7705串行接口失步后將其復位。復位后要延時500us再訪問 */
- CS_0();
- TM7705_Send8Bit(0xFF);
- TM7705_Send8Bit(0xFF);
- TM7705_Send8Bit(0xFF);
- TM7705_Send8Bit(0xFF);
- CS_1();
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_Send8Bit
- * 功能說明: 向SPI總線發送8個bit數據。 不帶CS控制。
- * 形 參: _data : 數據
- * 返 回 值: 無
- *********************************************************************************************************
- */
- static void TM7705_Send8Bit(uint8_t _data)
- {
- uint8_t i;
- for(i = 0; i < 8; i++)
- {
- if (_data & 0x80)
- {
- DI_1();
- }
- else
- {
- DI_0();
- }
- SCK_0();
- _data <<= 1;
- TM7705_Delay();
- SCK_1();
- TM7705_Delay();
- }
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_Recive8Bit
- * 功能說明: 從SPI總線接收8個bit數據。 不帶CS控制。
- * 形 參: 無
- * 返 回 值: 無
- *********************************************************************************************************
- */
- static uint8_t TM7705_Recive8Bit(void)
- {
- uint8_t i;
- uint8_t read = 0;
- for (i = 0; i < 8; i++)
- {
- SCK_0();
- TM7705_Delay();
- read = read<<1;
- if (DO_IS_HIGH())
- {
- read++;
- }
- SCK_1();
- TM7705_Delay();
- }
- return read;
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_WriteByte
- * 功能說明: 寫入1個字節。帶CS控制
- * 形 參: _data :將要寫入的數據
- * 返 回 值: 無
- *********************************************************************************************************
- */
- void TM7705_WriteByte(uint8_t _data)
- {
- CS_0();
- TM7705_Send8Bit(_data);
- CS_1();
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_Write3Byte
- * 功能說明: 寫入3個字節。帶CS控制
- * 形 參: _data :將要寫入的數據
- * 返 回 值: 無
- *********************************************************************************************************
- */
- static void TM7705_Write3Byte(uint32_t _data)
- {
- CS_0();
- TM7705_Send8Bit((_data >> 16) & 0xFF);
- TM7705_Send8Bit((_data >> 8) & 0xFF);
- TM7705_Send8Bit(_data);
- CS_1();
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_ReadByte
- * 功能說明: 從AD芯片讀取一個字(16位)
- * 形 參: 無
- * 返 回 值: 讀取的字(16位)
- *********************************************************************************************************
- */
- static uint8_t TM7705_ReadByte(void)
- {
- uint8_t read;
- CS_0();
- read = TM7705_Recive8Bit();
- CS_1();
-
- return read;
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_Read2Byte
- * 功能說明: 讀2字節數據
- * 形 參: 無
- * 返 回 值: 讀取的數據(16位)
- *********************************************************************************************************
- */
- static uint16_t TM7705_Read2Byte(void)
- {
- uint16_t read;
- CS_0();
- read = TM7705_Recive8Bit();
- read <<= 8;
- read += TM7705_Recive8Bit();
- CS_1();
- return read;
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_Read3Byte
- * 功能說明: 讀3字節數據
- * 形 參: 無
- * 返 回 值: 讀取到的數據(24bit) 高8位固定為0.
- *********************************************************************************************************
- */
- static uint32_t TM7705_Read3Byte(void)
- {
- uint32_t read;
- CS_0();
- read = TM7705_Recive8Bit();
- read <<= 8;
- read += TM7705_Recive8Bit();
- read <<= 8;
- read += TM7705_Recive8Bit();
- CS_1();
- return read;
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_WaitDRDY
- * 功能說明: 等待內部操作完成。 自校準時間較長,需要等待。
- * 形 參: 無
- * 返 回 值: 無
- *********************************************************************************************************
- */
- static void TM7705_WaitDRDY(void)
- {
- uint32_t i;
- for (i = 0; i < 4000000; i++)
- {
- if (DRDY_IS_LOW())
- {
- break;
- }
- }
- if (i >= 4000000)
- {
- printf("TM7705_WaitDRDY() Time Out ...\r\n"); /* 調試語句. 用語排錯 */
- }
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_WriteReg
- * 功能說明: 寫指定的寄存器
- * 形 參: _RegID : 寄存器ID
- * _RegValue : 寄存器值。 對于8位的寄存器,取32位形參的低8bit
- * 返 回 值: 無
- *********************************************************************************************************
- */
- void TM7705_WriteReg(uint8_t _RegID, uint32_t _RegValue)
- {
- uint8_t bits;
- switch (_RegID)
- {
- case REG_COMM: /* 通信寄存器 */
- case REG_SETUP: /* 設置寄存器 8bit */
- case REG_CLOCK: /* 時鐘寄存器 8bit */
- bits = 8;
- break;
- case REG_ZERO_CH1: /* CH1 偏移寄存器 24bit */
- case REG_FULL_CH1: /* CH1 滿量程寄存器 24bit */
- case REG_ZERO_CH2: /* CH2 偏移寄存器 24bit */
- case REG_FULL_CH2: /* CH2 滿量程寄存器 24bit*/
- bits = 24;
- break;
- case REG_DATA: /* 數據寄存器 16bit */
- default:
- return;
- }
- TM7705_WriteByte(_RegID | WRITE); /* 寫通信寄存器, 指定下一步是寫操作,并指定寫哪個寄存器 */
- if (bits == 8)
- {
- TM7705_WriteByte((uint8_t)_RegValue);
- }
- else /* 24bit */
- {
- TM7705_Write3Byte(_RegValue);
- }
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_ReadReg
- * 功能說明: 讀指定的寄存器
- * 形 參: _RegID : 寄存器ID
- * _RegValue : 寄存器值。 對于8位的寄存器,取32位形參的低8bit
- * 返 回 值: 讀到的寄存器值。 對于8位的寄存器,取32位形參的低8bit
- *********************************************************************************************************
- */
- uint32_t TM7705_ReadReg(uint8_t _RegID)
- {
- uint8_t bits;
- uint32_t read;
- switch (_RegID)
- {
- case REG_COMM: /* 通信寄存器 */
- case REG_SETUP: /* 設置寄存器 8bit */
- case REG_CLOCK: /* 時鐘寄存器 8bit */
- bits = 8;
- break;
- case REG_ZERO_CH1: /* CH1 偏移寄存器 24bit */
- case REG_FULL_CH1: /* CH1 滿量程寄存器 24bit */
- case REG_ZERO_CH2: /* CH2 偏移寄存器 24bit */
- case REG_FULL_CH2: /* CH2 滿量程寄存器 24bit*/
- bits = 24;
- break;
- case REG_DATA: /* 數據寄存器 16bit */
- default:
- return 0xFFFFFFFF;
- }
- TM7705_WriteByte(_RegID | READ); /* 寫通信寄存器, 指定下一步是寫操作,并指定寫哪個寄存器 */
- if (bits == 16)
- {
- read = TM7705_Read2Byte();
- }
- else if (bits == 8)
- {
- read = TM7705_ReadByte();
- }
- else /* 24bit */
- {
- read = TM7705_Read3Byte();
- }
- return read;
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_CalibSelf
- * 功能說明: 啟動自校準. 內部自動短接AIN+ AIN-校準0位,內部短接到Vref 校準滿位。此函數執行過程較長,
- * 實測約 180ms
- * 形 參: _ch : ADC通道,1或2
- * 返 回 值: 無
- *********************************************************************************************************
- */
- void TM7705_CalibSelf(uint8_t _ch)
- {
- if (_ch == 1)
- {
- /* 自校準CH1 */
- TM7705_WriteByte(REG_SETUP | WRITE | CH_1); /* 寫通信寄存器,下一步是寫設置寄存器,通道1 */
- TM7705_WriteByte(MD_CAL_SELF | __CH1_GAIN_BIPOLAR_BUF | FSYNC_0);/* 啟動自校準 */
- TM7705_WaitDRDY(); /* 等待內部操作完成 --- 時間較長,約180ms */
- }
- else if (_ch == 2)
- {
- /* 自校準CH2 */
- TM7705_WriteByte(REG_SETUP | WRITE | CH_2); /* 寫通信寄存器,下一步是寫設置寄存器,通道2 */
- TM7705_WriteByte(MD_CAL_SELF | __CH2_GAIN_BIPOLAR_BUF | FSYNC_0); /* 啟動自校準 */
- TM7705_WaitDRDY(); /* 等待內部操作完成 --- 時間較長,約180ms */
- }
- }
- /*
- *********************************************************************************************************
- * 函 數 名: TM7705_SytemCalibZero
- * 功能說明: 啟動系統校準零位. 請將AIN+ AIN-短接后,執行該函數。校準應該由主程序控制并保存校準參數。
- * 執行完畢后。可以通過 TM7705_ReadReg(REG_ZERO_CH1) 和 TM7705_ReadReg(REG_ZERO_CH2) 讀取校準參數。
- * 形 參: _ch : ADC通道,1或2
- * 返 回 值: 無
- *********************************************************************************************************
- */
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼- #include "stm32f4xx.h"
- #include "uart.h"
- #include "systick.h"
- #include "AD7705.h"
- #include "time.h"
- uint16_t receive_bit = 0,ADC_Value = 0,ADC_succse = 0,ADC_Value1,mm = 0,nn = 0;
- #define ADC_Error_MAX 10000
- float adc1,adc2;
- /*
- 如果ADC一直讀不出來,每次讀取ADC之前重新設置一下AD7705時鐘寄存器和設置寄存器
- */
- /**
- * @brief 主函數
- * @param 無
- * @retval 無
- */
-
- /**
- * 推薦使用查詢發送,中斷接收的方法來實現串口功能
- *
- *////
- void GPIO_config()
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH,ENABLE);
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOH,&GPIO_InitStruct);
- GPIO_SetBits(GPIOH,GPIO_Pin_10);
- }
- void delay(unsigned int x)
- {
- while(x--);
- }
- /*
- 形參為ADC的通道號 采用限幅濾波算法
- */
- uint16_t GET_ADC(uint8_t x)
- {
- unsigned j;
- ADC_Value = TM7705_ReadAdc(x); //使用AD7705ADC通道1進行檢測。
- if(ADC_Value != 0&&mm == 0){ADC_Value1 = ADC_Value;mm = 1;} //第一次獲取ADC
- if((ADC_Value1 - ADC_Value) < ADC_Error_MAX||(ADC_Value - ADC_Value1) < ADC_Error_MAX)
- {
- j = 0;
- ADC_Value1 = ADC_Value; //數據符合要求
- return ADC_Value;
- }
- else //數據不符合要求返回上次的值
- {
- j++;
- if(j >= 15){j = 0;bsp_InitTM7705();} //連續15次讀取的數據不合要求 重新初始化ADC
- return ADC_Value1;
- }
- }
- int main(void)
- {
- int i;
- Uart_Config();
- GPIO_config();
- TIME_config();
- systick_Init();
- TM7705_ResetHard(); //上電先AD7705復位
- bsp_InitTM7705(); //AD7705初始化
- TM7705_CalibSelf(1); //校準ADC通道一
- ADC_Value = TM7705_ReadAdc(1);
- TM7705_CalibSelf(2); //校準ADC通道二
- ADC_Value = TM7705_ReadAdc(2);
- while(1)
- {
- i++;
- if(i >= 10)
- {
- i = 0;
- adc1 = GET_ADC(1);
- bsp_DelayMS(10); //讀完延時一下 不要一直讀取
- adc2 = GET_ADC(2);
- bsp_DelayMS(10); //讀完延時一下 不要一直讀取
- }
- if(ADC_succse == 1)
- {
- ADC_succse = 0;
- printf("通道1的電壓值 = %fV\n",(float)adc1*5/65535);
- bsp_DelayMS(20); //延時一下,防止發的過快亂碼
- printf("通道2的電壓值 = %fV\n",(float)adc2*5/65535);
- bsp_DelayMS(20);
- }
- if(ADC_Value != 0)GPIO_ResetBits(GPIOH,GPIO_Pin_10);
- }
- }
- /*********************************************END OF FILE**********************/
復制代碼
所有資料51hei提供下載:
stm32f429+16位ADC AD7705.7z
(364.58 KB, 下載次數: 45)
2019-7-17 19:00 上傳
點擊文件名下載附件
親測在野火stm32f429開發板上實驗成功 下載積分: 黑幣 -5
|