PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 26-05-2013, 12:19 PM   #6
navypro
Đệ tử 1 túi
 
Tham gia ngày: May 2013
Bài gửi: 20
:
Trích:
Nguyên văn bởi navypro View Post
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();

}
}
}
navypro vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 12:40 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam