|
超声波模块支持GPIO和串口两种模式,一开始用GPIO模式- digitalWrite(TrigPin,HIGH);
- delayMicroseconds(50);
- digitalWrite(TrigPin,LOW);
- Time_Echo_us = pulseIn(EchoPin,HIGH,40000);
复制代码 结果发现在没有收到超声波信号的时候puseIn方法延时非常厉害,严重影响了其他传感器的测量,后来改用串口模式
- unsigned int hl = 0;
- unsigned int ll = 0;
- unsigned int Len_mm = 0;
- static uint32_t currentTime = 0;
- static uint32_t rcTime = 0;
- static uint16_t previousTime = 0;
- static uint16_t cycleTime = 0;
- void setup(){
- Serial.begin(9600);
- Serial3.begin(9600);
- }
- void loop(){
- if(currentTime>rcTime){//50Hz
- rcTime = currentTime + 20000;
- Serial3.write(0X55);
- }
- else{
- if(Serial3.available()>=2){
- hl=Serial3.read();
- ll=Serial3.read();
- Len_mm = (hl*256+ll)/10;
- if((Len_mm >1)&&(Len_mm<4000) ){
- Serial.print("OK:");
- Serial.print(Len_mm,DEC);
- Serial.println("cm");
- }else{
- Len_mm = 0;
- }
- Serial3.flush();
- }
- }
- currentTime = micros();
- cycleTime = currentTime - previousTime;
- previousTime = currentTime;
- }
复制代码 这段代码也是按mwc的传感器测量思路,测试频率50HZ
初步测试感觉比较成功,测量速度很快,下一步准备添加到MWC中 |
欢迎继续阅读楼主其他信息
|