domingo, 20 de enero de 2013

sr04

Code for 8 bit PICs

/*----------------------------------------------------- 
File:        hc-sr04_p8.pde
Author:      Edermar Dominguez <Edermar_12[_at_]hotmail.com>
Date:        31 Jan 2013
Changes by:  Oliver Göpferich <pinguplus[_at_]live.de>
Date:        Sat Mar 22 09:47:36 2014
Description: Measure the distance to an obstacle using
             the Ultrasonic sensor HC-SRC04
PIC:         PIC18F25k50
Should also work with any other 8 bit PIC18F
-----------------------------------------------------*/
 
int trig = 3;
int echo = 4;
unsigned long pulse;
float distance;
 
void setup() {
    Serial.begin(9600);
    pinMode (USERLED,OUTPUT);
 
    pinMode (echo,INPUT);      // Declare the echo pin as INPUT
    pinMode (trig,OUTPUT);     // Declare the trigger pin as OUTPUT
 
    digitalWrite(USERLED,LOW);
    }
 
 
// I'm using my own pulseIn() function, because builtin pulseIn()
// provides to high values with the 25k50
 
unsigned long myPulseIn(unsigned char pin, unsigned char state, unsigned long timeout)
{
 unsigned long width = 0; // keep initialization out of time critical area
 
 // convert the timeout from microseconds to a number of times through
 unsigned long numloops = 0;
 unsigned long maxloops = timeout / 10; //We have a microsecond by 10 loops (mean).
 
 
 // wait for any previous pulse to end
 while (digitalRead(pin) == state) {
  if (numloops++ == maxloops) {
   return 0;
    }
  }
 
 // wait for the pulse to start
 while (digitalRead(pin) != state)
  if (numloops++ == maxloops) {
   return 0;
    }
 
 // wait for the pulse to stop
 while (digitalRead(pin) == state) {
  width++;
  }
 
 //There will be some error introduced by the interrupt handlers.
 //At last loop, each interaction have 12us + 60us from digitalRead() instructions
 
   //return width * 12 + 60;
 
   return width * 9;
}
 
 
void medirdistance() {
    // set the trigger for 2 ms low
    digitalWrite(trig, LOW);
    delayMicroseconds(2);
 
    // Set the trigger for 15 ms high (minimum 10 ms)
    digitalWrite(trig, HIGH);
    delayMicroseconds(15);
 
    // Set the trigger low. This starts the measuring procedure.
    digitalWrite(trig, LOW);
 
    // Measure the length of the input signal
    pulse = myPulseIn(echo, HIGH, 3000);
 
    // The speed of sound is 29 cm/second (/29). The ultrasonic pulse travels
    // the distance to the obstacle and back (/2).
    distance = (float)(((pulse)/29.0/2.0));
 
    Serial.printf("Puls: %d\r\n", pulse);
    Serial.printf("distance: %f cm\r\n", distance);
 
    toggle(USERLED);
 
    // Just wait for a second
    delay(1000);
}
 
 
void loop() {
    //run repeatedly:
    medirdistance();
}

No hay comentarios:

Publicar un comentario