电子秤stm32程序.docx
- 文档编号:12874961
- 上传时间:2023-04-22
- 格式:DOCX
- 页数:75
- 大小:42.88KB
电子秤stm32程序.docx
《电子秤stm32程序.docx》由会员分享,可在线阅读,更多相关《电子秤stm32程序.docx(75页珍藏版)》请在冰豆网上搜索。
电子秤stm32程序
#include"led.h"
#include"sys.h"
#include"delay.h"
#include"adc.h"
#include"lcd.h"
#include"usart.h"
#include"lcdfot.h"
#include"hx711.h"
#include"key.h"
intmain(void)
{
u8num,flag;
floatvol1;
delay_init();
uart_init(9600);
LED_Init();
LCD_Init();
Adc_Init();
KEY_Init();
HX711_Init();
POINT_COLOR=RED;
HZ_show();
LCD_ShowString(54+25,55,200,24,16,":
000.00g");//提示信息显示
LCD_ShowString(54+25,85,200,24,16,":
00.00$");//提示信息显示
LCD_ShowString(54+25,115,200,24,16,":
000.00$");//提示信息显示
LCD_ShowString(54+25,145,200,24,16,":
000.00g");//提示信息显示
while
(1)
{
vol1=HX711_count();if(KEY_Scan
(1)==WKUP_PRES)//单价设置num的值可直接用
{
num++;if(num==100)num=0;
LCD_ShowxNum(54+25+8,85,num,2,16,0);}
if(KEY_Scan
(1)==KEY0_PRES)//控制金额是否累加flag=1是累加flag=0不累加{
HZ_show_moy();
flag++;
LED0=0;
if(flag==2)
{
LED0=1;
LCD_ShowString(54+25+32+30,115,200,16,16,"");
flag=0;
}
printf("%d",flag);
}
delay_ms(300);
LED1=!
LED1;
}
}
#include"lcd.h"
#include"lcdfot.h"
u8table1[]=
{
0x00,0x00,0x00,0x00,0x00,0x04,0x02,0x00,0x04,0x02,0x00,0x04,0x02,0x00,0x24,0x0A,
0xFF,0xA4,0x0A,0x7F,0x24,0x0A,0x49,0x24,0x12,0x49,0x24,0x12,0x49,0x24,0x12,0x49,
0x24,0x1F,0xFF,0xFC,0x12,0x49,0x24,0x12,0x49,0x24,0x12,0x49,0x24,0x22,0x49,0x24,
0x22,0x49,0x24,0x22,0xFF,0xA4,0x02,0x40,0x64,0x06,0x00,0x24,0x06,0x00,0x0C,0x02,
0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,
};/*重*/
u8table2[]=
{
0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x40,0x02,0x00,0x40,0x02,0x00,0x40,0x12,0x3F,
0xDF,0xD2,0x3F,0x52,0x92,0x25,0x52,0x92,0x25,0x52,0x92,0x25,0x52,0x92,0x25,0x52,
0x92,0x25,0x5F,0xFC,0x25,0x5F,0xFC,0x25,0x52,0x92,0x25,0x52,0x92,0x25,0x52,0x92,
0x25,0x52,0x92,0x3F,0x5F,0xD2,0x20,0x5F,0xB2,0x00,0x40,0x12,0x00,0x40,0x04,0x00,
0xC0,0x04,0x00,0x40,0x00,0x00,0x00,0x00,
};/*量*/
u8table3[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x40,0x00,0x00,0x40,0x03,
0xFE,0x40,0x02,0x22,0x40,0x22,0x22,0x40,0x32,0x22,0x40,0x1E,0x22,0x40,0x02,0x22,
0x40,0x03,0xFF,0xFE,0x02,0x22,0x40,0x06,0x22,0x40,0x0A,0x22,0x40,0x72,0x22,0x40,
0x22,0x22,0x40,0x02,0x22,0x40,0x03,0xFE,0x40,0x02,0x00,0x40,0x00,0x00,0xC0,0x00,
0x00,0xC0,0x00,0x00,0x40,0x00,0x00,0x00,
};/*单*/
u8table4[]=
{
0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x10,0x00,0x00,0x60,0x00,0x01,0xC0,0x00,0x07,
0xFF,0xFE,0x3C,0x80,0x00,0x30,0x10,0x02,0x00,0x20,0x04,0x00,0x40,0x08,0x00,0x80,
0x18,0x03,0x3F,0xE0,0x06,0x3E,0x00,0x3C,0x00,0x00,0x30,0x00,0x00,0x0C,0x00,0x00,0x02,0x00,0x01,0x01,0x3F,0xFE,0x00,0x80,0x00,0x00,0xC0,0x00,0x00,0x60,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};/*价*/
u8table5[]=
{
0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x01,0x08,0x08,0x01,
0x08,0x0C,0x01,0x08,0x1C,0x01,0x08,0x2C,0x01,0x08,0x4C,0x01,0x08,0x88,0x01,0x0B,
0x08,0x3F,0xFE,0x08,0x3F,0xF8,0x08,0x01,0x08,0x08,0x01,0x08,0x08,0x01,0x08,0x48,0x01,0x08,0x68,0x01,0x08,0x38,0x03,0x08,0x18,0x01,0x08,0x1E,0x00,0x18,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};/*去*/
u8table6[]=
{
0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x04,0x00,0x00,0x18,0x00,0x00,0xE2,0x07,
0xFF,0x82,0x02,0x00,0x02,0x02,0x00,0x02,0x02,0x08,0x04,0x02,0x0E,0x04,0x02,0x09,
0x08,0x02,0x08,0xC8,0x7F,0xF0,0x70,0x3F,0xF0,0x30,0x02,0x08,0x70,0x02,0x09,0x88,
0x02,0x0B,0x0C,0x02,0x0E,0x0C,0x02,0x98,0x04,0x07,0x08,0x06,0x06,0x00,0x06,0x00,
0x00,0x04,0x00,0x00,0x04,0x00,0x00,0x00,
};/*皮*/
u8table7[]=
{
0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x08,0x02,0x00,0x10,0x02,0x00,0x21,0x02,0x00,
0x41,0x82,0x00,0xC1,0x42,0x01,0x91,0x32,0x03,0x11,0x1A,0x0E,0x11,0x02,0x38,0x11,
0x02,0x30,0x1F,0xFC,0x28,0x11,0x02,0x04,0x11,0x02,0x02,0x11,0x04,0x01,0x11,0x1A,
0x00,0xA1,0xE2,0x00,0xD1,0x42,0x00,0x62,0x02,0x00,0x62,0x02,0x00,0x30,0x04,0x00,
0x20,0x04,0x00,0x20,0x00,0x00,0x00,0x00,
};/*金*/u8table8[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x08,0x80,0x0C,0x31,0x00,0x04,0xD3,0xFC,0x07,0x96,0x84,0x24,0x5C,0x84,0x1C,0x58,0x84,0x04,0x68,0x84,0x05,0xCD,0xFC,0x06,0x06,0x00,0x0C,0x02,0x02,0x16,0x00,0x02,0x11,0xFF,0x84,0x11,0x00,0x08,0x13,0x00,0x10,0x1D,0x47,0xE0,0x11,0x3F,0x00,0x11,0x20,0x20,0x11,0x00,0x10,0x11,0xFF,0xD8,0x23,0xFF,0x8C,0x00,0x00,0x06,0x00,0x00,0x00,
};/*额*/u8table9[]=
{
0x00,0x00,0x00,0x01,0x7C,0x12,0x54,0x94,0x54,0x90,0x55,0xB2,0x56,0xB1,0x7C,0xDE,0x54,0xD0,0x54,0x90,0x55,0x14,0x54,0x32,0x7C,0x19,0x00,0x00,0x00,0x00,0x00,0x00,};/*累*/
u8table10[]=
{
0x08,0x01,0x08,0x02,0x08,0x0C,0xFF,0xF0,0x08,0x02,0x08,0x01,0x0F,0xFE,0x00,0x00,
0x00,0x00,0x1F,0xFE,0x10,0x04,0x10,0x04,0x10,0x04,0x1F,0xFE,0x00,0x00,0x00,0x00,};/*加*/
//显示一个汉字
//
u8lcdoneHZ(u8x,u8y,unsignedchar*code)
{
u8temp,t,y0,t1;
u8table[72];
y0=y;
POINT_COLOR=RED;for(t=0;t<72;t++)
{
temp=*code++;
for(t1=0;t1<8;t1++)
{
if(temp&0x80)LCD_DrawPoint(x,y);
temp<<=1;
y++;
if((y-y0)==24)
{
y=y0;x++;break;
}
}
}
u8lcdone16HZ(u8x,u8y,unsignedchar*code)
{
u8temp,t,y0,t1;
u8table[32];
y0=y;
POINT_COLOR=RED;for(t=0;t<32;t++)
{
temp=*code++;
for(t1=0;t1<8;t1++)
{
if(temp&0x80)LCD_DrawPoint(x,y);temp<<=1;
y++;
if((y-y0)==16)
{
y=y0;
x++;break;
}
}
}
}
voidHZ_show(void)
{
lcdoneHZ(30,50,table1);//重量lcdoneHZ(54,50,table2);lcdoneHZ(30,80,table3);//单价lcdoneHZ(54,80,table4);
lcdoneHZ(30,110,table7);//金额
lcdoneHZ(54,110,table8);//金额
lcdoneHZ(30,140,table5);//去皮lcdoneHZ(54,140,table6);
voidHZ_show_moy(void)
{lcdone16HZ(54+25+32+30,115,table9);lcdone16HZ(54+25+32+30+17,115,table10);
}
#ifndef__LCDFOT_H
#define__LCDFOT_H
#include"sys.h"
u8lcdoneHZ(u8x,u8y,unsignedchar*code);//显示24位汉字
u8lcdone16HZ(u8x,u8y,unsignedchar*code);//显示16位汉字
voidHZ_show(void);
voidHZ_show_moy(void);
#endif
#include"hx711.h"
#include"delay.h"
#include"usart.h"
voidHX711_Init(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//使能PE端口时钟
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIOB.5
//LED0-->PB.5端口配置//推挽输出//IO口速度为50MHz//根据设定参数初始
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIOB.5
//LED0-->PB.5端口配置//上拉输入
//IO口速度为50MHz
//根据设定参数初始
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6);
}
u32HX711_read(void)
{
u32count;
u8i;
delay_us
(1);
HX711_SCL=0;
count=0;
while(HX711_DOUT);
for(i=0;i<24;i++)
{
HX711_SCL=1;count=count<<1;
HX711_SCL=0;if(HX711_DOUT)count++;
}
HX711_SCL=1;count=count^0x800000;delay_us
(1);
HX711_SCL=0;
returncount;
}
//得到的质量单位是g克
floatHX711_count(void)
{
u8a;
u32adc,wei;
floatvol;
for(a=10;a>0;a--)
{adc=HX711_read();
wei=wei+adc;
}
wei=wei/10;
printf("hx711值:
%d\r\n",wei);
vol=((float)(wei-7431260)/1118)-1.8-1.55;//注意改参数
wei=0;
printf("重量=%f\r\n",vol);
returnvol;
}
#ifndef__HX711_H
#define__HX711_H
#include"sys.h"
#defineHX711_SCLPAout(5)
#defineHX711_DOUTPAin(6)
voidHX711_Init(void);
u32HX711_read(void);
floatHX711_count(void);
#endif
#include"lcd.h"
#include"stdlib.h"
#include"font.h"
#include"usart.h"
#include"delay.h"
////////////////////////////////////////////////////////////////////////////////////本程序只供学习使用,未经作者许可,不得用于其它任何用途
//2.4/2.8寸/3.5寸TFT液晶驱动
//支持驱动IC型号包括:
ILI9341/ILI9325/RM68042/RM68021/ILI9320/ILI9328/LGDP4531/LGDP4535/SPFD5408/SSD1289/1505/B505/C505等
//正点原子@ALIENTEK//*****************************************************************************
//V1.2修改说明
//支持了SPFD5408的驱动,另外把液晶ID直接打印成HEX格式.方便查看LCD驱动IC.//V1.3
//加入了快速IO的支持
//修改了背光控制的极性(适用于V1.8及以后的开发板版本)
//对于1.8版本之前(不包括1.8)的液晶模块,请修改LCD_Init函数的LCD_LED=1;为
LCD_LED=1;
//V1.4//修改了LCD_ShowChar函数,使用画点功能画字符。
//加入了横竖屏显示的支持
//V1.520110730
//1,修改了B505液晶读颜色有误的bug.
//2,修改了快速IO及横竖屏的设置方式.
//V1.620111116
//1,加入对LGDP4535液晶的驱动支持
//V1.720120713
//1,增加LCD_RD_DATA函数
//2,增加对ILI9341的支持
//3,增加ILI9325的独立驱动代码
//4,增加LCD_Scan_Dir函数(慎重使用)
//6,另外修改了部分原来的函数,以适应9341的操作//V1.820120905
//1,加入LCD重要参数设置结构体lcddev
//2,加入LCD_Display_Dir函数,支持在线横竖屏切换
//V1.920120911
//1,新增RM68042驱动(ID:
6804),但是6804不支持横屏显示!
!
原因:
改变扫描方式,//导致6804坐标设置失效,试过很多方法都不行,暂时无解。
//V2.020120924
//在不硬件复位的情况下,ILI9341的ID读取会被误读成9300,修改LCD_Init,将无法识别//的情况(读到ID为9300/非法ID),强制指定驱动IC为ILI9341,执行9341的初始化。
//V2.120120930
//修正ILI9325读颜色的bug。
//V2.220121007
//修正LCD_Scan_Dir的bug。
//////////////////////////////////////////////////////////////////////////////////
//LCD的画笔颜色和背景色
u16POINT_COLOR=0x0000;//画笔颜色
u16BACK_COLOR=0xFFFF;//背景色
//管理LCD重要参数
//默认为竖屏
_lcd_devlcddev;
//写寄存器函数
//regval:
寄存器值
voidLCD_WR_REG(u16regval)
{
LCD->LCD_REG=regval;//写入要写的寄存器序号
}
//写LCD数据
//data:
要写入的值
voidLCD_WR_DATA(u16data)
{
LCD->LCD_RAM=data;
}
//读LCD数据
//返回值:
读到的值
u16LCD_RD_DATA(void)
{
returnLCD->LCD_RAM;
}
//写寄存器
//LCD_Reg:
寄存器地址
//LCD_RegValue:
要写入的数据
voidLCD_WriteReg(u8LCD_Reg,u16LCD_RegValue)
{
LCD->LCD_REG=LCD_Reg;//写入要写的寄存器序号LCD->LCD_RAM=LCD_RegValue;//写入数据
}
//读寄存器
//LCD_Reg:
寄存器地址
//返回值:
读到的数据
u16LCD_ReadReg(u8LCD_Reg)
{
LCD_WR_REG(LCD_Reg);//写入要读的寄存器序号
delay_us(5);
returnLCD_RD_DATA();//返回读到的值
}
//开始写GRAM
voidLCD_WriteRAM_Prepare(void)
{
LCD->LCD_REG=lcddev.wramcmd;
}
//LCD写GRAM
//RGB_Code:
颜色值
voidLCD_WriteRAM(u16RGB_Code)
{
LCD->LCD_RAM=RGB_Code;//写十六位GRAM
}
//从ILI93xx读出的数据为GBR格式,而我们写入的时候为RGB格式。
//通过该函数转换
//c:
GBR格式的颜色值
//返回值:
RGB格式的颜色值
u16LCD_BGR2RGB(u16c)
{
u16r,g,b,rgb;b=(c>>0)&0x1f;g=(c>>5)&0x3f;
r=(c>>11)&0x1f;rgb=(b<<11)+(g<<5)+(r<<0);
return(rgb);
}
//当mdk-O1时间优化时需要设置
//延时i
voidopt_delay(u8i)
{
while(i--);
}//读取个某点的颜色值//x,y:
坐标//返回值:
此点的颜色u16LCD_ReadPoint(u16x,u16y)
{u16r=0,g=0,b=0;if(x>=lcddev.widt
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 电子秤 stm32 程序