- Code chương trình được biên dịch trên phần mềm CCS cho PIC 16F877A
#include <16F877A.h>
#include <def_877a.h>#device *=16 ADC=10#include <lcd.h>#FUSES NOWDT#FUSES HS#FUSES NOPUT#FUSES NOPROTECT#FUSES NODEBUG#FUSES NOBROWNOUT#FUSES NOLVP#FUSES NOCPD#FUSES NOWRT#FUSES RESERVED#use delay(clock=20000000)int8 mode,tmin,tmax;int16 ct;int1 bat,anphim,docnhietdo;int8 cnt=0;void readkb(){anphim=0;if(INPUT(PIN_C0)==0){delay_ms(150);while(INPUT(PIN_C0)==0);mode++;if(mode==3) mode=0;anphim=1;}else{if(INPUT(PIN_C1)==0){delay_ms(150);if(INPUT(PIN_C1)==0){if(mode==1){ if(tmin>0) tmin--; anphim=1; }if(mode==2){ if(tmax>tmin+1) tmax--; anphim=1; }}}else{if(INPUT(PIN_C2)==0){delay_ms(150);if(INPUT(PIN_C2)==0){if(mode==1){if(tmin<tmax-1) tmin++; anphim=1;}if(mode==2){if(tmax<90) tmax++; anphim=1;}}}elseif(INPUT(PIN_C3)==0){delay_ms(150);while(INPUT(PIN_C3)==0);if (ct>=tmin&&ct<=tmax){bat=~bat;if(bat==1) OUTPUT_BIT(PIN_D0,1);if(bat==0) OUTPUT_BIT(PIN_D0,0);anphim=1;}}}}}void check(){if (ct<tmin) { OUTPUT_BIT(PIN_D0,0); bat=0; }if (ct>tmax) { OUTPUT_BIT(PIN_D0,1); bat=1; }}void hiennhietdo(){int8 t,chuc,donvi;setcursor(0,0);wrtchar("- DO NHIET DO -");t = ct;chuc = t/10 + 48;donvi = t%10 + 48;setcursor(0,1);wrtchar("NHIET DO: ");wrtchar(chuc);wrtchar(donvi);wrtchar("*C");delay_ms(50);}void settmin(){int8 t,chuc,donvi;setcursor(0,0);wrtchar("- HEN TAT QUAT -");t = tmin;chuc = t/10 + 48;donvi = t%10 + 48;setcursor(0,1);wrtchar("NEU DUOI: ");wrtchar(chuc);wrtchar(donvi);wrtchar("*C");delay_ms(50);}void settmax(){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(){switch (mode){case 0: hiennhietdo(); break;case 1: settmin(); break;Page 17case 2: settmax(); break;}}#INT_TIMER1void ct_phuc_vu_ngat(){CLEAR_INTERRUPT(INT_TIMER1);DISABLE_INTERRUPTS(GLOBAL);cnt++;if(cnt==10){docnhietdo=1;ct = READ_ADC();ct = ct*0.49;cnt=0;}else docnhietdo=0;SET_TIMER1(3035);ENABLE_INTERRUPTS(GLOBAL);}void main(){Setup_ADC(ADC_CLOCK_INTERNAL);setup_adc_ports(AN0);SET_ADC_CHANNEL(0);delay_us(20);mode = 0;tmin = 20;tmax = 27;lcd_init();wrtcmd(0x01);wrtcmd(0x28);wrtcmd(0x0c);SETUP_TIMER_1(T1_INTERNAL|T1_DIV_BY_8);SET_TIMER1(3035);ENABLE_INTERRUPTS(INT_TIMER1);ENABLE_INTERRUPTS(GLOBAL);ct = READ_ADC();ct = ct*0.49;while(1){selectmode();do{if(docnhietdo==1){if(mode==0) { hiennhietdo(); }check();}readkb();}while(anphim==0);}}
Mạch mô phỏng:
Không có nhận xét nào:
Đăng nhận xét