本帖最后由 lerbang 于 2018-6-30 20:57 编辑  
 
HX711模块是24位AD转换模块,下面是由NV32F100FD401驱动HX711模块的程序。 
/**************************************************************** 
2018年3月2日 
文件名:main.c(FD40HX711A1.c) 
****************************************************************/ 
#include "common.h" 
#include "gpio.h" 
#include "sysinit.h" 
#include "start.h" 
#include "TM1638.h" 
#include "HX711.h" 
 
void gpio_Init(void); 
u32 get_weight(void); 
int main(void) 
{ 
 
        u32 b; 
        sysinit(); 
        cpu_identify();                 
        gpio_Init(); 
        GPIO_PinSet(GPIO_PTE7); //PTE7 置 1 
        init_TM1638();  
        Display(12045608); 
        Delayms(2000); 
 
        LedDisplay(10,11,12,13,14,15,16,17,0x02,0xff);   
        Delayms(2000); 
 
        LedDisplay(18,19,20,21,22,23,24,25,0xff,0xff);   
 
 
        while(1) 
        { 
           /****************************************** 
           HX711 AD转换并通过Display(b)在数码管显示 
           *******************************************/ 
 
 
           b=0;               
           b=get_weight();//读取AD 输出数据,赋值给b                   
           Display(b); 
           Delayms(2000); 
             
        } 
} 
 
 
 
 
u32 get_weight(void) 
{ 
    u32 sum=0; 
    u32 a; 
    u8 count=0; 
 
    for(count=0;count<6;count++) 
        { 
         a=0; 
         a=ReadCount(); 
         a=a^0x800000; 
         a=a>>8; 
         sum+=a; 
        } 
    a=0; 
    a=sum/5; 
    return a; 
} 
 
 
 
 
 
void gpio_Init(void) 
{ 
 
        GPIO_Init(GPIOA, GPIO_PTA0_MASK, GPIO_PinOutput);    // TM1638_DIO P26 
        GPIO_Init(GPIOA, GPIO_PTA1_MASK, GPIO_PinOutput);   // TM1638_CLK  P25 
        GPIO_Init(GPIOB, GPIO_PTH2_MASK, GPIO_PinOutput);   // TM1638_STB  P39 
        GPIO_Init(GPIOB, GPIO_PTE7_MASK, GPIO_PinOutput);   //FMQ          P38 
 
        GPIO_Init(GPIOA, GPIO_PTC3_MASK, GPIO_PinOutput);   //HX711_SCK    P7  
        GPIO_Init(GPIOA, GPIO_PTC2_MASK, GPIO_PinInput);   //HX711_DIO    P8 
} 
 
 
 
/**************************************************************************************** 
程序运行。显示12045608和相关字母。 
即运行了以下程序: 
Display(12045608); 
        Delayms(2000); 
 
        LedDisplay(10,11,12,13,14,15,16,17,0x02,0xff);   
        Delayms(2000); 
 
        LedDisplay(18,19,20,21,22,23,24,25,0xff,0xff);   
既时显示HX711的AD转换值。 
 
****************************************************************************************/ 
 
/********************************************************************* 
*文件名:HX711.c 
*日  期:2018年3月2日 
*功  能:HX711的AD转换驱动与NV32FD40芯片 
********************************************************************/ 
#include "gpio.h" 
#include"TM1638.h" 
#include "HX711.h" 
 
 
u32 ReadCount(void) //AD 驱动程序 
{ 
u32 Count; 
u8 i; 
 
 
HX711_SCK_L; //使能AD(HX711_SCK 置低) 
Count=0; 
while(GPIO_BitRead(GPIO_PTC2)); //AD 转换未结束则等待,否则开始读取。起了同步作用。 
Delayus(10);  
  for (i=0;i<24;i++) //24 位时钟脉冲从最高位到最低位逐位输出 
  { 
  HX711_SCK_H; //HX711_SCK 置高(发送脉冲) 
 
  Count=Count<<1; //下降沿来时变量Count 左移一位,右侧补零 
  Delayus(10); 
  HX711_SCK_L; //HX711_SCK; 置低 
 
  if(GPIO_BitRead(GPIO_PTC2)) Count++;//当数据输出为高电平,Count 加1 
  Delayus(10); 
  } 
  HX711_SCK_H; //第25 个脉冲到来 
  Count=Count^0x800000; 
//  Delayus(10);   //延长一个机器周期 
  HX711_SCK_L; //第25 个脉冲结束 
  Delayus(10); 
  return Count; //输出Count 
 
} 
 
/********************************************************************* 
*文件名:HX711.h 
*日  期:2018年3月1日 
*功  能:HX711与NV32FD40芯片连接的相关设置 
*********************************************************************/ 
 
#ifndef        _HX711_H_ 
#define        _HX711_H_ 
#include "gpio.h" 
#include <stdio.h> 
 
 
//HX711的IO口操作函数 
 
 
 
 
 
#define        HX711_SCK_H    GPIO_PinSet(GPIO_PTC3)        //HX711_SCK 置1。  
#define        HX711_SCK_L    GPIO_PinClear(GPIO_PTC3)      //HX711_SCK 置0。 
 
u32 ReadCount(void); 
 
#endif 
 
附工程文件 
 
FD40HX711A.rar
(1.58 MB, 下载次数: 12)
 
 
 
                 
           
 
 
 
 
 
 
 
 
 
 
 
 
 |