/*****************************
更新日期2017年2月28日
模拟串口参考stc官方程序
来自arduino实例
使用11.0592MHz内部RC;
VL53L0X:
SCL-->P3.3
SDA-->P3.2
串口:
P3.0 RXD
P3.1 TXD
*******************************/
#include"stc.h"
#include"i2c.h"
#include"VL53L0X.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
/***********全局变量定义**************/
bit flag;
uchar val1;
short val_s;
uchar gbuf[16];
uint acnt ;
uint scnt;
uint dist ;
uchar DeviceRangeStatusInternal;
#define BAUD 0xFE80 // 9600bps @ 11.0592MHz
//sfr AUXR = 0x8E;
sbit RXB = P3^0; //define UART TX/RX port
sbit TXB = P3^1;
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;
void UART_INIT();
void delay()
{
unsigned char i;
for(i=0; i<100; i++);
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void delay_ms(unsigned int time)
{
unsigned char
i;
for(i=1; i<=time; i++)
Delay1ms();
}
void uartsend_int16(short val)
{
uchar val1;
val1=val/10000;
while(!TEND);
TEND=0;
TBUF='0'+val1;
TING=1;
val1=val%10000/1000;
while(!TEND);
TEND=0;
TBUF='0'+val1;
TING=1;
val1=val%1000/100;
while(!TEND);
TEND=0;
TBUF='0'+val1;
TING=1;
val1=val%100/10;
while(!TEND);
TEND=0;
TBUF='0'+val1;
TING=1;
val1=val%10/1;
while(!TEND);
TEND=0;
TBUF='0'+val1;
TING=1;
}
void uartprintf(char *s)
{
int i=0;
while(s[i]!='\0')
{
while(!TEND);
TEND=0;
TBUF=s[i];
TING=1;
i++;
}
}
void uartprintenter()
{
while(!TEND);
TEND=0;
TBUF=0x0d;
TING=1;
while(!TEND);
TEND=0;
TBUF=0x0a;
TING=1;
}
void main()
{
uchar val = 0;
int cnt = 0;
TMOD = 0x00; //timer0 in 16-bit auto reload mode
AUXR = 0x80; //timer0 working at 1T mode
TL0 = BAUD;
TH0 = BAUD>>8; //initial timer0 and set reload value
TR0 = 1; //tiemr0 start running
ET0 = 1; //enable timer0 interrupt
PT0 = 1; //improve timer0 interrupt priority
EA = 1; //open global interrupt switch
UART_INIT();
uartprintf("start");
while (1)
{
vl53l0x_send(VL53L0X_REG_SYSRANGE_START, 0x01);
while (cnt < 100) { // 1 second waiting time max
delay_ms(10);
val = vl53l0x_read(VL53L0X_REG_RESULT_RANGE_STATUS);
if (val & 0x01) break;
cnt++;
}
if (val & 0x01) uartprintf("ready");
else uartprintf("not ready");
uartprintenter();
//vl53l0x_read_block(0x14, 12);
gbuf[0]=vl53l0x_read(0x14);
gbuf[7]=vl53l0x_read(0x14+7);
gbuf[6]=vl53l0x_read(0x14+6);
gbuf[9]=vl53l0x_read(0x14+9);
gbuf[8]=vl53l0x_read(0x14+8);
gbuf[11]=vl53l0x_read(0x14+11);
gbuf[10]=vl53l0x_read(0x14+10);
acnt = makeuint16(gbuf[7], gbuf[6]);
scnt = makeuint16(gbuf[9], gbuf[8]);
dist = makeuint16(gbuf[11], gbuf[10]);
DeviceRangeStatusInternal = ((gbuf[0] & 0x78) >> 3);
if(DeviceRangeStatusInternal==11&&dist>20&&dist<1200)
{
uartprintf("distance:");
uartsend_int16(dist);
uartprintf("mm");
uartprintenter();
}
else
{
uartprintf("error");
uartprintenter();
}
delay_ms(100);
}
}
void tm0() interrupt 1 using 1
{
if (RING)
{
if (--RCNT == 0)
{
RCNT = 3; //reset send baudrate counter
if (--RBIT == 0)
{
RBUF = RDAT; //save the data to RBUF
RING = 0; //stop receive
REND = 1; //set receive completed flag
}
else
{
RDAT >>= 1;
if (RXB) RDAT |= 0x80; //shift RX data to RX buffer
}
}
}
else if (!RXB)
{
RING = 1; //set start receive flag
RCNT = 4; //initial receive baudrate counter
RBIT = 9; //initial receive bit number (8 data bits + 1 stop bit)
}
if (--TCNT == 0)
{
TCNT = 3; //reset send baudrate counter
if (TING) //judge whether sending
{
if (TBIT == 0)
{
TXB = 0; //send start bit
TDAT = TBUF; //load data from TBUF to TDAT
TBIT = 9; //initial send bit number (8 data bits + 1 stop bit)
}
else
{
TDAT >>= 1; //shift data to CY
if (--TBIT == 0)
{
TXB = 1;
TING = 0; //stop send
TEND = 1; //set send completed flag
}
else
{
TXB = CY; //write CY to TX port
}
}
}
}
}
void UART_INIT()
{
TING = 0;
RING = 0;
TEND = 1;
REND = 0;
TCNT = 0;
RCNT = 0;
}
- 1
- 2
- 3
前往页