本設計是采用的惠斯特電橋接四個應變片的仿真,實例代碼和仿真原理圖如下
1.jpg (369.88 KB, 下載次數(shù): 59)
下載附件
2023-3-15 23:39 上傳
2.jpg (246.45 KB, 下載次數(shù): 46)
下載附件
2023-3-15 23:41 上傳
仿真通過改變滑動變阻器阻值實現(xiàn)
單片機源程序如下:
- #define#include<reg52.h>
- #include<intrins.h>
- #include <absacc.h>
- #include <math.h>
- #include<stdio.h>
- #define uchar unsigned char
- #define unit unsigned int
- #define DATA P0
- sbit lcden=P2^5;
- sbit lcdrw=P2^6;
- sbit lcdrs=P2^7;
- sbit feng=P3^4;
- sbit BYTE=P2^1;
- sbit BUSY=P2^2;
- //sbit CS=P2^2;
- sbit RC=P2^0;
- void init();
- void write_com(uchar com);
- void write_data(uchar date);
- void write_word(unsigned char *s);
-
- void DelayMS(unsigned int y) //延遲程序
- {
- unsigned int x;
- for(y;y>0;y--)
- for(x=110;x>0;x--);
- }
- void init(void) //LCD初始化程序
- {
- write_com(0x02);
- write_com(0x38);//顯示模式設置
- write_com(0x0c); //開顯示,不顯示光標,光標不閃爍
- write_com(0x06);//當讀或寫一個字符后地址指針加1,且光標加1,整屏顯示不移動
- write_com(0x01); //清屏
- DelayMS(50);
- }
- void write_com(uchar com)
- {
- lcdrs=0;//寫指令
- lcdrw=0;
- DATA=com;
- lcden=1;
- DelayMS(5);
- lcden=1;
- DelayMS(5);
- lcden=0;
- }
- void write_data(uchar date)
- {
- lcdrs=1;
- DATA=date;
- DelayMS(5);
- lcden=1;
- DelayMS(5);
- lcden=0;
- }
- void write_word(unsigned char *s)
- {
- while(*s>0)
- {
- write_data(*s);
- s++;
- }
- }
- void Display(int weight)
- {
- int bw,sw,gw,yw,ew;
- unit dat;
- dat=weight*100;
- bw=dat/10000; //取得百位數(shù)字
- sw=dat%10000/1000;//取得十位數(shù)字
- gw=dat%10000%1000/100;//取得個位數(shù)字
- yw=dat%10000%1000%100/10; //小數(shù)點后一位
- ew=dat%10000%1000%100%10; //小數(shù)點后二位
- // bw=dat/10000; //取得百位數(shù)字
- // sw=dat%10000/1000;//取得十位數(shù)字
- // gw=dat/100%10;//取得個位數(shù)字
- // yw=dat/10%10; //小數(shù)點后一位
- // ew=dat%10000; //小數(shù)點后二位
- write_com(0x89);
- write_data(0x30+bw);
- write_com(0x8a);
- write_data(0x30+sw);//數(shù)字+30得到該數(shù)字的LCD1602顯示碼
- write_com(0x8b);
- write_data(0x30+gw);//數(shù)字+30得到該數(shù)字的LCD1602顯示碼
- write_com(0x8c);
- write_data('.');
- write_com(0x8d);
- write_data(0x30+yw);//數(shù)字+30得到該數(shù)字的LCD1602顯示碼
- write_com(0x8e);
- write_data(0x30+ew);//數(shù)字+30得到該數(shù)字的LCD1602顯示碼
- write_com(0x8f);
- write_data(0x67); //顯示"g"符號,
- }
- void Display2(int weight)
- {
- int ww,qw,bw,sw,gw;
- unit dat;
- dat=weight;
- ww=dat/10000; //取得wan位數(shù)字
- qw=dat%10000/1000;//取得qian位數(shù)字
- bw=dat%10000%1000/100;//取得bai位數(shù)字
- sw=dat%10000%1000%100/10; //shi位
- gw=dat%10000%1000%100%10; //ge位
- write_com(0xC1);
- write_data(0x30+ww);
- write_com(0xC2);
- write_data(0x30+qw);//數(shù)字+30得到該數(shù)字的LCD1602顯示碼
- write_com(0xC3);
- write_data(0x30+bw);//數(shù)字+30得到該數(shù)字的LCD1602顯示碼
- write_com(0xC4);
- write_data(0x30+sw);//數(shù)字+30得到該數(shù)字的LCD1602顯示碼
- write_com(0xC5);
- write_data(0x30+gw);//數(shù)字+30得到該數(shù)字的LCD1602顯示碼
-
- }
- int ADS7825_Convert()
- {
- unit temp;
- uchar temp1,temp2;
- // CS=0;
- RC=1;
- RC=0;
- DelayMS(1);
- RC=1;
- while(BUSY==0);
- RC=1;
- BYTE=0;
- DelayMS(1);
- temp1=P1; //高八位
- BYTE=1;
- DelayMS(1);
- temp2=P1; //低八位
- // CS=1;
- // temp=temp1;
- temp=(temp1<<8)+temp2;
- return temp;
- }
- int DataProcess(result)
- {
- /*計算電壓值*/
- double voltage_value,weight_value;
- voltage_value = (unsigned long)(((double)result * 10 / 32767) * 1000 + 0.5);
- voltage_value = (unsigned long)(((double)result*0.0457-59.486));
- // weight_value = voltage_value/0.04;
- // return weight_value;
- return voltage_value;
- }
-
- void main(void)
- {
- int result,weight,result1,result2,result3,result4,result5,result6,result7,result8,result9,result10;
- //unsigned char str[8]={0};
- //DelayMS(500);
- lcdrs=0;
- lcdrw=0;
- lcden=0;
- feng=1;
- init();
- while(1)
- {
- result1=ADS7825_Convert();
- DelayMS(5);
- result2=ADS7825_Convert();
- DelayMS(5);
- result=(result1+result2)/2;
- result3=ADS7825_Convert();
- DelayMS(5);
- result=(result+result3)/2;
- result4=ADS7825_Convert();
- DelayMS(5);
- result=(result+result4)/2;
- result5=ADS7825_Convert();
- DelayMS(5);
- result=(result+result5)/2;
- result6=ADS7825_Convert();
- DelayMS(5);
- result=(result+result6)/2;
- result7=ADS7825_Convert();
- DelayMS(5);
- result=(result+result7)/2;
- result8=ADS7825_Convert();
- DelayMS(5);
- result=(result+result8)/2;
- result9=ADS7825_Convert();
- DelayMS(5);
- result=(result+result9)/2;
- result10=ADS7825_Convert();
- DelayMS(5);
- result=(result+result10)/2;
- DelayMS(100);
- Display2(result);
- result=(result1+result2+result3+result4+result5+result6+result7+result8+result9+result10)/10;
- weight=DataProcess(result);
- if(weight>=300)
- {
- feng=0;
- }
- else
- {
- feng=1;
- }
- write_com(0x80);
- write_word(" ");
- write_word("Weight:");
- Display(weight);
- DelayMS(500);
- }
- }
復制代碼
|