电子称DIY(贴应变片+写代码)
第一步、应变片介绍
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第二部分:应变片如何测量应变力的(对应的R1\R2\R3\R4实际上的贴片方法):
电桥的四个臂上接工作应变片,都参与机械变形,同处一个温度
场,温度影响相互抵消,电压输出灵敏度高。当 4 个应变片的材料、
阻值都想同时,可推导出以下公式:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第三部分、原理图(带器件参数):
部分图片来自网络
(注:以上原理图中的R1\R2\R3\R4均为图二对应的电阻。应和图二所示一致。)
第四部分、程序代码:
51代码采集部分:
1 /*============================================= 2 AD24位数值提取函数 3 一般提取6、7、8位数据 4 =============================================*/ 5 unsigned long ReadCount(void) 6 { 7 // unsigned long Count; 8 unsigned char i; 9 ADSK=0; 10 Count=0; 11 while(ADDO); 12 for (i=0;i<24;i++) 13 { 14 ADSK=1; 15 Count=Count<<1; 16 ADSK=0; 17 if(ADDO) Count++; 18 } 19 ADSK=1; 20 Count=Count^0x800000;//抑或 21 ADSK=0; 22 return(Count); 23 }
STM32代码:
1 /*============================================= 2 AD24位数值提取函数 3 一般提取6、7、8位数据 4 =============================================*/ 5 //#define ADD0_R GPIO_ResetBits(GPIOA,GPIO_Pin_1) 6 //#define ADD0_S GPIO_SetBits(GPIOA,GPIO_Pin_1) 7 //#define ADSK_R GPIO_ResetBits(GPIOA,GPIO_Pin_2) 8 //#define ADSK_S GPIO_SetBits(GPIOA,GPIO_Pin_2) 9 10 unsigned long ADC_ReadCount(void) 11 { 12 unsigned char i; 13 unsigned long Count; 14 Count=0; 15 GPIO_ResetBits(GPIOC,GPIO_Pin_5);//ADSK=0; 16 delay_us(20); 17 while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_4)==1)// while(ADDO); 18 delay_us(20); 19 for (i=0;i<24;i++) 20 { 21 GPIO_SetBits(GPIOC,GPIO_Pin_5);//ADSK=1; 22 delay_us(20); 23 Count=Count<<1; 24 GPIO_ResetBits(GPIOC,GPIO_Pin_5);//ADSK=0; 25 delay_us(20); 26 if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_4)==1) Count++; 27 delay_us(20); 28 } 29 GPIO_SetBits(GPIOC,GPIO_Pin_5);//ADSK=1; 30 delay_us(20); 31 Count=Count^0x800000;//抑或 32 GPIO_ResetBits(GPIOC,GPIO_Pin_5);//ADSK=0; 33 delay_us(20); 34 return(Count); 35 }
完整测试代码:
51带串口收发采集书籍的完整代码:
1 #include<reg52.h> 2 #include <math.h> 3 #define uint unsigned int 4 #define uchar unsigned char 5 uchar flag ,a ,i; 6 uchar code table[]="I Get "; 7 /////////////////////////////////////// 8 sbit ADDO = P1^5; 9 sbit ADSK = P1^0; 10 unsigned long Count; 11 void delay_50ms(uint t); 12 /*============================================= 13 AD24位数值提取函数 14 一般提取6、7、8位数据 15 =============================================*/ 16 unsigned long ReadCount(void) 17 { 18 // unsigned long Count; 19 unsigned char i; 20 ADSK=0; 21 Count=0; 22 while(ADDO); 23 for (i=0;i<24;i++) 24 { 25 ADSK=1; 26 Count=Count<<1; 27 ADSK=0; 28 if(ADDO) Count++; 29 } 30 ADSK=1; 31 Count=Count^0x800000;//抑或 32 ADSK=0; 33 return(Count); 34 } 35 /*============================================ 36 求平均值函数 37 ===============================================*/ 38 long averageValue(uchar times) 39 { 40 uchar i; 41 long sum = 0; 42 for (i = 0; i < times; i++) 43 { 44 sum += ReadCount(); 45 } 46 47 return sum / times; 48 } 49 /*=============================================== 50 串口初始化函数 51 ===============================================*/ 52 void init() 53 { 54 TMOD=0x20; 55 TH1=0xfd; 56 TL1=0xfd; 57 TR1=1;REN=1;SM0=0;SM1=1;EA=1;ES=0; 58 59 } 60 void delay_50ms(uint t) 61 { 62 uint j; 63 for( ;t>0;t--) 64 for(j=6245;j>0;j--); 65 66 } 67 68 void main() 69 { 70 71 unsigned long num; 72 uchar s[9]; 73 init(); 74 while(1) 75 { 76 /************************************/ 77 num=averageValue(20); 78 num=num-8650000; 79 num=num*416; 80 for(i=8;i>0;i--) 81 { 82 s[i-1]=(uchar)num%10+0x30; 83 if(num<1) 84 s[i-1]=0x30; 85 num=num /10; 86 } 87 s[8]='G'; 88 /************************************/ 89 /*================================================== 90 long val = (averageValue() - _offset);//_offset 91 return (float) val / _scale;//再.H函数中设置scale = 416.f 92 93 =====================================================*/ 94 95 96 97 ES=0;//关闭串口中断 98 for(i=0;i<9;i++) 99 { 100 SBUF=s[i];//通过串口发送数据 101 while(!TI);//检查是否发送数据发送完毕 102 TI=0; 103 } 104 delay_50ms(4); 105 } 106 107 } 108 109 void set() interrupt 4 110 { 111 RI=0; 112 a=SBUF; 113 flag=1; 114 115 }
STM32完整代码:
c代码.c 文件
1 #include<reg52.h> 2 #include <math.h> 3 #include <stdio.h> 4 #define uint unsigned int 5 #define uchar unsigned char 6 uchar flag ,a ,i; 7 uchar code table[]="I Get "; 8 /////////////////////////////////////// 9 sbit ADDO = P1^5; 10 sbit ADSK = P1^0; 11 unsigned long Count; 12 void delay_50ms(uint t); 13 /*============================================= 14 AD24位数值提取函数 15 一般提取6、7、8位数据 16 =============================================*/ 17 unsigned long ReadCount(void) 18 { 19 // unsigned long Count; 20 unsigned char i; 21 ADSK=0; 22 Count=0; 23 while(ADDO); 24 for (i=0;i<24;i++) 25 { 26 ADSK=1; 27 Count=Count<<1; 28 ADSK=0; 29 if(ADDO) Count++; 30 } 31 ADSK=1; 32 Count=Count^0x800000;//抑或 33 ADSK=0; 34 return(Count); 35 } 36 /*============================================ 37 求平均值函数 38 ===============================================*/ 39 long averageValue(uchar times) 40 { 41 uchar i; 42 long sum = 0; 43 for (i = 0; i < times; i++) 44 { 45 sum += ReadCount(); 46 } 47 48 return sum / times; 49 } 50 /*=============================================== 51 串口初始化函数 52 ===============================================*/ 53 void init() 54 { 55 TMOD=0x20; 56 TH1=0xfd; 57 TL1=0xfd; 58 TR1=1;REN=1;SM0=0;SM1=1;EA=1;ES=0; 59 60 } 61 void delay_50ms(uint t) 62 { 63 uint j; 64 for( ;t>0;t--) 65 for(j=6245;j>0;j--); 66 67 } 68 69 void main() 70 { 71 72 unsigned long num; 73 uchar s[25]; 74 init(); 75 while(1) 76 { 77 /************************************/ 78 num=averageValue(20); 79 num=num-8650000; 80 num=num*416; 81 sprintf(s,"称得重量:%f g",num); 82 83 /************************************/ 84 /*================================================== 85 long val = (averageValue() - _offset);//_offset 86 return (float) val / _scale;//再.H函数中设置scale = 416.f 87 88 =====================================================*/ 89 90 91 92 ES=0;//关闭串口中断 93 for(i=0;i<25;i++) 94 { 95 SBUF=s[i];//通过串口发送数据 96 while(!TI);//检查是否发送数据发送完毕 97 TI=0; 98 } 99 delay_50ms(4); 100 } 101 102 } 103 104 void set() interrupt 4 105 { 106 RI=0; 107 a=SBUF; 108 flag=1; 109 110 }
.h文件:
1 #ifndef __AD24_H 2 #define __AD24_H 3 #include "stm32f10x.h" 4 #include "delay.h" 5 void ADC24_Configtion(void); 6 long averageValue(unsigned char times); 7 8 #endif
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
附录:(此处R1\R2\R3\R4不一定和第二部分的图2中的R1\R2\R3\R4对应)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------贴应变片-------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
贴应变片:
用锉刀和粗砂纸等工具将试件在钢板上的贴片位置的油污、漆层、锈迹、电镀层除去,再用细砂纸打磨成 45°交叉纹,之后用镊子镊起丙酮棉球将贴片处擦洗干净,至棉球洁白为止。
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------应变片的其他贴法:-----------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
应变片的其他贴法:
温度补偿: