注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
lugl4313820的个人空间 http://home.eeworld.com.cn/space-uid-1269709.html [收藏] [复制] [分享] [RSS]
日志

【CW32L052测评】电子称

已有 306 次阅读2023-8-4 13:49

【材料】

1、CW32L052开发板。

2、HX711测重模块

3、5Kg称重转感器

【接线】

GPIOF4——SCK

GPIIOF5——DOUT

VCC——VCC

GND——GND

【代码】

hx711.h

#ifndef __HX711_H
#define __HX711_H

#include "main.h"
#include "cw32l052_gpio.h"
#include "cw32L052_rcc.h"

//PF04 SCK
//PF05 DOUT
#define HX711_SCK  CW_GPIOF->ODR_f.PIN4// PB0
#define HX711_DOUT GPIO_ReadPin(CW_GPIOF, GPIO_PIN_5)// PB1


extern void Init_HX711pin(void);
extern uint32_t HX711_Read(void);
extern void Get_Maopi(void);
extern void Get_Weight(void);

extern uint32_t HX711_Buffer;
extern uint32_t Weight_Maopi;
extern int32_t Weight_Shiwu;
extern uint8_t Flag_Error;

#endif

hx711.c:

/************************************************************************************
						
*************************************************************************************/
#include "HX711.h"


uint32_t HX711_Buffer;
uint32_t Weight_Maopi;
int32_t Weight_Shiwu;
uint8_t Flag_Error = 0;

//校准参数
//因为不同的传感器特性曲线不是很一致,因此,每一个传感器需要矫正这里这个参数才能使测量值很准确。
//当发现测试出来的重量偏大时,增加该数值。
//如果测试出来的重量偏小时,减小改数值。
//该值可以为小数
#define GapValue 109.5
void delay_1us(void)
{
	__NOP();
	__NOP();
}

void Init_HX711pin(void)
{
	__RCC_GPIOF_CLK_ENABLE();
	GPIO_InitTypeDef GPIO_InitStructure;


	//HX711_SCK
	GPIO_InitStructure.Pins = GPIO_PIN_4;
	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_Init(CW_GPIOF, &GPIO_InitStructure);

	GPIO_InitStructure.Pins = GPIO_PIN_5;
	GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
	GPIO_Init(CW_GPIOF, &GPIO_InitStructure); 
	
	GPIO_SetBits(CW_GPIOF,GPIO_PIN_4);					//初始化设置为0
}



//****************************************************
//读取HX711
//****************************************************
uint32_t HX711_Read(void)	//增益128
{
	
	unsigned long count; 
	unsigned char i; 
  	//HX711_DOUT=1; 
		CW_GPIOF->ODR_f.PIN5 = 1;
	
  	HX711_SCK=0; 
  	count=0; 
  	while(HX711_DOUT); 
  	for(i=0;i<24;i++)
	{ 
	  	HX711_SCK=1; 
	  	count=count<<1; 
		delay_1us();
		HX711_SCK=0; 
	  	if(HX711_DOUT)
			count++; 
		delay_1us();
	} 
 	HX711_SCK=1; 
  count=count^0x800000;//第25个脉冲下降沿来时,转换数据
	delay_1us();
	HX711_SCK=0;  
	return(count);
}

//****************************************************
//获取毛皮重量
//****************************************************
void Get_Maopi(void)
{
	Weight_Maopi = HX711_Read();	
} 

//****************************************************
//称重
//****************************************************
void 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)((float)Weight_Shiwu/GapValue); 	//计算实物的实际重量
																		//因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的GapValue这个除数。
																		//当发现测试出来的重量偏大时,增加该数值。
																		//如果测试出来的重量偏小时,减小改数值。
	}

	
}

主程序:

/******************************************************************************/

#include "main.h"
#include "log.h"

int32_t main(void)
{
	
	Init_HX711pin();
	LogInit();
	InitTick(SystemCoreClock);
	Get_Maopi();
	SysTickDelay(2000);
	Get_Maopi();				//重新获取毛皮重量
    CW_SYSCTRL->AHBEN_f.GPIOA = 1U;    //Open GPIOA Clk

    CW_GPIOA->ANALOG_f.PIN0 = 0U;      //Digital
    CW_GPIOA->BRR_f.BRR0 = 1U;         //Reset PA00
    CW_GPIOA->DIR_f.PIN0 = 0U;         //Output

		printf("start...\r\n");
    while(1)
    {
			Get_Weight();

			printf("净重量 = %d g\r\n",Weight_Shiwu); //打印 
			SysTickDelay(1000);
      CW_GPIOA->TOG = bv0;

    }

}


/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

【实验效果】

 

【总结】CW32L系列具有低功耗性能,可以实现电池供电的电子称。

本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章