Author Topic: Reset when reading from ADC  (Read 2128 times)

0 Members and 1 Guest are viewing this topic.

Offline AmoebaTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Reset when reading from ADC
« on: June 21, 2012, 02:37:51 PM »
Hi

So, I recently built a $50 robot board (using an ATMega 328p).  The board powers up and I can program it to control servos / output to UART etc fine.  However when I try and use the a2dConvert8bit() function the board just resets.  I have traced it down to the following line of code:

92: sbi(ADCSR, ADSC);                  // start conversion

However I don't really know what this is doing .... what is sbi() for??

The libraries I am using are all pretty much standard (packaged with the photovore source) bar a few changes here and there to get the 328p working.

I realise this is nowhere near enough information to help debug this, but being a total new comer to this embedded stuff I don't really know what you will need :(  So, let me know and I will post it!

Thanks
David

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 10
Re: Reset when reading from ADC
« Reply #1 on: June 23, 2012, 06:12:28 AM »
Hm, I'm thinking it could be one of two (three?) problems:

1) you are sinking too much current into your ADC
2) You are sourcing voltage for the ADC from something on a single power supply, and when you read it you brown out the board somehow.  THIS is pretty unlikely, but since I just managed to do it last week, I thought I'd mention it :-P
3) *MAYBE*: your voltage is WAY too high for the ADC and the chip is resetting

Unless you're doing something REALLY weird in terms of your code, just doing a "read adc" function call likely won't cause the issue.  Have you tried commenting out your read call then trying to run the program and see if it resets? (to do that, if you needed the ADC value, just write it down in code, like a value of 100 or something, so the code still does something).  If so, then it's probably a power issue, not a code issue.

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline mstacho

  • Supreme Robot
  • *****
  • Posts: 376
  • Helpful? 10
Re: Reset when reading from ADC
« Reply #2 on: June 23, 2012, 06:21:43 AM »
Oh, and a quick check of the datasheet says that "sbi" means "set bit in" register.  So sbi(ADCSR,ADSC) means "set the bit corresponding to ADSC in the ADCSR register to 1".  Now, as to what THAT means...well it involved a lot of google (I imagine that someone better at ATMegas would just know this...somehow...):

ADCSR: Analog to Digital Control and Status Register
ADSC: This is a bit in ADCSR that is 1 when you want to start convert (Analog to Digital START CONVERSION), and 0 when you're done.

So all that code code is tells the register that controls the ADC that you want to start the conversion.  So it is REALLY unlikely that that code is screwing up.

Did you possibly remap the pins or something?

MIKE
Current project: tactile sensing systems for multifingered robot hands

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Reset when reading from ADC
« Reply #3 on: June 24, 2012, 06:07:31 AM »
It could also be that you have enabled an interrupt for when the ADC conversion is completed - but you haven't set up the interrupt vector to point to your own code. The default setting for most un set vectors it to reboot the device.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AmoebaTopic starter

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Re: Reset when reading from ADC
« Reply #4 on: June 25, 2012, 01:04:52 PM »
Thanks for the replies!

I found the following in the init() function of the A2D.c file

sbi(ADCSR, ADIE);            // enable ADC interrupts

So I commented this out and it appears to work properly now...  I would like to understand more about this though as the code is provided as part of the $50 robot and has been tested many many times.  I guess it must be something related to the ATmega328p chip I am using rather than the ATmega8 which the guide suggested.

Doing a quick bit of searching reveals other people with similar issues:

http://www.societyofrobots.com/robotforum/index.php?topic=9587.0

I will do a little more reading :)

Thanks guys!
David

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Reset when reading from ADC
« Reply #5 on: June 26, 2012, 11:45:12 AM »
It works in two ways:
1. No interrupt. You tell it to start a conversion and then keep polling a register to find out when its finished. Then read the value from another register. Whilst polling you can't really do much for other devices.
2. With interrupts. Tell it to start doing a conversion and then go other stuff. When the conversion is finished there is an interrupt and you can retrieve the value.

ATMega328P - hmmm - could be that the code you've got has an interrput handler but its attaching it to the wrong interrupt number - ie ATMega328P is different to ATMega8 etc.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here