欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
STM32F1和24位ADC采集HX711稱重傳感器的程序分享
[打印本頁]
作者:
demons1
時間:
2017-7-3 12:06
標題:
STM32F1和24位ADC采集HX711稱重傳感器的程序分享
基于STM32的24位ADC采集重量程序
工程模版
-MDK版本:5.15
-固件庫版本:3.5.0
0.png
(48.23 KB, 下載次數: 93)
下載附件
2017-7-4 00:49 上傳
單片機源程序如下:
#include "stm32f10x.h"
#include <stdio.h>
#include "spi.h"
#include "lcd.h"
#include "touch.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);
void UART_PutChar(USART_TypeDef* USARTx, uint8_t Data);
void UART_PutStr(USART_TypeDef* USARTx, uint8_t *str);
USART_InitTypeDef USART_InitStructure;
ErrorStatus HSEStartUpStatus;
extern u16 xtemp,ytemp,display;
float retry_measure()
{
u8 i;
u32 weigh1=0;
float weigh2;
float weigh1_buf=0;
for(i=0;i<2;i++)
{
weigh2=Read_HX711();
weigh2=(weigh2-39000)/83886.08;
weigh1=weigh2*2000;
weigh1_buf+=weigh1/2.0;
}
return weigh1_buf;
}
int main(void)
{
u32 weigh1=0;
float num=0;
float weigh2;
float weigh1_buf=0;
u32 init_flag=0;
u8 i;
u8 error;
float amp=28.428;
#ifdef DEBUG
debug();
#endif
RCC_Configuration();
GPIO_Configuration();
spi1_Configuration();
USART_Configuration( );
lcd_init();
lcd_clear(0xFFFF);
while(1)
{
if(init_flag<2)
{
weigh1_buf=retry_measure();
}
init_flag=3;
if(weigh1_buf<=3) weigh1_buf=0;
num=0;
for(i=0;i<10;i++)
{
Delay_MS(500);
weigh2=Read_HX711();
weigh2=(weigh2-39000)/83886.08;
weigh1=weigh2*2000;
weigh1=(u32)(weigh1-weigh1_buf);
// printf("weigh2=%lf",weigh2);
if(weigh1>1000000) error=1;
else error=0;
if(weigh1>24.623)amp=28.435;
weigh1=(u32)(weigh1*amp);
num+=weigh1/10.0;
}
num=(int)num/10;
lcd_showString(60,35,"Weight Measure");
lcd_showString(80,70,"Weight is :");
lcd_showNum(64,105,num,8);
if(num<99)lcd_ShowChar(104,105,0+48,0);
lcd_ShowChar(112,105,'.',0);
lcd_showNum1(120,105,num,2);
lcd_ShowChar(136,105,'g',0);
lcd_showString(80,135,"Thank you!");
LCD_DrawRectangle(40, 215, 200, 275);//畫框
lcd_showString(60,235,"Key to calibrate");
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)==0)//PEN=0)
{
Convert_Pos();
}
else
display=0;
if(display)
{
lcd_showString(40,165,"calibrate ok");
init_flag=0;
}
else lcd_showString(40,165," ");
}
}
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--);
}
unsigned long Read_HX711(void) //ADC返回值
{
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;
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //A0:CS
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //A1: DC
GPIO_Init(GPIOA, &GPIO_InitStructure); //A2:RESET
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //復用推挽,TXD
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入,RXD
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //spi_SCK
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //spi_MISO
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //spi_MOSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //data_hx711
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //clk_hx711
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //spi_PEN
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //spi_CS2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void USART_Configuration(void)
{
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);
}
int fputc(int ch, FILE *f)
{
USART1->SR; //USART_GetFlagStatus(USART1, USART_FLAG_TC)
USART_SendData(USART1, (unsigned char) ch);
……………………
…………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
test.rar
(3.62 MB, 下載次數: 285)
2017-7-3 11:56 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
作者:
ccb318
時間:
2018-4-25 10:52
下來看看好不好用
作者:
wh941227
時間:
2018-4-28 16:24
下載下來參考一下 我用的是zigbee cc2530
作者:
時悟
時間:
2018-4-30 00:59
壓縮包錯誤了額。。求再發一次
作者:
KingSealEast
時間:
2018-5-4 17:12
今天剛好到貨HX711稱重模塊,下載來試試看。
作者:
weidaihe
時間:
2018-6-8 15:03
下下來試試 感謝
作者:
沖寶
時間:
2018-6-12 16:22
非常感謝,學習了
作者:
wilso6745
時間:
2018-6-13 15:53
樓主厲害啊~~下來學習
作者:
plj213
時間:
2018-7-25 17:52
感謝分享。。。。
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1