#include "gpio.h"
#include "hx711.h"
#include "ddl.h"
#define HX711_GapValue 405
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
unsigned long Weight_Shiwu = 0;
void Get_Maopi()
{
Weight_Maopi = HX711_Read();
}
long Get_Weight()
{
HX711_Buffer = HX711_Read();
Weight_Shiwu = HX711_Buffer; ///将传感器的输出值储存
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取实物的AD采样数值。
Weight_Shiwu = (long)((float)Weight_Shiwu/HX711_GapValue);
return Weight_Shiwu;
}
int32_t main(void)
{
// LED_GPIO_Config();
ADInit();
Get_Maopi();
while(1)
{
Get_Weight();
}
}
HX711.C
#include "hx711.h"
#include "gpio.h"
#include "ddl.h"
#if 0
void LED_GPIO_Config(void)
{
stc_gpio_config_t stcGpioCfg;
///< 打开GPIO外设时钟门控
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
///< 端口方向配置->输入
stcGpioCfg.enDir = GpioDirIn;
///< 端口驱动能力配置->高驱动能力
stcGpioCfg.enDrv = GpioDrvL;
///< 端口上下拉配置->上拉
stcGpioCfg.enPuPd = GpioPu;
///< 端口开漏输出配置->开漏输出关闭
stcGpioCfg.enOD = GpioOdDisable;
///< 端口输入/输出值寄存器总线控制模式配置->AHB
stcGpioCfg.enCtrlMode = GpioAHB;
///< GPIO IO PD04初始化(PD04在STK上外接KEY(USER))
Gpio_Init(GpioPortD, GpioPin4, &stcGpioCfg);
///< 端口方向配置->输出(其它参数与以上(输入)配置参数一致)
stcGpioCfg.enDir = GpioDirOut;
///< 端口上下拉配置->下拉
stcGpioCfg.enPuPd = GpioPd;
///< GPIO IO PD05初始化(PD05在STK上外接LED(LED3))
Gpio_Init(GpioPortD, GpioPin5, &stcGpioCfg);
}
#endif
void ADInit(void)
{
stc_gpio_config_t stcGpioCfg;
///< 打开GPIO外设时钟门控
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
///< 端口方向配置->输出
stcGpioCfg.enDir = GpioDirOut;
///< 端口驱动能力配置->高驱动能力
stcGpioCfg.enDrv = GpioDrvL;
///< 端口上下拉配置->无上下拉
stcGpioCfg.enPuPd = GpioNoPuPd;
///< 端口开漏输出配置->开漏输出关闭
stcGpioCfg.enOD = GpioOdDisable;
///< 端口输入/输出值寄存器总线控制模式配置->AHB
stcGpioCfg.enCtrlMode = GpioAHB;
///< GPIO IO PA08初始化
Gpio_Init(GpioPortC, GpioPin7, &stcGpioCfg); //CLK
///< 端口方向配置->输入
stcGpioCfg.enDir = GpioDirIn;
///< 端口驱动能力配置->高驱动能力
stcGpioCfg.enDrv = GpioDrvL;
///< 端口上下拉配置->无上下拉
stcGpioCfg.enPuPd = GpioNoPuPd;
///< 端口开漏输出配置->开漏输出关闭
stcGpioCfg.enOD = GpioOdDisable;
///< 端口输入/输出值寄存器总线控制模式配置->AHB
stcGpioCfg.enCtrlMode = GpioAHB;
///< GPIO IO PA09初始化
Gpio_Init(GpioPortC, GpioPin6, &stcGpioCfg); //DATA
}
unsigned long HX711_Read(void) //时钟配置,128增益
{
unsigned long val = 0;
unsigned char i = 0;
Gpio_SetIO(GpioPortC, GpioPin6);
delay1us(1);
Gpio_ClrIO(GpioPortC, GpioPin7);
delay1us(1);
while(Gpio_GetInputIO(GpioPortC, GpioPin6));
for(i=0; i<24; i++)
{
Gpio_SetIO(GpioPortC, GpioPin7);
delay1us(1);
val = val << 1;
Gpio_ClrIO(GpioPortC, GpioPin7);
delay1us(1);
if(Gpio_GetInputIO(GpioPortC, GpioPin6))
{
val++;
}
}
Gpio_SetIO(GpioPortC, GpioPin7);
val ^= 0x800000;
delay1us(1);
Gpio_ClrIO(GpioPortC, GpioPin7);
delay1us(1);
return val;
}
|