My code on ATtiny13A:
#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
#include "dbg_putchar.h"
void ADC_init()
{
// Set the ADC input to PB4/ADC2
ADMUX |= (1 << MUX1);
//ADMUX |= (1 << ADLAR);
ADMUX |= (1 << REFS0);
// Set the prescaler to clock/128 & enable ADC
// At 9.6 MHz this is 75 kHz.
// See ATtiny13 datasheet, Table 14.4.
ADCSRA |= (1 << ADPS1) | (1 << ADPS0) | (1 << ADEN);
}
int adc_read (void)
{
// Start the conversion
ADCSRA |= (1 << ADSC);
// Wait for it to finish
while (ADCSRA & (1 << ADSC));
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
int ADC_val = (high<<8) | low;
return ADC_val;
}
void wyslij_wynik_pomiaru(int wynik)
{
char str[4];
itoa(wynik, str, 10);
dbg_putchar(str[0]);
if (wynik > 9) dbg_putchar(str[1]);
if (wynik > 99) dbg_putchar(str[2]);
if (wynik > 999) dbg_putchar(str[3]);
dbg_putchar('a');
}
int main(void)
{
DDRB = _BV(1);
dbg_tx_init();
ADC_init();
int wynik_poczatkowy = adc_read();
int wynik_nowy;
while(1)
{
wynik_nowy = adc_read();
if (abs(wynik_poczatkowy-wynik_nowy) > 150) {
wynik_poczatkowy = wynik_nowy;
PORTB |= _BV(1); //turn on ESP8266
_delay_ms(5000);
wyslij_wynik_pomiaru(wynik_nowy); //send data
_delay_ms(5000);
PORTB &= ~_BV(1); //turn off ESP8266
_delay_ms(14900);
}
_delay_ms(100);
}
}
When you read the voltage level of the ADC and it is less than 1023, but steady, then everything works correctly: IF loop executes only once, however when the voltage level read on the ADC is equal to 1023, then the IF is executed continuously (condition is still met, but it should not).
Why is this happening and how to fix it?
adc_read()
is 1023. The "ADC=1023" bit is referring to the AVRsADC(L/H)
16bit register which with the current settings is right aligned so the upper 6 bits will always be 0. \$\endgroup\$.elf
file (produced when you compiled the code) to somewhere like dropbox. I can't see anything obvious which suggests there is an odd compiler optimisation happening somewhere. \$\endgroup\$