STM32写的HX711程序

2019-07-21 02:14发布

我用32写的一个读HX711的程序用来做一个称重传感器,为什么会出现重量越大数值越小
while(1)
{
PAin(1)=1;
delay_us(5);
PAout(2)=0;
temp=0;
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==1);
// while(1)
// {
PAout(2)=0;
for(i=0;i<24;i++)
{
PAout(2)=1;
// GPIO_SetBits(GPIOA,GPIO_Pin_2);

temp=temp<<1;
delay_us(10);
PAout(2)=0;
// GPIO_ResetBits(GPIOA,GPIO_Pin_2);
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==1)
{
temp++;
}
}
GPIO_SetBits(GPIOA,GPIO_Pin_2);
delay_us(10);
// temp=temp^0x800000;
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
// temp=(temp+22020)*0.0676-67880;
// temp=(temp*0.000000298)*240;
temp=(temp*0.00298)/42;
Weight=temp-1;
printf("%f ",Weight);
LCD_ShowNum(30,30,Weight,16,16);
delay_ms(1000);

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
48条回答
Scy
2019-07-22 08:20
回复【9楼】新生:
---------------------------------
void HX711_Init(void) //初始化HX711端口
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  //使能PB端口时钟

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  //SCK
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;  //DOUT
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);  //根据设定参数初始化GPIOB.5
}

一周热门 更多>