標題: STM32 HX711程序 [打印本頁]

作者: cxyingljyu    時間: 2017-5-11 11:21
標題: STM32 HX711程序
nclude <stdio.h>
#include "stm32f10x_gpio.h"

// sbit ADDO=P2^0;
// sbit ADSK=P2^1;

#define ADSK_H  GPIO_SetBits (GPIOA ,GPIO_Pin_1)
#define ADSK_L  GPIO_ResetBits(GPIOA ,GPIO_Pin_1)


void static Delay(u32 del)
{
        while(del--);
}



void GPIO_Configuration_XH711(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
        
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
        
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}










//讀711 AD數(shù)據(jù)子程序

//**********************************************************/
u32 ReadCount(void)
{
        u32 Count;
        u16 ADval;
        u8 i;
        ADSK_L;
        Count=0;
        while(GPIO_ReadInputDataBit(GPIOA ,GPIO_Pin_2));
        for(i=0;i<24;i++)   //讀24個bit
        {
                ADSK_H;
                Delay(5);
                Count=Count<<1;
                ADSK_L;
                Delay(5);
                  if(GPIO_ReadInputDataBit(GPIOA ,GPIO_Pin_2))Count++;
        }
        ADSK_H; //發(fā)送第25個脈沖,表示下次轉(zhuǎn)換使用A通道128db
        Delay(5);
//        Count=Count^0x800000;
        ;
        ADSK_L;         
        Delay(5);
/*        if((Count & 0x800000) == 0x800000)
        {
                Count = ~(Count - 1);
        }                  */
//        ADval = (int)(Count >> 8);//取高十六位有效值
        ADval = (int)(Count>>8);
//        ADDO=1;
        return(ADval);
}

u32 Weighing(void)
{        
        u32 X1;   
        u8 t,t1,count=0;               
        u16 databuffer[30];                                             //過采樣緩沖區(qū)
        u16 temp=0;        
        u32 X;
    do{                                                                     //循環(huán)讀數(shù)30次                                   
                X=ReadCount();
                if(X>100)             //如果是在測量在有效區(qū)范圍的值,標示此讀數(shù)有效
                {         
                        databuffer[count]=X;
                        count++;  
                }                 
        }while(count<30);                                            
        if(count==30)                                    //每次度數(shù)一定要讀到30次數(shù)據(jù),否則丟棄
        {  
            do                                            //將數(shù)據(jù)X升序排列
                {        
                        t1=0;                  
                        for(t=0;t<count-1;t++)
                        {
                                if(databuffer[t]>databuffer[t+1])//升序排列
                                {
                                        temp=databuffer[t+1];
                                        databuffer[t+1]=databuffer[t];
                                        databuffer[t]=temp;
                                        t1=1;
                                }  
                        }
                }while(t1);           
        }                        
        /* 從排序過的數(shù)組里中間抽取連續(xù)的10組數(shù)據(jù),進行取平均值,獲得較高的精度 */
        X1 = 0;
        for(count = 10; count < 20; count++){
                X1 = X1 + databuffer[count];
        }        
        X1 = X1 / 10;        
return X1;
}





作者: xuanmic    時間: 2017-7-23 13:42
謝謝樓主的分享。
作者: yujj    時間: 2017-8-12 14:34
你這程序錯了
作者: lyl_420819    時間: 2018-9-26 16:52

謝謝樓主的分享。




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1