绳式位移传感器51单片机程序
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #include<stdio.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit wei=P2^7;
- sbit duan=P2^6;
- sbit adwr=P3^6;
- sbit adrd=P3^7;
- uchar flag,a;
- uchar flag_uart,flag_time,flag_on,a,i,t0_num,ad_val;
- float ad_vo;
- uchar code table[]={ //定义共阴极数码管显示的16个字符的编码
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- void delay(uint x)
- {
- uint i,j;
- for(i=x;i>0;i--)
- for(j=110;j>0;j--);
- }
- void init() //初始化函数
- {
- TMOD=0x21;
- SCON=0x50;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- TH1=0xfd;
- TL1=0xfd;
- TR1=1;
- ET0=1;
- REN=1;
- SM0=0;
- SM1=1;
- EA=1;
- ES=1;
- }
- void display(uchar value)
- {
- uchar bai,shi,ge;
- bai=value/100;
- shi=(value%100)/10;
- ge=value%10;
- duan=1;
- P0=table[bai];
- duan=0;
- P0=0xff;
- wei=1;
- P0=0x7e;
- wei=1;
- delay(5);
-
- duan=1;
- P0=table[shi];
- duan=0;
- P0=0xff;
- wei=1;
- P0=0x7d;
- wei=1;
- delay(5);
- duan=1;
- P0=table[ge];
- duan=0;
- P0=0xff;
- wei=1;
- P0=0x7b;
- wei=1;
- delay(5);
- }
- uchar get_ad()
- {
- uchar adval;
- adwr=1;
- _nop_();
- adwr=0;
- _nop_();
- adwr=1;
- P1=0xff;
- adrd=1;
- _nop_();
- adrd=0;
- _nop_();
- adval=P1;
- adrd=1;
- return adval;
- }
- void main()
- {
- init();
- wei=1;
- P0=0x7f;
- wei=0;
- while(1)
- {
- if(flag_uart==1)
- {
- flag_uart=0;
- ES=0;
- TI=1;
- switch(flag_on)
- {
- case 0:puts("turn on ad!\n");
- TR0=1;
- break;
- case 1:printf("turn off ad!\n");
- TR0=0;
- break;
- case 2:puts("error!\n");
- break;
- }
- while(!TI);
- TI=0;
- ES=1;
- }
- if(flag_time==1)
- {
- flag_time=0;
- ad_val=get_ad();
- ad_vo=(float)ad_val*(5.0/256.0)*40.0;
- ES=0;
- TI=1;
- // printf("the displacement is %fmm\n",ad_vo);
- printf("%8f\n",ad_vo);
- while(!TI);
- TI=0;
- ES=1;
- }
- display(ad_val);
- }
- }
- void time()interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
tongxin.rar
(42.02 KB, 下载次数: 37)
|