stm32c8t6
iic 4脚OLED
HX711按代码接线
- #include "stm32f10x.h"
- #include "delay.h"
- #include "HX711.h"
- #include "oled.h"
- #include "bmp.h"
- int main(void)
- {
- unsigned char m=0,gtemp=0,i,zm,mt[10]={0,0,0,0,0,0,0,0,0,0};
-
- s32 k=0;
- Init_HX711pin();
- delay_init();
-
- OLED_Init();
- OLED_ColorTurn(0);//0正常显示,1 反色显示
- OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
-
- Get_Maopi(); //称空板重量
- delay_ms(1000);
- delay_ms(1000);
- Get_Maopi(); //重新获取空板重量
-
- while(1)
- {
- Get_Weight();
-
- if(gtemp > Weight_Shiwu)
- { zm = zm + gtemp;}
- delay_ms(500);
- if(Weight_Shiwu!=0)
- { gtemp=Weight_Shiwu;}
-
- if(Weight_Shiwu==0 && gtemp!=Weight_Shiwu)
- {
- m++;
- gtemp=Weight_Shiwu;
-
- }
- OLED_Refresh();
- OLED_ShowChinese(0,10,0,16,1);//茄
- OLED_ShowChinese(18,10,1,16,1);//子
- OLED_ShowChinese(36,10,2,16,1);//数
- OLED_ShowChinese(54,10,3,16,1);//量
- OLED_ShowChar(70,10,':',16,1);//:
- OLED_ShowNum(79,10,m,3,16,1);
- OLED_ShowPicture(109,10,16,16,BMP1,1);
-
- OLED_ShowChinese(0,28,0,16,1);//茄
- OLED_ShowChinese(18,28,1,16,1);//子
- OLED_ShowChinese(36,28,4,16,1);//重
- OLED_ShowChinese(54,28,3,16,1);//量
- OLED_ShowChar(70,28,':',16,1);//:
- OLED_ShowNum(79,28,Weight_Shiwu,4,16,1);
- OLED_ShowString(114,28,"g",16,1);
- OLED_ShowChinese(0,46,0,16,1);//茄
- OLED_ShowChinese(18,46,1,16,1);//子
- OLED_ShowChinese(36,46,4,16,1);//重
- OLED_ShowChinese(54,46,3,16,1);//量
- OLED_ShowChar(70,46,':',16,1);//:
- OLED_ShowNum(79,46,zm,4,16,1);
- OLED_ShowString(114,46,"g",16,1);
-
-
- }
- }
复制代码- #include "HX711.h"
- #include "delay.h"
- u32 HX711_Buffer;
- u32 Weight_Maopi;
- s32 Weight_Shiwu;
- u8 Flag_Error = 0;
- //校准参数
- //因为不同的传感器特性曲线不是很一致,因此,每一个传感器需要矫正这里这个参数才能使测量值很准确。
- //当发现测试出来的重量偏大时,增加该数值。
- //如果测试出来的重量偏小时,减小改数值。
- //该值可以为小数
- #define GapValue 106.5
- void Init_HX711pin(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PF端口时钟
- //HX711_SCK
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // 端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB
-
- //HX711_DOUT
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//输入上拉
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- GPIO_SetBits(GPIOB,GPIO_Pin_0); //初始化设置为0
- }
- //****************************************************
- //读取HX711
- //****************************************************
- u32 HX711_Read(void) //增益128
- {
- unsigned long count;
- unsigned char i;
- HX711_DOUT=1;
- delay_us(1);
- HX711_SCK=0;
- count=0;
- while(HX711_DOUT);
- for(i=0;i<24;i++)
- {
- HX711_SCK=1;
- count=count<<1;
- delay_us(1);
- HX711_SCK=0;
- if(HX711_DOUT)
- count++;
- delay_us(1);
- }
- HX711_SCK=1;
- count=count^0x800000;//第25个脉冲下降沿来时,转换数据
- delay_us(1);
- HX711_SCK=0;
- return(count);
- }
- //****************************************************
- //获取毛皮重量
- //****************************************************
- void Get_Maopi(void)
- {
- Weight_Maopi = HX711_Read();
- }
- //****************************************************
- //称重
- //****************************************************
- void Get_Weight(void)
- {
- HX711_Buffer = HX711_Read();
- if(HX711_Buffer > Weight_Maopi)
- {
- Weight_Shiwu = HX711_Buffer;
- Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取实物的AD采样数值。
-
- Weight_Shiwu = (s32)((float)Weight_Shiwu/GapValue); //计算实物的实际重量
- //因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的GapValue这个除数。
- //当发现测试出来的重量偏大时,增加该数值。
- //如果测试出来的重量偏小时,减小改数值。
- }
-
- }
复制代码
Keil代码下载:
称+oled Keil代码.7z
(207.22 KB, 下载次数: 145)
|