用51单片机和tlc2543实现电子称的功能仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
proteus仿真图
附上c代码
单片机源程序如下:
- #include "reg51.h"
- #include "TLC2543.h"
- #include "LCD_1602.h"
- #define D 1 //按键消抖延时
- #define RATE 0.2442 //AD采样值与实际质量换算比率
- #define MAX 99 //单价上限值
- void InqInit(void); //初始化外部中断0
- void DisplayWeight(uchar x, uchar y, uint weight, uint useless); //显示净重量
- void DisplayAD(uchar x, uchar y, uint ad); //显示AD采样值
- void DisplayPrice(uchar x, uchar y, uint price); //显示单价
- void DisplayTotal(uchar x, uchar y, uint price, uint weight, uint useless); //显示总金额
- volatile uint weight = 0; //重量
- volatile uint useless = 0; //皮重(去皮量)
- volatile bit startconversion = 1; //count计时启动停止控制变量
- uchar channl = 0x00; //通道选择
- void main(void)
- {
- uint count = 0; //控制转换速度
- LCD_Init(); //初始化LCD1602
- ADCInit(); //初始化TLC2543
- StartConversion(channl); //手动开启一次转换,选择通道0
- while(1)
- {
- if(startconversion)
- count++;
- if(count > 500)
- {
- count = 0;
- weight = StartConversion(channl); //读取采样值,开启下一次转换
- DisplayWeight(0,0,weight,useless); //显示净重量
- DisplayAD(9,0,weight); //显示AD采样值
- }
- }
- }
- void DisplayWeight(uchar x, uchar y, uint weight, uint useless) //显示净重量
- {
- static bit flag = 1; //框架显示次数控制
- if(flag)
- {
- Write_String(x,y,"W: g"); //显示“WT: g”
- flag = 0;
- }
- if(weight >= useless)
- {
- Write_Char(x+2,y,' '); //清除负号
- weight -= useless; //计算净重
- }
- else
- {
- Write_Char(x+2,y,'-'); //显示负号
- weight = useless - weight;
- }
- weight = weight * RATE + 0.5; //将AD采样值转换为对应质量
-
- Write_Char(x+3,y,weight / 1000 + 48);
- Write_Char(x+4,y,weight % 1000 / 100 + 48);
- Write_Char(x+5,y,weight % 100 / 10 + 48);
- Write_Char(x+6,y,weight % 10 + 48);
- }
- void DisplayAD(uchar x, uchar y, uint ad) //显示AD采样值
- {
- static bit flag = 1; //框架显示次数控制
- if(flag)
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
电子秤.rar
(96.99 KB, 下载次数: 54)
|