Discussion in "PIC Microcontroller Discussion" started by    Morris    Sep 19, 2009.
Sat Sep 19 2009, 03:08 PM
#1
Hi,
I was making a project of RF Card Reader with PIC12F629.
The microcontroller's tasks are as following:
1) IO simulate RS232 communication only for sending a package with 15 bytes
2) Receiving data from U2270B which is a RFID read/write base station
3) Sending the real card no. as a RS232 package form
The rom & ram size have been occupied 90% approximately.
When the device was running, reading card no. is right,
but the head 2 bytes of package were always 0, and the other bytes were right.
Why? the head 2 bytes i only defined at the first, and never changed them.
i defined : unsigned char RS232_buffer[15]={0x3C,0x0f,0,0,0,0x02,0,0,0,0,0,0,0,0,0x82};
Who can help me?

Finally i apologized that i have left for a long time,
because the world financial crisis affected me in this second quarter
But i will always support RICKEY'S WORLD.

Tue Sep 22 2009, 08:17 PM
#2
Hi morris welcome back..
are you talkin about a finished project or a project to be done yet?
coz 90% space for that much project seems too high to me.
Wed Sep 23 2009, 01:18 AM
#3
Hi,Ajay
Not finished project.i have planned and the systme included as below:
1). RF CardReader
2). Main Controller between CardReader & PC
3). PC Software for recorder
The first step i had to make the CardReader.but it was running wrongly.
Later on i'll attach its code and circuit diagram after i arrange and clean up them.
Wed Sep 23 2009, 11:03 AM
#4
you are using EM4001 RFID card reader so it wont be a problem to make it work. connect reader to PC serial port, see how response comes in what format etc. or simply check datasheet if you have.

which controller are you plannin to use?
Wed Sep 23 2009, 01:22 PM
#5
Although the card reader can work,the first 2 bytes of a package(15bytes)
are always wrong.:(
i attached the schematics & code as below.
i think the code is very very pell-mell due to i was in a hurry.
If you have any question or problem, i will check&make it up again.


//------------------------------------------------------------------------------------------
#include "pic12f6x.h"
#include "pic.h"
#include "math.h"
#include "string.h"
#include "BusProtocol.h"

#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif

#ifndef ERROR
#define ERROR 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

#define PRELOAD_RS232 0x3D //2400baud
#define RFID_PERIOD 0x37 //200us

#define TXD GPIO2
#define RFID_DATA GPIO4

#define Enable_RFID_EX() IOCB4=1;GPIF=0;GPIE=1
#define Disable_RFID_EX() IOCB4=0;GPIF=0;GPIE=0
#define OnRFID() GPIO5=1
#define OffRFID() GPIO5=0

static volatile uchar ucTick0=0;
static volatile uchar count=0,t_count=0;
static volatile uchar RFCount=0;

static volatile bit RFBit=0; //Port Voltage Level Changed Interrupt Flag
static volatile bit StartRec=0; //1: Start Receive
static volatile bit RFID_IN=0; //RFID Data Input
static volatile bit Send_FLAG=0; //Send package Flag
//static volatile bit bSent=FALSE; //1: already sent a package
static volatile bit TM0_FLAG=0; //Timer 0 Overflow Interrupt

//16 bytes to receive 128 bits RFID data
uchar RFID_Buffer[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

//15 bytes for Rs232 package
uchar RS232_Buffer[15]={0x3C,0x0F,0x00,0x00,0x00,0x02,0xBB,0x00,0x00,0,0,0,0,0,0x82};;

bit SendTo(volatile uchar *str);
void RS232SendEnd(void);
void Lmove_bite(void);
bit find_head(void);
void get_data(void);
bit Data_L_check(void);
bit Data_R_check(void);
void Get_EffectData(uchar edata);
bit Data_Sever(void);
void Data_receive(void);
bit ReadRFID(void);
void WaitTF0(void);

void DelayUs(uint i)
{
while(i--);
}
void DelayMs(uint j)
{
while(j--)
{
DelayUs(200);
}
}
//-----------------------------------------------------
void interrupt
ISR(void) //@ 0x10
{
if(GPIE==1 && GPIF==1)
{
RFBit = 1;
if(!StartRec)
{
if(RFCount == 2)
{
StartRec = 1;
}
TMR0 = RFID_PERIOD;
RFCount = 0;
}
GPIO = GPIO;
GPIF = 0;
}

if(T0IE==1 && T0IF==1)
{
T0IF=0;
if(!Send_FLAG)
{
TMR0 = RFID_PERIOD;
RFCount++;
if(RFCount > 9)RFCount = 0;
}
else TMR0 = PRELOAD_RS232;
ucTick0++;
if(ucTick0==2)
{
ucTick0=0;
TM0_FLAG = 1;
}
}
}
/*********************************************
Function Name:Lmove_bite
Description: Rotate 1 bit left for 128 bits
*********************************************/
void Lmove_bite(void)
{
uchar temp;
uchar tempData0=0 ;
for(temp=0 ; temp<0x10 ; temp++ )
{
if( temp == 0 ){ tempData0 = ( (RFID_Buffer[0]&0x01) <<7 ) ;}
if( temp == 0x0f ){ RFID_Buffer[temp] =( (RFID_Buffer[temp]>>1) | tempData0 ) ; }
else {RFID_Buffer[temp] = ( (RFID_Buffer[temp]>>1) | ( (RFID_Buffer[temp+1]&0x01) <<7 ) ) ;}
}
}
/*******************************************
Function Name:find_head
Description: find 9 bits of '1' of head
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
********************************************/
bit find_head(void)
{
uchar *pRFID = &RFID_Buffer;
for(count=0;count<128;count++)
{
if( (pRFID[0]==0x55) && (pRFID[1]==0x55) && ((pRFID[2]&0x03)==0x01)\
&&((pRFID[15]&0xC0)==0x80)){return 1;}
else {Lmove_bite();}
}
return 0 ;
}
/*******************************************
Function Name: get_data
Description: Sort effective 55 bits out except for the head of 128bits
********************************************/
void get_data(void)
{
uchar *Read_Buffer = &RFID_Buffer;
uchar c1,c2,c3,t1;
for( count=0 ,t_count=0 ; count <= 126 ; )
{
c1 = count+18;
c2 = c1/8;
c3 = c1+1;
t1 = t_count/8;
if(( Read_Buffer[c2]&(0x01<<(c1%8)))>(Read_Buffer[(c3/8)]&(0x01<<(c3%8)))) //1
{ Read_Buffer[t1] = ( Read_Buffer[t1] | ( 0x01 << (t_count%8) ) ) ; }
else//if(( Read_Buffer[c2]&(0x01<<(c1%8)))<( Read_Buffer[(c3/8)]&(0x01<<(c3%8)))) //0
{ Read_Buffer[t1] = ( Read_Buffer[t1] & ( ~ ( 0x01 << (t_count%8) ) ) ) ; }
t_count++;
count=count+2;
}
}
/*********************************************
Function Name:Data_L_check
Description: Column Data Check
********************************************/
bit Data_L_check(void)
{
uchar temp;
uchar *Read_Buffer = &RFID_Buffer;
for(count=0 ; count < 0x04 ; count++)
{
temp = 0 ;
t_count = count ;
for( ; t_count <= 53 ; )//0x37 ; )
{
if( ( Read_Buffer[(t_count/8)] & (0x01<<(t_count%8)) ) )//>> (t_count%8) )
{
temp ++ ;
}
t_count += 5 ;
}
if((temp%2)!= 0)
{
return 0 ; //Column Check Error
}
}
return 1 ;
}
/*********************************************
Function Name:Data_R_check
Description: Row Check
********************************************/
bit Data_R_check(void)
{
uchar temp,sum=0;
uchar *Read_Buffer = &RFID_Buffer;
uchar t_buf[5];
uchar m;
count=39;
for(t_count=0 ; t_count<= 45 ; )//0x32 ; )
{
for(temp=0;temp<5;temp++)
{
m = t_count + temp;
t_buf[temp] = (( Read_Buffer[m/8] & (0x01<<(m%8))) >> (m%8)) ;
sum += t_buf[temp];
}
if((sum%2) == 0)
{t_count += 5 ;sum = 0;}
else
{return 0 ;} //Row Check Error
for(temp=0 ; temp<0x04 ; temp++ )
{
Get_EffectData( t_buf[temp] ) ;
count--;
}
}
return 1 ;
}
/*********************************************
Function Name:Get_EffectData
Description: Get real data of RFID card
*********************************************/
void Get_EffectData(uchar edata)
{
uchar *pRS232=&RS232_Buffer;
uchar m=9+(count/8);
uchar n=(1<<(count%8));
switch(edata)
{
case 1:
pRS232[m]|=n;
return ;
case 0:
pRS232[m] &= (~n);
return ;
}
}
/*********************************************
Function Name:ChangeToCardNo
Description: Change to recognizable card num for customer
*********************************************/
void ChangeToCardNo(void)
{
uchar *pRS232=&RS232_Buffer;
uchar temp1,temp2;
for(temp2=0;temp2<2;temp2++)
{
temp1 = pRS232[13-temp2];
pRS232[13-temp2] = pRS232[9+temp2];
pRS232[9+temp2] = temp1;
}
for(temp1=0;temp1<3;temp1++)
{
pRS232[10+temp1] = pRS232[11+temp1];
}
temp1 = 0;
for(temp2=2;temp2<13;temp2++)
{
temp1 ^= pRS232[temp2];
}
pRS232[13]=temp1;
}

/*********************************************
Function Name:Data_Sever
Description: RFID data service
0 : error 1 : right
*********************************************/
bit Data_Sever(void)
{
if(find_head() == 0 ) //find a right head of 128bits Manchester Code
{ return 0 ; }
get_data() ;
if(Data_L_check() == 0 )
{ return 0 ; }
if(Data_R_check() == 0 )
{ return 0 ; }
return 1 ;
}
/*********************************************
Function Name:WaitRFBit
Description: RFID data input port level changed
*********************************************/
//---------------------------------------------------
void WaitRFBit(void)
{
while(!RFBit);
RFBit = 0;
}
/*********************************************
Function Name:Data_Receive
Description: Receive RFID data
*********************************************/
void Data_Receive(void)
{
uchar temp=0;
uchar BitCount=0,Bptr=0;
uchar *pRFID = &RFID_Buffer;
for(BitCount=0;BitCount<128;)
{
if(RFCount<=1)//200us<time<400us (time=1 * 1/2T)
temp=1;
else if(RFCount==2||RFCount==3)temp=2; //400us<time<600us (time=2 * 1/2T)
TMR0=RFID_PERIOD;
RFCount=0;
RFID_IN = RFID_DATA;
for(count=0;count<temp;count++)
{
GIE = 0;
Bptr = BitCount/8;
t_count = pRFID[Bptr];
t_count >>= 1;
if(!RFID_IN)t_count |= 0x80;
else t_count &= 0x7F;
pRFID[Bptr] = t_count;
BitCount++;
GIE = 1;
}
WaitRFBit();
}
}
/*******************************************
Function Name: ReadRFID
Description: Wait to read RFID Card
********************************************/
bit ReadRFID(void)
{
while(!StartRec);
Data_Receive();
GIE = 0;
if( Data_Sever() )
{
ChangeToCardNo();
return TRUE;
}
else //Check Error
{
GIE = 1;
StartRec = 0; //restart to receive
return FALSE;
}
}
//--------------------------------------------------
//*********Internal RC oscillator Calibration*********/
void IntRC_Calibration(void)
{
#asm
bsf _STATUS,5
call 3FFH
movwf _OSCCAL
bcf _STATUS,5
#endasm
}
//----------------------------------------------------
void TMR0_Init(void)
{
ucTick0 = 0;
TM0_FLAG = 0;
TMR0 = RFID_PERIOD;
OPTION |= 0x08; //Prescale is assigned to WDT
T0IF = 0;
T0IE = 1;
}
//---------------------------------------------
void Port_Init(void)
{
TRISIO = 0x18;
OPTION = 0x80; //disable GPIO pull-up
GPIO = 0x3F;
CMCON = 0x07; //Digital IO , Compator Off
}
//-----------------------------------------------
void Devices_Init(void)
{
PIE1 = 0x00;
PIR1 = 0x00;
INTCON = 0x00;
GPIO = 0x00;
IOCB = 0x00;
GPIF = 0;
GPIO = GPIO;
Port_Init();
TMR0_Init();
DelayMs(10);
}
//-------------------------------
void main(void)
{
DelayMs(50);
IntRC_Calibration();
Devices_Init();

Send_FLAG = FALSE;
OnRFID();
Enable_RFID_EX();
while(1)
{
if(ReadRFID())
{
if(SendTo(RS232_Buffer));
else RS232SendEnd();
DelayMs(600);
}
}
}

//---------------------------------------------------------------------------------------------
/***********************IO simulate UART***********************/
void RS232SendInit(void)
{
GIE = 0;
Send_FLAG = TRUE;
Disable_RFID_EX();
T0IE = 0;
TMR0 = PRELOAD_RS232;
ucTick0 = 0;
TM0_FLAG = 0;
T0IF = 0;
GIE = 1;
}
void RS232SendEnd(void)
{
Send_FLAG = FALSE;
TMR0 = RFID_PERIOD;
T0IF = 0;
StartRec = 0;
RFCount = 0;
T0IE = 1;
Enable_RFID_EX();
}

void WaitTF0( void )
{
while(!TM0_FLAG);
TM0_FLAG=0;
}

bit WByte(uchar input)
{
uchar i=8;
T0IE = 1;
TXD = 0;
WaitTF0();
while(i--)
{
TXD = (bit)(input&0x01);
NOP();NOP();NOP();NOP();NOP();NOP();
if(TXD != (input&0x01))return ERROR;
WaitTF0();
input >>= 1;
}
TXD = 1;
WaitTF0();
T0IE = 0;
return TRUE;
}

//Send RS232 Package
bit SendTo(volatile uchar *str)
{
uchar i=15;
uchar temp;
RS232SendInit();
while(i--)
{
temp = str[16-i];
if(WByte(temp))DelayUs(500);
else return ERROR;
}
RS232SendEnd();
return TRUE;
}



[ Edited Wed Sep 23 2009, 01:35 PM ]
Wed Sep 23 2009, 08:40 PM
#6
What do you get if you just store the 15 bytes from the reader,
and what do you expect ?.


[ Edited Wed Sep 23 2009, 09:12 PM ]
Thu Sep 24 2009, 12:17 AM
#7
I want to get a right&proper package,but the first 2 bytes always are '0'.Why?I never changed them.
Thu Sep 24 2009, 06:57 AM
#8
post link to datasheet of this reader module. coz i am not able to understand what you're doin there in code.
Thu Sep 24 2009, 11:19 AM
#9
Thank you , Ajay
The card reader is made by myself in a hurry so no datasheet.
If you still cann't understand it, Pls wait me to tidy up the code and make some relevant instruction.
Thu Sep 24 2009, 03:58 PM
#10


I want to get a right&proper package,but the first 2 bytes always are '0'.Why?I never changed them.


Morris


I meant what stream of bits do you actually get, and what stream of bits would you expect to see
before doing any bit shifting etc.
Do you know what the data on the card is ?.

Get Social

Information

Powered by e107 Forum System

Conversation

poojab51296
Sun Aug 20 2017, 10:23 AM
i am intending to interface sd card with STM32F4 and want to write a file in sd card using SPI protocol but using HAL only. Can anyone help me PLEASE!!!!
poojab51296
Sun Aug 20 2017, 10:22 AM
hello
rawat742
Fri Aug 18 2017, 11:37 AM
hi @ angel patel i will attach a file i hope it will be helpfull for you
ExperimenterUK
Mon Aug 14 2017, 09:57 PM
@ angel patel... just log in and post your problem
Mon Aug 14 2017, 01:45 PM
angel patel
hey any one help to make the led scrolling display
desuza6
Sat Jul 29 2017, 07:09 AM
I see the code for step motor h l 298 to rotate 45 degree, but all delay time, if I want to input sensor to h the pulses to stepmotor
rammichael
Thu Jul 27 2017, 12:05 PM
okay fine
ExperimenterUK
Wed Jul 26 2017, 07:51 PM
Please post questions in the forum, the chat box is for chat
msd007
Wed Jul 26 2017, 11:27 AM
i need code if any one have aduc706 interfacing lcd 16*2
msd007
Wed Jul 26 2017, 11:26 AM
pl help me how to write code of interfacing ADuc7026 controlleo lcd pl any one help me hoe to initializend configure the direction

Downloads

Comments

abdullah6484
Tue Aug 22 2017, 10:21 PM
crank.tp
Mon Aug 21 2017, 02:17 PM
poojab51296
Sun Aug 20 2017, 10:18 AM
kira113
Sat Aug 19 2017, 08:40 AM
Pirateak
Thu Aug 17 2017, 02:02 PM
sathishksk71
Wed Aug 16 2017, 04:47 AM
rawat742
Sun Aug 13 2017, 05:49 PM
alparslanbagriyanik
Fri Aug 11 2017, 08:10 AM

Online

Guests: 66, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37882
Newest member: abdullah6484
Forum Activity
rawat742 Avatar
Posted By rawat742
Mon Aug 21 2017, 09:13 PM
Dear sir, I want purchase a debugger for 8051 , p...
rawat742 Avatar
Posted By rawat742
Mon Aug 21 2017, 09:09 PM
Dear Sir, I want to programe for de...
rawat742 Avatar
Posted By rawat742
Mon Aug 21 2017, 09:06 PM
Dear Sir, thanks now I have done almost my pro...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Aug 20 2017, 08:31 PM
"when i compile the program using keil ide i got s...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Aug 20 2017, 08:15 PM
You will probably get better results from a specia...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Aug 20 2017, 06:42 PM
Your data is retained in the simulation.Write data...
rawat742 Avatar
Posted By rawat742
Sun Aug 20 2017, 01:23 PM
Dear sir , I face a error mostly *** WARNING L1...
rawat742 Avatar
Posted By rawat742
Sun Aug 20 2017, 11:21 AM
Dear Sir, thanks I have done Acc to you now I am...
poojab51296 Avatar
Posted By poojab51296
Sun Aug 20 2017, 10:29 AM
Sir/Ma'am,I am trying to interface Sd card with ST...
kira113 Avatar
Posted By kira113
Sat Aug 19 2017, 08:52 AM
hello everyone i write a simple program using c la...