用ttl转usb串口分别与传感器的VCC、RX、TX、GND连接电脑,通过串口指令测试:(具体看说明书)
打开激光: $0003260130&
开始测量: $00022123&
开始连续测量: $00022426&
结束连续测量: $0003260029&
经测试精度较高
#include"stc12c5a60s2.h"
#include"absacc.h"
#include"stdio.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit K1=P1^0;
sbit K2=P1^1;
sbit RS=P2^7;
sbit RW=P2^6;
sbit EN=P2^5;
sbit Bee=P3^7;
#define DATA_OUT P0
u8 Mode;
void delay_ms(u16 ms)
{
u8 i;
u16 j;
for(j=ms;j>0;j--)
{for(i=220;i>0;i--);}
}
void LCD_Write_Cmd(u8 Cmd)
{
RS=0;
DATA_OUT=Cmd;
delay_ms(5);
EN=1;
delay_ms(5);
EN=0;
}
void LCD_Write_Data(u8 Data)
{
RS=1;
DATA_OUT=Data;
delay_ms(5);
EN=1;
delay_ms(5);
EN=0;
}
void LCD_Display_String(u8 Position,u8 *s)
{
LCD_Write_Cmd(0x80+Position);
while(*s>0)
{
LCD_Write_Data(*s++);
delay_ms(5);
}
}
void LCD_Display_Number(u8 Position,u8 Data)
{
LCD_Write_Cmd(0x80+Position);
LCD_Write_Data(0x30+Data);
}
void LCD_Init(void)
{
RW=0;
LCD_Write_Cmd(0x38);
LCD_Write_Cmd(0x0c);
LCD_Write_Cmd(0x06);
LCD_Write_Cmd(0x01);
}
void Uart_Init(void)
{
PCON &= 0x7F;
AUXR = 0x40; //Set 1T Mode
SCON = 0x50;
TMOD = 0x20; //Set Timer1 as 8-bit auto reload mode
TH1 = TL1 = 0xFD; //Set auto-reload vaule
TR1 = 1; //Timer1 start run
ES = 1; //Enable UART interrupt
EA = 1; //Open master interrupt switch
}
void CopeSerialData(unsigned char ucData) //串口接收数据并处理显示,单次测量模式
{
static unsigned char ucRxBuffer[28];
static unsigned char ucRxCnt = 0;
ucRxBuffer[ucRxCnt++]=ucData;
if (ucRxBuffer[0]!=0x24) //0x24对应的ascii码为$,以此判断是否为字头
{
ucRxCnt=0;
return;
}
if (ucRxCnt < 28) {return;} //判断是否大于28字节数据
else
{
LCD_Write_Cmd(0xC0);
LCD_Write_Data(ucRxBuffer[20]);
LCD_Write_Data(ucRxBuffer[21]);
LCD_Write_Data('.');
LCD_Write_Data(ucRxBuffer[22]);
LCD_Write_Data(ucRxBuffer[23]);
LCD_Write_Data(ucRxBuffer[24]);
// LCD_Write_Data(ucRxBuffer[25]);
// LCD_Write_Data(ucRxBuffer[26]);
LCD_Write_Data('m');
ucRxCnt=0;
}
}
void main(void)
{
LCD_Init();
Uart_Init();
delay_ms(5);
Bee=0;
LCD_Display_String(0,"D.J-913104520221");
delay_ms(500);
Bee=1;
while(1)
{
if(K1==0)
{
delay_ms(20);
if(K1==0)
{
Mode=1;
TI=1;
printf("$0003260130&");
TI=0;
while(!K1);
Bee=0;
delay_ms(100);
Bee=1;
}
}
if(K2==0)
{
delay_ms(20);
if(K2==0)
{
Mode=0;
TI=1;
printf("$00022123&");
TI=0;
while(!K2);
Bee=0;
delay_ms(100);
Bee=1;
}
}
}
}
void Uart() interrupt 4
{
if(RI)
{
RI=0;
switch(Mode)
{
case 0: CopeSerialData(SBUF); break;
case 1: break;
}
}
}
我最怕的就是焊接了,每次在面包板上测试好好的,一焊接就是各种问题,技术不行,这次也不来,刚焊上屏幕都没亮。。。。。。后来反复检查(每次检查才能感觉到到万用表是多么重要)查出了几处错误,所以板子背面连线很丑