go_away

Author Topic: AVR 324p programming make me crazy  (Read 812 times)

0 Members and 1 Guest are viewing this topic.

Offline AliaTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
AVR 324p programming make me crazy
« on: February 21, 2011, 01:07:41 PM »
As I said AVR 324p programming make me crazy. I am new for the microcontroller programming thing but I am trying my best. All what I am trying to do is using ultrasonic senosrs (SRF05) to detect any object. I donnot need to use PWM, but instead of that I used a delay. Trigger and echo signal result where satisfying (were checked using Oscilloscope) the problem was how I am ganna know the width of the echo signal. I am how the microcontroller will know ?? Some one gave me a suggestion to use a counter and delay but it didnot work. My code is below any one can help me ?

Code: [Select]
#include <avr/io.h>
#include <util/delay.h>

int main()
{
char time, count = 0;
DDRD = 0b00100001;
PORTD = 0x00;

while(1)
{
count = 0;

PORTD &= (1 << PD0); //  put 1 at PD0
_delay_ms(50);

PORTD &= (~(1 << PD0)); // put 0 at PD0
_delay_ms(50);

while ((PIND & (1 << PD2)) == 0)
;

while ((PIND & (1 << PD2)) != (1 << PD2))
{
_delay_us(10);
count++;
}
}
  return 0;
}


The second problem was with the servo motor (Futaba s3003). I need to deflect it to spesific angle but each time I am changing the duty cycle (1,1.5,2 ms) it goes to the same angle where the peroid is 20 ms ?? why

Offline hopslink

  • Robot Overlord
  • ****
  • Posts: 202
  • Helpful? 14
Re: AVR 324p programming make me crazy
« Reply #1 on: February 21, 2011, 06:47:47 PM »
It looks like you are trying to do the correct steps but there are a few issues with your code:
Code: [Select]
#include <avr/io.h>
#include <util/delay.h>

int main()
{
char time, count = 0;  //Echo pulse duration can be up to 30ms... - a count >255 (25.5ms) will overflow a char and wrap giving a false close reading
        // using a 16 bit int for count will ensure this never happens and you could also get more precise timing by using smaller 'ticks'
DDRD = 0b00100001;
PORTD = 0x00;

while(1)
{
count = 0;
               
                // output trigger pulse on PORTD,0
PORTD &= (1 << PD0); //  put 1 at PD0 - No it doesn't...  0 AND 1 = 0  ... use logic OR to set a bit X OR 1 = 1
_delay_ms(50);                           // Why so long? datasheet says minimum 10us so 20us should be plenty

PORTD &= (~(1 << PD0)); // put 0 at PD0 - Yes use AND to mask bits like this X AND 0 = 0
_delay_ms(50);                           // No delay here! - after 50ms you will have missed the Echo pulse... see the datasheet
               
                // wait for start of Echo pulse on PORTD,2
while ((PIND & (1 << PD2)) == 0)
                ;           // wait until PD2 == 1 - this is fine

                // measure the length of Echo pulse
while ((PIND & (1 << PD2)) != (1 << PD2))  // (while(PD2 != 1))... PD2 is ALWAYS 1 here - so this loop will NEVER iterate  use != 0
{
_delay_us(10);
count++;          // time the Echo pulse by counting 10us 'ticks'
}
               
                 // Add a delay to ensure a minimum of 50ms between pulses - see datasheet
                 // just delay 50ms for simplicity
                 // or calculate the minimum delay using your count value for faster readings
}
  return 0;
}


Offline AliaTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: AVR 324p programming make me crazy
« Reply #2 on: February 21, 2011, 09:39:55 PM »
Thanks a lot hopslink U highlight alot of thing I was missing. I will try it today

One more thing any suggestion for the servo ??

Offline AliaTopic starter

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: AVR 324p programming make me crazy
« Reply #3 on: February 25, 2011, 02:47:39 PM »
Yaaaaaaaaaaaaahooo it worked fine. Thanks hopslink

 


Get Your Ad Here

data_list