我在设计过程中遇到了问题,超声波测距模块和蓝牙分别可以单独正常运行,但是把他们放在一起超声波就接收不到信号,不能完成测距,我个人认为是定时器的复用导致问题,但是由于了解不足,不知道如何解决这个问题,还请大神帮我看看。
单片机源程序如下:
- #include <reg52.h>
- #include <1602.h>
- #include <intrins.h>
- #include <math.h>
- #include <stdlib.h>
- #include <string.h>
- #define VELOCITY_26C 3485 //26摄氏度时的声速,声速V= 331.5 + 0.607*温度;
- #define uchar unsigned char
- #define uint unsigned int
- sbit INPUT = P1^0; //回声接收端口
- sbit OUTPUT = P1^1; //超声触发端口
- sbit beep = P2^3 ; // 蜂鸣器
- sbit ds = P2^2;
- sbit RS = P3^5;
- sbit LCDEN = P3^4;
- long int distance=0; //距离变量
- long int thickness=0;
- char pass[3]; // 传递数据位
- uchar table[]="thk : ";
- uchar table0[]="temp: ";
- uchar count;
- extern void initLCD();
- extern void write_date(uchar date);
- extern void write_com(uchar com);
- extern void delay(uint x);
- //LCD1602
- void _nop_(void);
- void delayUs()
- {
- _nop_();
- }
- void delayMs(uint a)
- {
- uint i, j;
- for(i = a; i > 0; i--)
- for(j = 100; j > 0; j--);
- }
- void writeData(uchar dat)
- {
- RS = 1;
- P0 = dat;
- LCDEN = 1;
- delayUs();
- LCDEN = 0;
- delayMs(1);
- }
-
- void writeComm(uchar comm)
- {
- RS = 0;
- P0 = comm;
- LCDEN = 1;
- delayUs();
- LCDEN = 0;
- delayMs(1);
- }
-
- void writeString(uchar * str, uchar length)
- {
- uchar i;
- for(i = 0; i < length; i++)
- {
- writeData(str[i]);
- }
- }
- //HC-05
- void UART_Init()
- {
- TMOD = 0x20;
- TH1 = 0xfd;
- TL1 = 0xfd; // 波特率9600
- SM0 = 0;
- SM1 = 1; // 串口工作方式1 10位异步
- REN = 1; // 串口允许接收
- TR1 = 1;
- EA = 1;
- ES = 1; // 启用串口中断
- }
- // 发送单个字符函数
- void UART_SendChar(char ch)
- {
- SBUF = ch;
- while (!TI); // 等待发送完成
- TI = 0; // 清除发送中断标志
- }
- // 发送字符串函数
- void UART_SendString(char* str)
- {
- while (*str != '\0')
- {
- UART_SendChar(*str);
- str++;
- }
- }
- //DS18B20
- void dsInit()
- {
-
- unsigned int i;
- ds = 0;
- i = 100;
- while(i>0) i--;
- ds = 1;
- i = 4;
- while(i>0) i--;
- }
-
- void dsWait()
- {
- unsigned int i;
- while(ds);
- while(~ds);
- i = 4;
- while(i > 0) i--;
- }
- bit readBit()
- {
- unsigned int i;
- bit b;
- ds = 0;
- i++;
- ds = 1;
- i++; i++;
- b = ds;
- i = 8;
- while(i>0) i--;
- return b;
- }
- unsigned char readByte()
- {
- unsigned int i;
- unsigned char j, dat;
- dat = 0;
- for(i=0; i<8; i++)
- {
- j = readBit();
-
- dat = (j << 7) | (dat >> 1);
- }
- return dat;
- }
- void writeByte(unsigned char dat)
- {
- unsigned int i;
- unsigned char j;
- bit b;
- for(j = 0; j < 8; j++)
- {
- b = dat & 0x01;
- dat >>= 1;
-
- if(b)
- {
- ds = 0; i++; i++;
- ds = 1;
- i = 8; while(i>0) i--;
- }
- else
- {
- ds = 0;
- i = 8; while(i>0) i--;
- ds = 1;
- i++; i++;
- }
- }
- }
- void sendChangeCmd()
- {
- dsInit();
- dsWait();
- delayMs(1);
- writeByte(0xcc);
- writeByte(0x44);
- }
- void sendReadCmd()
- {
- dsInit();
- dsWait();
- delayMs(1);
- writeByte(0xcc);
- writeByte(0xbe);
- }
- int getTmpValue()
- {
- unsigned int tmpvalue;
- int value;
- float t;
- unsigned char low, high;
- sendReadCmd();
-
- low = readByte();
- high = readByte();
-
- tmpvalue = high;
- tmpvalue <<= 8;
- tmpvalue |= low;
- value = tmpvalue;
-
- t = value * 0.0625;
-
- value = t * 100 + (value > 0 ? 0.5 : -0.5); //大于0加0.5, 小于0减0.5
- return value;
- }
- void display_tmp(int v)
- {
- unsigned char count;
- unsigned char datas[] = {0, 0, 0, 0, 0};
- unsigned int tmp = abs(v);
- datas[0] = tmp / 10000;
- datas[1] = tmp % 10000 / 1000;
- datas[2] = tmp % 1000 / 100;
- datas[3] = tmp % 100 / 10;
- datas[4] = tmp % 10;
- writeComm(0xc0+6);
- if(v < 0)
- {
- writeString("- ", 2);
- }
- else
- {
- writeString("+ ", 2);
- }
- if(datas[0] != 0)
- {
- writeData('0'+datas[0]);
- }
- for(count = 1; count != 5; count++)
- {
- writeData('0'+datas[count]);
- if(count == 2)
- {
- writeData('.');
- }
- }
- }
- //HC-SR04
- void delayt(uint x)
- {
- uchar j;
- while(x-- > 0)
- {
- for(j = 0;j < 125;j++)
- {
- ;
- }
- }
- }
- void Init_MCU(void)//初始化单片机函数
- {
- TMOD = 0x01;
- TL0 = 0x66;
- TH0 = 0xfc; //1ms
- ET0 = 1; //开定时器2
- EA = 1; //总中断使能
- }
- void Init_Parameter(void)
- {
- OUTPUT =1;
- INPUT = 1;
- count = 0;
- distance = 0;
- }
- void display_char(uchar *point,uchar address)//显示字符串
- {
- uchar i;
- write_com(0x80 + address);
- for(i = 0;i < 16; i++)
- {
- write_date(*point);
- point++;
- }
- }
- void display_dis(int number,uchar address)//显示距离
- {
- uchar b,c,d,e;
- b= (number / 1000);
- c= (number / 100) % 10;
- d = (number / 10) % 10;
- e = number % 10;
- write_com(0x80 + address);
- write_date(b + 48);
- write_date(c + 48);
- write_date(d + 48);
- write_date(46); //小数点的ASCII
- write_date(e + 48);
- write_date(99); //"c"的ASCII
- write_date(109); //"m"的ASCII
- write_date(32); write_date(32); //空格的ASCII
- }
- void Trig_SuperSonic(void)//出发声波
- {
- OUTPUT = 1;
- delayt(1);
- OUTPUT = 0;
- }
- void Measure_Distance(void)//计算距离函数
- {
- uchar l;
- uint h,y;
- TR0 = 1;
- while(INPUT)
- {
- ;
- }
- TR0 = 0;
- l = TL0;
- h = TH0;
- y = (h << 8) + l;//回声信号时间差
- y = y - 0xfc66;//单位是us
- distance = y + 1000 * count;//计算总时间
- TL0 = 0x66;
- TH0 = 0xfc;
- delayt(30);
- distance = VELOCITY_26C * distance / 20000;
- thickness=210-distance;
-
- }
- void main(void)
- {
-
- initLCD();
- Init_MCU();
- Init_Parameter();
- sendChangeCmd();
- // UART_Init(); // 波特率9600
-
- writeComm(0xc0);
- writeString(table0, 8);
- display_char(table,0x00);
- beep=1;
- while(1)
- {
- ES=0;
- delayMs(1000); //温度转换时间需要750ms以上
- writeComm(0xc0);
- display_tmp(getTmpValue());
- sendChangeCmd();
-
- Trig_SuperSonic(); //触发超声波发射
- while(INPUT == 0) //等待回声
- {
- ;
- }
- Measure_Distance(); //计算脉宽并转换为距离
- display_dis(thickness,0x07); //显示厚度
-
- // if(thickness>=100)
- // {
- // ES=1;TR0=0;
- // UART_SendString("The thickness exceeds the warning value!");
- // ES=0;
- // beep=0;
- // }
- Init_Parameter(); // 参数重新初始化
- delayt(100);
- }
- }
- void timer0 (void) interrupt 1 //T0中断处理函数
- {
- TF0 = 0;
- TL0 = 0x66;
- TH0 = 0xfc;
- count++;
- if(count == 18)//超声波回声脉宽最多18ms
- {
- TR0 =0;
- TL0 = 0x66;
- TH0 = 0xfc;
- count = 0;
- }
- }
- void UART_INTERRUPT() interrupt 4
- {
- static int i = 0; // 静态局部变量,被初始化一次
- if (RI) { // 中断处理函数中,对于接收中断的响应
- RI = 0; // 清除接收中断标志位
- pass[i] = SBUF;
- i++;
- if (i == 2) {
- pass[i] = '\0'; // 添加字符串结束符
- i = 0;
- }
- }
- if (TI); // 处理发送中断
- }注释部分是蓝牙
复制代码
|