Code mình dùng để đo điện áp, khi mình build trên Atmega 8 thì chạy rất tốt, Nhưng khi qua con 16 hay 128 thì nó lại báo lỗi này. mình dùng codevisson 3.03.
include <mega16.h>
#include <delay.h>
#include <stdio.h>
#include <lcd.h>
#asm
.equ __lcd_port=0x12;
#endasm
#define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (0<<ADLAR))
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
delay_us(10);
ADCSRA|=(1<<ADSC);
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCW;
}
float vol, i;
void lcd(float so,char x,char y)
{
char str_data[15];
sprintf(str_data, "%1.2f", so);
lcd_gotoxy(x,y) ;
lcd_puts(str_data);
}
void main(void)
{
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN)| (1<<ADPS1) | (1<<ADPS0);
lcd_init(16);
DDRD=0xff;
DDRA=0;
while (1)
{
i=read_adc(0);
if(i>=1023)
{
i=read_adc(1);
if(i>=1023)
{
i=read_adc(2);
vol= (i/1023)*55);
}
else
{
vol= (i*10)/1023);
}
}
else
{
vol= (i*5)/1023);
}
lcd(vol,0,0);
}
}
include <mega16.h>
#include <delay.h>
#include <stdio.h>
#include <lcd.h>
#asm
.equ __lcd_port=0x12;
#endasm
#define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (0<<ADLAR))
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
delay_us(10);
ADCSRA|=(1<<ADSC);
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCW;
}
float vol, i;
void lcd(float so,char x,char y)
{
char str_data[15];
sprintf(str_data, "%1.2f", so);
lcd_gotoxy(x,y) ;
lcd_puts(str_data);
}
void main(void)
{
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN)| (1<<ADPS1) | (1<<ADPS0);
lcd_init(16);
DDRD=0xff;
DDRA=0;
while (1)
{
i=read_adc(0);
if(i>=1023)
{
i=read_adc(1);
if(i>=1023)
{
i=read_adc(2);
vol= (i/1023)*55);
}
else
{
vol= (i*10)/1023);
}
}
else
{
vol= (i*5)/1023);
}
lcd(vol,0,0);
}
}