MAIN.C
- #include<reg52.h>
- #include<Ruler.h>//调用卡尺驱动程序
- unsigned char Styles;//模式(0:mm 1:inch)
- unsigned char Sign;//正负号(0:正 1:负)
- sfr AUXR = 0x8E; //辅助寄存器
- void Init_Com()//串口初始化 9600/n/8/1 MCU:STC12C2052
- {
- AUXR = AUXR|0x40; // T1, 1T Mode
- TMOD = 0x20;
- SCON = 0x50;
- TH1 = 0xDC;
- TL1 = TH1;
- PCON = 0x00;
- TR1 = 1;
- }
- void SendOneByte(unsigned char c)
- {
- SBUF = c;
- while(!TI);
- TI = 0;
- }
- void main() //主函数
- {
- unsigned int Tem1;
- Init_Com();
- while(!CP); //等待拉高
- while(1)
- {
- Tem1=Read();//读出卡尺数据
- SendOneByte(Styles);//模式 0:mm 1:inch
- SendOneByte(Sign);//正负号 0:正 1负
- SendOneByte(Tem1>>8);//有效读数高字节
- SendOneByte(Tem1);//有效读数低字节
- /*数据发送速度取决于卡尺数据输出速度*/
- }
- }
- Ruler.H
- #include<reg52.h>
- //卡尺接口
- sbit Data = P1^7;//数据
- sbit CP = P1^6;//时钟
- extern unsigned char Styles;//模式(0:mm 1:inch)
- extern unsigned char Sign;//正负号(0:正 1:负)
- unsigned char PR_DATA(unsigned char Data_Temp)//高低位交换
- {
- unsigned char DATA;
- DATA=((Data_Temp&0x01)<<7|(Data_Temp&0x02)<<5|(Data_Temp&0x04)<<3|(Data_Temp&0x08)<<1
- |(Data_Temp&0x10)>>1|(Data_Temp&0x20)>>3|(Data_Temp&0x40)>>5|(Data_Temp&0x80)>>7);
- return DATA;
- }
- unsigned int Read()//读出数据 应用: ReadData=Read();
- {
- unsigned int Dat,p=24,Temp,mm,inch;
- unsigned char SignBit,Hbit,Lbit;
- while(p--)//开始读数据
- {
- while(CP);
- while(!CP);//等待时钟拉高
- Dat=(Dat<<1)|Data;
- switch (p)
- {
- case 16: Lbit=PR_DATA(Dat);break;
- case 8: Hbit=PR_DATA(Dat);break;
- case 0: SignBit=PR_DATA(Dat);break;
- default:break;
- }
- }
- Temp=(Hbit<<8)+Lbit;
- if((SignBit==0x00)||(SignBit==0x80))Sign=0; //正数标志
- if((SignBit==0x10)||(SignBit==0x90))Sign=1; //负数标志
- if((SignBit==0x00)||(SignBit==0x10))Styles=0; //mm模式
- if((SignBit==0x80)||(SignBit==0x90))Styles=1;; //inch模式
- if(Styles==0)//模式判断
- {
- mm=Temp;
- return mm; //mm模式
- }
- else
- {
- inch=Temp/2; //inch模式
- return inch;
- }
- }
复制代码
|