- #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
- #define HX711_DOUT GPIO_ReadPin(CW_GPIOF, GPIO_PIN_5)
- 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
LCD
- #include "lcd.h"
- /* 段码低8(左) */
- static const uint8_t num_L[19] = {
- 0x0d, //0
- 0x00, //1
- 0x0e, //2
- 0x0a, //3
- 0x03, //4
- 0x0b, //5
- 0x0f, //6
- 0x00, //7
- 0x0f, //8
- 0x0b, //9
- 0x07, //A
- 0x0f, //b
- 0x0d, //C
- 0x0e, //d
- 0x0f, //E
- 0x07, //F
- 0x07, //H
- 0x0d, //L
- 0x02, //-
- };
- /* 段码高8(右) */
- static const uint8_t num_H[19] = {
- 0x07,
- 0x06,
- 0x03,
- 0x07,//3
- 0x06,//4
- 0x05, //5
- 0x05, //
- 0x07, //7
- 0x07, //8
- 0x07, //9
- 0x07, //A
- 0x04, //B
- 0x01, //C
- 0x06, //d
- 0x01, //e
- 0x01, //f
- 0x06, //H/*
- 0x00, //L
- 0x00, //-
- };
- /* 函数功能:在指定的段码屏位置上显示指定序号的字符
- *参数wei:
- 指定需要显示的位
- *参数num:
- 需要显示的序号。
- */
- void lcd_show_string(uint8_t wei, uint8_t num)
- {
- if(wei>7 || num > sizeof(num_H))
- {
- return;
- }
- switch(wei)
- {
- case 7:
- {
- //显示第7个数码管
- CW_LCD->RAM[0] |= num_H[num]<<8 | num_L[num];
- break;
- }
- case 6:
- {
- //显示第6个数码管
- CW_LCD->RAM[0] |= (num_H[num]<<8 | num_L[num]) <<16;
- break;
- }
- case 5:
- {
- //显示第5个数码管
- CW_LCD->RAM[1] |= num_H[num]<<8 | num_L[num];
- break;
- }
- case 4:
- {
- //显示第4个数码管
- CW_LCD->RAM[1] |= (num_H[num]<<8 | num_L[num]) <<16;
- break;
- }
- case 3:
- {
- //显示第3个数码管
- CW_LCD->RAM[2] |= num_H[num]<<8 | num_L[num];
- break;
- }
- case 2:
- {
- //显示第2个数码管
- CW_LCD->RAM[3] |= (num_H[num]<<8);
- CW_LCD->RAM[2] |= (num_L[num]<<16);
- break;
- }
- case 1:
- {
- //显示第1个数码管
- CW_LCD->RAM[3] |= num_H[num]<<24 | num_L[num]<<16;
- break;
- }
- case 0:
- {
- //显示第0个数码管
- CW_LCD->RAM[4] |= (num_H[num]<<8 | num_L[num]);
- break;
- }
- }
- };
- void lcd_init(void)
- {
-
- LCD_InitTypeDef LCD_InitStruct = {0};
- RCC_LSI_Enable(); // 启动LSI为LCD提供时钟
- __RCC_LCD_CLK_ENABLE();
-
- LCD_InitStruct.LCD_Bias = LCD_Bias_1_3;
- LCD_InitStruct.LCD_ClockSource = LCD_CLOCK_SOURCE_LSI;
- LCD_InitStruct.LCD_Duty = LCD_Duty_1_4;
- LCD_InitStruct.LCD_ScanFreq = LCD_SCAN_FREQ_256HZ;
- LCD_InitStruct.LCD_VoltageSource = LCD_VoltageSource_Internal;
-
- LCD_Init(&LCD_InitStruct);
- LCD_COMConfig(LCD_COM0 | LCD_COM1 | LCD_COM2 | LCD_COM3 , ENABLE);
- LCD_SEG0to23Config(LCD_SEG0|LCD_SEG1|LCD_SEG2|LCD_SEG3|LCD_SEG4|LCD_SEG5|LCD_SEG6|LCD_SEG7|LCD_SEG8|LCD_SEG9|LCD_SEG10|LCD_SEG13|LCD_SEG14|LCD_SEG15|LCD_SEG16|LCD_SEG17, ENABLE);
-
- LCD_Cmd(ENABLE);
- LCD_ContrastConfig(LCD_Contrast_Level_6);
- LCD_DriveVoltageConfig(LCD_INRS_LEVEL_0);
-
- }
- void lcd_clear(void)
- {
- CW_LCD->RAM[0] =0;
- CW_LCD->RAM[1] =0;
- CW_LCD->RAM[2] =0;
- CW_LCD->RAM[3] =0;
- CW_LCD->RAM[4] =0;
- }
主程序代码:
- /******************************************************************************/
- #include "main.h"
- #include "log.h"
- void show_weight(uint32_t weight);
- int32_t main(void)
- {
- lcd_init();
- 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); //打印
- show_weight(Weight_Shiwu);
- SysTickDelay(1000);
- CW_GPIOA->TOG = bv0;
- }
- }
- void show_weight(uint32_t weight)
- {
- lcd_clear();
- if(weight>=10000)
- {
- lcd_show_string(4,weight/10000);
- lcd_show_string(3,weight%10000/1000);
- lcd_show_string(2,weight%1000/100);
- lcd_show_string(1,weight%100/10);
- lcd_show_string(0,weight%10);
- }
- else if(weight>=1000)
- {
- lcd_show_string(3,weight/1000);
- lcd_show_string(2,weight%1000/100);
- lcd_show_string(1,weight%100/10);
- lcd_show_string(0,weight%10);
- }
- else if(weight>=100)
- {
- lcd_show_string(2,weight/100);
- lcd_show_string(1,weight%100/10);
- lcd_show_string(0,weight%10);
- }
- else if(weight>=10)
- {
- lcd_show_string(1,weight/10);
- lcd_show_string(0,weight%10);
- }
- else{
- lcd_show_string(0,weight%10);
- }
-
- }
- /******************************************************************************
- * EOF (not truncated)
- ******************************************************************************/
- #ifdef USE_FULL_ASSERT
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] 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 */
【调试】
由于不同的传感器,不同的温度,传感器的AD转换需要较调,我这里采集了100克的法码进行了设调,即修改GapValue的值。
【工程效果】: