Hello everyone!
I am trying to display the results of ADC on LCD. The ADC is supposed to operate in a continuous mode. However, I can obtain only the first result of ADC, as afterwards it stops and the results on LCD do not change while I change the value of input voltage. In order to obtain changed results in is necessary to restart microcontroller. The microcontroller is Atmega16.
In order to determine the exact line in the program which causes stuck of ADC results I have also added eight LEDs to B port and send the results of ADC to this port. As a result, when I exclude lines with sprintf and printf in the following code LEDs connected to B port blink continuously while I change the value of input voltage, therefore in this case ADC operates continuously. However, when I include
lines with sprintf and printf LEDs blink only once, when I start simulation in Proteus, and do not follow the changes of input voltage. The numbers on LED also show only the first result of ADC conversion.
Where can be my mistake? (mistakes more likely).
#include <avr/io.h> #include <util/delay.h> #include <stdio.h> void ADC_INIT () { ADCSRA |= (1<<7); ADCSRA |= (1<<0)| (1<<1)| (1<<0); ADCSRA |= (1<<5); // ADATE = 1; SFIOR &= ~(1<<7) &~ (1<<6) &~ (1<<5); ADMUX &= ~(1<<7) &~ (1<<6); ADMUX &= ~(1<<0); } void send_command_to_LCD (unsigned char comm) { PORTC &= ~(1<<0); PORTD = comm; PORTC |= (1<<1); _delay_us(50); PORTC &= ~(1<<1); // _delay_us(80); } void send_data_to_LCD (unsigned char data) { PORTC |= (1<<0); PORTD = data; PORTC |= (1<<1); _delay_us(50); PORTC &= ~ (1<<1); _delay_us(80); } void LCD_Init () { _delay_ms(60); send_command_to_LCD (0x38); _delay_us(80); send_command_to_LCD (0x0F); _delay_us(80); send_command_to_LCD (0x01); _delay_ms(4); send_command_to_LCD (0x06); _delay_us(80); } static FILE stdout_LCD = FDEV_SETUP_STREAM(send_data_to_LCD, NULL, _FDEV_SETUP_WRITE); int main(void) { stdout = &stdout_LCD; DDRC |= (1<<1)|(1<<0); PORTC &= ~(1<<1) &~ (1<<0); DDRD = 0xff; DDRB = 0xff; PORTD = 0b00000000; LCD_Init (); ADC_INIT (); ADCSRA |= (1<<6); for (;;) { if (ADCSRA & (1<<4)) { unsigned int adc_result=ADCW; PORTB = adc_result; double adc_result_volts=(double)(adc_result*5)/1024; char mystr[5]; sprintf(&mystr, "%0.2f", adc_result_volts); printf("%s:",&mystr); ADCSRA|=(1<<4); } } }