OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2517|回复: 0

我把电子称hx711标准库程序移植到HAL库后串口数据输出不对/不变,错在那?

[复制链接]

2

主题

6

帖子

0

精华

初级会员

Rank: 2

积分
120
金钱
120
注册时间
2014-3-4
在线时间
32 小时
发表于 2019-10-24 23:54:38 | 显示全部楼层 |阅读模式
1金钱
本帖最后由 PYQ208 于 2019-10-25 22:38 编辑


//--------------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 );
}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-1 02:20

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表