Code pic
Upcoming SlideShare
Loading in...5

Code pic






Total Views
Slideshare-icon Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Code pic Code pic Document Transcript

    • 1. Code đo nhiệt độ dùng LM35+PIC16F877A+hiển thị tiếng Việt trên LCD#define<16f877a.h>#include"16f877A.h"#device adc=8#use delay(clock=12000000)#FUSES NOWDT,HS,NOPUT,NOPROTECT,NODEBUG,NOBROWNOUT,NOLVP,NOCPD,NOWRT#include<lcd.c>int8 read;int8 ketqua[10];int8 dem[10];byte const CGRAM[24]={ 0X0E,0X00,0X0E,0X11,0X1F,0X10,0X0E,0X04,0X01,0X03,0X01,0X07,0X09,0X09,0X07,0X00,0X0E,0X00,0X0E,0X11,0X11,0X11,0X0E,0x04,};byte const LED[10]={ 0b00000011,0b10011111,0b00100101,0b00001101,0b10011001,0b01001001,0b01000001,0b00011111,0b00000001,0b00001001,};void main(){int8 i,j,max,tam;int8 solan;set_tris_a(0xFF);set_tris_d(0x00);set_tris_b(0x00);set_tris_c(0x00);lcd_init();lcd_send_byte(0,0x2D);// viet cau lenh lcd 4 bit , 2 hang , 5*10dotslcd_send_byte(0,0x40); // viet cau lenh , ra lenh cho lcd chi toi cgram//lcd_send_byte(0,0x07);// Dich sang trai khi co hanh dong ghi vao CGRAMfor(i=0;i<24;i++)lcd_send_byte(1,CGRAM[I]);// viet du lieu vao cgram co dia chi la0x00 ++while(TRUE){setup_adc_ports(RA0_RA1_ANALOG_RA3_REF);setup_adc(ADC_CLOCK_INTERNAL);set_adc_channel(0);for(i=0;i<10;i++){if(adc_done()){ketqua[i]=read_adc();delay_us(20);}}setup_adc( ADC_OFF ); //Sau khi lay 10 gia tri xong thi tat ADCfor(i=0;i<10;i++){dem[i]=0;for(j=0;j<9;j++)
    • {if(ketqua[i]==ketqua[j])dem[i]++;}}tam=dem[0];max=ketqua[0];for(i=0;i<9;i++){if(tam<=dem[i+1]){tam=dem[i+1];max=ketqua[i+1];}}lcd_gotoxy(1,1);max=max/0.51;printf(lcd_putc,"Nhiet do max: %u",max);delay_us(20);//ting trung binhtam=0;for(i=0;i<10;i++){tam=tam+ketqua[i];}max=tam/10/0.51;LCD_GOTOXY(3,0);LCD_SEND_BYTE(1,N);LCD_SEND_BYTE(1,h);LCD_SEND_BYTE(1,i);LCD_SEND_BYTE(1,0x00);LCD_SEND_BYTE(1,t);LCD_SEND_BYTE(1, );LCD_SEND_BYTE(1,0x01);LCD_SEND_BYTE(1,0x02);LCD_SEND_BYTE(1, );LCD_SEND_BYTE(1,T);LCD_SEND_BYTE(1,B);LCD_SEND_BYTE(1,:);printf(lcd_putc,"%u",max);lcd_send_byte(0,0x1c);delay_ms(1000);}}2. Code đo nhiệt độ dùng LM35+PIC16F877A+Hiển thị LED 7 đoạn#define<16f877a.h>#include"16f877A.h"#device adc=8#use delay(clock=12000000)#FUSES NOWDT,HS,NOPUT,NOPROTECT,NODEBUG,NOBROWNOUT,NOLVP,NOCPD,NOWRTint8 read;byte const LED[10]={ 0b00001001,//00b00111111,//10b10010001,//20b00010011,//30b00100111,//40b01000011,//50b01000001,//60b00011111,//70b00000000,//8
    • 0b00000010,//9};void main(){int8 chuc,donvi;int1 done;int8 i;set_tris_a(0xFF);set_tris_d(0x00);set_tris_b(0x00);set_tris_c(0x00);setup_adc_ports(RA0_RA1_ANALOG_RA3_REF);setup_adc(ADC_CLOCK_INTERNAL);set_adc_channel(0);while(TRUE){setup_adc(ADC_START_ONLY);done=adc_done();while(!done){done=adc_done();}delay_ms(100);READ=READ_ADC();for(i=0;i=200;i++){chuc=read/10;donvi=read%10;output_low(pin_c0);output_b(led[chuc]);delay_ms(10);output_high(pin_c0);output_low(pin_c1);output_b(led[donvi]);delay_ms(10);output_high(pin_c1);}}}3. Mạch đo nhiệt độ Điều khiển động cơ, hiển thị LCDCode:#include <16F877A.h>#include <def_877a.h>#device *=16 ADC=10#include <lcd.h>#FUSES NOWDT //No Watch Dog Timer#FUSES HS //High speed Osc (> 4mhz for PCM/PCH)(>10mhz for PCD)#FUSES NOPUT //No Power Up Timer#FUSES NOPROTECT //Code not protected from reading#FUSES NODEBUG //No Debug mode for ICD#FUSES NOBROWNOUT //No brownout reset#FUSES NOLVP //No low voltage prgming, B3(PIC16) orB5(PIC18) used for I/O#FUSES NOCPD //No EE protection#FUSES NOWRT //Program memory not write protected#FUSES RESERVED //Used to set the reserved FUSE bits
    • #use delay(clock=20000000)int8 mode,tmin,tmax; // Khai bao cac bien dung trong ctint16 ct;int1 bat,anphim,docnhietdo;int8 cnt=0;void readkb() // Ham phat hien phim bam{anphim=0;if(INPUT(PIN_C0)==0) // Neu phim 1 duoc an (phim chon mode){delay_ms(150); // Ham delay chong rung phimwhile(INPUT(PIN_C0)==0); // Cho den khi nut duoc tha ramode++; // Tang mode len 1 de chuyen sang mode ke tiepif(mode==3) mode=0; // Lap lai mode 0 khi da chuyen qua het cacmodeanphim=1;}else{if(INPUT(PIN_C1)==0) // Neu phim 2 duoc an (phim giam){delay_ms(150); // Chong rung phim (cho phim duoc an trong 1 luc)if(INPUT(PIN_C1)==0){if(mode==1){ if(tmin>0) tmin--; anphim=1; } // Giam nguong tuy theomode, "tmin" >0 thì giảm 1if(mode==2){ if(tmax>tmin+1) tmax--; anphim=1; } // de "tmax" không nhỏ hơn"tmin" +1}}else{if(INPUT(PIN_C2)==0) // Neu phim 3 duoc an (phim tang){delay_ms(150); // Cho phim duoc an trong 1 lucif(INPUT(PIN_C2)==0){if(mode==1){if(tmin<tmax-1) tmin++; anphim=1; // Tangnguong tuy theo mode. Tmin khong the >tmax-1}if(mode==2){if(tmax<90) tmax++; anphim=1; // tmax khong lonhon 90do.}}}elseif(INPUT(PIN_C3)==0) // Neu phim 4 duoc an (phim bat/tat){delay_ms(150);while(INPUT(PIN_C3)==0); // Cho den khi nut duoc tha ra
    • if (ct>=tmin&&ct<=tmax) // Neu nhiet do nam trongkhoang giua 2 nguong{ // thi nut 4 co chucnang bat/tat quatbat=~bat;if(bat==1) OUTPUT_BIT(PIN_D0,1);if(bat==0) OUTPUT_BIT(PIN_D0,0);anphim=1;}}}}}void check() // Ham kiem tra nguong (tat quat neu nhiet do duoitmin{ // va bat quat neu nhiet do tren tmax)if (ct<tmin) { OUTPUT_BIT(PIN_D0,0); bat=0; }if (ct>tmax) { OUTPUT_BIT(PIN_D0,1); bat=1; }}void hiennhietdo() // Ham hien thi nhiet do do duoc tu LM35{int8 t,chuc,donvi;setcursor(0,0);wrtchar("- DO NHIET DO -"); // Hien thi dong trent = ct;chuc = t/10 + 48; // Tach so hang chuc va hang don vidonvi = t%10 + 48;setcursor(0,1);wrtchar("NHIET DO: "); // Hien thi so do nhiet do len LCDwrtchar(chuc);wrtchar(donvi);wrtchar("*C");delay_ms(50);}void settmin() // Ham cai dat nguong nhiet do min{int8 t,chuc,donvi;setcursor(0,0);wrtchar("- HEN TAT QUAT -");t = tmin; // Tach hang chuc va don vi cua tmin va hien thi raLCDchuc = t/10 + 48;donvi = t%10 + 48;setcursor(0,1);wrtchar("NEU DUOI: ");wrtchar(chuc);wrtchar(donvi);wrtchar("*C");delay_ms(50);}void settmax() // Ham cai dat nguong nhiet do max (tuong tu ham setmin){int8 t,chuc,donvi;setcursor(0,0);wrtchar("- HEN BAT QUAT -");t = tmax;
    • chuc = t/10 + 48;donvi = t%10 + 48;setcursor(0,1);wrtchar("NEU TREN: ");wrtchar(chuc);wrtchar(donvi);wrtchar("*C");delay_ms(50);}void selectmode() // Ham lua chon mode hien thi{switch (mode){case 0: hiennhietdo(); break;case 1: settmin(); break;case 2: settmax(); break;}}#INT_TIMER1 // Khai bao su dung ngat timer1void ct_phuc_vu_ngat(){CLEAR_INTERRUPT(INT_TIMER1); // Xoa co ngat timer1DISABLE_INTERRUPTS(GLOBAL); // Khong cho phep ngat khaccnt++; // Sau moi ngat ta tang bien demif(cnt==10) // khi cnt=10 thi da dem duoc 10 lan tuong ung voi1s{docnhietdo=1;ct = READ_ADC();ct = ct*0.49;cnt=0;}else docnhietdo=0;SET_TIMER1(3035);ENABLE_INTERRUPTS(GLOBAL);}void main() // Ham ct chinh{Setup_ADC(ADC_CLOCK_INTERNAL); // Cai dat bo chuyen doi ADCsetup_adc_ports(AN0); // Ngo vao Analog la chan AN0SET_ADC_CHANNEL(0); // Chon kenh chuyen doi ADC la kenh 0delay_us(20);mode = 0; // Khoi tao ban dau voi tmin=20*C va tmax=27*Ctmin = 20; // va hien thi mode do nhiet do dau tientmax = 27;lcd_init(); // Khoi tao LCDwrtcmd(0x01);wrtcmd(0x28);wrtcmd(0x0c);SETUP_TIMER_1(T1_INTERNAL|T1_DIV_BY_8); // Cai dat timer1 voi bo chiatan 1:8SET_TIMER1(3035); // Gia tri khoi tao cho timer1 ( dem den 65535// voi clock 20Mhz chia 8 => mot ngat tuong ung voi0.1s )ENABLE_INTERRUPTS(INT_TIMER1); // Cho phep ngat timer1
    • ENABLE_INTERRUPTS(GLOBAL); // Cho phep ngat toan cucct = READ_ADC(); // Doc gia tri nhiet do dau tienct = ct*0.49;while(1) // Vong lap vo han{selectmode();do{if(docnhietdo==1) // Neu nhiet do duoc doc thi hien thira LCD neu dang{ // o mode hien thi va tien hanh kiemtra nguongif(mode==0) { hiennhietdo(); }check(); // Kiem tra nguong}readkb();}while(anphim==0); // Lien tuc lap lai viec kiem tra xem nhietdo} // co duoc doc hay ko va kiem tra phim bam}