RFID Module and DSPIC33

General discussion on mikroC PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
rmdi
Posts: 9
Joined: 10 Sep 2010 16:07

RFID Module and DSPIC33

#1 Post by rmdi » 02 Jul 2013 23:50

Hi i'm trying to translate RFID module example to a DSPICFJ128MC802 Running with PLL at 32mhz as suggested by manual. Using a 8mhz Crystal. All works, interrupts and so on but i'm stuck at synchronization phase. The main problem comes in:

if (!(_data[data_index] ^ _data[data_index-1]))
{
bad_synch = 1;
break; //bad synchronization
}

This means that it will looping forever until a synchronization is achieved. I can't figure out what is wrong. May be you have a similar experience or you can use a similar MCU. (Using PPS and PLL). I'm also wondering if this is related to clock, may be a higher or double rate but is not working.

Thanks in advance for any help, will be appreciated.


sbit OUT at RB8_bit;
sbit RDY_CLK at RB9_bit;
sbit SHD at RB10_bit;
sbit MOD at RB11_bit;

sbit OUT_Direction at TRISB8_bit;
sbit RDY_CLK_Direction at TRISB9_bit;
sbit SHD_Direction at TRISB10_bit;
sbit MOD_Direction at TRISB11_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


/////////////////LCD

char txt[15];
// LCD module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB6_bit;
sbit LCD_D5 at LATA0_bit;
sbit LCD_D6 at LATA1_bit;
sbit LCD_D7 at LATA4_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISA0_bit;
sbit LCD_D6_Direction at TRISA1_bit;
sbit LCD_D7_Direction at TRISA4_bit;

////////////////////

// This is external INT4 interrupt (for sync and sample)
// - this interrupt is enabled once we get falling edge on RA15 (in the INT0 interrupt routine)
// it is enabled until we get 128 data bits

void interruptuno() iv IVT_ADDR_INT1INTERRUPT {
cnt++; // count interrupts on INT4 pin
//UART_Write(cnt);
PORTB.B15 = 1;
INT1IF_bit = 0;
}

// This is external INT3 interrupt (for sync start)
// - once we get falling edge on RA14 we are disabling INT3 interrupt
void interruptdos() iv IVT_ADDR_INT2INTERRUPT {
cnt = 0;
PORTB.B14 = 1;
sync_flag = 1;
INT2IF_bit = 0;
INT2IE_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;
}

void PLL(){
PLLFBD = 30;
PLLPRE_0_bit = 0;
PLLPRE_1_bit = 0;
PLLPRE_2_bit = 0;
PLLPRE_3_bit = 0;
PLLPRE_4_bit = 0;
PLLPOST_0_bit = 0;
PLLPOST_1_bit = 0;
while(OSCCON.B12 != 1 && OSCCON.B13 != 0 && OSCCON.B14 != 0){
}
}

void mapeo() {
PPS_Mapping(9, _INPUT, _INT1);
PPS_Mapping(8, _INPUT, _INT2);
PPS_Mapping(1, _INPUT, _U1RX);
PPS_Mapping(0, _OUTPUT, _U1TX);
}



// main program
void main() {
PLL();
ADPCFG = 0xFFFF;
CMCONbits.C1EN = 0;
CMCONbits.C2EN = 0;
mapeo();
TRISB = 0x0000;
TRISA = 0x0000;
PORTB = 0;
PORTA = 0;

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);
UART1_Write_Text("RFID TEST...");
UART1_Write(13);
UART1_Write(10);

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
PORTB.B8 = 1;
Lcd_Out(1,1,"RFID READER");
Lcd_Out(2,1,"TEST #1");
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);


sync_flag = 0; // sync_flag is set when falling edge on RA14 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
IPC5bits.INT1IP = 7;
IPC7bits.INT2IP = 4;
//IPC13bits. = 0b0000000100010000; // Set INT3 & INT4 priority levels
INT2EP_bit = 1;
INT1EP_bit = 0;
INT2IF_bit = 0;
INT1IF_bit = 0;
INT2IE_bit = 0;
INT1IE_bit = 0;
NSTDIS_bit = 1; // no nesting of interrupts



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;
INT2IF_bit = 0;
INT2IE_bit = 1; // enable external interrupt at RA14 (start sync procedure)

while (sync_flag == 0) { // waiting for falling edge on RA14
asm nop
}

while (cnt != 16) { // waiting 16 clocks on RA15 (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 RA14
while (cnt != 32) { // getting bit from RA14 every 32 clocks on RA15
asm nop
}
PORTB.B7 = 1;
cnt = 0; // reseting interrupt counter
_data[data_index] = OUT & 1; // geting bit
UART1_Write(_data[data_index]);
//UART1_Write(_data[data_index]);
if(data_index & 1)
if (!(_data[data_index] ^ _data[data_index-1]))
{
bad_synch = 1;
PORTB.B13 = 1;
//UART1_Write(255);
break; //bad synchronisation
}
}

INT1IE_bit = 0; // disable external interrupt on RA15 (for sync and sample)
if (bad_synch)
continue; // try again
cnt1 = 0;
PORTB.B2 = 1;
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

Lcd_Out(1,1,"CRC CHECK OK!");
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 == 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);

}
else{
UART1_Write_Text("CRC CHECK FAILED!"); // 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)
}
}

}
}

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: RFID Module and DSPIC33

#2 Post by filip » 03 Jul 2013 10:19

Hi,

First of all, you sure that the interrupts are firing correctly ?
Secondly, is the oscillator set appropriately ?

Please use LAT registers for the output pins SHD and MOD due to the Read Modify Write issue :
sbit SHD at LATB10_bit;
sbit MOD at LATB11_bit;
Regards,
Filip.

rmdi
Posts: 9
Joined: 10 Sep 2010 16:07

Re: RFID Module and DSPIC33

#3 Post by rmdi » 03 Jul 2013 20:03

Yes, both interrupts are being fired correctly and synchronization counter at rdy/clk pint interrupt is counting well. But whe it reach this point:

if (!(_data[data_index] ^ _data[data_index-1]))
{
bad_synch = 1;
break; //bad synchronisation
}

it detects a bad sync. So program gets bakc to while loop's begining. I have tried 32, 40, 64, 80 MHZ with no luck. Serial data is working fine. I'm trying with Easy24-33 Board feeding RFID with 3.3v and tried with 5.0v but althoug i'm using 5volts tolerant pins in MCU RFID reader shows to be more stable with 3.3v. Im seriously thinking in clock issues.

Thank you for your help

rmdi
Posts: 9
Joined: 10 Sep 2010 16:07

Re: RFID Module and DSPIC33

#4 Post by rmdi » 03 Jul 2013 22:21

Now it works! tried your LATCH use suggestion and USING 5v. Not working at 3.3V Thank you :D

divivoma
Posts: 20
Joined: 12 Dec 2013 19:15

Re: RFID Module and DSPIC33

#5 Post by divivoma » 20 May 2016 18:27

Hello Filip,
Please could you describe me better how this issue was solved?
I'm facing exactly the same issue with the PIC24FJ128GA310 on EasyPICFusion..
I've also tried defining:

Code: Select all

//pin def:

sbit RFID_OUT at         RB0_bit;//INPUT used for the sync interrupt
sbit RFID_RDY_CLK at  RB1_bit;//INPUT 

sbit RFID_SHD at         LATB2_bit;
sbit RFID_MOD at         LATB3_bit;

//in the main init:

    LATB = 0x00;
    TRISB = 0X0003; //RB0 (RFID_OUT) E RB1 (RFID_RDY_CLK) as input

    RFID_SHD = 0;
    RFID_MOD = 0;

Thank you for any advices.

Marco

Post Reply

Return to “mikroC PRO for dsPIC30/33 and PIC24 General”