|
Tài trợ cho PIC Vietnam |
dsPIC - Bộ điều khiển tín hiệu số 16-bit Theo dự kiến của Microchip, vào khoảng năm 2011 dsPIC sẽ có doanh số lớn hơn PIC |
|
Ðiều Chỉnh | Xếp Bài |
18-02-2013, 05:50 PM | #1 |
Nhập môn đệ tử
Tham gia ngày: Feb 2013
Bài gửi: 6
: |
nhờ giúp đỡ
các anh ơi giúp em code cảm biến nhiệt độ này với em chạy mại mà nó không được. Em đang rất cần mong được sự giúp đỡ. Em cảm ơn nhiều
#include"p24fj128ga010.h" #include"libpic30.h" #define ld AD1PCFGbits.PCFG0 #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.LATB1 #define LED LATBbits.LATB12 #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(); //--------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; __delay32(100); while(1) { if(flag==1) { LATBbits.LATB1=1; lcd_gotoxy(1,1); lcd_putstr("NHIET DO LA:"); lcd_gotoxy(2,2); LCD_IN_INTER(ADCvalue/2.048); } 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.TRISB1 = 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=1; // dat che do analog cho chan AD1CON1 = 0x80E4;//bat adc //ASAM=1; tu dong lay mau //SSRC<2:0>=111; //SAMP=0; bo khuyech dai S/H dang giu mau //DONE=0 ; qua trinh lay mau chua ket thc hoac chua bat dau AD1CON2 = 0x0000; //VR+=AVDD; VR-= AVSS; //BUFM=0;//bo dem duwoc cau hinh thanh mot bo dem 16 word //ALTS=0; luon cho bo da hop A AD1CON3 = 0X0F05; //ADRC=0;//su dung xung noi //SAMC<4:0>=1111; 31 Tad //ADCS<7:0>=5;// 5 Tcy AD1CHS = 0x0000;//chon kenh 0 doc tin hieu giua AN0 va AVS AD1CSSL = 0x0000;//khong quet ngo vao IFS0bits.AD1IF=0;//xoa co ngat _AD1IP=4;//muc ngay uu tien IEC0bits.AD1IE=1;//cho phep xay ra ngat } void __attribute__ (( interrupt, auto_psv)) _ADC1Interrupt ( void) { flag=1;set co ngat len 1 IFS0bits.AD1IF=0;//xoa ngat ADCvalue=ADC1BUF8;//doc gia tri |
|
|