Hi everyone!
I'm trying to compare the ADC value with a constant.
The problem is that, I cannot to read the value of ADC. It always equals to zero. I cheked it by using the condition:
void PWM_D(void) { t4=0; // variable for PWM duty cycle control float konst=0.5; if (ADC0<konst) { t4=2000; } else if (ADC0>konst) { t4=1750; } else if (ADC0==konst) { t4=1650; } }
And it's always in the first statement (ADC0<konst).
In the same time, I use UART to chek the real value of ADC, the value is more then 2 V. It means, that "t4" should be equal to the 1650. But it is not.
This is the part of my code:
#include <util/delay.h> #include <avr/io.h> #include <avr/interrupt.h> #define PortInit DDRB|=(1<<7) #define LedOn PORTB|=(1<<7) #define LedOff PORTB&=~(1<<7) // Задаем переменные unsigned int ADC7; float n; uint8_t num=0; float ocr3c;// Регулировк скважности ШИМа "D" // Переменные для задания энергии int k=49; int t; int t4; //Переменные для хранениея АЦП volatile float ADC0; int period=2500; int g=4992; float time; void PWM_D(void) { t4=0; float konst=0.5; if (ADC0<konst) { t4=2000; } else if (ADC0>konst) { t4=1750; } else if (ADC0==konst) { t4=1650; } } int main(void) { DDRB=(1<<PORTB7)|(1<<PORTB6); DDRE=(1<<PORTE5)|(1<<PORTE4)|(1<<PORTE3); Energy(); PWM_D(); //Инициализация АЦП PWM_ini(); //Инициализация ШИМ UART0_Init(); //Инициализация порта на uART ADCInit(); sei(); ADCSRA|=(1<<ADSC); while(1) { PWM_D(); //Инициализация АЦП } } ISR(TIMER1_OVF_vect) { } ISR(ADC_vect) { uint8_t theLOW=ADCL; uint16_t ADC_ten_bits=ADCH<<8|theLOW; if(ADMUX==0b01000000) { n=(float) ADC_ten_bits/204.8;// 1024/5=204,8 uart0_tr((unsigned char)n+0x30); uart0_tr('.'); uart0_tr((unsigned char)(n*10)%10+0x30); uart0_tr((unsigned char)(n*100)%10+0x30); uart0_tr((unsigned char)(n*1000)%10+0x30); uart0_tr(0x0D); //делаем "Enter". "0x0D" - это Enter в ASCII коде ADC0=(float) ADC_ten_bits/204.8; // measuring of ADC0 ADMUX=0b01000001; } else if(ADMUX==0b01000001) { n=(float) ADC_ten_bits/204.8;// 1024/5=204,8 uart0_tr((unsigned char)n+0x30); uart0_tr('.'); uart0_tr((unsigned char)(n*10)%10+0x30); uart0_tr((unsigned char)(n*100)%10+0x30); uart0_tr((unsigned char)(n*1000)%10+0x30); uart0_tr(0x20); //делаем "Space". "0x0D" - это Пробел в ASCII коде ADMUX=0b01000111; ADCSRA|=(1<<ADSC); } else if(ADMUX==0b01000111) { n=(float) ADC_ten_bits/204.8;// 1024/5=204,8 uart0_tr((unsigned char)n+0x30); uart0_tr('.'); uart0_tr((unsigned char)(n*10)%10+0x30); uart0_tr((unsigned char)(n*100)%10+0x30); uart0_tr((unsigned char)(n*1000)%10+0x30); uart0_tr(0x20); //делаем "Space". "0x0D" - это Пробел в ASCII коде ADMUX=0b01000000; ADCSRA|=(1<<ADSC); } }
What's wrog with that?