Hello.
I've got problem while measuring voltage.
First of all, ADC3 is working perfectly BUT ADC4 and 5 not.
I don't think it's a problem from circuit because I measured voltage on the pin and it was about 1.55V
Voltage ref = 2.73V so it should be about (result/1023) = 0.567. Yet it shows something like 0.005
When I disconnect anything it shows full 1. I think it's a problem with my code or it's optimization by compiler.
I don't think I could destroy ADC inside Atmega because it's basicly one with MUXED input.
THIS code works:
But not the others. Maybe MUX is not 3 2 1 0 but 0 1 2 3 ?
What is wrong here?
Here is my code:
I've got problem while measuring voltage.
First of all, ADC3 is working perfectly BUT ADC4 and 5 not.
I don't think it's a problem from circuit because I measured voltage on the pin and it was about 1.55V
Voltage ref = 2.73V so it should be about (result/1023) = 0.567. Yet it shows something like 0.005
When I disconnect anything it shows full 1. I think it's a problem with my code or it's optimization by compiler.
I don't think I could destroy ADC inside Atmega because it's basicly one with MUXED input.
THIS code works:
Code:
ADMUX = 192 | 3; //ADC3 with internal reference
ADCSRA = ADCSRA | (1<<ADSC); // Start ADC conversion
while(!(ADCSRA & (1<<ADIF))) // Wait for AD interrupt flag
;
adResult = ((ADCL) | ((ADCH)<<8)); // Shift high and low to 10-bit
voltage = (0.0252113*adResult);
But not the others. Maybe MUX is not 3 2 1 0 but 0 1 2 3 ?
What is wrong here?
Here is my code:
Code:
#include <avr/io.h>
//#include <math.h> // Needed for trunc function
#include <util/delay.h>
#include <stdlib.h>
#include <D:\Archiwum\AVR\Projekty\Multi\HD44780.h>
/* Function Definitions */
void ioinit(void);
// SBI and CBI to set/clear bits
#define sbi(port_name, pin_number) (port_name |= 1<<pin_number)
#define cbi(port_name, pin_number) ((port_name) &= (uint8_t)~(1 << pin_number))
uint8_t DPStatus = 0x00;
volatile uint8_t resDP = 0;
int main()
{
float adResult = 0;
float voltage = 0;
float current1 = 0;
float current2 = 0;
ioinit();
LCD_Initalize();
while(1)
{ // Every loop starts with ADC measurement
// measurement is processed and display updated
ADMUX = 192 | 5; //ADC5 with internal reference
ADCSRA = ADCSRA | (1<<ADSC); // Start ADC conversion
while(!(ADCSRA & (1<<ADIF))) // Wait for AD interrupt flag
;
adResult = ((ADCL) | ((ADCH)<<8)); // Shift high and low to 10-bit
current1 = (adResult/1023);
ADMUX = 192 | 3; //ADC3 with internal reference
ADCSRA = ADCSRA | (1<<ADSC); // Start ADC conversion
while(!(ADCSRA & (1<<ADIF))) // Wait for AD interrupt flag
;
adResult = ((ADCL) | ((ADCH)<<8)); // Shift high and low to 10-bit
voltage = (0.0252113*adResult);
ADMUX = 192 | 4; //ADC4 with internal reference
ADCSRA = ADCSRA | (1<<ADSC); // Start ADC conversion
while(!(ADCSRA & (1<<ADIF))) // Wait for AD interrupt flag
;
adResult = ((ADCL) | ((ADCH)<<8)); // Shift high and low to 10-bit
current2 = (adResult/1023);
float mcurrent = (voltage - current2);
LCD_GoTo(1,0);
if (voltage <10) LCD_WriteText("0");
char volt[5] = " ";
dtostrf(voltage,3,3,volt);
LCD_WriteText(volt);
LCD_WriteText("V ");
if (current1 <10) LCD_WriteText("0");
char amp[5] = " ";
dtostrf(current1,3,3,amp);
LCD_WriteText(amp);
LCD_WriteText("A ");
char mamp[5] = " ";
dtostrf(mcurrent,3,3,mamp);
LCD_GoTo(1,1);
LCD_WriteText("Limit: ");
LCD_WriteText(mamp);
LCD_WriteText("A ");
_delay_ms(50);
}
return 0;
}
void ioinit(void)
{
DDRD = 0xFF; // Set data direction for port D (A,B,...,G)
PORTD = 0; // Initialize all segments off
DDRB = 0b00000111; // Set data direction for digits and button (PB5)
PORTB = 0; // All digits off, button high
DDRC = 0b00000000; // PC0-2 are input, rest output
/* Set up ADC */
ADCSRA = (1<<ADEN); // Enable ADC
}