|
Tài trợ cho PIC Vietnam |
Analog - Bộ nhớ - Bảo mật - Cảm biến nhiệt độ Thảo luận việc sử dụng và thay thế các sản phẩm Analog, Memory, KEELOG và cảm biến nhiệt độ của Microchip cho các thiết kế |
|
Ðiều Chỉnh | Xếp Bài |
10-02-2013, 05:05 PM | #1 |
Đệ tử 1 túi
Tham gia ngày: Jan 2013
Bài gửi: 21
: |
xin giúp đỡ code cảm biến nhiệt độ
mong các bạn bớt chút thời gian giúp mình, mình đang rất cần. MÌnh cảm ơn rất nhiều
#include"p24fj128ga010.h" #include"libpic30.h" _CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_OFF & COE_OFF & FWDTEN_OFF); _CONFIG2(FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRI); #define LCD_RS LATBbits.LATB13 #define LCD_RW LATBbits.LATB14 #define LCD_E LATBbits.LATB15 #define LCD_D4 LATBbits.LATB2 #define LCD_D5 LATBbits.LATB3 #define LCD_D6 LATBbits.LATB4 #define LCD_D7 LATBbits.LATB5 #define vol LATBbits.LATB0 #define LED LATBbits.LATB10 #define LCD_clear 0x01 //Xoa man hinh LCD #define LCD_home 0x02 //Tro ve dau dong #define LCD_normal 0x06 //Che do nhap du lieu binh thuong #define LCD_normal_s 0x07 //Che do nhap du lieu binh thuong, dich man hinh #define LCD_off 0x08 //Tat man hinh #define LCD_on 0x0C //Bat man hinh #define LCD_on_crsr 0x0E //Bat man hinh va con tro #define LCD_on_blink 0x0F //Bat man hinh va con tro (nhap nhay) #define LCD_4b2l 0x28 //Che do 2 dong, giao tiep 4 bit du lieu #define LCD_homeL1 0x80 //Ve dau dong 1 #define LCD_homeL2 0xC0 //Ve dau dong 2 #define lcd_line_two 0x40 //--------------------------------------------------------------------------------------------------------- //Cac prototype cho cac chuong trinh con unsigned char flag,ADCvalue; void Init_PORTS(void); //void T1_init(); //void Delay_ms(unsigned long i_ms); void Init_LCD(void); void LCD_OK(void); void LCD_CMD(unsigned char cmd); void LCD_DATA (unsigned char dat); void lcd_putstr(char *buffer); void lcd_gotoxy(int x, int y); void LCD_IN_INTER(unsigned int d); void LCD_IN_FLOAT( float d); void ADC_init(); //void ADC_test(void); //--------CHUONG TRINH CHINH---------------------------------------------------------------------------- int main (void) { __delay32(1000); Init_PORTS(); //Khoi tao cac cong I/O Init_LCD(); //Khoi tao LCD ADC_init(); flag=0; AD1CON1bits.ADON=1; LATBbits.LATB10=0; __delay32(100); while(1) { if(flag==1) { LATBbits.LATB10=1; //LATBbits.LATB10=0; //__delay32(100); lcd_gotoxy(1,1); lcd_putstr("NHIET DO LA:"); lcd_gotoxy(2,2); LCD_IN_INTER(ADCvalue); } flag=0; }; } //--------Chuong trinh con khoi tao TRANG THAI BAN DAU---------------------------------------------------------- void Init_PORTS(void) { TRISBbits.TRISB0 = 1; TRISBbits.TRISB2 = 0; //LCD_D4 TRISBbits.TRISB3 = 0; //LCD_D5 TRISBbits.TRISB4 = 0; //LCD_D6 TRISBbits.TRISB5 = 0; //LCD_D7 TRISBbits.TRISB13 = 0; //LCD_RS TRISBbits.TRISB14 = 0; //LCD_RW TRISBbits.TRISB15 = 0; //LCD_E TRISBbits.TRISB12 = 0; // vol TRISBbits.TRISB10 = 0; // led } void Init_LCD(void) { LCD_CMD(0x02); //Dat che do giao tiep 4-bit, man hinh 2 dong __delay32(1); LCD_CMD(0x28); //Tat man hinh __delay32(1); LCD_CMD(0x0C); //Che do nhap du lieu binh thuong __delay32(1); LCD_CMD(0x06 ); //Bat man hinh, hien con tro nhap nhay __delay32(1); LCD_CMD(0x01); //Xoa man hinh __delay32(9); } void LCD_OK(void) { unsigned u; LCD_E = 1; //Tao xung Enable for (u = 0; u < 2; u++); //keo dai toi thieu 1 us LCD_E = 0; } void LCD_CMD(unsigned char cmd) { unsigned temp1; LCD_RW = 0; LCD_RS = 0; //Dat che do xuat lenh temp1 = cmd >> 4; //Lay 4 bit cao LCD_D4 = temp1 & 1; //Xuat 4 bit cao LCD_D5 = (temp1 & 2) >>1; LCD_D6 = (temp1 & 4) >>2; LCD_D7 = (temp1 & 8) >>3; LCD_OK(); temp1 = cmd & 0x0F; //Lay 4 bit thap LCD_D4 = temp1 & 1; //Xuat 4 bit thap LCD_D5 = (temp1 & 2) >>1; LCD_D6 = (temp1 & 4) >>2; LCD_D7 = (temp1 & 8) >>3; LCD_OK(); __delay32(1); } void LCD_DATA (unsigned char dat) { unsigned temp2; LCD_RW = 0; LCD_RS = 1; //Dat che do xuat du lieu temp2 = dat >> 4; //Lay 4 bit cao LCD_D4 = temp2 & 1; //Xuat 4 bit cao LCD_D5 = (temp2 & 2) >>1; LCD_D6 = (temp2 & 4) >>2; LCD_D7 = (temp2 & 8) >>3; LCD_OK(); temp2 = dat & 0x0F; //Lay 4 bit thap LCD_D4 = temp2 & 1; //Xuat 4 bit thap LCD_D5 = (temp2 & 2) >>1; LCD_D6 = (temp2 & 4) >>2; LCD_D7 = (temp2 & 8) >>3; LCD_OK(); __delay32(5); } void lcd_putstr(char *buffer) { while(*buffer != '\0') { LCD_DATA(*buffer); /* calling another function */ /* to write each char to the lcd module */ buffer++; __delay32(1); }; } //-----------Chuong trinh con nhay den cot x, dong y------------------------------------------------------------------- void lcd_gotoxy(int x, int y) { int address; switch(y) { case 1: { address=0; address+=x-1; LCD_CMD(0x80|address); break; } case 2: { address=lcd_line_two; address+=x-1; LCD_CMD(0xc0|address); break; } } } void LCD_IN_INTER(unsigned int d) { unsigned i,j,k,l; l=d/1000;//nghin k=(d%1000)/100;//tram j=((d%1000)%100)/10;//chuc i=((d%1000)%100)%10;//donvi LCD_DATA(0x30+l); LCD_DATA(0x30+k); LCD_DATA(0x30+j); LCD_DATA(0x30+i); } //=======giu so thuc len lcd============================= void LCD_IN_FLOAT( float d) { unsigned int e, i,j,k,l; // d=round(d,1); e=(int)(d*10); l=e/1000;//nghin k=(e%1000)/100;//tram j=((e%1000)%100)/10;//chuc i=((e%1000)%100)%10;//donvi LCD_DATA(0x30+l); LCD_DATA(0x30+k); LCD_DATA(0x30+j); lcd_putstr("."); LCD_DATA(0x30+i); } void ADC_init() { AD1PCFGbits.PCFG0=0; // dat che do analog cho chan AD1CON1 = 0x80E4; AD1CON2 = 0x0000; AD1CON3 = 0X0F05; AD1CHS = 0x0000;//chon kenh 0 doc tin hieu giua AN0 va AVS AD1CSSL = 0x0000;//khong quyet ngo vao IFS0bits.AD1IF=0; IEC0bits.AD1IE=1; _ASAM=1; } void __attribute__ (( interrupt)) _ADC1Interrupt ( void) { flag=1; IFS0bits.AD1IF=0; ADCvalue=ADC1BUF8; } |
|
|