$crystal <Attiny2313.h>
$regfile <Delay.h>
#define Sw Pind.6
#define Jmp Pinb.0
#define Vcc Pinb.2
#define Clk Portb.3
#define Rst Portb.4
#define In Portb.1
#define Out Pinb.1
#define Led Portd.0
#define Buzz Portd.1
#define Prz Portb.5
#define Del 15
Char Pytanie[48] = {1 , 0 , 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 0} ; / / Tablica Zawierajaca Zadawane Karcie Pytanie
Char Int_licz , Int_licz1 , Int_licz2 = 0 , Wrong_card ; / / Na Podstawie Ktorego Generuje Ona Odpowiedz
Unsigned Int Bin;
Bit Ok = 1;
Eeprom Unsigned Int Chip_code[20] ; / / Tablica Przechowujaca Aktywowane Karty Telefoniczne
Eeprom Int Licz_kart;
Char N;
Int Li_kart;
Void Reset(void) / / Procedura Resetujaca Karte Chipowa
{
Rst = 1;
Delay_us(del);
Clk = 1;
Delay_us(del);
Clk = 0;
Delay_us(del);
Rst = 0;
Delay_us(del);
}
Void Inc_adss(void) / / Procedura Zwiekszajaca Adres Na Karcie Chipowej
{
Rst = 0;
Delay_us(del);
Clk = 1;
Delay_us(del);
Clk = 0;
Delay_us(del);
}
Void Wr_bit(void) / / Procedura Zapisujaca Bit
{
Rst = 1;
Delay_us(del);
Rst = 0;
Delay_us(del);
Clk = 1;
Delay_us(del);
Clk = 0;
Delay_us(del);
}
Void Wr_carry(void) / / Procedura Zapisujaca Bit Carry Na Karcie Chipowej
{
Rst = 1;
Delay_us(del);
Rst = 0;
Delay_us(del);
Clk = 1;
Delay_us(del);
Clk = 0;
Delay_us(del);
Rst = 1;
Delay_us(del);
Rst = 0;
Delay_us(del);
Clk = 1;
Delay_us(del);
Clk = 0;
Delay_us(del);
}
Unsigned Int Get_code(void) / / Pobiera Unikalne 16 Bitowe Slowo Karty
{ / / Na Podstawie Slowa Wejsciowego
Char I , J , K , L;
Unsigned Int Code = 0 , Bin;
In = 1;
Ddrb.1 = 1;
Reset();
Code = 0;
For(i = 0 ; I < 110 ; I + + )
{
Inc_adss();
}
Wr_bit();
For(i = 0 ; I < 177 ; I + + )
{
Inc_adss();
}
For(j = 0 ; J < 48 ; J + + )
{
In = Pytanie[j];
Inc_adss();
}
Ddrb.1 = 0;
In = 1;
For(k = 0 ; K < 16 ; K + + )
{
For(l = 0 ; L < 160 ; L + + )
{
Inc_adss();
}
If(out == 1)
{code = Code + 1;
}
If(k! = 15) Code = Code << 1;
}
Return Code;
}
Void Dzwiek(void) / / Generuje Dzwiek - Podwojny Krotki
{ Int Q;
For(q = 0 ; Q < 2 ; Q + + )
{
Buzz = 0;
Delay_ms(70);
Buzz = 1;
Delay_ms(40);
}
}
Char Check_card(void) / / Procedurka Porownujaca Slowo Pobrane Z Karty Z
{unsigned Int C ; / / Zawartoscia Eepromu Jezeli Ok. To Zwraca 1
Int J;
C = Get_code();
For(j = 0 ; J < Licz_kart ; J + + )
{if(chip_code[j] == C)
Return 1;
}
Return 0;
}
/ / Procedurka Obslugi Przerwania Od Timera T0(generowane Co Ok. 10ms)
Interrupt [tim0_ovf] Void Timer0_ovf_isr(void)
{
#asm( "cli")
If(!sw && Vcc)
{ Bin = Get_code();
If(!check_card() && Bin! = 0 && Bin! = 65535 && Licz_kart < 20)
{chip_code[licz_kart] = Bin;
Licz_kart + = 1;
Li_kart + = 1;
Dzwiek();
Wrong_card = 0 ; }
Else
{
}
While(vcc)
;
}
Ddrd.6 = 0 ; / / Sw Jako Wejscie
Portd.6 = 1;
If(sw == 0)
{if( + + Int_licz2 > 15)
{ Int_licz2 = 0;
Buzz = 0;
Led = 1;
Delay_ms(400);
Buzz = 1;
Led = 0;
Licz_kart = 0;
Li_kart = 0;
}
} Else {int_licz2 = 0;
}
If(wrong_card > 4)
{ Int_licz1 = 0;
Wrong_card = 0;
Buzz = 0;
Prz = 0;
For(n = 0 ; N < 20 ; N + + )
Delay_ms(1000);
Buzz = 1;
}
If( + + Int_licz1 == 3000)
{ Wrong_card = 0;
}
If(jmp == 0 && Prz == 1)
{ If( + + Int_licz == 30)
{int_licz = 0;
Prz = 0 ; }
}
Else Int_licz = 0;
#asm( "sei")
}
Void Main(void)
{
Portb = 56;
Ddrb = 56;
Portd.0 = 1;
Ddrd.0 = 1;
Portd.1 = 1;
Ddrd.1 = 1;
Tccr0 = 0x05;
Tcnt0 = 0x00;
Tccr1a = 0x00;
Tccr1b = 0x00;
Tcnt1h = 0x00;
Tcnt1l = 0x00;
Ocr1ah = 0x00;
Ocr1al = 0x00;
Gimsk = 0x00;
Mcucr = 0x00;
Timsk = 0x02;
Acsr = 0x80;
Ddrb.2 = 0 ; / / Vcc Jako Wejscie
Portb.2 = 1;
Ddrd.6 = 0 ; / / Sw Jako Wejscie
Portd.6 = 1;
Ddrb.0 = 0 ; / / Jmp Jako Wejscie
Portb.0 = 1;
Clk = 0;
Rst = 0;
Led = 0;
Li_kart = Licz_kart;
#asm( "sei")
While(1)
{
Int I;
Delay_ms(3);
If(vcc == 1)
{led = 1;
Bin = Get_code();
If(check_card() && Bin! = 0 && Bin! = 65535)
{
If(jmp == 1)
{
Dzwiek();
Prz = !prz;
}
Else
{
Prz = 1;
Dzwiek();
}
}
Else
{
Wrong_card + = 1;
Int_licz1 = 0;
}
Led = 0;
While(vcc == 1)
;
}
};
}