3. 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畛 LCD
Code:
#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) or
B5(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
4. #use delay(clock=20000000)
int8 mode,tmin,tmax; // Khai bao cac bien dung trong ct
int16 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 phim
while(INPUT(PIN_C0)==0); // Cho den khi nut duoc tha ra
mode++; // Tang mode len 1 de chuyen sang mode ke tiep
if(mode==3) mode=0; // Lap lai mode 0 khi da chuyen qua het cac
mode
anphim=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 theo
mode, "tmin" >0 th狸 gi畉m 1
if(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 luc
if(INPUT(PIN_C2)==0)
{
if(mode==1)
{
if(tmin<tmax-1) tmin++; anphim=1; // Tang
nguong tuy theo mode. Tmin khong the >tmax-1
}
if(mode==2)
{
if(tmax<90) tmax++; anphim=1; // tmax khong lon
hon 90do.
}
}
}
else
if(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
5. if (ct>=tmin&&ct<=tmax) // Neu nhiet do nam trong
khoang giua 2 nguong
{ // thi nut 4 co chuc
nang bat/tat quat
bat=~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 duoi
tmin
{ // 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 tren
t = ct;
chuc = t/10 + 48; // Tach so hang chuc va hang don vi
donvi = t%10 + 48;
setcursor(0,1);
wrtchar("NHIET DO: "); // Hien thi so do nhiet do len LCD
wrtchar(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 ra
LCD
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() // Ham cai dat nguong nhiet do max (tuong tu ham setmin)
{
int8 t,chuc,donvi;
setcursor(0,0);
wrtchar("- HEN BAT QUAT -");
t = tmax;
6. 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 timer1
void ct_phuc_vu_ngat()
{
CLEAR_INTERRUPT(INT_TIMER1); // Xoa co ngat timer1
DISABLE_INTERRUPTS(GLOBAL); // Khong cho phep ngat khac
cnt++; // Sau moi ngat ta tang bien dem
if(cnt==10) // khi cnt=10 thi da dem duoc 10 lan tuong ung voi
1s
{
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 ADC
setup_adc_ports(AN0); // Ngo vao Analog la chan AN0
SET_ADC_CHANNEL(0); // Chon kenh chuyen doi ADC la kenh 0
delay_us(20);
mode = 0; // Khoi tao ban dau voi tmin=20*C va tmax=27*C
tmin = 20; // va hien thi mode do nhiet do dau tien
tmax = 27;
lcd_init(); // Khoi tao LCD
wrtcmd(0x01);
wrtcmd(0x28);
wrtcmd(0x0c);
SETUP_TIMER_1(T1_INTERNAL|T1_DIV_BY_8); // Cai dat timer1 voi bo chia
tan 1:8
SET_TIMER1(3035); // Gia tri khoi tao cho timer1 ( dem den 65535
// voi clock 20Mhz chia 8 => mot ngat tuong ung voi
0.1s )
ENABLE_INTERRUPTS(INT_TIMER1); // Cho phep ngat timer1
7. ENABLE_INTERRUPTS(GLOBAL); // Cho phep ngat toan cuc
ct = READ_ADC(); // Doc gia tri nhiet do dau tien
ct = ct*0.49;
while(1) // Vong lap vo han
{
selectmode();
do
{
if(docnhietdo==1) // Neu nhiet do duoc doc thi hien thi
ra LCD neu dang
{ // o mode hien thi va tien hanh kiem
tra nguong
if(mode==0) { hiennhietdo(); }
check(); // Kiem tra nguong
}
readkb();
}
while(anphim==0); // Lien tuc lap lai viec kiem tra xem nhiet
do
} // co duoc doc hay ko va kiem tra phim bam
}