PDA

View Full Version : PIC16F877A Mạch đếm số người ra/vào phòng


trunghieu266
26-11-2012, 01:08 AM
các bác xem dùm cái code trên sửa thế nào: mình định hạn chế số người đi vào trên 100 thì sẽ không đếm nữa mà khi chạy mô phỏng và chạy ra mạch thật đều ko được, nó đếm hẳn lên mấy ngàn @@:
#include<16f877a.h>
#Fuses HS,NOLVP,NOPUT,NOWDT
#use delay(clock=12000000)
#use fast_io(b)
#use fast_io(d)
#byte RB = 0x06
#bit RB7 = RB.7
#byte RD = 0x08
#bit G = RD.3
int8 maled[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x9 0};
int8 maG[10]={1,1,0,0,0,0,0,1,0,0};
signed int16 i, tram, nghin, chuc,donvi,n=0;
int1 s1=0,s2=0;
#INT_EXT
void demtien()
{
s1=1;
if(s2==1)
{
i++;
if(i==100) i=100;// đoạn này đây ạ!
s1=0;
s2=0;
disable_interrupts(INT_TIMER0);
}
else
{
enable_interrupts(INT_TIMER0);
set_timer0(0);
n=0;
}
}
#INT_RB
void ngat()
{
if(RB7==0)
{
s2=1;
if(s1==1)
{
i--;
if(i<0) i=0;
s1=s2=0;
disable_interrupts(INT_TIMER0);
}
else
{
enable_interrupts(INT_TIMER0);
set_timer0(0);
n=0;
}
disable_interrupts(INT_RB);
}
}
#INT_TIMER0
void ngat_timer0()
{
n++;
if(n==220)
{
n=0;
if(((s1==1)&&(s2==0))||((s1==0)&&(s2==1)))
s1=s2=0;
disable_interrupts(INT_TIMER0);
}
set_timer0(0);
}
void hienthi(int16 a)
{
nghin = a/1000;
tram = (a%1000)/100;
chuc = ((a%1000)%100)/10;
donvi = ((a%1000)%100)%10;
//===================
output_high(PIN_D4);
output_b((maled[nghin]<<1)|0x81);G=maG[nghin];
delay_ms(1);
output_low(PIN_D4);
output_b(0xFF);
//================
output_high(PIN_D5);
output_b((maled[tram]<<1)|0x81);G=maG[tram];
delay_ms(1);
output_low(PIN_D5);
output_b(0xFF);
//===============
output_high(PIN_D6);
output_b((maled[chuc]<<1)|0x81);G=maG[chuc];
delay_ms(1);
output_low(PIN_D6);
output_b(0xFF);
//==================
output_high(PIN_D7);
output_b((maled[donvi]<<1)|0x81);G=maG[donvi];
delay_ms(1);
output_low(PIN_D7);
output_b(0xFF);
}
void khoitao()
{
set_tris_b(0x81);
set_tris_d(0x00);
enable_interrupts(global);
enable_interrupts(INT_EXT);
enable_interrupts(H_to_L);
enable_interrupts(INT_RB);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
i=0;
}
void main()
{
khoitao();
while(true)
{
hienthi(i);
if(RB7==1) enable_interrupts(INT_RB);
}

}

lambaotrung
03-12-2012, 09:40 PM
nếu hiển thi LED 7ge thi nguyên cứu thuật toán display_led7.

còn về thuật toán đếm thì đơn giản thôi mà

INT16 DEM;

#int_ext
VOID NGAT()
{
DEM++; // KHI XẢY RA NGẮT NGOÀN "RB0" THI BIẾN "DEM=DEM+1;"
IF(DEM>=100)
{RB1=1} // ĐƯA GIÁ TRỊ TẠI CHÂN RB1 LEN "1" MẮT VÀO CHÂN NÀY CÒN LED SE THAY NO SÁNG LÊN. ĐƠN GIẢN MÀ.

}


NẾU MUỐN HIỆN GIA TRỊ LÊN LED7GEN THÌ PHẢI CHIA BIẾN "DEM"
VOID XULY(INT16 X)
{
INT TRAM, CHUC, DONVI;
TRAM=(X%1000)/100;
CHUC=(X%100)/10;
DONVI=(X%10);


}

lambaotrung
03-12-2012, 09:48 PM
//=== CÒN ĐÂY LÀ THUẬT TOAN HIỂN THI 1 GIÁ TRỊ RA LED7 ===//
void led7(unsigned int16 x)
{
//============= Convert DATA ==================================///
//==== Phu trach chia nho 1 bien so thanh cac so con 8bit =====//
nghin=(x%10000)/1000;
tram=(x%1000)/100;
chuc=(x%100)/10;
donvi=x%10;
//================================================== =============//

//============ Hien thi lan luot cac so 8bit===========//
//int8 const led[]={63,6,91,79,102,109,125,39,127,111}; // Bo so cho LED KATOT chung

int const led[] = {0b01000000,0b01111001,0b00100100,0b00110000,0b000 11001,0b00010010,0b00000010,0b01111000,0b00000000, 0b00010000}; // BO so cho Anot chung

for(i=8;i>=1;i--) //========== LED7 thu 1========//
{
a=led[donvi]; // ============ Hien thi hang " DONVI " ===//
value = bit_test(a,i-1);
output_bit(PIN_C1,value);
output_high(PIN_C0);//dich du lieu
output_low(PIN_C0);
}

output_high(PIN_C2);// chot du lieu
output_low(PIN_C2);
delay_ms(50);
output_low(PIN_C3);// xoa du lieu
output_high(PIN_C3);// xoa du lieu
}

//=================================================//

lambaotrung
03-12-2012, 09:50 PM
Chúc bạn thành công