关于用stm32去做电子秤的一些问题
刚开始是用51写的,lcd1602显示,写的也算可以,数据显示的也很稳定,后来想做的项目51实现不了,就想着用32去实现,可这就出现了一些问题,我用的是正点原子的mini板子,hx711压力传感
第一个问题就是,程序下进去后,再复位或者断开电源,lcd就不显示内容了,后里发现是主函数的uart_init 函数,不能去掉,因为在 LCD_Init 函数里面调用了 printf,所以一旦你去掉这个初始化,就会死机了,就不再会显示
第二就是显示数字不稳定,因为这是24位的,很灵敏,如果给的电压不稳定,就会导致显示突变,刚开始我的未放置东西,数字都会显示9988294,这个数字也不是什么无效数字,就是不准,但是放置物品显示却正确,我也调了好长时间的时序,发现并没什么用,后来才发现是电压的影响,而且连接线松了也会造成影响
关于hx711很多卖家都会提供资料,不过基本上都是51的,用32写的话,只需要模拟io口就行了
#include "sys.h"
#include "delay.h"
#include "hx711.h"
unsigned long Weight_Shiwu = 0;
unsigned long Weight_Maopi = 0;
void hx711_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
//DT <-----> PC0
//SCK <-----> PC13
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
unsigned long Hx711_Read()
{
unsigned long val = 0; //DT <-----> PC0
unsigned int i = 0; //SCK <-----> PC13
GPIO_SetBits(GPIOC, GPIO_Pin_0);
delay_us(1);//
GPIO_ResetBits(GPIOC, GPIO_Pin_13);
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0));
for(i=0; i<24; i++)
{
GPIO_SetBits(GPIOC,GPIO_Pin_13);
delay_us(1);
val=val<<1;
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0))
val++;
delay_us(1);
}
GPIO_SetBits(GPIOC,GPIO_Pin_13);
val = val^0x800000;
delay_us(1);
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
delay_us(1);
return val;
}
void Get_Maopi()
{
Weight_Maopi = Hx711_Read();
}
void Get_Weight()
{
Weight_Shiwu = Hx711_Read();
Weight_Shiwu = Weight_Shiwu - Weight_Maopi;
if(Weight_Shiwu > 0)
{
Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/430);
}
else
{
Weight_Shiwu = 0;
}
}
#include "stm32f10x.h"
#include "hx711.h"
#include "lcd.h"
#include "delay.h"
#include "usart.h"
int main(void)
{
delay_init();
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(9600);
LCD_Init();
hx711_Init();
Get_Maopi();
LCD_ShowString(160,150,240,320,12, "g");
while(1)
{
Get_Weight();
LCD_ShowxNum(100,150,Weight_Shiwu,10,12,0);
}
}