Pages: [1]   Go Down
Author Topic: Need explanation regarding Interrupts  (Read 778 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just bought a new ultrasonic sensor and i'm struggling at understanding the code.

Code:

#define TIMEOUT_OVERFLOW 1000
 
int TrigPin = 2;
int EchoPin = 3;
unsigned long ultrasoundDuration;
 
void setup()
{
  Serial.begin(9600);
 
}
 
void loop()
{
  unsigned char pin = 0;
  unsigned int time_flag = 0;
 
  pinMode(TrigPin, OUTPUT); //pin is output
  pinMode(EchoPin, INPUT); // pin is now input
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(2);
  digitalWrite(TrigPin, LOW);
  delayMicroseconds(10);
  digitalWrite(TrigPin, HIGH);
 
  // wait for a LOW pulse
  //ultrasoundDuration = pulseIn(EchoPin, HIGH);// will not work for HIGH pulse
 
  TCCR1A = 0x00;
  TCNT1 = 0x0000;
  TCCR1B = 0x01;
  pin = digitalRead(EchoPin);
  while(pin)
  {
    pin = digitalRead(EchoPin);
    time_flag++;
    if(time_flag > TIMEOUT_OVERFLOW)
      break;
  }
  TCCR1B = 0x00;
  ultrasoundDuration = TCNT1;
  ultrasoundDuration = ultrasoundDuration / 16;
  // get results
  Serial.print(ultrasoundDuration);
  Serial.print(" us, ");
  //Serial.print(ultrasoundDuration/148, DEC); //divide with 148 and you get inches
  //Serial.print(" inches, ");
  Serial.print(ultrasoundDuration*0.017, DEC); //divide with 58 and you get cm
  Serial.print(" cm");
  Serial.println();
  delay(100);
}

Whats "pin" doing in the whilestatement? And what does TCCR1A = 0x00, TCNT1 = 0x0000 and TCCR1B = 0x01 mean? Is there a tutorial about this? Couldnt find anything because I didnt know what I had to look for.

sincerely

bluebubble
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4058
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There aren't any interrupts actually happening here. The ATmega328P microcontroller has three Timer/Counters. This code uses Timer/Counter1 to measure the time between sending the ping and receiving the echo. TCNT1 is the timer register in the MCU, TCCR1A and TCCR1B are control registers associated with the timer. See the ATmega328P datasheet, section 16; there are also good writeups on timers on Nick Gammon's site and on Dean Camera's site.
« Last Edit: June 05, 2012, 03:21:29 pm by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Toledo, OH
Offline Offline
God Member
*****
Karma: 36
Posts: 510
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I just bought a new ultrasonic sensor and i'm struggling at understanding the code.

Whats "pin" doing in the whilestatement? And what does TCCR1A = 0x00, TCNT1 = 0x0000 and TCCR1B = 0x01 mean? Is there a tutorial about this? Couldnt find anything because I didnt know what I had to look for.

sincerely

bluebubble

I would suggest using a library to control the sensor as this code seems incomplete.  To toot my own horn, I suggest using the NewPing library: http://arduino.cc/forum/index.php/topic,106043.0.html  It's quite easy to implement and it works.  Also, I have working interrupt code in my development version that I'll be releasing soon.  Not that you really need to use interrupts to use a ping sensor, it doesn't sound like you really need it anyway, unless I'm missing something.

Tim
Logged

Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your help. I tried your library teckel, but it din't work. I also read both articels about the timer, but I still don't understand the purpose of

Code:
  TCCR1A = 0x00;
  TCNT1 = 0x0000;
  TCCR1B = 0x01;
 
  (later)
  TCCR1B = 0x00;

  ultrasoundDuration = TCNT1;

what do those 0x00 do?
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1271
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

0x as a prefix denotes a hexadecimal number. 0xff is 255 decimal. 0x00 is just zero.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4058
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  TCCR1A = 0x00;  //sets the counter for normal mode (as opposed to CTC or a PWM mode)
  TCNT1 = 0x0000;  //TCNT1 is the actual counter value, set it to zero
  TCCR1B = 0x01;  //sets the counter to count at the system clock frequency (e.g. 16MHz) and starts it counting
 
  (later)
  TCCR1B = 0x00;  //stops the counter

  ultrasoundDuration = TCNT1;  //assigns the count value to ultrasoundDuration
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Pages: [1]   Go Up
Jump to: