STM32项目开源:智能语音电子秤(代码+原理图+仿真)

STM32项目开源:智能语音电子秤(代码+原理图+仿真)
文章目录一、前言二、项目功能简介三、PCB设计四、原理图设计五、程序设计六、包含内容七、资料分享一、前言实物项目成品仿真项目成品哔哩哔哩开源视频链接复制浏览器打开https://www.bilibili.com/video/BV1MAtp6mE5G/?vd_source10da4d04d580bd6b34f7298120737c50资料分享见文末二、项目功能简介功能介绍1使用LCD1602液晶显示当前重量大小、单价和总价以及阈值2按键可以设置当前物品单价、具有去皮清零和删除单价功能3按确认按键液晶显示计算出来的重量单价总价4称重范围是0到10kg可以按键设置超重阈值超过设定阈值蜂鸣器进行声光报警5语音模块可以播报当前重量和总价当重量超过阈值时语音播报提示超重警告6屏幕上的数据上传到蓝牙APP可监控数据以及设置单价、去皮清零和删除单价等功能三、PCB设计四、原理图设计五、程序设计代码如下#includesys.h#includeGPIO.h#includeLCD1602.h#includedelay.h#includesys.h#includestdio.h#includeusart.h#includetimer.h#includestring.h#includehx711.h#defineucharunsignedchar#defineuintunsignedint//定义量程系数#defineRATIO0.00423////定义标识volatileunsignedcharFlagKeyPress0;//有键按下标志处理完毕清0volatileunsignedcharFlagSetPrice0;//价格设置状态标志设置好为1。//显示用变量intCounter;inti,iTemp;//称重用变量unsignedlongFullScale;//满量程AD值/1000unsignedlongAdVal;//AD采样值unsignedlongweight;//重量值单位gunsignedlongprice;//单价长整型值单位为分unsignedlongmoney;//总价长整型值单位为分unsignedlongpre_weight;//重量值单位gunsignedlongpre_money;//键盘处理变量unsignedcharkeycode;unsignedcharDotPos;//小数点标志及位置/*延时*/voiddelay(inti){intj,k;for(j0;ji;j)for(k0;k500;k);}//重新找回零点每次测量前调用voidTo_Zero(){FullScaleReadCount();price0;}//显示单价单位为元两位整数一位小数voidDisplay_Price(){LCD1602_write_com(0x8c);LCD1602_write_data(price/1000x30);LCD1602_write_data(price%100/100x30);LCD1602_write_data(.);LCD1602_write_data(price%100x30);}//显示重量单位kg一位整数三位小数voidDisplay_Weight(){LCD1602_write_com(0x83);LCD1602_write_data(weight/10000x30);LCD1602_write_data(.);LCD1602_write_data(weight%1000/1000x30);LCD1602_write_data(weight%100/100x30);LCD1602_write_data(weight%100x30);}//显示总价单位为元三位整数一位小数voidDisplay_Money(){if(money9999)//超出显示量程{LCD1602_write_com(0x800x404);LCD1602_write_word(---.-);return;}if(money1000){LCD1602_write_com(0x800x404);LCD1602_write_data(money/10000x30);LCD1602_write_data(money%1000/1000x30);LCD1602_write_data(money%100/100x30);LCD1602_write_data(.);LCD1602_write_data(money%100x30);}elseif(money100){LCD1602_write_com(0x800x404);LCD1602_write_data(0x20);LCD1602_write_data(money%1000/1000x30);LCD1602_write_data(money%100/100x30);LCD1602_write_data(.);LCD1602_write_data(money%100x30);}elseif(money10){LCD1602_write_com(0x800x404);LCD1602_write_data(0x20);LCD1602_write_com(0x800x405);LCD1602_write_data(0x20);LCD1602_write_data(money%100/100x30);LCD1602_write_data(.);LCD1602_write_data(money%100x30);}else{LCD1602_write_com(0x800x404);LCD1602_write_data(0x20);LCD1602_write_com(0x800x405);LCD1602_write_data(0x20);LCD1602_write_com(0x800x406);LCD1602_write_data(00x30);LCD1602_write_data(.);LCD1602_write_data(money%100x30);}}//数据初始化voidData_Init(){price0;DotPos0;}//按键响应程序参数是键值//返回键值// 7 8 9 10(清0)// 4 5 6 11(删除)// 1 2 3 12(未定义)// 14(未定义) 0 15(.) 13(确定价格)voidKeyPress(uchar keycode){switch(keycode){case0:case1:case2:case3:case4:case5:case6:case7:case8:case9://目前在设置整数位要注意price是整型存储单位为分if(DotPos0){//最多只能设置到千位if(price100){priceprice*10keycode*10;}}//目前在设置小数位elseif(DotPos1)//小数点后第一位{pricepricekeycode;DotPos2;}Display_Price();break;case10://清零键To_Zero();Display_Price();FlagSetPrice0;DotPos0;break;case11://删除键按一次删除最右一个数字if(DotPos2){priceprice/10;priceprice*10;DotPos--;}elseif(DotPos1){priceprice/100;priceprice*10;DotPos--;}elseif(DotPos0){priceprice/100;priceprice*100;}Display_Price();break;case13://确认键FlagSetPrice1;DotPos0;break;case15://小数点按下DotPos1;//小数点后第一位break;}}unsignedshortweight_value_filter(unsignedshortwvalue)//重量值滤波{/*----------------------------------------------------------------*//* Local Variables *//*----------------------------------------------------------------*/unsignedchari,j;staticunsignedshortlastest_5times_value[5]{0};staticunsignedcharfilter_count0;staticunsignedshortcalibration_voltage0;unsignedcharmax_equal_index0;unsignedcharvalue_equal_count[5]{0};/*----------------------------------------------------------------*//* Code Body *//*----------------------------------------------------------------*/if(filter_count5){filter_count;lastest_5times_value[filter_count-1]wvalue;returnlastest_5times_value[filter_count-1];}else{i0;while(i4){lastest_5times_value[i]lastest_5times_value[i1];i;}lastest_5times_value[4]wvalue;}/*Find Most Times Digit*/for(i0;i5;i){for(j0;j5;j){if(lastest_5times_value[i]lastest_5times_value[j]){value_equal_count[i];}}}for(i0;i4;i){if(value_equal_count[max_equal_index]value_equal_count[i1]){max_equal_indexi1;}}/*If calibration_voltage exist in the lastest_5times_value;Return the calibration_voltage*/for(i0;i4;i){if(calibration_voltagelastest_5times_value[i]){returncalibration_voltage;}}calibration_voltagelastest_5times_value[max_equal_index];returncalibration_voltage;}voidget_weight(void)//获得称重{volatileunsignedlongcur_adc_value0;//称重得到重量值weight单位为gcur_adc_valueReadCount();//printf(AdVal1%ld\r\n, cur_adc_value);if(cur_adc_valueFullScale){weight0;}else{cur_adc_valuecur_adc_value-FullScale;if(cur_adc_value55){if(cur_adc_value%10055){cur_adc_valuecur_adc_value(100-(cur_adc_value%100));}else{cur_adc_valuecur_adc_value-(cur_adc_value%100);}}else{cur_adc_value0;}weightcur_adc_value*RATIO;//计算重量}weightweight_value_filter(weight);//重量值滤波//printf(AdVal2%ld\r\n, cur_adc_value);}intmain(void){u8 test_interval0;delay_init();//延时函数初始化KEY_GPIO_Init();//按键初始化HX711_GPIO_Init();delay_ms(500);//上电瞬间加入一定延时在初始化LCD_Init();//屏幕初始化delay_ms(300);LCD1602_write_com(0x80);//指针设置LCD1602_write_word(Welcome to use! );To_Zero();LCD1602_write_com(0x80);//指针设置LCD1602_write_word(WE: . PR: . );LCD1602_write_com(0x800x40);//指针设置LCD1602_write_word(MON: . );Display_Price();while(1){//每0.5秒称重一次if(test_interval25){test_interval0;//称重得到重量值weight单位为gget_weight();//如果超量程显示‘-.---’if(weight10000){pre_weight10000;LCD1602_write_com(0x83);LCD1602_write_word(-.---);}//如果不超量程else{//显示重量值if(pre_weight!weight){pre_weightweight;Display_Weight();}//如果单价设定好了则计算价格if(FlagSetPrice1){moneyweight*price/1000;//money单位为分//显示总金额if(pre_money!money){pre_moneymoney;Display_Money();}}else{LCD1602_write_com(0x800x404);LCD1602_write_word( . );}}}//获取按键keycodeKEYSCAN();//有效键值0-15if((keycode16)(FlagKeyPress0)){FlagKeyPress1;KeyPress(keycode);FlagKeyPress0;delay(100);}delay(1);}}六、包含内容七、资料分享资料获取请查看主页介绍搜索或观看开源视频获取

最新新闻

日新闻

周新闻

月新闻