Bạn xem code này nha:
Code:
#include<16f887.h>
#device *=16 ADC=10
#fuses HS,NOLVP,NOWDT,NOPUT
#use delay (clock=12M)
#use fast_io(c)
int16 gt2=0;
int tai=0;
void checkTAI(){ // kiem tra ADC tu tai.
gt2=read_adc();
if (gt2>640 && gt2<680) // muc cao nhat cua tai oke
tai=1;
else
tai=0;
}
void checkDEN(){ // chuong trinh kiem tra den tin hieu
if(tai)
output_high(pin_c3); // den tin hieu bao oke
else
output_low(pin_c3); // den tin hieu ko sang
}
void main(){
set_tris_c(0x00);
output_c(0x00);
setup_adc(adc_clock_internal);
setup_adc_ports(sAN0);
while (1){
delay_us(20);
checkTAI();
checkDEN();
}
}