找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 11310|回复: 8
打印 上一主题 下一主题
收起左侧

STM32F1和24位ADC采集HX711称重传感器的程序分享

  [复制链接]
跳转到指定楼层
楼主
基于STM32的24位ADC采集重量程序
工程模版
-MDK版本:5.15
-固件库版本:3.5.0

单片机源程序如下:
  1. #include "stm32f10x.h"               
  2. #include <stdio.h>
  3. #include "spi.h"
  4. #include "lcd.h"
  5. #include "touch.h"

  6. void Delay_MS(u16 dly);
  7. void delay_us(u16 dly1);
  8. void RCC_Configuration(void);
  9. void GPIO_Configuration(void);
  10. unsigned long Read_HX711(void);

  11. void USART_Configuration(void);//串口程序
  12. int fputc(int ch,FILE *f);
  13. int GetKey (void);
  14. void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data);
  15. void UART_PutStr(USART_TypeDef* USARTx, uint8_t *str);
  16. USART_InitTypeDef USART_InitStructure;
  17. ErrorStatus HSEStartUpStatus;
  18. extern        u16 xtemp,ytemp,display;

  19. float retry_measure()
  20. {
  21.          u8 i;
  22.          u32 weigh1=0;
  23.          float weigh2;
  24.          float weigh1_buf=0;

  25.           for(i=0;i<2;i++)
  26.                                           {
  27.                                        
  28.                                             weigh2=Read_HX711();
  29.               weigh2=(weigh2-39000)/83886.08;
  30.               weigh1=weigh2*2000;
  31.                                             weigh1_buf+=weigh1/2.0;        
  32.                                              
  33.                                     }
  34.                                                 return weigh1_buf;
  35. }
  36.         
  37. int main(void)
  38. {
  39.     u32 weigh1=0;
  40.           float num=0;
  41.     float weigh2;
  42.           float weigh1_buf=0;
  43.           u32 init_flag=0;
  44.           u8 i;
  45.           u8 error;
  46.     float amp=28.428;
  47.         
  48.     #ifdef DEBUG
  49.       debug();
  50.     #endif
  51.      RCC_Configuration();
  52.     GPIO_Configuration();
  53.     spi1_Configuration();   
  54.    
  55.     USART_Configuration( );
  56.           lcd_init();
  57.     lcd_clear(0xFFFF);
  58.         
  59.     while(1)
  60.         {
  61.                                         if(init_flag<2)
  62.                                         {
  63.                                           weigh1_buf=retry_measure();
  64.                                                 
  65.                             }
  66.                                         init_flag=3;
  67.                                         if(weigh1_buf<=3) weigh1_buf=0;
  68.                                         num=0;
  69.           for(i=0;i<10;i++)
  70.         {        
  71.                                                   
  72.           Delay_MS(500);
  73.           weigh2=Read_HX711();
  74.           weigh2=(weigh2-39000)/83886.08;
  75.           weigh1=weigh2*2000;
  76.                                         weigh1=(u32)(weigh1-weigh1_buf);
  77. //                                        printf("weigh2=%lf",weigh2);
  78.                                  
  79.                             if(weigh1>1000000) error=1;
  80.                             else error=0;
  81.                                         if(weigh1>24.623)amp=28.435;
  82.           weigh1=(u32)(weigh1*amp);
  83.           num+=weigh1/10.0;
  84.                                 }                        
  85.                 num=(int)num/10;
  86.                                         lcd_showString(60,35,"Weight Measure");
  87.           lcd_showString(80,70,"Weight is :");
  88.                                         lcd_showNum(64,105,num,8);
  89.                                   if(num<99)lcd_ShowChar(104,105,0+48,0);
  90.                                   lcd_ShowChar(112,105,'.',0);
  91.                                   lcd_showNum1(120,105,num,2);
  92.                                   lcd_ShowChar(136,105,'g',0);
  93.                                   lcd_showString(80,135,"Thank you!");
  94.                                   LCD_DrawRectangle(40, 215, 200, 275);//画框
  95.                                   lcd_showString(60,235,"Key to calibrate");
  96.                                         if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)==0)//PEN=0)
  97.                                         {               
  98.                                                         Convert_Pos();                                                        
  99.                                         }
  100.                                         else
  101.                                                  display=0;
  102.                                        
  103.                                   if(display)
  104.                                         {
  105.                                                 lcd_showString(40,165,"calibrate ok");
  106.                                                 init_flag=0;
  107.                                         }
  108.                                                 else lcd_showString(40,165,"             ");            
  109.                                         }
  110.                                 
  111.                
  112. }


  113. void Delay_MS(u16 dly)
  114. {
  115.         u16 i,j;
  116.         for(i=0;i<dly;i++)
  117.         for(j=1000;j>0;j--);
  118. }
  119. void delay_us(u16 dly1)
  120. {
  121.         u16 i;
  122.         for(i=dly1;i>0;i--);
  123. }


  124. unsigned long Read_HX711(void)   //ADC返回值               
  125. {
  126. unsigned long val = 0;
  127. unsigned char i = 0;

  128. GPIO_SetBits(GPIOB,GPIO_Pin_11);                   //DOUT=1
  129. GPIO_ResetBits(GPIOB,GPIO_Pin_12);                 //SCK=0
  130. while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11));   //DOUT=0  
  131. delay_us(1);
  132. for(i=0;i<24;i++)
  133. {
  134.    GPIO_SetBits(GPIOB,GPIO_Pin_12);                   //SCK=1
  135.    val=val<<1;       //左移
  136.    delay_us(1);  
  137.    GPIO_ResetBits(GPIOB,GPIO_Pin_12);    //SCK=0
  138.    if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11))   //DOUT=1
  139.       val++;
  140.    delay_us(1);
  141. }
  142. GPIO_SetBits(GPIOB,GPIO_Pin_12);
  143. delay_us(1);
  144. GPIO_ResetBits(GPIOB,GPIO_Pin_12);
  145. delay_us(1);  
  146. return val;  
  147. }


  148. void RCC_Configuration(void)
  149. {
  150.       
  151.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);              
  152.             
  153.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO, ENABLE);
  154.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  155.                  
  156. }

  157. void GPIO_Configuration(void)
  158. {
  159.       
  160.    GPIO_InitTypeDef        GPIO_InitStructure;
  161.         
  162.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
  163.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     //A0:CS
  164.          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      //A1: DC
  165.    GPIO_Init(GPIOA, &GPIO_InitStructure);                //A2:RESET
  166.          
  167.          
  168.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  169.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     //复用推挽,TXD
  170.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  171.    GPIO_Init(GPIOA, &GPIO_InitStructure);
  172.         

  173.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  174.          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  175.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;       //上拉输入,RXD
  176.    GPIO_Init(GPIOA, &GPIO_InitStructure);
  177.         
  178.          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;           //spi_SCK
  179.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  180.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  181.    GPIO_Init(GPIOA, &GPIO_InitStructure);
  182.          
  183.          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;           //spi_MISO
  184.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  185.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  186.    GPIO_Init(GPIOA, &GPIO_InitStructure);
  187.          
  188.          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;           //spi_MOSI
  189.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  190.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  191.    GPIO_Init(GPIOA, &GPIO_InitStructure);

  192.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;          //data_hx711
  193.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;        
  194.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  195.    GPIO_Init(GPIOB,&GPIO_InitStructure);                                 

  196.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;          //clk_hx711
  197.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         
  198.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           
  199.    GPIO_Init(GPIOB,&GPIO_InitStructure);
  200.          
  201.          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;          //spi_PEN
  202. //   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         
  203.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;           
  204.    GPIO_Init(GPIOA,&GPIO_InitStructure);
  205.          
  206.          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;          //spi_CS2
  207.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         
  208.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;           
  209.    GPIO_Init(GPIOA,&GPIO_InitStructure);
  210.          
  211.                                       
  212. }

  213. void USART_Configuration(void)
  214. {
  215.          
  216.   USART_InitStructure.USART_BaudRate = 9600;
  217.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  218.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  219.   USART_InitStructure.USART_Parity =  USART_Parity_No ;
  220.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  221.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  222.   USART_Init(USART1, &USART_InitStructure);
  223.   USART_Cmd(USART1, ENABLE);
  224. }

  225. int fputc(int ch, FILE *f)

  226. {
  227.     USART1->SR;  //USART_GetFlagStatus(USART1, USART_FLAG_TC)
  228.    
  229.     USART_SendData(USART1, (unsigned char) ch);
  230.    
  231. ……………………

  232. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
test.rar (3.62 MB, 下载次数: 284)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:199618 发表于 2018-4-25 10:52 | 只看该作者
下来看看好不好用
回复

使用道具 举报

板凳
ID:311165 发表于 2018-4-28 16:24 | 只看该作者
下载下来参考一下  我用的是zigbee cc2530  
回复

使用道具 举报

地板
ID:196302 发表于 2018-4-30 00:59 | 只看该作者
压缩包错误了额。。求再发一次
回复

使用道具 举报

5#
ID:321356 发表于 2018-5-4 17:12 | 只看该作者
今天刚好到货HX711称重模块,下载来试试看。
回复

使用道具 举报

6#
ID:314218 发表于 2018-6-8 15:03 | 只看该作者
下下来试试  感谢
回复

使用道具 举报

7#
ID:350361 发表于 2018-6-12 16:22 | 只看该作者
非常感谢,学习了
回复

使用道具 举报

8#
ID:350783 发表于 2018-6-13 15:53 | 只看该作者
楼主厉害啊~~下来学习
回复

使用道具 举报

9#
ID:138247 发表于 2018-7-25 17:52 | 只看该作者
感谢分享。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表