|
Tài trợ cho PIC Vietnam |
Luận văn tốt nghiệp Nếu bạn thắc mắc vì sao chúng tôi muốn phổ biến các luận văn tốt nghiệp? Xin xem tại đây |
|
Ðiều Chỉnh | Xếp Bài |
30-04-2014, 07:15 PM | #6 |
Nhập môn đệ tử
Tham gia ngày: Apr 2012
Bài gửi: 2
: |
code của mình:
#include <16f877a.h> //#include <def_877a.h> #fuses hs, nolvp, nowdt, noput, noprotect #include <math.h> //dung cac ham toan hoc //#device *=16 ADC=10 #use delay(clock=20000000) #define LCD_ENABLE_PIN PIN_C7 #define LCD_RS_PIN PIN_C5 #define LCD_RW_PIN PIN_C6 #define LCD_DATA4 PIN_D4 #define LCD_DATA5 PIN_D5 #define LCD_DATA6 PIN_D6 #define LCD_DATA7 PIN_D7 #include <LCD.C> #use fast_io(a) #use fast_io(b) #use fast_io(c) #use fast_io(d) //==================khai bao ham con============= int quetphim(); //khai bao ham quet phim int checkphim(b); //khai bao ham check phim void pwm(); //khai bao ham tao xung PWM void ghi_tocdo(); //khai bao ham ghi toc do dong co void ghi_thoigian(); //khai bao ham ghi thoi gian void clear(); //khai bao ham xoa cac thiet lap truoc do, tro ve trang thai dau void read_rom(); //Khai bao ham doc du lieu tu eeprom //==================khai bao bien su dung trong chuong trinh ================ int8 i,t,a,b,c,d,sttphim,duty,ct,l,m; //bien nguyen 8bit 0-255 int16 s_xung,s_vong,setpoint,error,luu,tg,tg1,dem; //bien nguyen 16bit 0-65535 //================ bien luu eeprom============== int8 e0,e1,e2,e3,j,k; int16 e; //================chuong trinh quet phim========== //quet phim so int quetphim() //su dung cau truc: int ten_chuong_trinh(){} de thuc hien quet phim, lan luot cho RB4, RB5, RB6, RB7=0 và ung voi moi gia tri do kiem tra trang thai tren cac chan dau vao RA0, RA1,RA2, RA3 { output_b(0xe0);// B4=0 - dua tin hieu dien 0V vao hang thu nhat, kiem tra xem phim nao duoc nhan bang ham checkphim(b); a=0; //cho a=0 là có muc dich.. lien quan dem ham checkphim(b) trong ham checkphim(b) gia tri cua a duoc thay doi, nhu vay neu qua trinh phat hien ra phim duoc nhan thi se thay doi a, neu khong thi a van bang 0 va ko co gia tri nao duoc tra ve b=1; //gia tri cua b lien quan den ham checkphim(b).. cau lenh while(b), do do nhat thiet phai dat gia tri ban dau cho b thi ham checkphim(b) moi duoc thuc hien checkphim(b); //goi ham kiem tra phim nhan, neu co phim duoc nhan thi ham checkphim se gan gia tri cho a (a=1) va tiep tuc thuc hien lenh tiep theo tra ve gia tri bien sttphim if (a!=0) {delay_ms(200); return (sttphim);} output_b(0xd0);// B5=0 a=0; b=2; checkphim(b); if (a!=0) {delay_ms(200); return (sttphim);} output_b(0xb0);// B6=0 a=0; b=3; checkphim(b); if (a!=0) {delay_ms(200); return (sttphim);} output_b(0x70);// B7=0 a=0; b=4; checkphim(b); if (a!=0) {delay_ms(200); return (sttphim);}} //============chuong trinh check phim============= int checkphim(b) //ham kiem tra phim duoc nhan { switch (b) //cau lenh re nhanh- lan luot gan b=1,2,3,4 (ham quet phim) de kiem tra xem co phim nao duoc nhan khi quet lan luot tung hang { case 1: //neu b=1 thi thuc hien chuong trinh duoi if(!input(pin_a0)) //kiem tra xem neu chan dau vao RA0=0 thi thuc hien chuong trinh duoi (do ta thiet lap phan cung nen phai dung nghich dao PIN_A0) {sttphim=1; //gan cho bien sttphim gia tri bang 1 a=1;} //gan gia tri cho a=1 else if(!input(pin_a1)) //neu chan RA0!=0 tuc la phim1 khong duoc nhan, tiep tuc kiem tra chan RA1, neu RA1=0 thi thuc hien chuong trinh duoi {sttphim=2; a=1;} else if(!input(pin_a2)) //neu chan RA1!=0 tuc la phim 2 khong duoc nhan, tiep tuc kiem tra chan RA2 {sttphim=3; a=1;} else if(!input(pin_a3)) //neu chan RA2!0 tuc la phim 3 khong duoc nhan tiep tuc kiem tra chan RA3 { sttphim=10;//thuan a=1;} else {} break; case 2: if(!input(pin_a0)) {sttphim=4; a=1;} else if(!input(pin_a1)) {sttphim=5; a=1;} else if(!input(pin_a2)) {sttphim=6; a=1;} else if(!input(pin_a3)) {sttphim=11;//nghich a=1;} else {} break; case 3: if(!input(pin_a0)) {sttphim=7; a=1;} else if(!input(pin_a1)) {sttphim=8; a=1;} else if(!input(pin_a2)) {sttphim=9; a=1;} else if(!input(pin_a3)) {sttphim=12;//stop a=1;} else {} break; case 4: if(!input(pin_a0)) {sttphim=0;//0 a=1;} else if(!input(pin_a1)) {sttphim=15;//save a=1;} else if(!input(pin_a2)) {sttphim=14;//clear a=1;} else if(!input(pin_a3)) {sttphim=13;//set a=1;} else {} break;} return (sttphim); } ////////////////////////////// chuong trinh nhan xung tu encoder////////////////////////////////////////////// ///////////////////////////// ngat ngoai, nhan xung tu encoder/////////////////////////////////////////////// #int_ext //khai bao su dung ngat ngoai nhan xung tren chan RB0 void RB0_isr() { s_xung++;//dem so xung o chan RB0 } ///////////////////////////////ngat timer1, tinh toan pwm va hien thi///////////////////////////////////////// #int_timer1 void timer1_isr() { set_timer1(-62500); //thiet lap gia tri ban dau cho bo dem timer1 la -62500 if (t==5){ //kiem tra gia tri bien t, neu t=5 thi tinh so vong thong qua bien s_xung, xoa man hinh LCD, tiep tuc kiem tra bien ct S_vong=s_xung*2; //xung tren phut //ht lcd_gotoxy(1,1); printf(lcd_putc," "); if(ct==1) //neu bien ct=1 thi xuat ra man hinh LCD gia tri cua bien "luu" { lcd_gotoxy(1,1); printf(lcd_putc,"TD_dat=%lu v/p",luu);} else { //neu kiem tra ma bien ct!=1 thi xuat ra man hinh gia tri bien "tg" lcd_gotoxy(1,1); printf(lcd_putc,"TG_dat=%lu s",tg);} lcd_gotoxy(1,2); printf(lcd_putc," "); if (d==2) //kiem tra gia tri cua bien "d", neu d=2 thi xuat ra LCD (o hang 2) gia tri bien "s_vong" roi tiep tuc kiem tra gia tri bien "d" {lcd_gotoxy(1,2); printf(lcd_putc,"TD_tt=-%luv/p",s_vong);} if (d==1) //kiem tra neu d=1 thi in ra man hinh LCD (o hang 2) gia tri bien "s_vong" dong thoi thuc hien cac lenh tiep theo {lcd_gotoxy(1,2); printf(lcd_putc,"TD_tt= %luv/p",s_vong);} //========================= //xuat pwm pwm(); //thuc hien ham pwm(); //======================== s_xung=0; //gan cho bien s_xung=0; s_vong=0; //gan cho bien s_vong=0; t=0; //gan cho bien t=0 set_timer1(-62500); } else //neu d!=1, bat dau tang bien "t" t++; //dat gia tri ban dau cho timer1 la -62500 set_timer1(-62500); } //ngat timer0, dat thoi gian #int_timer0 //khai bao de su dung ham ngat timer0 void time0_irs() //ham ngat { set_timer0(-235); //gui den timer0 gia tri -235 if (dem>=tg1) //so sanh bien "dem" voi bien "tg1", neu bien dem lon hon hoac bang bien "tg1" thi thuc hien day lenh trong cap {} { output_high(pin_e0);//xuat dien ap 5V ra chan RE0 delay_us(100); output_low(pin_e0);//xuat dien ap 0V ra chan RE0 dem=0; //cho gia tri bien dem tro ve gia tri 0, tang lai tu dau if (d==1) //tiep tuc kiem tra gia tri bien d, neu d=1 thi gan cho d gia tri bang 2, d=2 (cho d nhay qua nhay lai) {d=2;} else if (d==2) //neu bien "d" khong phai bang 1 thi kiem tra xem no co bang 2 khong neu no bang 2 thi lai cho no nhay ve gia tri d=1 {d=1;} else{} //neu d khong bang 2 thi dat lai gia tri ban dau cho timer0 la -235 set_timer0(-235); } else { //neu d cung khong bang 1 not thi thuc hien tang bien dem va dat gia tri ban dau cho timer0 la -235 dem++; set_timer0(-235);} } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////chuong trinh chinh/////////////////////////////////////////////////////////////////// void main() { i=0; sttphim=0; setpoint=0;tg=0; duty=0; c=0; d=0; ct=0;l=0;m=0; e0=0;e1=0;e2=0;e3=0;e=0;k=0; //================================================== ========== //1:ngo vao;0: la ngo ra set_tris_b(0b00001111);//4 chan RB4-RB7 xuat du lieu ra ban phim set_tris_a(0b00001111);//4 chan RA0-RA3 nhan du lieu tu ban phim set_tris_c(0b00000000);//2 chan RC0 va RC1 xuat PWM set_tris_D(0b00000000);//port D la port xuat du lieu ra LCD //================================================== ========== setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //thiet lap timer1 su dung xung clock noi fosc/4, chia tan 1:8 (tan so 20Mhz/4/8=0.625Mhz=>t=1.6s /* timer1 la bo dinh thoi su dung xung noi,bo chia 1:8 thay doi moi 1600ns (1,6us) Dung timer1 de ngat moi 0.1s do vay ta dat gia tri cho timer1 la : 0.1s/1600ns=62500(D)=F424(H) =>gia tri nap la FFFF-F424=BDB */ setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); //thiet lap timer0 su dung xung clock noi, chia tan 1:256 (tan so 20Mhz/4/256=0.019531Mhz=> t=51.2s enable_interrupts(int_ext);//khoi dong ngat ngoai ext_int_edge(H_TO_L); // xung tu cao xuong thap - chon xung canh xuong enable_interrupts(global);// khoi dong bit ngat GIE - cho phep ngat toan cuc setup_timer_2(T2_DIV_BY_4,249,1); /*timer2 dung dinh thoi cho bo PWM mode: bo chia thoi gian (prescale) cua timer2 1:4 period: gia tri nap cho thanh ghi PR2 postscale : bo chi ra,chon 1:1 PWM khong dung Thach anh 20MHz, PWM fre: 10000Hz, thay doi duty cycle(%) de thay doi toc do*/ setup_ccp1(CCP_PWM); setup_ccp2(CCP_PWM); set_pwm1_duty(0); //value=0, pwm1=0, dong co dung set_pwm2_duty(0); //value=0, pwm2=0, dong co dung //================================================== ========= lcd_init(); //khoi tao LCD lcd_send_byte(0,0x01); lcd_gotoxy(1,1); printf(lcd_putc,"CHUONG TRINH DK "); //xuat ra LCD (tren hai hang) dong chu "CHUONG TRINH DK TD DONG CO DC" trong vong 1s roi xoa man hinh LCD lcd_gotoxy(1,2); printf(lcd_putc," TD DONG CO DC "); delay_ms(1000); lcd_gotoxy(1,1); printf(lcd_putc," "); lcd_gotoxy(1,2); printf(lcd_putc," "); read_rom(); //Thuc hien lenh read_rom(); - tuc la doc du lieu tu bo nho ROM lcd_gotoxy(1,1); printf(lcd_putc,"CHON CHE DO :_ "); //Xuat ra LCD dong chu "CHON CHE DO :_ while (true) //vong lap vo tan { while (c==0) //kiem tra neu c=0 thi chuong trinh di vao vong lap vo tan { quetphim(); //thuc hien quet phim nhan xem phim nao duoc nhan, dua vao do de chon che do hoat dong là che do 1, hay che do 2 if (a!=0) //neu a=0 thi thuc hien lenh "if" tiep theo { if (sttphim==1) //neu phim duoc nhan la phim 1 thi cho bien ct=1,c=1; cac lenh tiep theo la cac lenh quet chon che do (chuong trinh) hoat dong { ct=1; c=1; lcd_gotoxy(1,1); printf(lcd_putc,"CHON CHE DO :_%u",ct);} //neu chon che do 1 thi xuat ra man hinh "CHON CHE DO :_1" if (sttphim==2) //Tiep tuc kiem tra neu phim duoc nhan la phim 2 thi cho ct=2, c=1, { ct=2; c=1; lcd_gotoxy(1,1); printf(lcd_putc,"CHON CHE DO :_%u",ct);} //neu chon che do 2 (nhan phim 2) thi xuat ra man hinh "CHON CHE DO :_2" } } if (ct==1) //kiem tra neu ct=1 thi thuc hien cac lenh in ra LCD nhu ben duoi {lcd_gotoxy(1,1); printf(lcd_putc,"TD_dat=_ v/p"); lcd_gotoxy(1,2); printf(lcd_putc,"TD_luu=%lu v/p",e);} else { lcd_gotoxy(1,1); printf(lcd_putc,"TD_dat=_ v/p"); lcd_gotoxy(1,2); printf(lcd_putc,"TG_dat=_ s");} While (c==1) //neu c=1 thi vao vong lap vo tan { quetphim(); //kiem tra xem phim nao duoc nhan if (a!=0) //neu a!=0 thi tiep tuc kiem tra bang lenh if tiep theo { if (sttphim>=0 && sttphim<=9)//ban phim tu 0--->9 - neu phim duoc nhan la phim0-phim9 { if (m==0) // kiem tra xem neu m=0 thi thuc hien ham ghi toc do {ghi_tocdo();} if((ct==2)&&(m==1)) // kiem tra xem nau ct=2 (dang o che do 2) va m=1 thi thuc hien ham ghi_thoigian(); {ghi_thoigian();} } if (sttphim==14) //kiem tra neu phim duoc nhan la phim 14 thi thuc hien lenh xoa clear(); {clear();} if ((sttphim==13)&&(i==0)&&(e==0)) //kiem tra neu nhan phim 13, dong thoi i=0, e=0 thi thuc hien lenh ben duoi { lcd_gotoxy(1,1); printf(lcd_putc," ");//xoa man hinh lcd_gotoxy(1,1); printf(lcd_putc,"Phai nhap TD_dat"); // in ra LCD dong nhac: "phai nhap TD_dat" trong vong 1s delay_ms(1000); lcd_gotoxy(1,1); printf(lcd_putc," ");//xoa man hinh lcd_gotoxy(1,1); printf(lcd_putc,"TD_dat=_"); //in ra LCD dong lenh "TD_dat=_" } if ((sttphim==15)&&(i!=0)) //luu vao eeprom neu nhan phim 15 va gia tri i!=0 { write_eeprom(0,e0); delay_ms(100); write_eeprom(1,e1); delay_ms(100); write_eeprom(2,e2); delay_ms(100); write_eeprom(3,e3); delay_ms(100); write_eeprom(4,i); delay_ms(100); lcd_gotoxy(1,2); printf(lcd_putc," "); //xoa man hinh (dong2) lcd_gotoxy(1,2); printf(lcd_putc," LUU THANH CONG ");//in ra man hinh dong chu "LUU THANH CONG" } if ((sttphim==13)&&((i!=0)||(e!=0)))//Neu nhan phim 13 va gia tri i khac 0, e khac 0 { if (ct==1)//kiem tra xem neu dang o che do 1 thi cho bien c=2 { c=2; if (e!=0 && i==0){ luu=e;} if ((ct==2)&&(l>=2)) { c=2;} m=1; }}} while (c==2) { quetphim(); if(a!=0) { if(sttphim==10) { lcd_gotoxy(1,2); printf(lcd_putc," "); lcd_gotoxy(1,2); printf(lcd_putc," QUAY THUAN "); if (ct==2) {enable_interrupts(int_timer0); set_timer1(-235);} enable_interrupts(int_timer1); enable_interrupts(global); set_timer1(-62500); set_pwm1_duty(duty); d=1; } if (sttphim==11) { lcd_gotoxy(1,2); printf(lcd_putc," "); lcd_gotoxy(1,2); printf(lcd_putc," QUAY NGHICH "); if (ct==2) {enable_interrupts(int_timer0); set_timer1(-235);} enable_interrupts(int_timer1); enable_interrupts(global); set_timer1(-62500); set_pwm2_duty(duty); d=2; } if (sttphim==12)//stop { disable_interrupts(int_timer0); set_timer0(0); disable_interrupts(int_timer1); set_pwm1_duty(0); set_pwm2_duty(0); lcd_gotoxy(1,2); printf(lcd_putc," "); lcd_gotoxy(1,2); printf(lcd_putc," STOP "); duty=0; d=0; } if (sttphim==14) { clear(); c=0; lcd_gotoxy(1,1); printf(lcd_putc," "); lcd_gotoxy(1,2); printf(lcd_putc," "); lcd_gotoxy(1,1); printf(lcd_putc,"CHON CHE DO :_ "); }}}}} void pwm() { if((luu>s_vong)&& duty<250) { error=luu-s_vong; if (error>1000) duty=duty+50; else if (error>100) duty=duty+20; else if (error>30) {duty=duty+5;} else if (error>20) duty=duty+1.5; else if (error>10) duty=duty+(0.05*error); else {duty=duty+(0.025*error);} } if (luu<(s_vong-2)&& (duty>0)) {error=s_vong-luu; if (error>=10) duty=duty-(0.05*error); else (duty=duty-(0.02*error)); } if(luu==s_vong) duty=duty; if((duty>=250)&&(luu>s_vong)) { lcd_gotoxy(1,1); printf(lcd_putc," "); lcd_gotoxy(1,1); printf(lcd_putc," TD_tt=MAX ");} if (d==0) {set_pwm1_duty(0); set_pwm2_duty(0);} else if (d==1) {set_pwm1_duty(duty); set_pwm2_duty(0);} else {set_pwm1_duty(0); set_pwm2_duty(duty);} } //========chuong trinh con nhap toc do======= void ghi_tocdo() { if(i>=0&&i<=3) { // giai thuat luu eeprom if (i==0) e0=sttphim; else if (i==1) e1=sttphim; else if (i==2) e2=sttphim; else {e3=sttphim;} //=============================== setpoint*=10; setpoint+=sttphim; i++; lcd_gotoxy(1,1); printf(lcd_putc," "); lcd_gotoxy(1,1); printf(lcd_putc,"TD_dat=%lu v/p",setpoint); luu=setpoint; } else { lcd_gotoxy(1,1); printf(lcd_putc," "); lcd_gotoxy(1,1); printf(lcd_putc,"0<TD_dat<=9999"); delay_ms(1000); lcd_gotoxy(1,1); printf(lcd_putc," "); lcd_gotoxy(1,1); printf(lcd_putc,"TD_dat=%lu s",setpoint); }} //=========chuong trình con nhap thoi gian========= void ghi_thoigian() { if(l>=0&&l<=3) { tg*=10; tg+=sttphim; l++; lcd_gotoxy(1,2); printf(lcd_putc," "); lcd_gotoxy(1,2); printf(lcd_putc,"TG_dat=%lu s",tg); tg1=tg/0.12; } else { lcd_gotoxy(1,2); printf(lcd_putc," "); lcd_gotoxy(1,2); printf(lcd_putc," 0<TG_dat<=999 "); delay_ms(1000); lcd_gotoxy(1,2); printf(lcd_putc," "); lcd_gotoxy(1,2); printf(lcd_putc,"TG_dat=%lu s",tg); }} //============chuong trinh con clear========= void clear() { setpoint=0; tg=0; i=0;l=0;m=0; e0=0;e1=0;e2=0;e3=0; lcd_gotoxy(1,1); printf(lcd_putc," "); lcd_gotoxy(1,1); printf(lcd_putc,"TD_dat=_"); lcd_gotoxy(1,2); printf(lcd_putc," "); lcd_gotoxy(1,2); printf(lcd_putc,"TG_dat=_"); } //=======chuong trinh con luu toc do vao epprom====== void read_rom() { k=read_eeprom(4); for (j=0;j<k;j++) { e=e*10; e=e+read_eeprom(j); }} //==========THE END======== |
|
|