Sign In

Issues With ADC

Dragonleon bautistasantiago@yahoo.com United States

Hardware: ATmega328P At 16MHz Software: C Project Atmel Studio 7.0

The program requires to read different frequencies from 1 2 3 Hz, when I put voltage 1 2 3 4 5 V [volts] but it does not work. in the oscilloscope. I do not know if the issue is with my atmega328p board or I miss something in the code. Always appear the frequency at 1.87kHz at any voltage like if it is not working the channel analog or ADC channel ADC1. I will appreciate a lot if you figure out the solution.
Thanks

Images

Program


#define F_CPU	16000000
#include<avr/io.h>
#include<util/delay.h>

void Initialize_ADC0(void);

int main(void)
{
	int i, voltage = 0;
	char toggle = 0;
	
	DDRB = 1 << 5;              	// Set PB5 output
	Initialize_ADC0();          	// Initialize ADC0 for manual trigger

	while(1)
	{
		ADCSRA = 0xC7;        		// start conversion
		_delay_us(260);			    // ensure max sampling rate not exceeded
		voltage = ADC & 0x3FF;     		// read voltage
		
		//if(voltage<10||voltage >0){
			
			toggle = toggle ^ 1;
			PORTB = (toggle & 0x01) << 5;	// toggle LED
						//_delay_ms(500);

			if (voltage<20){
				_delay_ms(500);
			}
			else if (voltage <40){
				_delay_ms(250);
			}
			else if (voltage <60){
				_delay_ms(125);
			}
			else if(voltage < 80){
				_delay_ms(62);
			}
			else if(voltage < 100){
				_delay_ms(51);
			}
		
	}

	return 0;
}
void Initialize_ADC0(void)
{
	ADCSRA = 0x87;	//Turn On ADC and set prescaler (CLK/128)
	ADCSRB = 0x00;	//turn off autotrigger
	ADMUX = 0x01;    	//Set ADC channel ADC1
}

Replies

R Avinash Gupta
2016-06-04 05:04:02
are you trying to generate different frequencies (1,2,3,4 & 5) Hz when you input voltages of (1,2,3,4 & 5)?

This is what I understood from the code.

But you have written that "The program requires to read different frequencies..." that is confusing.
R Dragonleon
2016-06-04 05:29:21
Yes, I need to get different frequencies when I input different voltages.
R Avinash
2016-06-08 13:42:23
Hello,

The proper method to generate waveform is to use the Compare module of the MCU. I have developed a code that can generate 1 Hz square wave signal on OC1A pin (PB1)(PIN 15 on 28 PIN DIP IC). The hex code is attached. Check it with your hardware and inform if you can successully get 1 HZ out.

Once this is ok I will complete the code as per your need.
R Avinash
2016-06-09 14:10:02
Waiting for your reply ...

Post a reply to Dragonleon

Think you can help Dragonleon ? Then post your thoughts that might help Dragonleon. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.