基于STM32的24位ADC采集重量程序
工程模版
-MDK版本:5.15
-固件库版本:3.5.0
单片机源程序如下:
- #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)
|