用51單片機和tlc2543實現電子稱的功能仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
TIM截圖20171221152508.png (44.82 KB, 下載次數: 25)
下載附件
proteus仿真圖
2017-12-21 15:25 上傳
附上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)
2017-12-21 17:05 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|