adc not working on atmega328p

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi, guys can someone take a look at my code and help me understand what's wrong.

void ADC_Initialize()
{    
	ADCSRA = (ADEN) | (ADSC) | (ADATE) | (ADIE) | (ADPS2) | (ADPS1) | (ADPS0);
	DIDR0 = (ADC0D);
}

ISR(ADC_vect)
{
	adc_val = (ADCH << 8) | ADCL;
}

     and this is the main code below.

/*
 * oled lcd.c
 *
 * Created: 24-02-2020 18:10:18
 * Author : jeet
 */ 
#define F_CPU 16000000UL

#include <avr/io.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include "oled.c"
#include "adc.c"

int main(void)
{	
	sei();
	
	OLED_Initialize();
	ADC_Initialize();
	
	while (1)
	{
		OLED_ClearScreen();
		OLED_PrintLongInteger(adc_val,0);
		OLED_Render();
		_delay_ms(1000);
	}
	
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Bit numbers must be converted to bit masks.

 

void ADC_Initialize()
{
	ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADATE) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
	DIDR0 = (1 << ADC0D);
}

Or

void ADC_Initialize()
{
	ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADATE) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
	DIDR0 = _BV(ADC0D);
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are using the bit positions as bitmasks.

 

ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADATE) | (1 << ADIF) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
DIDR0 = (1 << ADC0D);

 

Edit: kabasan answered while I was typing...

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

jeet55 wrote:

adc_val = (ADCH << 8) | ADCL;

This can be just:

adc_val = ADC;

 

the compiler knows how to read 16 bit registers!

 

Jim

 

 

FF = PI > S.E.T

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
the compiler knows how to read 16 bit registers!
And which order they should be read...

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

jeet55 wrote:

#include "oled.c"
#include "adc.c"

I'd get out of this habit too if I were you. Dennis Ritchie had a plan about splitting code across multiple files when he wrote C and his intention was not to do it like this!

 

You may want to read an article in the tutorial forum by abcminiuser about "managing large projects" which shows how extern references in C should be done.

 

EDIT: this tutorial .... https://www.avrfreaks.net/forum/...

Last Edited: Wed. Jul 1, 2020 - 01:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
I'd get out of this habit too if I were you.

Absolutely!

 

and here's another article  on the subject:

 

http://c-faq.com/decl/decldef.html

 

Note that this is standard 'C' stuff - nothing specific to AVR or Atmel Studio.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson re-worked your code for you in your other thread: https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I missed the fact this was a cross post - wonder if one should be locked or I wonder if anyone dares trying to merge two threads? I don't!