本设计基于16年TI杯进行设计,矩阵按键输入,OLED进行显示,实现功能:数字输入、小数点输入、累加、去皮、清零、计算价格等。
单片机源程序如下:
- #include "sys.h"
- #include "usart.h"
- #include "HX711.h"
- #include "delay.h"
- #include "skey.h"
- #include "led.h"
- #include "oled.h"
- #include "stdio.h"
- float Weight = 0.0;
- int a=0;
- double fl=0.0;
- double f2 = 0.0;
- char str[10];
- char str1[10];
- char str2[10];
- float num[10];
- float t = 0.0;
- float e= 0.0;
- void menu(void);
- int main(void)
- {
- int s=0;
-
- // unsigned int option1=0;
- // long cmp = 0;
- // long cmp2= 0;
- // float temp = 0.0;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
- LED_Init();
- KeyBoard_Init();
- delay_init(168); //延时初始化
- uart_init(115200); //串口初始化波特率为115200
- OLED_Init(); //初始化OLED
- Init_Hx711();
- delay_ms(1000);
- Get_Maopi();
- delay_ms(100);
- // OLED_ShowNum(0,48,' ',3,16);
- OLED_ShowString(0,0,"Weight=",16);
- OLED_ShowString(0,16,"price =",16);
- OLED_ShowString(0,32,"money =",16);
- OLED_Refresh_Gram();//更新显示到OLED
- LED0 = 0;
- while(1)
- {
- s=Read_KeyValue();
- OLED_ShowNum(90,48,s,3,16);
- LED0 = !LED0;
- Weight = Get_Weight();
- Weight = Weight/1;
- OLED_Refresh_Gram();
- if(Weight<0)
- {
- OLED_ShowChar(60,0,'-',16,1);
- Weight*=-1;
- OLED_ShowNum(66,0,Weight,5,16);
- }
- else if(Weight>=0)
- {
- OLED_ShowChar(60,0,' ',16,1);
- OLED_ShowNum(66,0,Weight,5,16);
- }
- OLED_Refresh_Gram(); //更新显示到OLED
- t = Weight;
- menu();
- delay_ms(10);
- }
- }
- void menu(void)
- {
- unsigned int option=0;
- int i=0;
- int j=0;
-
- option=Read_KeyValue();
- if(option==1||option==2||option==3||option==4||option==5||option==6||option==7||option==8||option==9)
- {
-
- delay_ms(100);
- a=a*10+option;
- if(a>=1000)
- a=0;
- OLED_ShowNum(0,48,a,5,16);
- OLED_Refresh_Gram();
- }
- if(option == 10)//数字0
- {
- delay_ms(100);
- a=a*10+0;
- if(a>=1000)
- a=0;
-
- OLED_ShowNum(0,48,a,5,16);
- OLED_Refresh_Gram();
- }
- if(option==16)//清零
- {
- a= 0;
- OLED_Clear();
- OLED_Init(); //初始化OLED
- KeyBoard_Init();
- Init_Hx711();
- OLED_ShowString(0,0,"Weight=",16);
- OLED_ShowString(0,16,"price =",16);
- OLED_ShowString(0,32,"money =",16);
- f2 = 0.0;
- OLED_Refresh_Gram();//更新显示到OLED
- }
- if(option==11)//去皮
- {
- Get_Maopi();
- }
-
- if(option==12)//小数点
- {
- e = ((float)a)/10;
- delay_ms(100);
- sprintf(str,"%2.2f", e);
- OLED_ShowString(60,16,str,16);
- OLED_Refresh_Gram();
- }
- if(option == 13)//计算价格
- {
- fl = (Weight/1000.0)*e;
- sprintf(str1,"%4.2f", fl);
- OLED_ShowString(72,32,str1,16);
- OLED_Refresh_Gram();
- }
- if(option == 14)//累加
- {
- j++;
- if(j>10) j =0;
- for(i=0;i<j;i++)
- {
- num[i] = fl;
- f2 += num[i];
- }
-
- sprintf(str2,"%4.2f", f2);
- OLED_ShowString(72,32,str2,16);
- OLED_Refresh_Gram();
-
- }
- }
复制代码
所有资料51hei提供下载:
HX711-电子秤.7z
(316.68 KB, 下载次数: 96)
|