I' m facing the same issue described here but on PIC24FJ128GA310 on EasyPICFusionv7:
http://forum.mikroe.com/viewtopic.php?f=101&t=55292
Below my complete code, running @8MHz or 32MHz no difference were found:
(For the sake of clarity I don't post the CRC_Check() that is always the same..)
Code: Select all
sbit RFID_OUT at RB0_bit;//INPUT used for the sync interrupt
sbit RFID_RDY_CLK at RB1_bit;//INPUT
//sbit RFID_SHD at RB2_bit;
//sbit RFID_MOD at RB3_bit; //I've tried the LATB suggestion of the other topic
sbit RFID_SHD at LATB2_bit;
sbit RFID_MOD at LATB3_bit;
sbit RFID_OUT_Direction at TRISB0_bit;
sbit RFID_RDY_CLK_Direction at TRISB1_bit;
sbit RFID_SHD_Direction at TRISB2_bit;
sbit RFID_MOD_Direction at TRISB3_bit;
//RFID Example Variables:
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 EXT_INT1() iv IVT_ADDR_INT1INTERRUPT ics ICS_AUTO
{
cnt++; // count interrupts on INT1 pin
IFS1.INT1IF =0; //Clear INT1
}
void EXT_INT2() iv IVT_ADDR_INT2INTERRUPT ics ICS_AUTO
{
cnt = 0;
sync_flag = 1;
IFS1.INT2IF = 0;
IEC1.INT2IE = 0;
IFS1.INT1IF = 0;
IEC1.INT1IE = 1;
}
void main()
{
LATA = 0x00;
TRISA = 0X0000;
LATB = 0x00;
TRISB = 0X0003; //RB0 (RFID_OUT) E RB1 (RFID_RDY_CLK) as input
LATC = 0x00;
TRISC = 0X0000;
LATD = 0x00;
TRISD = 0X0000;
LATE = 0x00;
TRISE = 0X0000;
LATF = 0x00;
TRISF = 0X0010; //RF4 U1Rx
LATG = 0x00;
TRISG = 0X0000;
ANSA = 0x0000;
ANSB = 0x0000;
ANSC = 0x0000;
ANSD = 0x0000;
ANSE = 0x0000;
Unlock_IOLOCK();
PPS_Mapping_NoLock(10, _INPUT, _U1RX); // Sets pin RP10 to be Input, and maps U1Rx - RF4
PPS_Mapping_NoLock(17, _OUTPUT, _U1TX); // Sets pin RP17 to be Output, and maps U1TX - RF5
PPS_Mapping_NoLock(1, _INPUT, _INT1); // Sets pin RP1 to be Output, and maps INT1 --> RB1 --> RDY/CLK
PPS_Mapping_NoLock(0, _INPUT, _INT2); // Sets pin RP0 to be Input, and maps INT2 --> RB0 --> OUT
Lock_IOLOCK();
INTCON1.NSTDIS = 1; //No nesting of interrupt
INTCON2.INT1EP = 0; //rising edge --> RB1 RFID_RDY_CLK
INTCON2.INT2EP = 1; //Falling edge --> RB0 RFID_OUT
IPC5bits.INT1IP = 7;
IPC7bits.INT2IP = 4;
IFS1.INT1IF = 0; //Clear INT1
IFS1.INT2IF = 0; //Clear INT2
IEC1.INT1IE = 0; //Disable external interrupt INT1
IEC1.INT2IE = 0; //Disable external interrupt INT2
AD1CON1.ADON = 0; //Disable ADC
//RFID_OUT_Direction = 1; // All this is redundant if I use the TRISB = 0x0003 , right?
//RFID_RDY_CLK_Direction = 1;
//RFID_SHD_Direction = 0;
//RFID_MOD_Direction = 0;
RFID_SHD = 0;
RFID_MOD = 0;
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
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
while (1)
{ // Endless loop
bad_synch = 0; // set bad synchronization variable to zero
cnt = 0; // reseting interrupt counter
sync_flag = 0; // reseting sync flag
IFS1.INT1IF = 0;
IEC1.INT1IE = 0;
IFS1.INT2IF = 0;
IEC1.INT2IE = 1; // enable external interrupt on OUT(start sync procedure)
DBG_PIN =0;
DBG_PIN2=0;
while (sync_flag == 0)
{ // waiting for falling edge on RB0 - OUT - INT2
asm nop
}
while (cnt != 16)
{ // waiting 16 clocks on RB1 (positioning for sampling)
asm nop
}
cnt = 0;
_data[0] = RFID_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] = RFID_OUT & 1; // geting bit
if(data_index & 1)
{
if(!(_data[data_index] ^ _data[data_index-1]))
{
bad_synch = 1;
break; //bad synchronisation
}
}
}
IEC1.INT1IE = 0; // disable external interrupt on RB1 - CLK(for sync and sample)
if (bad_synch)
{
continue; // try again, repeat from the main ---> I'M BLOCKED HERE..
}
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);
}
}
}
}
Marco