Author Topic: ATMega32 and sensors  (Read 3290 times)

0 Members and 1 Guest are viewing this topic.

Offline boblTopic starter

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
ATMega32 and sensors
« on: July 19, 2007, 02:04:44 PM »
Hello,

First of all thanks for the $50 robot tutorial, it certainly has kept me busy. I was well pleased to see the leds flashing indicating my first program actually was running on the microcontroller. My photovore robot is starting to take shape, but I am having a little problem with getting the sensors to work. This is undoubtedly because I decided to not follow the tutorial very accurately and have used the ATMega32 instead. What I want to do as an intermediate step is turn on a LED when the robot senses it should go left, and another LED when it's supposed to go right, based on the sensor readings. I cannot get this to work.

So I have a few questions:

What should configure_ports look like for the ATMega32, currently it is still:
Code: [Select]
void configure_ports(void)
{
DDRC = 0x00;  //configure all C ports for input
PORTC = 0x00; //make sure pull-up resistors are turned off
DDRD = 0xFF;  //configure all D ports for output
}
What is actually happening in this function, what are DDRC, PORTC, DDRD? I have not got problems with the c-code, but the microcontroller stuff is new to me.

The current code (see below) seems to mostly keep the left LED on, and the right one flickers now and again. This is independent of whether the sensors are plugged in!

Also I have measured the voltage on the PCx pins, and some of them (PD2, PD3, PD5) seem to be at ~3V, whereas the others, (PD0, PD1, PD4, PD6, PD7) are 0V. Is that normal?

I take it I have correctly put AREF at 5v. Does PORTC coincide with PC0...PC7, note that they are pin 22 to 29, pin 21 is PD7.

Further more, are the a2d.h etc. in the $50 tutorial hand-written or could you find similar functionality in the AVR library?

Finally, in the makefile, what is the highest I can savely set F_CPU to for the ATMega32, I have currently set it at 3686400 but I might as well make the best use of the chip if it can go higher without an external oscillator.

BTW, I have used the (parallel port) programmer (http://www.active-robots.com/products/controllr/atmel-avr.shtml, AVR ISP Downloader) with ponyprog, and it works very well (not at all slow).

Thanks for any help, and aplogies for the messy post!

Mark

My code:

Code: [Select]
while (1)
{
//store sensor data
sensor_right=a2dConvert8bit(PC4);
sensor_left=a2dConvert8bit(PC5);

//detects more light on left side of robot
if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold)
{
//go left
PORT_ON(PORTD, PD2);
PORT_OFF(PORTD, PD3);

//servo_left(44);
//servo_right(44);
}
//detects more light on right side of robot
else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold)
{
//go right

PORT_OFF(PORTD, PD2);
PORT_ON(PORTD, PD3);

//servo_left(25);
//servo_right(25);
}

//light is about equal on both sides
else if (sensor_left == 0 && sensor_right == 0)
{ //go straight
PORT_OFF(PORTD, PD2);
PORT_OFF(PORTD, PD3);

//servo_left(25);
//servo_right(44);
}
else
{
PORT_ON(PORTD, PD2);
PORT_ON(PORTD, PD3);

}
delay_cycles(10000);

}
return 0;
}

Note that PCn and PDn are just macros in the header file for n.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: ATMega32 and sensors
« Reply #1 on: July 22, 2007, 08:50:42 AM »
To configure ports, you must first decide which ports you want as analog inputs, and which ports you want as digital outputs.

Just let me know and Ill write some quick config code for you.

This post might help you some:
http://www.societyofrobots.com/robotforum/index.php?topic=1372.0

Offline boblTopic starter

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: ATMega32 and sensors
« Reply #2 on: July 23, 2007, 12:46:26 PM »
To configure ports, you must first decide which ports you want as analog inputs, and which ports you want as digital outputs.

Just let me know and Ill write some quick config code for you.

This post might help you some:
http://www.societyofrobots.com/robotforum/index.php?topic=1372.0


But surely the following lines take care of this.

Code: [Select]
DDRC  = 0x00;  //configure all C ports for input
PORTC = 0x00;  //make sure pull-up resistors are turned off
DDRD  = 0xFF;  //configure all D ports for output

Port C all for input (sensors), Port D all for digital output (LEDS). Port D behaves as expected. Port C is the problem. The reason why it is a problem is with the sensors. I put the left LED on when the robot is in a state where it should go left, and the right led if it should go right. But then it seems to behave funny, even though I wait for plenty cycles after every loop, one led flickers really fast, so the robot does not behave in a way I believe is consistent with the code. The only thing I can think of is that the battery cannot supply enough current for the sensors and the microchip, and it keeps resetting?

I would appreciate the quick config code if you could spare the time.

Regards,

Mark


Offline boblTopic starter

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: ATMega32 and sensors
« Reply #3 on: July 29, 2007, 06:04:40 AM »
Reading the ATMega23(L) documentation, I see that port C is not consistently used for ADC in their processors. It looks like I should be using port A instead! Hence it doesn't work. I cannot try it at the moment, but I am sure it will solve this problem. I just figured that they would be more consistent with their pins, and I did not know what I was soldering at the time.

Mark

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
Re: ATMega32 and sensors
« Reply #4 on: July 29, 2007, 07:20:50 AM »
hey Mark,

Quote
What is actually happening in this function, what are DDRC, PORTC, DDRD? I have not got problems with the c-code, but the microcontroller stuff is new to me.

these are registers that controll the behaviour of the I/O pins.
you write to control registers as if they were memory locations but the value stored there affects the opperation of different hardware modules in the microcontroller.

Quote
Also I have measured the voltage on the PCx pins, and some of them (PD2, PD3, PD5) seem to be at ~3V, whereas the others, (PD0, PD1, PD4, PD6, PD7) are 0V. Is that normal?

presuming these are set as outputs, try setting the logic on these pins:
Code: [Select]
DDRD  = 0xFF;  //configure all D ports for output
PORTD = 0x00; //set PD[0-7] to 0V
like this they should all read 0V.

Quote
Further more, are the a2d.h etc. in the $50 tutorial hand-written or could you find similar functionality in the AVR library?

there are a few libaries out there.
i have used this with some sucsess in the past:
http://www.mil.ufl.edu/~chrisarnold/components/microcontrollerBoard/AVR/avrlib/

Quote
Finally, in the makefile, what is the highest I can savely set F_CPU to for the ATMega32, I have currently set it at 3686400 but I might as well make the best use of the chip if it can go higher without an external oscillator.

i haven't looked at Admin's makefile but i suspect F_CPU is used for calibrating timing routines and will not set the clock speed of the AVR.
to set the clock speed you will need to set the fuse bits.

dunk.

 


Get Your Ad Here