go_away

Author Topic: Generate 38 khz in ATmega 32  (Read 3939 times)

0 Members and 1 Guest are viewing this topic.

Offline spitfireTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Generate 38 khz in ATmega 32
« on: May 09, 2009, 10:40:05 PM »
I need to generate 38 khz frequency in ATmega 32 mcu in the timer 1 so that my TSOP sensor will work.
Please help me in writing the program for it.

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: Generate 38 khz in ATmega 32
« Reply #1 on: May 10, 2009, 05:48:52 AM »
Well, a few years back, when I was a beginner in robotics, I was stubborn to use Timer 2 for the 38kHz frequency, because Timer 1 would controll the motors. Using Timer 1 is fine, because it's a 16 bit timer, but Timer 2 is a 8 bit timer, so for the 38kHz frequency, I needed a 9.728 MHz cristal (PWM frequency is cristal/256). But here's a thought. You need to turn on the PWM, wait like 10 ms, read the sensor and so on. While waiting, the robot wasn't doing anything anyway. So, why bother using the timer when I can generate the signal in software during the wait time? Now I'm using the software method and it works perfectly.

Here is the code in Arduino:
Code: [Select]
int Obstacle;

int DoProxy(){
  int LeftVal=0;
  int RightVal=0;
  Obstacle = 0;
  IR38Write();
  LeftVal = digitalRead(LeftProxyPin);
  IR38Write();
  RightVal = digitalRead(RightProxyPin);
  if (LeftVal == LOW){
    Obstacle = Obstacle + 1;
  }
  if (RightVal == LOW){
    Obstacle = Obstacle + 2;
  }
  Serial.println ("LeftVal   RightVal");  // debug - remember to comment out
  Serial.print (LeftVal, DEC);            // debug - remember to comment out
  Serial.print ("             ");         // debug - remember to comment out
  Serial.println (RightVal, DEC);           // debug - remember to comment out
  Serial.println (Obstacle, DEC);         // debug - remember to comment out
  return Obstacle;
}

void IR38Write() {                   //this function will generate 38.461 kHz frequency, 50% duty cycle, for 10 ms
  for(int i = 0; i <= 384; i++) {  //repeat 384 times 26 us = 10.010 ms for a IR burst
    digitalWrite(IRpin, HIGH);      //turn IR LED pin High
    delayMicroseconds(12);        //for 12 us
    digitalWrite(IRpin, LOW);      //turn IR LED pin Low
    delayMicroseconds(12);        //for 12 us
  }
}

Now you will need to translate the Arduino functions into regular C statements, but I think the ideea is clear enough.
« Last Edit: May 10, 2009, 05:58:46 AM by Ro-Bot-X »
Check out the uBotino robot controller!

Offline spitfireTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Re: Generate 38 khz in ATmega 32
« Reply #2 on: May 10, 2009, 08:16:46 AM »
@Ro-Bot-X

Thank you for your post.I think is also a nice idea.

But is there anyone who could help me in using timer 1 of ATmega 32...?

 


Get Your Ad Here

data_list