[应用相关]

【转】HX711电子称传感器数据读取

[复制链接]
1268|2
手机看帖
扫描二维码
随时随地手机跟帖
Brand2|  楼主 | 2018-4-27 19:44 | 显示全部楼层 |阅读模式
/*Include---------------------------*/
#include"stm32f10x_lib.h"                //包含所有的头文件
#include <stdio.h>
//----------------函数声明--------------------
void Delay_MS(u16 dly);
void delay_us(u16 dly1);
void RCC_Configuration(void);
void GPIO_Configuration(void);
unsigned long Read_HX711(void);
void USART_Configuration(void);
int fputc(int ch,FILE *f);
int GetKey (void);
USART_InitTypeDef USART_InitStructure;
ErrorStatus HSEStartUpStatus;

/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
    u32 weigh1;
        float weigh2;
        #ifdef DEBUG
        debug();
        #endif

        //------------初始化------------
        RCC_Configuration();
        GPIO_Configuration();
        USART_Configuration( );
        while(1)
        {
          Delay_MS(1000);
          weigh2=Read_HX711();
          weigh2=weigh2/83886.08;
          weigh1=weigh2*2000;
          GPIO_SetBits(GPIOA,GPIO_Pin_11);               
          printf("%d\n",weigh1);
        }
               
}

/*******************************************************************************
* Function Name  : Delay_Ms
* Description    : delay 1 ms.
* Input          : dly (ms)
* Output         : None
* Return         : None
*******************************************************************************/
void Delay_MS(u16 dly)
{
        u16 i,j;
        for(i=0;i<dly;i++)
        for(j=1000;j>0;j--);
}
void delay_us(u16 dly1)
{
        u16 i;
        for(i=dly1;i>0;i--);
}
/*******************************************************************************
* Function Name  : Delay_Ms
* Description    : Read weigh
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/

unsigned long Read_HX711(void)  //读HX711芯片输出的数据。
{
unsigned long val = 0;
unsigned char i = 0;

GPIO_SetBits(GPIOB,GPIO_Pin_11);    //DOUT=1
GPIO_ResetBits(GPIOB,GPIO_Pin_12);    //SCK=0
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11));   //等待DOUT=0  
delay_us(1);
for(i=0;i<24;i++)
{
GPIO_SetBits(GPIOB,GPIO_Pin_12);    //SCK=1
val=val<<1;
delay_us(1);  
GPIO_ResetBits(GPIOB,GPIO_Pin_12);    //SCK=0
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11))   //DOUT=1
val++;
delay_us(1);
}
GPIO_SetBits(GPIOB,GPIO_Pin_12);
delay_us(1);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
delay_us(1);  
return val;  
}   
/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
        //----------使用外部RC晶振-----------
        RCC_DeInit();                        //初始化为缺省值
        RCC_HSEConfig(RCC_HSE_ON);        //使能外部的高速时钟
        while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);        //等待外部高速时钟使能就绪
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);   
    FLASH_SetLatency(FLASH_Latency_2);
        RCC_HCLKConfig(RCC_SYSCLK_Div1);                //HCLK = SYSCLK
        RCC_PCLK2Config(RCC_HCLK_Div1);                        //PCLK2 =  HCLK
        RCC_PCLK1Config(RCC_HCLK_Div2);                        //PCLK1 = HCLK/2
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);        //PLLCLK = 8MHZ * 9 =72MHZ
        RCC_PLLCmd(ENABLE);                        //Enable PLLCLK

        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);        //Wait till PLLCLK is ready
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);        //Select PLL as system clock
        while(RCC_GetSYSCLKSource()!=0x08);                //Wait till PLL is used as system clock source        
        //---------打开相应外设时钟--------------------
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);        //使能APB2外设的GPIOA的时钟        
                  //开启串口时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

                 
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : 初始化GPIO外设
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
        //CLK:PB5  CLR:PE11  Data:PE10
        GPIO_InitTypeDef        GPIO_InitStructure;                //声明一个结构体变量

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;         //选择PC.10(TXD) 和 PC.11
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         //管脚频率为50MHZ
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;         //输出模式为推挽输出
        GPIO_Init(GPIOB,&GPIO_InitStructure);                                 //初始化PB5

           //配置USART1_Tx为复合推挽输出
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
          GPIO_Init(GPIOA, &GPIO_InitStructure);
        
          //配置 USART1_Rx 为浮空输入
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
          GPIO_Init(GPIOA, &GPIO_InitStructure);

          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;         //选择PC.10(TXD) 和 PC.11
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;         //浮空输入
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  //IO口速度为50MHz
        GPIO_Init(GPIOB,&GPIO_InitStructure);                                 //初始化PB5

           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         //管脚频率为50MHZ
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           
        GPIO_Init(GPIOB,&GPIO_InitStructure);                                 //初始化PB5

        
           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;         //选择PC.10(TXD) 和 PC.11
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         //管脚频率为50MHZ
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           
        GPIO_Init(GPIOA,&GPIO_InitStructure);                                 //初始化PB5
}
/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : 设置串口USART1
* Input          : ch,*f
* Output         : None
* Return         : None
*******************************************************************************/
void USART_Configuration(void)
{
          //串口配置: 波特率 115200 数据位 8 停止位 1  奇偶位 NONE  
  USART_InitStructure.USART_BaudRate = 9600;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity =  USART_Parity_No ;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    //初始化串口
  USART_Init(USART1, &USART_InitStructure);
  //启动串口
  USART_Cmd(USART1, ENABLE);
}
/*******************************************************************************
* Function Name  : fputc
* Description    : 重定向这个C库(stdio)printf函数  文件流——》串口USART1
* Input          : ch,*f
* Output         : None
* Return         : None
*******************************************************************************/
int fputc(int ch, FILE *f)

{

USART_SendData(USART1, (unsigned char) ch);// USART1 可以换成 USART2 等

while (!(USART1->SR & USART_FLAG_TXE));

return (ch);

}

// 接收数据

int GetKey (void) {

while (!(USART1->SR & USART_FLAG_RXNE));

return ((int)(USART1->DR & 0x1FF));

}
mmuuss586| | 2018-4-28 09:28 | 显示全部楼层
感谢分享;

使用特权

评论回复
liubinghui| | 2018-6-12 14:18 | 显示全部楼层
楼主 请教下除以83886.08这个什么意思。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

157

主题

824

帖子

2

粉丝