qloi
30-08-2012, 09:42 PM
Các cao nhân có ai đã làm touch button theo phương pháp CVD sủ dụng ADC hoàn chỉnh cho mình hỏi đoạn code mình viết bên dưới theo như chỉ dẫn của Microchip nhưng có vẻ nó hoạt động không ổn định(không dứt khoát về 0 hoặc 1 mà nháy nháy hoặc sáng mờ) và không được nhạy lắm.khi lập trình 1 nút thì còn đở , bổ sung thêm 1 nút thì có khi vừa khởi động là sáng lun,có khi rờ nút này thì đèn kia lại sáng.giúp dùm nhé,ai có code điều khiển nhiều nút theo phương pháp này cho mình tham khảo với ?
#include <16f88.h>
#device *=16 ADC=10
#fuses intrc
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(b)
//#byte porta = 0x05
int16 average[5];
int16 temp=0,temp1=0,sens1=0,sens2=0;
unsigned char i;
void read_sensor1()
{
set_adc_channel(0);
set_tris_a(0);
output_a(0x01);
set_tris_a(0xff);
set_adc_channel(1);
sens1 = read_adc();
//sens1 = sens1*5;
delay_us(10);
if(sens1<temp) output_high(pin_b0);
else output_low(pin_b0);
}
void read_sensor2()
{
set_adc_channel(0);
set_tris_a(0);
output_a(0x01);
set_tris_a(0xff);
set_adc_channel(2);
sens2 = read_adc();
//sens2 = sens2*5;
delay_us(10);
if(sens2<temp1) output_high(pin_b1);
else output_low(pin_b1);
}
void main()
{
setup_adc(adc_clock_internal);
setup_adc_ports(all_analog | VSS_VDD);
set_tris_b(0xc0);
output_b(0);
set_adc_channel(0); //chon kenh 0 lam analog in
set_tris_a(0); //port a out all
output_a(0x01); //set chan a0 = 1
set_tris_a(0xff); //set port a in all
set_adc_channel(1);//chon read adc nut 1
for(i=0;i<5;i++) //lay mau 5 lan
{
average[i] = read_adc();
delay_us(8);
temp = temp + average[i]; //cong don va lay gia tri trung bình
}
temp = temp/5;
set_adc_channel(0);
set_tris_a(0);
output_a(0x01);
set_tris_a(0xff);
set_adc_channel(2);
for(i=0;i<5;i++)
{
average[i] = read_adc();
delay_us(8);
temp1 = temp1 + average[i];
}
temp1 = temp1/5;
while(1)
{
read_sensor1();
read_sensor2();
}
}
#include <16f88.h>
#device *=16 ADC=10
#fuses intrc
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(b)
//#byte porta = 0x05
int16 average[5];
int16 temp=0,temp1=0,sens1=0,sens2=0;
unsigned char i;
void read_sensor1()
{
set_adc_channel(0);
set_tris_a(0);
output_a(0x01);
set_tris_a(0xff);
set_adc_channel(1);
sens1 = read_adc();
//sens1 = sens1*5;
delay_us(10);
if(sens1<temp) output_high(pin_b0);
else output_low(pin_b0);
}
void read_sensor2()
{
set_adc_channel(0);
set_tris_a(0);
output_a(0x01);
set_tris_a(0xff);
set_adc_channel(2);
sens2 = read_adc();
//sens2 = sens2*5;
delay_us(10);
if(sens2<temp1) output_high(pin_b1);
else output_low(pin_b1);
}
void main()
{
setup_adc(adc_clock_internal);
setup_adc_ports(all_analog | VSS_VDD);
set_tris_b(0xc0);
output_b(0);
set_adc_channel(0); //chon kenh 0 lam analog in
set_tris_a(0); //port a out all
output_a(0x01); //set chan a0 = 1
set_tris_a(0xff); //set port a in all
set_adc_channel(1);//chon read adc nut 1
for(i=0;i<5;i++) //lay mau 5 lan
{
average[i] = read_adc();
delay_us(8);
temp = temp + average[i]; //cong don va lay gia tri trung bình
}
temp = temp/5;
set_adc_channel(0);
set_tris_a(0);
output_a(0x01);
set_tris_a(0xff);
set_adc_channel(2);
for(i=0;i<5;i++)
{
average[i] = read_adc();
delay_us(8);
temp1 = temp1 + average[i];
}
temp1 = temp1/5;
while(1)
{
read_sensor1();
read_sensor2();
}
}