Arkadaşlar bir türlü mikro Elektronikanın Modülünü çalıştıramadım.. bu modülle ilgili Çalışma yapan var mı ?
Not: üzerinde em4095 cipi var..
(http://www.mikroe.com/img/development-tools/accessory-boards/various/rfid-reader/preview/rfid_reader_thumb01.png)
www.elektrovadi.com (http://www.elektrovadi.com) sitesinde o kart satılıyor. Ürün linkine gidersen örnek uygulama kodları bulunuyor.
Oradaki Kodları kullanarak ben çalıştırmayı başaramadım.. o yüzden daha önce çalışma yapan arkadaşlardan döküman veya yardım rica ediyorum
Çinden 1 kart 1 anahtarlık ve rfid modülünü 40TL ye aldım. Arduino kodlarını da paylaştılar. Çalıştırdım...
http://www.ebay.com/itm/160700133830?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649 (http://www.ebay.com/itm/160700133830?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649)
70 lira bana uçuk geldi.
mesaj birleştirme:: 20 Ekim 2012, 13:22:45
buraya bir bak
https://www.picproje.org/index.php/topic,24119.0.html (https://www.picproje.org/index.php/topic,24119.0.html)
Petmak firmasının Rf id modullerinden epeyce kullandım. Turkiyede üretiliyor. Daha ucuz. Datasheet vs hep Türkçe. Mail atarsan teknik olarak yardimdada bulunurlar.
sonmicro'ya da bakın derim.
Arkadaşlar aranızda Ccs c ile ilgilenip benimle kodlarını paylasabilcek biri Var mi? En azindan kart okutma ve lcd de Yazdirma ve seri Port kodlarini..bahsettiklerinizin uzerinde hangi chip var bilmiyorum. Benim rfid modulumde em4095 cipi var..baska chipi olan rfid moduller isime yarar mi bilmem. Bunu almis bulunduk..
Alıntı yapılan: Mucit23 - 20 Ekim 2012, 13:52:40
Petmak firmasının Rf id modullerinden epeyce kullandım. Turkiyede üretiliyor. Daha ucuz. Datasheet vs hep Türkçe. Mail atarsan teknik olarak yardimdada bulunurlar.
petmak dan bende memnunum forumdan Hoguz nickli akadasimiz sanirim firmada degil ama tavsiye ederim
Arkadaşlar Ben artık almış bulundum.. 90 tl verdiğim şeyi çöpe atıp yenisini almam doğru olmaz herhalde?
Yokmu Yardımcı olabilecek kimse?? En azından kendi kullandıgınız rfid modelleri hangisiyle onlarla ilgili ccs c de yazdığınız dökümanlarınızı paylaşabilecek kimse yokmu? MikroElektronika'Nın modülü olmasada olur.. farklı okuyuculara ait ccs c ve isis dosyalarınızı paylaşabilirmisiniz.
Alıntı yapılan: xoom - 22 Ekim 2012, 12:29:07
Yokmu Yardımcı olabilecek kimse?? En azından kendi kullandıgınız rfid modelleri hangisiyle onlarla ilgili ccs c de yazdığınız dökümanlarınızı paylaşabilecek kimse yokmu? MikroElektronika'Nın modülü olmasada olur.. farklı okuyuculara ait ccs c ve isis dosyalarınızı paylaşabilirmisiniz.
bir ise yaramzki baska modüllerin kodlari ! ben anlamyiorum hangi konuda zorluk cekiyorsunuz lcd de Merhaba dünya yazisini yazdirabiliyormusunuz ? donaniminiz dogrumu ? bu modül bir sekilde haberlesiyor kodlarinizda bu haberlesme icin gerekli pinleri configre ettinizmi ? hem yardim istiyorsunuz hemde elimizde düzgünce veri yok ayrica allah askina 3-4 baslik acipda yazmayin tek bir baslikdan ne sorununuz varsa yazin bizde ordan cevap vermeye calisalim en son bir baslikda size bir sey sordum donanim calisiyormu diye ama cevap yok cünkü bir sürü baslik var
Hocam 2 farklı başlıkta hareket etmemin amacı aynı modülü kullanan arkadaş o başlıkta paylaşmış fakat kendisi pek girmiyor sanırım.. neyse bu başlık altından cevap vereyim sorularınıza..
1- Modül çalışıyor tabiki..
2-Siz benim mesajlarımı okuyormuyorsunuz sanırım.. sordugunuz tüm sorulara diğer başlıkta cevap vermişim..
"ocam Programı çalıştırdıgımda ekranda " RFID kartınızı Okutunuz!" yazıyor ama kartı okutuyorum malesef ekrana birşey gelmiyor?
Aşağıdaki Kodla okuma fonksiyonunu çağırmış olmuyormuyum?"
--Bu demek oluyor ki LCD'de bişeyler görebiliyorum yani lcd kullanmayı biliyorum..
--Okuma kodunu göremiyorum diyorsunuz nasıl bi kod görmek istiyorsunuz..
--if(em4102_read(code))
{Bu parantezler arasında okuma varmı diye kontrol ediyorum
}
3- Ben şuan öğrenmek için rfid ile uğraşıyorum ileride seri portuda kullanacağımm.. içinde seri port kodlarının olması çok sorun değil..
ayrıca çok agresif davranıyorsunuz.. Yardımınız için yinede teşekkürler..
#include "C:\RFID Reader-F877.h"
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,PUT // Sigorta ayarları
#use delay(clock=4000000)
#include <em4095.c>
#include <em4102.c>
#include <lcd.c>
#use rs232 (baud=9600, xmit=pin_c6, rcv=pin_c7, parity=N, stop=1) // Seri Port Giriş ucları tanımlandı..
unsigned int8 code[5];
void main(void)
{
rf_init();
lcd_init();
while(1)
{
printf(lcd_putc,"\fRFID Kartınızı Okutunuz:");
Delay_ms(100);
if(read_4102(code))
{
disable_interrupts(global);
delay_ms(200);
lcd_gotoxy(2,1);
printf("%X-%X-%X-%X-%X"code[0],code[1],code[2],code[3],code[4]);
enable_interrupts(global);
}
}
}
bakin bemim c bilgim iyi değildir ama yinede bence bu kod ile okuma yapmiyorsunuz siz sadece init etmisiniz ama sürekli okuma yapmyiorsunuz önce okumayi yapip ondan sonra
if(read_4102(code)) buysa bunu yap diyeceksiniz (bence ) ayrica burda code ne ? sizin daha önce tanimladiginiz bir degermi ? ki kodda göremedim
mesaj birleştirme:: 22 Ekim 2012, 14:58:51
alin size mis gibi kod =)
/*
* Project name
RFiD (Displaying CRC check of RFid card via Usart)
* Copyright
(c) mikroElektronika, 2010.
* Revision History
20091220:
- initial release;
20101021:
- added active comments, sbit approach, code reorganized...
* Description
The code demonstrates using two external interrupts to read data sent
by EM4095 chip (clock - RDY/CLK; data - OUT).
Upon correct identification of the card, results are displayed via USART
along with the card specific number.
* Test configuration:
MCU: PIC18F4520
http://ww1.microchip.com/downloads/en/DeviceDoc/39631E.pdf
Dev.Board: EasyPIC6
http://www.mikroe.com/eng/products/view/297/easypic6-development-system/
Oscillator: HS-PLL, 32.0000 MHz
Ext. Modules: mE RFid Reader Board
ac:RFid_reader
http://www.mikroe.com/eng/products/view/185/rfid-reader-board/
SW: mikroC PRO for PIC
http://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/
* NOTES:
- mE RFid Reader Board should be connected to PORTB
- Make sure you turn on the apropriate switches to enable USART communication (board specific)
- Upon correct CRC check program will send "CRC CHECK OK!" via USART
- Usage of P18 family of MCUs and clock setting >= 32 MHz is recommended when working with RFid Reader Board
*/
sbit OUT at RB0_bit;
sbit RDY_CLK at RB1_bit;
sbit SHD at RB2_bit;
sbit MOD at RB3_bit;
sbit OUT_Direction at TRISB0_bit;
sbit RDY_CLK_Direction at TRISB1_bit;
sbit SHD_Direction at TRISB2_bit;
sbit MOD_Direction at TRISB3_bit;
unsigned short sync_flag, // in the sync routine if this flag is set
one_seq, // counts the number of 'logic one' in series
data_in, // gets data bit depending on data_in_1st and data_in_2nd
cnt, // interrupt counter
cnt1, cnt2; // auxiliary counters
unsigned short data_index; // marks position in data arrey
char i;
char _data[256];
char data_valid[64];
char bad_synch; // variable for detecting bad synchronization
void Interrupt() {
// This is external INT1 interrupt (for sync and sample)
// it is enabled until we get 128 data bits
if (INT1IF_bit && INT1IE_bit) {
cnt++; // count interrupts on INT1 pin (RB1)
INT1IF_bit = 0;
}
// This is external INT0 interrupt (for sync start)
// - once we get falling edge on RB0 we are disabling INT0 interrupt
else if (INT0IF_bit && INT0IE_bit) {
cnt = 0;
sync_flag = 1;
INT0IF_bit = 0;
INT0IE_bit = 0;
INT1IF_bit = 0;
INT1IE_bit = 1;
}
}
char CRC_Check(char *bit_array) {
char row_count, row_bit, column_count;
char row_sum, column_sum;
char row_check[5];
char column_check[11];
// row parity check:
row_count = 9; // count rows
while (row_count < 59) {
column_count = 0; // count columns
while (column_count < 5) {
row_check[column_count] = bit_array[row_count+column_count];
column_count++;
}
row_bit = 0; // count row bits
row_sum = 0;
while (row_bit < 4) {
row_sum = row_sum + row_check[row_bit];
row_bit++;
}
if (row_sum.B0 != row_check[4].B0) {
return 0;
}
row_count = row_count + 5;
}
// end row parity check
// column parity check
column_count = 9; // count columns
while (column_count < 13) {
row_bit = 0; // count column bits
row_count = 0; // count rows
while (row_bit < 11) {
column_check[row_bit] = bit_array[column_count+row_count];
row_bit++;
row_count = row_count + 5;
}
row_bit = 0; // count column bits
column_sum = 0;
while (row_bit < 10) {
column_sum = column_sum + column_check[row_bit];
row_bit++;
}
if (column_sum.B0 != column_check[10].B0) {
return 0;
}
column_count++;
}
// end column parity check
if (bit_array[63] == 1) {
return 0;
}
return 1;
}
// main program
void main() {
ADCON1 = 0x0F; // AD converter off
CMCON = 7;
OUT_Direction = 1;
RDY_CLK_Direction = 1;
SHD_Direction = 0;
MOD_Direction = 0;
SHD = 0;
MOD = 0;
UART1_Init(19200); // Initialise USART communication
Delay_ms(100);
sync_flag = 0; // sync_flag is set when falling edge on RB0 is detected
one_seq = 0; // counts the number of 'logic one' in series
data_in = 0; // gets data bit
data_index = 0; // marks position in data arrey
cnt = 0; // interrupt counter
cnt1 = 0; // auxiliary counter
cnt2 = 0; // auxiliary counter
// setup interrupts
INTEDG0_bit = 0; // Interrupt on falling edge on RB0
INTEDG1_bit = 1; // Interrupt on rising edge on RB1
INT0IF_bit = 0; // Clear INT0IF
INT1IF_bit = 0; // Clear INT1IF
INT0IE_bit = 0; // turn OFF interrupt on INT0
INT1IE_bit = 0; // turn OFF interrupt on INT1
GIE_bit = 1; // enable GIE
while (1) {
bad_synch = 0; // set bad synchronization variable to zero
cnt = 0; // reseting interrupt counter
sync_flag = 0; // reseting sync flag
INT1IF_bit = 0;
INT1IE_bit = 0; // disable external interrupt on RB1 (for sync and sample)
INT0IF_bit = 0;
INT0IE_bit = 1; // enable external interrupt on RB0 (start sync procedure)
while (sync_flag == 0) { // waiting for falling edge on RB0
asm nop
}
while (cnt != 16) { // waiting 16 clocks on RB1 (positioning for sampling)
asm nop
}
cnt = 0;
_data[0] = OUT & 1;
for (data_index = 1; data_index != 0; data_index++) { // getting 128 bits of data from RB0
while (cnt != 32) { // getting bit from RB0 every 32 clocks on RB1
asm nop
}
cnt = 0; // reseting interrupt counter
_data[data_index] = OUT & 1; // geting bit
if(data_index & 1)
if (!(_data[data_index] ^ _data[data_index-1]))
{
bad_synch = 1;
break; //bad synchronisation
}
}
INT1IE_bit = 0; // disable external interrupt on RB1 (for sync and sample)
if (bad_synch)
continue; // try again
cnt1 = 0;
one_seq = 0;
for(cnt1 = 0; cnt1 <= 127; cnt1++) { // we are counting 'logic one' in the data array
if (_data[cnt1 << 1] == 1) {
one_seq++;
}
else {
one_seq = 0;
}
if (one_seq == 9) { // if we get 9 'logic one' we break from the loop
break;
}
} // (the position of the last 'logic one' is in the cnt1)
if ((one_seq == 9) && (cnt1 < 73)) { // if we got 9 'logic one' before cnt1 position 73
// we write that data into data_valid array
data_valid[0] = 1; // (it has to be before cnt1 position 73 in order
data_valid[1] = 1; // to have all 64 bits available in data array)
data_valid[2] = 1;
data_valid[3] = 1;
data_valid[4] = 1;
data_valid[5] = 1;
data_valid[6] = 1;
data_valid[7] = 1;
data_valid[8] = 1;
for(cnt2 = 9; cnt2 <= 63; cnt2++) { // copying the rest of data from the data array into data_valid array
cnt1++;
data_valid[cnt2] = _data[cnt1 << 1];
}
if (CRC_Check(data_valid) == 1) { // if data in data_valid array pass the CRC check
UART1_Write_Text("CRC CHECK OK!"); // Writing of the CRC Check confirmation through USART communication
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)
for (i = 0; i <= 64; i++){ // This part of the code
// dislays the number of the specific RfID CARD
if (data_valid[i] == 0) {
Uart1_Write('0');
}
else {
Uart1_Write('1'); // at the end of this for loop you will get a string of "0" and "1"
}
} // specific to a single RfID CARD
UART1_Write(13); // Cariage return (view ASCII chart)
UART1_Write(10); // Line Feed (view ASCII chart)
Delay_ms(500);
}
}
}
}
ayrica datasheeti inceledinizmi ?
http://www.emmicroelectronic.com/webfiles/product/rfid/ds/EM4095_DS.pdf (http://www.emmicroelectronic.com/webfiles/product/rfid/ds/EM4095_DS.pdf)
mesaj birleştirme:: 22 Ekim 2012, 16:11:03
http://www.protonbasic.co.uk/showthread.php/57682-Rfid-with-EM4095?highlight=em4095 (http://www.protonbasic.co.uk/showthread.php/57682-Rfid-with-EM4095?highlight=em4095)
1.si ben sizi CCS C biliyorsunuz diye bu bölümde yardım isteğinde bulunmuştum bu konuda özür dilerim..
2.unsigned int8 code[5]; olarak code değişkenini zaten ben tanımlıyorum..
3.if(read_4102(code)) // Bu kod ile herhangi bir Tag Kart okuyucuya yaklaştırılırsa demek..
4.printf("%X-%X-%X-%X-%X"code[0],code[1],code[2],code[3],code[4]); // buradada aldıgım kodu Lcd'ye bastırıyorum.. yani kodu okuyunca seri porttan gönder onu yap bunu yap diye bir kod yazmadım.
LCD'de yazdırabildikten sonra Seri Porttan gönderme işlemi yaptıracağım..
-----------------
Gel gelelim bir yerde hata yaptığımı düşünerek forumdan yardım istiyorum hatam nerede diye.. Kaldıkı forumda CCS bilip pek soruma cvp veren çıkmadı..Sizin desteğiniz hariç
Alıntı yapılan: xoom - 22 Ekim 2012, 18:22:11
1.si ben sizi CCS C biliyorsunuz diye bu bölümde yardım isteğinde bulunmuştum bu konuda özür dilerim..
2.unsigned int8 code[5]; olarak code değişkenini zaten ben tanımlıyorum..
3.if(read_4102(code)) // Bu kod ile herhangi bir Tag Kart okuyucuya yaklaştırılırsa demek..
4.printf("%X-%X-%X-%X-%X"code[0],code[1],code[2],code[3],code[4]); // buradada aldıgım kodu Lcd'ye bastırıyorum.. yani kodu okuyunca seri porttan gönder onu yap bunu yap diye bir kod yazmadım.
LCD'de yazdırabildikten sonra Seri Porttan gönderme işlemi yaptıracağım..
-----------------
Gel gelelim bir yerde hata yaptığımı düşünerek forumdan yardım istiyorum hatam nerede diye.. Kaldıkı forumda CCS bilip pek soruma cvp veren çıkmadı..Sizin desteğiniz hariç
ayrintili aciklamaya gerek yokdu azda olsa c bilyiorum
neyse yukaridaki sorumu anlamadiniz, sanirim iletisim problemi yasiyoruz
Hocam sorunuzu gayet iyi anladım.. bende başka forumlarda kullanım şeklini arkadaşlar böyle kullanmış bende onlara göre kullandım.. örneğin..
https://www.dropbox.com/sh/rz1owonue7urv64/Rvu3x165_b (https://www.dropbox.com/sh/rz1owonue7urv64/Rvu3x165_b)
veya bu hocam CCS C de
https://www.dropbox.com/sh/1a5jx8y8w3skl9l/zi024WDLXQ (https://www.dropbox.com/sh/1a5jx8y8w3skl9l/zi024WDLXQ)
bu kisim niye kodunuzda yok ?
cevap='H';
for(i=0;i<=5000;i++)
{
if (kbhit())
{
cevap=getch();
break;
}
delay_ms(1);
}
kontrol(cevap);
---------------------------------------------------------------------------------------------
printf("%X-%X-%X-%X-%X"code[0],code[1],code[2],code[3],code[4]);
bu kisim sadece en basda herhangi bir deger gösterir sonra okuma yapildiktan sonra id nin degerini gösterir diye düsünüyorum sizdede problem bu konunuda yukaridaki bölüm yani printf sonrasi okuma kismi yok, hatta bu yukaridaki kismi yani okuma kismini printf den önce yapsaniz bence daha güzel olur lütfen deneyip sonucu yazarmisiniz.
Hocam Yok..O kısımı yazan arkadaşın projesi şu şekilde çalışıyor..
Arkadaş kartını Readar'a yaklaştırıyor ve LCD 'de kartın ID' sini görüyor..
Sonra Delphi'de hazırladığı bir arayüz programı ile Kart'a Evet mi Hayır'mı diye geri dönüş cevabı istiyor..Arkadaş Delphi'de hazırladıgı programdan E yazarsa Yeşil led yanıyor H yazarsa Kırmızı led yanıyor buzzer ötüyor falan.. ( Bunlarla benim şuan işim yok.. )
ben Şuan kartı Okutup LCD'de okumak istiyorum bu kadar..
peki bu kisim ne is yapiyor ?
for(i=0;i<=5000;i++)
{
if (kbhit())
{
cevap=getch();
break;
}
delay_ms(1);
}
kontrol(cevap);
Bahsettiğim Olayı yapıyor..
Kbhit(); // herhangi bir tuşa basılıp basılmadığını kontrol eder. Basılmadıysa 0 basıldıysa 1 döndürür.
basıldıysa basılan tuşu
cevap=getch(); // bu kodla basılan tuşu alır.. cevap adlı değişkene aktarır..
daha sonra
kontrol(cevap); // Ya doğru() veya yanlis() Fonksiyonlarını çalıştırır ve yeşil veya kırmızı ledi yakıp ikaz verdriyor..
Gel gelelim Ben halen lcd'de ID yi okuyamadım.. 2 olasılık kaldı ya RFid bozuk yada Benim Kodlar Yanlis.
kodu erhan yazmis bizim forumdaki erhan mi acaba ?
Hayır Bu forumda değil.. Diğer forumun ismini yazmam doğrumu bilmiyorum..
bence sorun olmazda simdi ben sorunu gercekten merak ediyorum hata nerde ve niye calismiyor birde dropboxdaki linkde bu dosya yok
#include <em4102.c>
mesela bu kbhit hic bir yerde tanimli degil burda ya bis dosya eksik yada kodda eksiklik var (dropboxdakinden bahsediyorum)
Hocam O dosyalar CCS C Compiler'ın içinde zaten var. em4095.c ve em4012.c kütüphaneleri mevcut.. hazır Rfid için Pic C compiler RFID için 2-3 hazır kütüphane koymuş.. tabi sen bu değilde başka bir RFID modül kullanırsan bu kutuphaneleri kullanman gerekmiyor.
diğer sorunuz ise kbhit() veya Getc() bunlar CCS de hazır fonksiyonlar hocam bunları tanımlamanız gerekmiyor.
Sorunu Kurban Bayramından sonra çözeceğim artık.. Şimdilik sadece araştırma geliştirme çalışmaları yapabiliyorum. Bu kısmı geçmeden Seri port kısmına mı geçsem acaba..
Erhan hocamın bir projesi var bir bak istersen gördüğüm kadarıyla aynı modülü kullanmış 16F88 ile yapmış fakat kod konusunda fikir verebilir.
http://www.mcu-turkey.com/pic16f88-ile-rf-id-okuyucu-uygulamasi/ (http://www.mcu-turkey.com/pic16f88-ile-rf-id-okuyucu-uygulamasi/)
Hocam zaten Yukaridaki kodlar ve 2. sayfanın ilk mesajındaki kodlar erhan hocama ait. fakat kendisi burada olsa keşke çok yardımcı olacaktı..
Arkadaşlar aşağıdaki kodlardan kırmızı ile yazılmış if döngüsünün if(read_4102(code)) sorgusunun nasıl çalıştığını aşağıdaki koda göre açıklayabilecek bir Usta CCS C ci arıyorum..
em4102.c ve em4095.c dosyalarını ekledim.. fakat yazılan bu kodtan if sorgusundaki read_4102(code) sorgusunu anlayamadım..
em4102.c dosyasında read_4102 adlı fonskiyona dallanıyor fakat ordan nasıl bir değerle geliyor anlamadım.. yada if sorgusunun true veya 1 olabilmesi için code adlı değişkenin değişmesi gerekir ama sorguya girmeden değişemeyeceği için oradaki mantıgı yakalayamadım..lütfen yardım..
kodlar aşağıda..
#include "C:\RFid.h"
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,PUT // Sigorta ayarları
#use delay(clock=4000000)
#include <em4095.c>
#include <em4102.c>
#include <lcd.c>
#use rs232 (baud=9600, xmit=pin_c6, rcv=pin_c7, parity=N, stop=1) // Seri Port Giriş ucları tanımlandı..
unsigned int8 code[5];
void main(void)
{
rf_init();
lcd_init();
while(1)
{
printf(lcd_putc,"\fRFID Kartınızı Okutunuz:");
Delay_ms(100);
[b][color=red] if(read_4102(code))[/color][/b]
{
disable_interrupts(global);
delay_ms(200);
lcd_gotoxy(2,1);
printf("%X-%X-%X-%X-%X"code[0],code[1],code[2],code[3],code[4]);
enable_interrupts(global);
}
}
}
em4102.c dosyası
/////////////////////////////////////////////////////////////////////////
//// EM4102.c ////
//// ////
//// This file contains drivers for a 4102 RF transponder ////
//// ////
//// int1 read_4102(int8* data) ////
//// - Call this funtion to read a 4102 transponder ////
//// - Pass in a pointer to a 5 byte array ////
//// - The first byte will have the customer code and the last ////
//// four bytes will contain the ID number ////
//// ////
//// - Returns FALSE if a parity check error occurred ////
//// - Returns TRUE if a transponder was read successfully ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2004 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////
#ifndef TRANSPONDER_4102_DRIVERS
#define TRANSPONDER_4102_DRIVERS
//#define UNIVERSAL_FORMAT
// Function Prototypes
int1 read_4102(int8* data);
int1 header_search_4102();
int1 decode_data_4102(int8* data);
// Purpose: Reads the ID number and data number
// Inputs: A pointer to a 5 byte array to fill
// * The first byte will have the ID
// * The last 4 bytes will have the data
// Outputs: TRUE if read successful, FALSE if read failed
int1 read_4102(int8* data)
{
int8 i;
RF_get(sizeof(RFbuffer)*8, TRUE); // Fill the buffer with data
for(i=0; i<2; ++i)
{
while(!END_OF_RFBUFFER)
{
if(header_search_4102()) // Try to find 9 consecutive 1s
{
if(decode_data_4102(data)) // Try to decode the data after the header
{
RFbuffer_fill(0xAA); // Prevents false detection
#ifdef UNIVERSAL_FORMAT
i=data[1];
data[1]=data[3];
data[3]=i;
i=data[0];
data[0]=data[4];
data[4]=i;
#endif
return TRUE; // Return sucessful read
}
}
}
RFbuffer_invert(); // Invert the buffer because the
} // Manchester encoded data could have
// been read starting at the wrong edge
RFbuffer_fill(0xAA); // Prevents false detection
return FALSE; // Return error
}
// Purpose: Search for the header consisting of 9 ones
// Inputs: None
// Outputs: TRUE if the header was found, FALSE if it was not found
int1 header_search_4102()
{
int bitCounter = 0;
// Loops until 9 consecutive 1s are found
// or the end of the receive buffer is reached
while(!END_OF_RFBUFFER)
{
if(RFbuffer_getBit() == 1)
{
if(++bitCounter == 9)
{
return TRUE;
}
}
else
{
bitCounter = 0;
}
}
return FALSE;
}
// Purpose: Decodes the ID number and data number
// Inputs: A pointer to a 5 byte array to fill
// * The first byte will have the ID
// * The last 4 bytes will have the data
// Outputs: TRUE if read successful, FALSE if read failed
int1 decode_data_4102(int8* data)
{
int1 bit = 0;
int8 count = 1;
int1 parity = 0;
int1 colParity1 = 0;
int1 colParity2 = 0;
int1 colParity3 = 0;
int1 colParity4 = 0;
// Loop until 40 bits of data and 10 bits of parity are received
for(count=1; count <= 50; ++count)
{
bit = RFbuffer_getBit();
if(END_OF_RFBUFFER)
{
return FALSE;
}
if(count % 5 == 0)
{
// Check for row parity
if(parity != bit)
{
return FALSE;
}
parity = 0;
}
else
{
// Store a bit of data
#ifdef UNIVERSAL_FORMAT
shift_left(data, 5, bit);
#else
shift_right(data, 5, bit);
#endif
// Calculate row parity
parity ^= bit;
// Calculate column parity
switch (count % 5)
{
case 1: colParity1 ^= bit; break;
case 2: colParity2 ^= bit; break;
case 3: colParity3 ^= bit; break;
case 4: colParity4 ^= bit; break;
}
}
}
// Check for column parity
if(colParity1 != RFbuffer_getBit() || colParity2 != RFbuffer_getBit() ||
colParity3 != RFbuffer_getBit() || colParity4 != RFbuffer_getBit() )
{
return FALSE;
}
// Check for stop bit
if(RFbuffer_getBit() != 0)
{
return FALSE;
}
// Prevents reading all zeros for customer ID and tag ID
for(count=0; count<5 && data[count] == 0; ++count);
if(count == 5)
{
return FALSE;
}
// Return TRUE if no errors in decoding received transponder data
return TRUE;
}
#endif
em4095.c dosyası
/////////////////////////////////////////////////////////////////////////
//// em4095.c ////
//// This file contains drivers for a EM4095 RFID basestation. ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// ////
//// Pin Layout ////
//// ------------------------------------------------------------ ////
//// | | ////
//// | 1: VSS GND | 16: DC2 | ////
//// | | | ////
//// | 2: RDY/CLK RF_RDY_CLK | 15: FCAP | ////
//// | | | ////
//// | 3: ANT1 | 14: SHD RF_SHD | ////
//// | | | ////
//// | 4: DVDD | 13: DEMOD_OUT RF_DEMOD_OUT | ////
//// | | | ////
//// | 5: DVDS | 12: MOD RF_MOD | ////
//// | | | ////
//// | 6: ANT2 | 11: AGND | ////
//// | | | ////
//// | 7: VDD +5V | 10: CDEC_IN | ////
//// | | | ////
//// | 8: DMOD_IN | 9: CDEC_OUT | ////
//// ------------------------------------------------------------ ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2004 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////
#ifndef EM4095
#define EM4095
#ifndef RF_SHD
#define RF_RDY_CLK PIN_C0 // External interrupt used to read clock
#define RF_SHD PIN_B1 // High disables the antenna signal
#define RF_MOD PIN_B2 // High does 100% modulation
#define RF_DEMOD_OUT PIN_C2 // Data read in interrupt service routine
#endif
// Provide a buffer for storing recieved data and data to be sent
#define RFBUFFER_SIZE 20
int8 RFbuffer[RFBUFFER_SIZE];
int8 RFbuffer_index = 0;
int8 RFbuffer_bitIndex = 0;
#define END_OF_RFBUFFER (RFbuffer_index == sizeof(RFbuffer))
/////////////////////////////////////////////////////////////////////////
//// Read modes available for reading data from a transponder
/////////////////////////////////////////////////////////////////////////
int8 RF_readMode;
#define RF_MANCHESTER_DATA 0 // Reads Manchester encoded data
#define RF_MEASURE_WIDTHS 1 // Measure a series of widths
#define RF_FIND_WIDTH 2 // Find a specific width
#define RF_FIND_PATTERN 3 // Find a pattern of widths
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//// Global Variables
/////////////////////////////////////////////////////////////////////////
int1 bitValue = 1;
int1 storeData = TRUE;
int1 RE_FE_TOGGLE = 1;
int1 RF_widthFound = FALSE;
int1 RF_patternFound = FALSE;
int8 RF_widthToFind = 0;
int8* RF_findWidths = 0;
int8 RF_uncertainty = 0;
int8 timer0_overflows = 0;
int8 dataTransferred = 0;
int16 old_clock = 0;
/////////////////////////////////////////////////////////////////////////
// Purpose: Initializes the 4095 into sleep mode
// Sets up the timers and interrupts
void rf_init()
{
output_low(RF_SHD);
output_low(RF_MOD);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1);
setup_ccp1(CCP_CAPTURE_RE);
setup_ccp2(CCP_COMPARE_INT);
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
}
// Purpose: Powers down the RF antenna
#define rf_powerDown() output_high(RF_SHD);
// Purpose: Powers up the RF antenna
#define rf_powerUp() output_low(RF_SHD);
// Purpose: Select which edge to begin reading data
void RF_readEdge(int1 edge)
{
if(edge)
{
setup_ccp1(CCP_CAPTURE_RE);
RE_FE_TOGGLE = 1;
}
else
{
setup_ccp1(CCP_CAPTURE_FE);
RE_FE_TOGGLE = 0;
}
}
// Purpose: Interrupt service routine to handle compare 1 interrupts.
// Reads incoming data from a transponder and stores it in
// the global buffer.
#INT_CCP1
void isr_ccp1()
{
int8 width;
// Toggle between capturing rising and falling edges to meausure width
if(RE_FE_TOGGLE)
{
setup_ccp1(CCP_CAPTURE_FE);
RE_FE_TOGGLE = 0;
}
else
{
setup_ccp1(CCP_CAPTURE_RE);
RE_FE_TOGGLE = 1;
}
// Calculate the width
width = CCP_1 - old_clock;
old_clock = CCP_1;
switch(RF_readMode)
{
// Use to receive manchester formatted data from a transponder
case RF_MANCHESTER_DATA:
{
if(width > 54) // Check for a phase change
{
bitValue = ~bitValue; // Invert the save bit value
storeData = TRUE; // Force a bit store
}
if(storeData)
{
shift_right(RFbuffer+RFbuffer_index, 1, bitValue);
++dataTransferred;
if(++RFbuffer_bitIndex == 8)
{
RFbuffer_bitIndex = 0;
++RFbuffer_index;
}
}
storeData = ~storeData;
break;
}
// Use to read high and low widths
case RF_MEASURE_WIDTHS:
{
RFbuffer[RFbuffer_index++] = width;
++dataTransferred;
break;
}
// Use to search for a certain pulse width
case RF_FIND_WIDTH:
{
if(width > (RF_widthToFind - RF_uncertainty)
&& width < (RF_widthToFind + RF_uncertainty))
{
RF_widthFound = TRUE;
}
break;
}
case RF_FIND_PATTERN:
{
if(width > RF_findWidths[RFbuffer_index] - RF_uncertainty
&& width < RF_findWidths[RFbuffer_index] + RF_uncertainty)
{
if(++RFbuffer_index == dataTransferred)
{
RF_patternFound = TRUE;
}
}
else
{
if(RFbuffer_index > 0)
{
int8 pos, i, j;
pos = RFbuffer_index-1; // Save the initial position
// Try to match partial pattern
while(--RFbuffer_index != 0)
{
if(width > RF_findWidths[RFbuffer_index] - RF_uncertainty
&& width < RF_findWidths[RFbuffer_index] + RF_uncertainty)
{
for(i=pos, j=RFbuffer_index-1; j!=255; --i, --j)
{
if(RF_findWidths[j] != RF_findWidths[i])
{
break;
}
}
if(j == 255)
{
break;
}
}
}
}
}
break;
}
}
}
// Purpose: This interrupt service routine is used
// to send data to a transponder
// Inputs: None
// Outputs: None
#INT_CCP2
void isr_ccp2()
{
static int1 mode = 1;
if(mode == 1 && !END_OF_RFBUFFER)
{
// Output high to modulate the antenna, so send a 0 with modulation pin high
output_bit(RF_MOD, !bit_test(RFbuffer[RFbuffer_index], RFbuffer_bitIndex));
if(++RFbuffer_bitIndex == 8) // Increment the buffer indexes as necessary
{
RFbuffer_bitIndex = 0;
++RFbuffer_index;
}
CCP_2 += 30; // Wait for half the bit period minus two RF periods
mode = 0; // Toggle the mode
}
else
{
output_low(RF_MOD); // No modulation
CCP_2 += 34; // Wait for half the bit period plus 2 RF periods before sending another bit
++dataTransferred; // Increment the bits transferred counter
mode = 1; // Toggle the mode
}
}
// Purpose: Interrupt for timer 0. Keeps track of the number of
// overflows for timeouts.
// Inputs: None
// Outputs: None
#INT_RTCC
void isr_rtcc()
{
++timer0_overflows;
}
// Purpose: Fill the buffer with data read from the basestation
// Inputs: 1) The number of bits to read
// 2) TRUE start on rising edge
// FALSE start on falling edge
// Outputs: The number of bits read. Could be used to check for timeout
int8 RF_get(int8 numBits, int1 edge)
{
RF_readEdge(edge);
RF_readMode = RF_MANCHESTER_DATA;
storeData = TRUE;
bitValue = 0;
RFbuffer_index = 0;
RFbuffer_bitIndex = 0;
dataTransferred = 0;
timer0_overflows = 0;
old_clock = 0;
set_timer1(0);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
while(dataTransferred < numBits && timer0_overflows < 15);
disable_interrupts(INT_CCP1);
RFbuffer_index = 0;
RFbuffer_bitIndex = 0;
return dataTransferred;
}
// Purpose: Send data from the buffer to the transponder
// Inputs: 1) Send numBits of data to the transponder
// 2) The index in the buffer to start at
// 3) The bit position at the index to start at
// Outputs: None
void RF_send(int8 numBits, int8 index, int8 bitPosition)
{
RFbuffer_index = index;
RFbuffer_bitIndex = bitPosition;
dataTransferred = 0;
CCP_2 = 3; //
set_timer1(0); // Cause an interrupt imediately
enable_interrupts(INT_CCP2);
while(dataTransferred < numBits);
disable_interrupts(INT_CCP2);
}
// Purpose: Search for a certain pulse width
// Inputs: 1) The width length in clocks
// 2) Uncertainty to search over a range
// 3) TRUE start on rising edge
// FALSE start on falling edge
// ex) numClocks = 128; uncertainty = 6; range = 122 to 134
// Outputs: TRUE if width was found, FALSE if not found
int1 RF_findWidth(int8 numClocks, int8 uncertainty, int1 edge)
{
RF_readEdge(edge);
RF_readMode = RF_FIND_WIDTH;
RF_widthToFind = numClocks;
RF_widthFound = FALSE;
RF_uncertainty = uncertainty;
timer0_overflows = 0;
old_clock = 0;
set_timer1(0);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
while(RF_widthFound == FALSE && timer0_overflows < 50);
disable_interrupts(INT_CCP1);
return RF_widthFound;
}
// Purpose: Measure a number of pulse widths, both high and low
// Inputs: 1) The number of widths to measure
// 2) TRUE start on rising edge
// FALSE start on falling edge
// Outputs: The number of widths that were measured. If there is
// no transponder in range, the timeout could occur.
int8 RF_measureWidths(int8 numWidths, int1 edge)
{
RF_readEdge(edge);
RF_readMode = RF_MEASURE_WIDTHS;
dataTransferred = 0;
RFbuffer_index = 0;
timer0_overflows = 0;
old_clock = 0;
set_timer1(0);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
while(dataTransferred < numWidths && timer0_overflows < 50);
disable_interrupts(INT_CCP1);
return dataTransferred;
}
// Purpose: Measure a number of pulse widths, both high and low
// Inputs: 1) A pointer to an array of widths. It is safe to use RFbuffer.
// 2) The number of widths in the pattern
// 3) Uncertainty to search over a range
// 4) TRUE start on rising edge
// FALSE start on falling edge
// Outputs: The number of widths that were measured. If there is
// no transponder in range, the timeout could occur.
int8 RF_findPattern(int8* widths, int8 numWidths, int8 uncertainty, int1 edge)
{
RF_readEdge(edge);
RF_readMode = RF_FIND_PATTERN;
RF_patternFound = FALSE;
RFbuffer_index = 0;
RF_findWidths = widths;
dataTransferred = numWidths;
RF_uncertainty = uncertainty;
timer0_overflows = 0;
old_clock = 0;
set_timer1(0);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
while(RF_patternFound == FALSE && timer0_overflows < 40);
disable_interrupts(INT_CCP1);
return RF_patternFound;
}
// Purpose: Set every byte in the buffer to data
// Inputs: None
// Outputs: None
void RFbuffer_fill(int8 data)
{
int i;
for(i=0; i<sizeof(RFbuffer); ++i)
{
RFbuffer[i] = data;
}
}
// Purpose: Inverts every byte in the buffer
// Inputs: None
// Outputs: None
void RFbuffer_invert()
{
int i;
for(i=0; i<sizeof(RFbuffer); ++i)
{
RFbuffer[i] = ~RFbuffer[i];
}
}
// Purpose: Get a bit of data from the buffer and increment to the next bit
// Inputs: None
// Ouputs: A bit of data
int1 RFbuffer_getBit()
{
int1 bit;
if(!END_OF_RFBUFFER)
{
bit = bit_test(RFbuffer[RFbuffer_index], RFbuffer_bitIndex);
if(++RFbuffer_bitIndex == 8)
{
++RFbuffer_index;
RFbuffer_bitIndex = 0;
}
}
return bit;
}
// Purpose: Get a byte of data from the buffer
// Inputs: None
// Outputs: The byte of data
int8 RFbuffer_getByte()
{
if(!END_OF_RFBUFFER)
{
int8 i;
int8 data;
for(i=0; i<8; ++i)
{
shift_right(&data, 1, RFbuffer_getBit());
}
return data;
}
}
// Purpose: Set the value of the next bit in the buffer
// Inputs: None
// Outputs: None
void RFbuffer_setBit(int1 bit)
{
if(!END_OF_RFBUFFER)
{
if(bit)
{
bit_set(RFbuffer[RFbuffer_index], RFbuffer_bitIndex);
}
else
{
bit_clear(RFbuffer[RFbuffer_index], RFbuffer_bitIndex);
}
if(++RFbuffer_bitIndex >= 8)
{
++RFbuffer_index;
RFbuffer_bitIndex = 0;
}
}
}
// Purpose: Set the value of the next byte in the buffer
// Inputs: None
// Outputs: None
void RFbuffer_setByte(int8 data)
{
if(!END_OF_RFBUFFER)
{
int8 i;
for(i=0; i<8; ++i)
{
RFbuffer_setBit(bit_test(data, 7));
rotate_left(&data, 1);
}
}
}
#endif
ben bununla if(read_4102(code)) funktion cagrildigini bilmiyordum ! ilk kez böyle bir sey gördüm sanirim burda if bakyior icerik varmi diye peki siz bu funktionu if ile degilde önce direk cagirip sonra printf ile degeri ekrana basin bakalaim ne gözükcek ? mantikli olmazmi ?
yani if(read_4102(code)) yerine
(read_4102(code);
temp= code;
printf("deger",code);
ayrica burda bir virgül eksik degilmi ? " den sonra (virgül), sonra code[0]
printf("%X-%X-%X-%X-%X"code[0],code[1],code[2],code[3],code[4]);
ben c ansiye göre konusuyorum ccs ye degil ama hemen hemen ayni olmasi gerek
Cevabı buldum ama halen kafam karışık..
Cevap şöyle.. if() komutu ile ya TRUE/FALSE yada 0 ve1 mi diye kontrol edilir..aşağıdaki kodda
if(read_4102(code)) ile
em4102.c dosyasına dallanır ve bu fonksiyon 1 veya 0 ile geri döner.. bu sebeple if döngüsünün içi if(1 veya True ) olursa if komutunun ait olan parantezler içindeki kodlar icra etmeye başlar..
Gel gelelim herşey doğru ama sonuç yok..
LCD'yi geçtim
output_high ( Pin_a0) yazdım ama sonuç olumsuz.. çözebilirsem sonucu paylaşacağım..
o zaman sizde hic bir sey calismyior dediigniz gibi ledi yak söndür programi yapin bakalim ne olcak (ayri bir program)
belki osilatör mclr vs hatalariniz vardir devre kendi boardunuzmu ? hazirmi ?
yok hocam pic çalışıyor bende çalışmayan tek şey kartı antene yaklaştırınca lcd de kartın ID'sini okumayı bırakın aynı zamanda basit kart yaklaştırınca filanca pindeki ledi yak diyorum yine olmuyor.. ama enerji verir vermeez başka porta ledi yakma söndürme komutu veriyorum onlar çalışıyor.. fakat kartı yaklaştırınca çalışması beklenen if(read_4102(code)) çalışmıyor ki döngü içindeki ledi bile yakamıyor..
anladim peki dediigm yöntemi denedinizmi ? fonksioynu direk cagirmayi deneyin (if olmadan)
bişey değişeceğini zannetmiyorum ama deneyeceğim yarın.
Halen Daha yerimde saymaktayım.. bir türlü ilerleme olmadı.. nedendir bilmem if(read_4102(code)) ile yazmış olduğum döngüye program girmiyor kart yaklaştırıyorum ama sonuç değişmedi..
Diğer bölümde sordum ama bu bölümde de tekrar sormak istiyorum.. Acaba bu rfid modülün Aşağıdaki Pin tanımlamaları
#define RF_RDY_CLK PIN_C5 // External interrupt used to read clock
#define RF_SHD PIN_C4 // High disables the antenna signal
#define RF_MOD PIN_C3 // High does 100% modulation
#define RF_DEMOD_OUT PIN_C2 // Data read in interrupt service routine
Bu pinler rastgele boş pinlerle değiştirebilirmiyim yoksa bazıları özellikle ccp1 Gibi pinlere mi bağlanmak gerekir bilgisi olan var mı ?
/////////////////////////////////////////////////////////////////////////
//// em4095.c ////
//// This file contains drivers for a EM4095 RFID basestation. ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// ////
//// Pin Layout ////
//// ------------------------------------------------------------ ////
//// | | ////
//// | 1: VSS GND | 16: DC2 | ////
//// | | | ////
//// | 2: RDY/CLK RF_RDY_CLK | 15: FCAP | ////
//// | | | ////
//// | 3: ANT1 | 14: SHD RF_SHD | ////
//// | | | ////
//// | 4: DVDD | 13: DEMOD_OUT RF_DEMOD_OUT | ////
//// | | | ////
//// | 5: DVDS | 12: MOD RF_MOD | ////
//// | | | ////
//// | 6: ANT2 | 11: AGND | ////
//// | | | ////
//// | 7: VDD +5V | 10: CDEC_IN | ////
//// | | | ////
//// | 8: DMOD_IN | 9: CDEC_OUT | ////
//// ------------------------------------------------------------ ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2004 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////
#ifndef EM4095
#define EM4095
#ifndef RF_SHD
#define RF_RDY_CLK PIN_C5 // External interrupt used to read clock
#define RF_SHD PIN_C4 // High disables the antenna signal
#define RF_MOD PIN_C3 // High does 100% modulation
#define RF_DEMOD_OUT PIN_C2 // Data read in interrupt service routine
#endif
// Provide a buffer for storing recieved data and data to be sent
#define RFBUFFER_SIZE 20
int8 RFbuffer[RFBUFFER_SIZE];
int8 RFbuffer_index = 0;
int8 RFbuffer_bitIndex = 0;
#define END_OF_RFBUFFER (RFbuffer_index == sizeof(RFbuffer))
/////////////////////////////////////////////////////////////////////////
//// Read modes available for reading data from a transponder
/////////////////////////////////////////////////////////////////////////
int8 RF_readMode;
#define RF_MANCHESTER_DATA 0 // Reads Manchester encoded data
#define RF_MEASURE_WIDTHS 1 // Measure a series of widths
#define RF_FIND_WIDTH 2 // Find a specific width
#define RF_FIND_PATTERN 3 // Find a pattern of widths
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//// Global Variables
/////////////////////////////////////////////////////////////////////////
int1 bitValue = 1;
int1 storeData = TRUE;
int1 RE_FE_TOGGLE = 1;
int1 RF_widthFound = FALSE;
int1 RF_patternFound = FALSE;
int8 RF_widthToFind = 0;
int8* RF_findWidths = 0;
int8 RF_uncertainty = 0;
int8 timer0_overflows = 0;
int8 dataTransferred = 0;
int16 old_clock = 0;
/////////////////////////////////////////////////////////////////////////
// Purpose: Initializes the 4095 into sleep mode
// Sets up the timers and interrupts
void rf_init()
{
output_low(RF_SHD);
output_low(RF_MOD);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1);
setup_ccp1(CCP_CAPTURE_RE);
setup_ccp2(CCP_COMPARE_INT);
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
}
// Purpose: Powers down the RF antenna
#define rf_powerDown() output_high(RF_SHD);
// Purpose: Powers up the RF antenna
#define rf_powerUp() output_low(RF_SHD);
// Purpose: Select which edge to begin reading data
void RF_readEdge(int1 edge)
{
if(edge)
{
setup_ccp1(CCP_CAPTURE_RE);
RE_FE_TOGGLE = 1;
}
else
{
setup_ccp1(CCP_CAPTURE_FE);
RE_FE_TOGGLE = 0;
}
}
// Purpose: Interrupt service routine to handle compare 1 interrupts.
// Reads incoming data from a transponder and stores it in
// the global buffer.
#INT_CCP1
void isr_ccp1()
{
int8 width;
// Toggle between capturing rising and falling edges to meausure width
if(RE_FE_TOGGLE)
{
setup_ccp1(CCP_CAPTURE_FE);
RE_FE_TOGGLE = 0;
}
else
{
setup_ccp1(CCP_CAPTURE_RE);
RE_FE_TOGGLE = 1;
}
// Calculate the width
width = CCP_1 - old_clock;
old_clock = CCP_1;
switch(RF_readMode)
{
// Use to receive manchester formatted data from a transponder
case RF_MANCHESTER_DATA:
{
if(width > 54) // Check for a phase change
{
bitValue = ~bitValue; // Invert the save bit value
storeData = TRUE; // Force a bit store
}
if(storeData)
{
shift_right(RFbuffer+RFbuffer_index, 1, bitValue);
++dataTransferred;
if(++RFbuffer_bitIndex == 8)
{
RFbuffer_bitIndex = 0;
++RFbuffer_index;
}
}
storeData = ~storeData;
break;
}
// Use to read high and low widths
case RF_MEASURE_WIDTHS:
{
RFbuffer[RFbuffer_index++] = width;
++dataTransferred;
break;
}
// Use to search for a certain pulse width
case RF_FIND_WIDTH:
{
if(width > (RF_widthToFind - RF_uncertainty)
&& width < (RF_widthToFind + RF_uncertainty))
{
RF_widthFound = TRUE;
}
break;
}
case RF_FIND_PATTERN:
{
if(width > RF_findWidths[RFbuffer_index] - RF_uncertainty
&& width < RF_findWidths[RFbuffer_index] + RF_uncertainty)
{
if(++RFbuffer_index == dataTransferred)
{
RF_patternFound = TRUE;
}
}
else
{
if(RFbuffer_index > 0)
{
int8 pos, i, j;
pos = RFbuffer_index-1; // Save the initial position
// Try to match partial pattern
while(--RFbuffer_index != 0)
{
if(width > RF_findWidths[RFbuffer_index] - RF_uncertainty
&& width < RF_findWidths[RFbuffer_index] + RF_uncertainty)
{
for(i=pos, j=RFbuffer_index-1; j!=255; --i, --j)
{
if(RF_findWidths[j] != RF_findWidths[i])
{
break;
}
}
if(j == 255)
{
break;
}
}
}
}
}
break;
}
}
}
// Purpose: This interrupt service routine is used
// to send data to a transponder
// Inputs: None
// Outputs: None
#INT_CCP2
void isr_ccp2()
{
static int1 mode = 1;
if(mode == 1 && !END_OF_RFBUFFER)
{
// Output high to modulate the antenna, so send a 0 with modulation pin high
output_bit(RF_MOD, !bit_test(RFbuffer[RFbuffer_index], RFbuffer_bitIndex));
if(++RFbuffer_bitIndex == 8) // Increment the buffer indexes as necessary
{
RFbuffer_bitIndex = 0;
++RFbuffer_index;
}
CCP_2 += 30; // Wait for half the bit period minus two RF periods
mode = 0; // Toggle the mode
}
else
{
output_low(RF_MOD); // No modulation
CCP_2 += 34; // Wait for half the bit period plus 2 RF periods before sending another bit
++dataTransferred; // Increment the bits transferred counter
mode = 1; // Toggle the mode
}
}
// Purpose: Interrupt for timer 0. Keeps track of the number of
// overflows for timeouts.
// Inputs: None
// Outputs: None
#INT_RTCC
void isr_rtcc()
{
++timer0_overflows;
}
// Purpose: Fill the buffer with data read from the basestation
// Inputs: 1) The number of bits to read
// 2) TRUE start on rising edge
// FALSE start on falling edge
// Outputs: The number of bits read. Could be used to check for timeout
int8 RF_get(int8 numBits, int1 edge)
{
RF_readEdge(edge);
RF_readMode = RF_MANCHESTER_DATA;
storeData = TRUE;
bitValue = 0;
RFbuffer_index = 0;
RFbuffer_bitIndex = 0;
dataTransferred = 0;
timer0_overflows = 0;
old_clock = 0;
set_timer1(0);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
while(dataTransferred < numBits && timer0_overflows < 15);
disable_interrupts(INT_CCP1);
RFbuffer_index = 0;
RFbuffer_bitIndex = 0;
return dataTransferred;
}
// Purpose: Send data from the buffer to the transponder
// Inputs: 1) Send numBits of data to the transponder
// 2) The index in the buffer to start at
// 3) The bit position at the index to start at
// Outputs: None
void RF_send(int8 numBits, int8 index, int8 bitPosition)
{
RFbuffer_index = index;
RFbuffer_bitIndex = bitPosition;
dataTransferred = 0;
CCP_2 = 3; //
set_timer1(0); // Cause an interrupt imediately
enable_interrupts(INT_CCP2);
while(dataTransferred < numBits);
disable_interrupts(INT_CCP2);
}
// Purpose: Search for a certain pulse width
// Inputs: 1) The width length in clocks
// 2) Uncertainty to search over a range
// 3) TRUE start on rising edge
// FALSE start on falling edge
// ex) numClocks = 128; uncertainty = 6; range = 122 to 134
// Outputs: TRUE if width was found, FALSE if not found
int1 RF_findWidth(int8 numClocks, int8 uncertainty, int1 edge)
{
RF_readEdge(edge);
RF_readMode = RF_FIND_WIDTH;
RF_widthToFind = numClocks;
RF_widthFound = FALSE;
RF_uncertainty = uncertainty;
timer0_overflows = 0;
old_clock = 0;
set_timer1(0);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
while(RF_widthFound == FALSE && timer0_overflows < 50);
disable_interrupts(INT_CCP1);
return RF_widthFound;
}
// Purpose: Measure a number of pulse widths, both high and low
// Inputs: 1) The number of widths to measure
// 2) TRUE start on rising edge
// FALSE start on falling edge
// Outputs: The number of widths that were measured. If there is
// no transponder in range, the timeout could occur.
int8 RF_measureWidths(int8 numWidths, int1 edge)
{
RF_readEdge(edge);
RF_readMode = RF_MEASURE_WIDTHS;
dataTransferred = 0;
RFbuffer_index = 0;
timer0_overflows = 0;
old_clock = 0;
set_timer1(0);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
while(dataTransferred < numWidths && timer0_overflows < 50);
disable_interrupts(INT_CCP1);
return dataTransferred;
}
// Purpose: Measure a number of pulse widths, both high and low
// Inputs: 1) A pointer to an array of widths. It is safe to use RFbuffer.
// 2) The number of widths in the pattern
// 3) Uncertainty to search over a range
// 4) TRUE start on rising edge
// FALSE start on falling edge
// Outputs: The number of widths that were measured. If there is
// no transponder in range, the timeout could occur.
int8 RF_findPattern(int8* widths, int8 numWidths, int8 uncertainty, int1 edge)
{
RF_readEdge(edge);
RF_readMode = RF_FIND_PATTERN;
RF_patternFound = FALSE;
RFbuffer_index = 0;
RF_findWidths = widths;
dataTransferred = numWidths;
RF_uncertainty = uncertainty;
timer0_overflows = 0;
old_clock = 0;
set_timer1(0);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
while(RF_patternFound == FALSE && timer0_overflows < 40);
disable_interrupts(INT_CCP1);
return RF_patternFound;
}
// Purpose: Set every byte in the buffer to data
// Inputs: None
// Outputs: None
void RFbuffer_fill(int8 data)
{
int i;
for(i=0; i<sizeof(RFbuffer); ++i)
{
RFbuffer[i] = data;
}
}
// Purpose: Inverts every byte in the buffer
// Inputs: None
// Outputs: None
void RFbuffer_invert()
{
int i;
for(i=0; i<sizeof(RFbuffer); ++i)
{
RFbuffer[i] = ~RFbuffer[i];
}
}
// Purpose: Get a bit of data from the buffer and increment to the next bit
// Inputs: None
// Ouputs: A bit of data
int1 RFbuffer_getBit()
{
int1 bit;
if(!END_OF_RFBUFFER)
{
bit = bit_test(RFbuffer[RFbuffer_index], RFbuffer_bitIndex);
if(++RFbuffer_bitIndex == 8)
{
++RFbuffer_index;
RFbuffer_bitIndex = 0;
}
}
return bit;
}
// Purpose: Get a byte of data from the buffer
// Inputs: None
// Outputs: The byte of data
int8 RFbuffer_getByte()
{
if(!END_OF_RFBUFFER)
{
int8 i;
int8 data;
for(i=0; i<8; ++i)
{
shift_right(&data, 1, RFbuffer_getBit());
}
return data;
}
}
// Purpose: Set the value of the next bit in the buffer
// Inputs: None
// Outputs: None
void RFbuffer_setBit(int1 bit)
{
if(!END_OF_RFBUFFER)
{
if(bit)
{
bit_set(RFbuffer[RFbuffer_index], RFbuffer_bitIndex);
}
else
{
bit_clear(RFbuffer[RFbuffer_index], RFbuffer_bitIndex);
}
if(++RFbuffer_bitIndex >= 8)
{
++RFbuffer_index;
RFbuffer_bitIndex = 0;
}
}
}
// Purpose: Set the value of the next byte in the buffer
// Inputs: None
// Outputs: None
void RFbuffer_setByte(int8 data)
{
if(!END_OF_RFBUFFER)
{
int8 i;
for(i=0; i<8; ++i)
{
RFbuffer_setBit(bit_test(data, 7));
rotate_left(&data, 1);
}
}
}
#endif