Trích:
Nguyên văn bởi navypro
cái code này hơi dài nhưng đại thể là trong while(1) em dùng tiếp một while nữa điều kiện được lấy ra từ ADC nếu giá trị đọc được tới cái ngưỡng kia thì sẽ thoát nhưng mà vào chạy cái là đơ ngay không chạy được nưa
|
code đây bác:
#include <16f877a.h>
#device *=16 ADC=10
#fuses XT,NOBROWNOUT NOWDT
#use delay (clock=4000000)
#include "hienthiLCD.c"
// dinh nghia port
#byte Porta =0x05
#byte Portb =0x06
#byte Portc =0x07
#byte Portd =0x08
#byte Porte =0x09
//dinh nghia cac chan cong nan tin hieu vao
#bit in_lan =porta.1
#bit in_noi =Porte.0
#bit chedo_1 =Porte.1
#bit chedo_2 =Porte.2
// dinh nghia chan xuat tin hieu ra led
#bit tren =Portd.0
#bit duoi =Portd.1
#bit lan =Portd.2
#bit noi =Portd.3
#bit che_do_2 =Portd.4
#bit che_do_1 =portd.5
#bit tau_mat_nuoc =portd.6
#bit tau_ngam =portd.7
// khai bao cac bien
int count;
unsigned int16 suon_len, suon_xuong;
int16 d;
int8 adc;
float p,g;
int x;
//chuong trinh con do ap suat tuc thoi tai chan AN0
void ap_suat()
{
Set_ADC_channel ( 0 ) ;
Delay_us (10 );// delay 10 us
adc= read_adc();
p=0.00488*adc; //dien ap thu duoc o chan cong AN0
}
// ngat timer 1 dung tang bien dem khi xay ra ngat
#int_TIMER1
void TIMER1_isr()
{
++count;
}
// dung ngat CCP2 de xac dinh do rong xung
#int_CCP2
void CCP2_isr()
{
suon_len = CCP_2;
suon_xuong = CCP_1;
d = suon_len-suon_xuong;
set_timer1(0);
}
// chuong trinh doi do rong xung ra goc nghieng
void goc_nghieng()
{
if(d<126)
{
g=-1*((126-d)*30/75);
}
if(d>126)
{
x=(d-126)%5;
if(x==0)
g=((d-126)*30/75);
else
g=1+((d-126)*30/75);
}
if(d==126)
g=0;
}
void matnuoc_7()
{
goc_nghieng();
if(g>-3)
{
output_low(pin_d0);
output_high(pin_d1);
}
if(g<-4)
{
output_high(pin_d0);
output_low(pin_d1);
}
}
void ngam_7()
{
goc_nghieng();
if(g>10.5)
{
output_high(pin_d0);
output_low(pin_d1);
}
else
{
output_high(pin_d0);
output_low(pin_d1);
}
}
void do_7()
{
if(p>=0.3202)
{
output_high(pin_d0);
output_low(pin_d1);
}
if(p<=0.2604)
{
output_high(pin_d0);
output_low(pin_d1);
}
}
void main()
{
lcd_init();
set_tris_a(0xff);
set_tris_e(0xff);
set_tris_b(0x00);
set_tris_c(0xff);
set_tris_d(0x00);
output_d(0x00);
//khoi dong chuc nang capture cua thiet bi
setup_ccp2(CCP_CAPTURE_FE);
setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_4 );//set timer1 to run at system clock/4
enable_interrupts(INT_TIMER1); //unmask Timer1 overflow interrupt
enable_interrupts(INT_CCP2); //unmask capture event interrupt
enable_interrupts(global); //enable all unmasked interrupts
// khoi dong ADC
setup_adc(ADC_CLOCK_INTERNAL );
Setup_ADC_ports(AN0);
while(true)
{
ap_suat();
goc_nghieng();
lcd_gotoxy (1,1);
printf(lcd_putc,"p= %1.2f;g= %f",p,g);
//kiem tra
while(p<0.2802)
{
output_high(pin_d6);
output_low(pin_d7);
lcd_gotoxy(1,2);
printf(lcd_putc," NUOC ");
matnuoc_7();
}
}
}