找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2871|回复: 5
收起左侧

数字卡尺单片机电路及程序

[复制链接]
ID:198598 发表于 2017-5-9 13:49 | 显示全部楼层 |阅读模式


1

1

2

2

3

3

4

4


MAIN.C
  1. #include<reg52.h>
  2. #include<Ruler.h>//调用卡尺驱动程序
  3. unsigned char Styles;//模式(0:mm  1:inch)
  4. unsigned char Sign;//正负号(0:正  1:负)

  5. sfr AUXR        =   0x8E;   //辅助寄存器

  6. void Init_Com()//串口初始化  9600/n/8/1  MCU:STC12C2052
  7. {
  8.         AUXR = AUXR|0x40;  // T1, 1T Mode
  9.     TMOD = 0x20;
  10.     SCON = 0x50;
  11.     TH1 = 0xDC;
  12.     TL1 = TH1;
  13.     PCON = 0x00;
  14.     TR1 = 1;
  15. }

  16. void SendOneByte(unsigned char c)
  17. {
  18.     SBUF = c;
  19.     while(!TI);
  20.     TI = 0;
  21. }

  22. void main()       //主函数
  23. {
  24.         unsigned int Tem1;
  25.         Init_Com();
  26.         while(!CP);   //等待拉高
  27.         while(1)
  28.         {
  29.                 Tem1=Read();//读出卡尺数据

  30.                 SendOneByte(Styles);//模式  0:mm  1:inch
  31.                 SendOneByte(Sign);//正负号  0:正  1负
  32.                 SendOneByte(Tem1>>8);//有效读数高字节
  33.                 SendOneByte(Tem1);//有效读数低字节

  34.                 /*数据发送速度取决于卡尺数据输出速度*/
  35.         }
  36. }
  37. Ruler.H
  38. #include<reg52.h>

  39. //卡尺接口
  40. sbit Data = P1^7;//数据
  41. sbit CP = P1^6;//时钟

  42. extern unsigned char Styles;//模式(0:mm  1:inch)
  43. extern unsigned char Sign;//正负号(0:正  1:负)

  44. unsigned char PR_DATA(unsigned char Data_Temp)//高低位交换
  45. {
  46.     unsigned char DATA;
  47.     DATA=((Data_Temp&0x01)<<7|(Data_Temp&0x02)<<5|(Data_Temp&0x04)<<3|(Data_Temp&0x08)<<1
  48.         |(Data_Temp&0x10)>>1|(Data_Temp&0x20)>>3|(Data_Temp&0x40)>>5|(Data_Temp&0x80)>>7);
  49.     return DATA;
  50. }

  51. unsigned int Read()//读出数据  应用: ReadData=Read();
  52. {
  53.         unsigned int Dat,p=24,Temp,mm,inch;
  54.         unsigned char SignBit,Hbit,Lbit;
  55.         while(p--)//开始读数据
  56.         {
  57.                 while(CP);   
  58.                 while(!CP);//等待时钟拉高
  59.                 Dat=(Dat<<1)|Data;
  60.                 switch (p)
  61.                 {        
  62.                         case 16:        Lbit=PR_DATA(Dat);break;
  63.                         case 8:                Hbit=PR_DATA(Dat);break;
  64.                         case 0:                SignBit=PR_DATA(Dat);break;
  65.                         default:break;
  66.                 }
  67.         }
  68.         Temp=(Hbit<<8)+Lbit;

  69.         if((SignBit==0x00)||(SignBit==0x80))Sign=0;                //正数标志
  70.         if((SignBit==0x10)||(SignBit==0x90))Sign=1;                //负数标志
  71.         if((SignBit==0x00)||(SignBit==0x10))Styles=0;        //mm模式
  72.         if((SignBit==0x80)||(SignBit==0x90))Styles=1;;        //inch模式

  73.         if(Styles==0)//模式判断
  74.         {
  75.                 mm=Temp;
  76.                 return mm;         //mm模式
  77.         }
  78.         else
  79.         {
  80.                 inch=Temp/2; //inch模式
  81.                 return inch;
  82.         }
  83. }
复制代码

回复

使用道具 举报

ID:198598 发表于 2017-5-9 13:50 | 显示全部楼层
程序有毛病吗 ?为出来啥乱显示
回复

使用道具 举报

ID:198598 发表于 2017-5-9 13:53 | 显示全部楼层
程序有毛病吗
回复

使用道具 举报

ID:223038 发表于 2017-7-28 17:07 | 显示全部楼层
朋友,您的问题解决了么?
回复

使用道具 举报

ID:223038 发表于 2017-7-28 20:42 | 显示全部楼层
哥们,能加你好友吗?
回复

使用道具 举报

ID:198598 发表于 2017-8-8 16:47 | 显示全部楼层
zhangcan1101 发表于 2017-7-28 20:42
哥们,能加你好友吗?

没有 一直乱码 QQ335280843
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表