|
本帖最后由 pyq208 于 2019-10-25 22:32 编辑
//--------------hx711.h------------------------
/* Define to prevent recursive inclusion -------------------------------------*/
#define __HX711_H
#define __HX711_H
#ifndef __gpio_H
#define __gpio_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
uint32_t HX711_Buffer;
uint32_t Weight_Maopi;
int32_t Weight_Shiwu;
#define GapValue 106.5//该值可以为小数
#endif
//--------------hx711.c------------------------
/*******************************************************
*******************************************************/
#include "HX711.h"
//****************************************************DOUT _ PB1
//读取HX711
//****************************************************SCK _ PB0
uint32_t HX711_Read(void) //增益128
{
unsigned long count;
unsigned char i;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET); //(data, 1);
delay_us(1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); //(sck, 0);
count = 0;
while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1 == 1)); //
for (i = 0; i < 24; i++)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); //(sck, 1);
count = count << 1;
delay_us(1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);//(sck, 0);
if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == 1) //
count++;
delay_us(1);
}
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); //(sck, 1);
count = count ^ 0x800000; //第25个脉冲下降沿来时,转换数据
delay_us(1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); //(sck, 0);/1000
return (count);
}
//****************************************************
//获取毛皮重量
//****************************************************
void Get_Maopi(void)
{
Weight_Maopi = HX711_Read();
}
//****************************************************
//称重
//****************************************************
uint32_t Get_Weight(void)
{
HX711_Buffer = HX711_Read();
if (HX711_Buffer > Weight_Maopi)
{
Weight_Shiwu = HX711_Buffer;
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取实物的AD采样数值。
Weight_Shiwu = (int32_t)(Weight_Shiwu / GapValue); //计算实物的实际重量
return (Weight_Shiwu/1000);
}
}
//--------------hx711_main------------------------
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "HX711.h"
extern void delay_us ( uint16_t us );
extern uint32_t HX711_Read(void);
extern void Get_Maopi(void);
extern uint32_t Get_Weight(void);
int tf=0;
void SystemClock_Config(void);
//---------- 【 printf 重定向函数】 -----------
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit (& huart1 , ( uint8_t *)& ch , 1 , 0xFFFF );
return ch ;
}
int main(void)
{
int32_t Weight;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
MX_USART1_UART_Init();
MX_TIM4_Init();
Get_Maopi(); //获取毛皮
HAL_Delay ( 1000 );
while (1)
{
tf++;
Weight = Get_Weight ();
HX711_Read();
printf ( " 净重量 = %d g\r\n" , Weight ); // 打印 Weight_Shiwu;
HAL_GPIO_TogglePin ( LED1_GPIO_Port , LED1_Pin ); //LED 取反
HAL_Delay ( 1000 );
}
}
//----- 用定时器 4 实现微秒级别的延时 --------
void delay_us ( uint16_t us )
{
uint16_t differ = 0xffff - us - 5 ;
HAL_TIM_Base_Start (& htim4 );
__HAL_TIM_SET_COUNTER (& htim4 , differ ); // 查询计数器的计数值
while ( differ < 0xffff - 5 )
{
differ = __HAL_TIM_GET_COUNTER (& htim4 ); // 查询计数器的计数值
}
HAL_TIM_Base_Stop (& htim4 );
}
|
|