Chào các bạn! Mình viết code đọc 4 kênh ADC hiển thị lên LCD, Trong chương trình con adc_convert() tham số truyền vào là kênh ADC đươc lựa chọn, nhưng mình chỉ đọc được kênh 0 thôi, không đọc được các kênh khác. Các bạn xem giúp mình với
#include <htc.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & SWBOREN & IESODIS & FCMDIS & LVPDIS & DEBUGDIS); //1st config. Word
#define rs 0
#define e 1
#define lcd_data PORTB
#define rs_1 (PORTC|=(1<<rs))
#define rs_0 (PORTC&=(~(1<<rs))
#define pule_e (PORTC|=(1<<e); PORTC&=(~(1<<e))
//ham chuyen doi adc
unsigned int adc_convert (unsigned char channel)
{
int adc=0;
ADCON0|= (channel<<3);
ADCON0|= (1<<1);// bat dau chuyen doi
while (ADCON0&0x04);// cho chuyen doi xong
adc=ADRESH;
adc<<=8;
adc|=ADRESL;
return adc;
}
//ham xuat lenh ra lcd
void lcd_command(unsigned char command)
{
lcd_data=command;
rs_0;
pule_e;
_delay(10000);
}
//ham xuat ki tu ra lcd
void lcd_char (unsigned char chr)
{
lcd_data=chr;
rs_1;
pule_e;
_delay(10000);
}
//ham hien thi 1 chuoi tren lcd
void lcd_display(char row, char col, char*s)
{
switch (row)
{
case 0: lcd_command(0x80+col);
break;
case 1: lcd_command(0xC0+col);
break;
}
while (*s!=0) lcd_char(*s++);
}
//ham khoi tao lcd
void init_lcd()
{
lcd_command(0x38);
lcd_command(0x0C);
lcd_command(0x38);
lcd_command(0x01);
_delay(10000);
}
void main(void)
{
int temp;
ANSEL=0xFF;
ANSELH=0x00;
TRISB=0x00;
TRISC=0x00;
TRISA=0xFF;
ADCON0=0x01;
ADCON1=0xB1;// Vref+ = AN3
init_lcd();
while(1)
{
temp=adc_convert(0);
lcd_display(0,0,"nhietdo:");
lcd_char((temp/100)+ 0x30);
lcd_char(((temp%100)/10) + 0x30);
lcd_char((temp%10) + 0x30);
_delay(100000);
};
}
#include <htc.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & SWBOREN & IESODIS & FCMDIS & LVPDIS & DEBUGDIS); //1st config. Word
#define rs 0
#define e 1
#define lcd_data PORTB
#define rs_1 (PORTC|=(1<<rs))
#define rs_0 (PORTC&=(~(1<<rs))
#define pule_e (PORTC|=(1<<e); PORTC&=(~(1<<e))
//ham chuyen doi adc
unsigned int adc_convert (unsigned char channel)
{
int adc=0;
ADCON0|= (channel<<3);
ADCON0|= (1<<1);// bat dau chuyen doi
while (ADCON0&0x04);// cho chuyen doi xong
adc=ADRESH;
adc<<=8;
adc|=ADRESL;
return adc;
}
//ham xuat lenh ra lcd
void lcd_command(unsigned char command)
{
lcd_data=command;
rs_0;
pule_e;
_delay(10000);
}
//ham xuat ki tu ra lcd
void lcd_char (unsigned char chr)
{
lcd_data=chr;
rs_1;
pule_e;
_delay(10000);
}
//ham hien thi 1 chuoi tren lcd
void lcd_display(char row, char col, char*s)
{
switch (row)
{
case 0: lcd_command(0x80+col);
break;
case 1: lcd_command(0xC0+col);
break;
}
while (*s!=0) lcd_char(*s++);
}
//ham khoi tao lcd
void init_lcd()
{
lcd_command(0x38);
lcd_command(0x0C);
lcd_command(0x38);
lcd_command(0x01);
_delay(10000);
}
void main(void)
{
int temp;
ANSEL=0xFF;
ANSELH=0x00;
TRISB=0x00;
TRISC=0x00;
TRISA=0xFF;
ADCON0=0x01;
ADCON1=0xB1;// Vref+ = AN3
init_lcd();
while(1)
{
temp=adc_convert(0);
lcd_display(0,0,"nhietdo:");
lcd_char((temp/100)+ 0x30);
lcd_char(((temp%100)/10) + 0x30);
lcd_char((temp%10) + 0x30);
_delay(100000);
};
}
Comment