Thông báo

Collapse
No announcement yet.

Đo điện áp bằng ADC và hiển thị trên LCD

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Đo điện áp bằng ADC và hiển thị trên LCD

    Mọi người xem giúp mình sai ở đâu, mà đo điện áp toàn hiển thị bét nhè với
    thanks mọi người, file mô phỏng mình đính kèm
    Code:
    #include<htc.h>
    #include"lcd.h"
    #include<math.h>
    #include <stdio.h>
    #include <stdlib.h>
    __CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
    #define _XTAL_FREQ 4000000
    //khai bao cac chan I/O
    const char cgram_dat1[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    							0x08,0x04,0x0e,0x01,0x0f,0x11,0x0f,0x00,//chu a huyen
    							0x04,0x0c,0x0a,0x0e,0x11,0x1e,0x0e,0x00,//chu ee sac
    							0x02,0x04,0x12,0x12,0x12,0x12,0x0C,0x00,//chu u sac	
    							0x00,0x04,0x0A,0x0E,0x11,0x11,0x0E,0x00,//chu oo
    							0x99
    							};
    const char cgram_dat2[]=	{
    							0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//
    							0x00,0x0E,0x11,0x11,0x11,0x0E,0x04,0x00,//chu o nang 1
    							0x04,0x0A,0x0E,0x11,0x1E,0x10,0x0E,0x00,//chu ee 2
    							0x04,0x0A,0x0E,0x11,0x1E,0x0E,0x04,0x00,//chu ee nang 3
    							0x04,0x08,0x0E,0x01,0x0F,0x11,0x0F,0x00,//chu a sac 4
    							0x1e,0x11,0x11,0x1d,0x11,0x11,0x1e,0x00,	// CHU DD 5
    							0x02,0x07,0x02,0x0E,0x12,0x13,0x0E,0x00,//chu dd 6
                                0x99										// Ma ket thuc chuoi la 99H,
    							};
    const char cgram_dat3[]={
    							0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    							0x1C,0x12,0x11,0x39,0x11,0x11,0x12,0x1C,//chu DD
    							0x04,0x0A,0x0E,0x11,0x1E,0x0E,0x04,0x00,//chu ee nang
    							0x04,0x08,0x0E,0x01,0x0F,0x11,0x0F,0x00,//chu a sac
    							0x04,0x0C,0x0A,0x0E,0x01,0x0F,0x11,0x0F,//chu aa sac
    							0x04,0x0c,0x0a,0x0e,0x11,0x1e,0x0e,0x00,	//chu ee sac
    							0x02,0x04,0x12,0x12,0x12,0x12,0x0C,0x00,//chu u sac
    							0x99
    						};
    void nap_kitu(unsigned int x)
    {
    	unsigned int i;
    	// Xu ly nap ma ky tu dac biet vao CGRAM.
    	i = 0;                     // Dia chi ky tu dac biet trong bo ma cgram_dat.
    	lcd_put_byte(0,0x40);         // Lenh = 40H - Dat CGRAM co dia chi bat dau la 00H.
    	while(lcd_busy());            // Kiem tra LCD bao ban.
    	if(x == 1)
    	{
    		while(cgram_dat1[i]!=0x99)      // Kiem tra nap xong du lieu cho cac ky tu dac biet,
    		{                           // ky tu ket thuc chuoi la 99H.
    			lcd_put_byte(1,cgram_dat1[i]);      // Ghi cac ma ky tu dac biet vao CGRAM theo dia chi.
    			while(lcd_busy());         // Kiem tra LCD bao ban.      
    			i++;
    		}
    	}
    	if(x == 2)
    	{
    		while(cgram_dat2[i]!=0x99)      // Kiem tra nap xong du lieu cho cac ky tu dac biet,
    		{                           // ky tu ket thuc chuoi la 99H.
    			lcd_put_byte(1,cgram_dat2[i]);      // Ghi cac ma ky tu dac biet vao CGRAM theo dia chi.
    			while(lcd_busy());         // Kiem tra LCD bao ban.      
    			i++;
    		}
    	}
    	if(x == 3)
    	{
    		while(cgram_dat3[i]!=0x99)      // Kiem tra nap xong du lieu cho cac ky tu dac biet,
    		{                           // ky tu ket thuc chuoi la 99H.
    			lcd_put_byte(1,cgram_dat3[i]);      // Ghi cac ma ky tu dac biet vao CGRAM theo dia chi.
    			while(lcd_busy());         // Kiem tra LCD bao ban.      
    			i++;
    		}
    	}
    }
    //ham doc gia tri ADC
    unsigned int read_ADC()
    {
    	__delay_us(5);	//Cho AD lay mau
    	GODONE=1;		//Bat dau thuc hien chuyen doi
    	while(GODONE);
    	return(((unsigned int)ADRESH)<<8)|ADRESL;//ghep byte thap va byte cao
    }
    //ham chuyen doi adc hien thi lcd
    void adc_lcd(unsigned int i)
    {
    	char ng,tr,ch,dv;
    	ng=i/1000;
    	tr=(i-1000*ng)/100;
    	ch=(i-1000*ng-100*tr)/10;
    	dv=i-1000*ng-100*tr-ch*10;
    	//hien thi len lcd
    	lcd_gotoxy(6,1);
    	lcd_putc(ng+0x30);
    	lcd_gotoxy(7,1);
    	lcd_putc(tr+0x30);
    	lcd_gotoxy(8,1);
    	lcd_putc(ch+0x30);
    	lcd_gotoxy(9,1);
    	lcd_putc(dv+0x30);
    }
    //khoi tao adc
    void adc_init()
    {
        ANSEL|=1<<3;                            //BAT BIT 3	
    	TRISA3=1;
    	
    	VCFG1=0;
    	VCFG0=0;
    	
    	ADCS0=1;
    	ADCS1=1; //chon xung clock chuyen doi AD tu mach RC trong chip
    
    	//chon kenh AN3
    	CHS3=0;
    	CHS2=0;
    	CHS1=1;
    	CHS0=1;
    	
    	ADFM=1;//dinh dang 10bit ket qua chuyen doi AD
    	ADON=1;//cho phep ADC hoat dong
    }
    void dodienap()
    {
    	unsigned int val_adc,val_temp;
    	nap_kitu(2);
    	lcd_puts("\f   \5o \6i\3n \4p \n  Vin=     (mV) ");
    	while(1)
    	{
    			val_adc=read_ADC();	
    			val_temp=val_adc*5000/1024;
    			if(val_temp < 2500)
    				RC0=1;
    			else
    				RC0=0;
    			adc_lcd(val_temp);
    	}
    }
    void main()
    {
    	ANSEL=0;	//Cau hinh cac chan analog deu la digital I/O
    	ANSELH=0;
    	WPUB=0x0F;	//Cho phep dien tro PULL UP tren chan RB0..3
    	TRISB=0b00000000;
    	RBPU=0;
    	TRISD=0b00000000;
    	TRISE=0;
    	TRISC0=0;
    	adc_init();
    	lcd_init();
    	dodienap();	
    }
    Attached Files

  • #2
    hix ai help em với

    Comment

    Về tác giả

    Collapse

    thangvc91 Tìm hiểu thêm về thangvc91

    Bài viết mới nhất

    Collapse

    Đang tải...
    X