STM32-无法读取rfid标签

时间:2018-10-02 12:55:54

标签: arm rfid

我将STM32微处理器与RFID EM4095配合使用,试图读取RFID卡,但同步总是很差。我正在为DMOUD_OUTRDY_CLK信号使用2个中断。这是我正在采取的步骤:

  1. 等待DMOD_OUT的下降沿并禁用DMOD_out中断并启用RDYCLCK中断。

  2. 等待RDYCLK上的16个上升沿。

  3. 从dmod中获取1位

  4. 等待RDYCLK上的32个上升沿。

  5. 从dmodout中获取1位(请与它的前任(bit == predessor?bad_sync = 1)进行核对)

  6. 重复4和5以获得127位。

主要功能:

 volatile  char cnt = 0;
static volatile unsigned char dump_last = 0;
static volatile unsigned char aux = 0;
static volatile unsigned char count = 0;
static volatile unsigned char latch = 0;
static volatile unsigned char page = 0;
 volatile  char sync_flag = 0;
static volatile unsigned char finish = 0;
unsigned char buf[128];

int // 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
// interrupt counter
cnt1, cnt2; // auxiliary counters
volatile unsigned short data_index; // marks position in data arrey
volatile unsigned char i;
volatile unsigned char _data[256];
volatile unsigned char data_valid[64];
volatile unsigned char bad_synch;
int main(void) {

  HAL_Init();
  SystemClock_Config();

  MX_GPIO_Init();


  MX_NVIC_Init();

  HAL_GPIO_WritePin(GPIOA, SHD_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOA, MOD_Pin, GPIO_PIN_SET);
  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

  while (1) {

    HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
    HAL_NVIC_DisableIRQ(EXTI3_IRQn);
    HAL_GPIO_WritePin(GPIOA, SHD_Pin, GPIO_PIN_SET);
    HAL_GPIO_WritePin(GPIOA, MOD_Pin, GPIO_PIN_SET);            
    bad_synch = 0;              // set bad synchronization variable to zero
    cnt = 0;                    // reseting interrupt counter
    sync_flag = 0;              // reseting sync flag
        HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
        HAL_NVIC_EnableIRQ(EXTI3_IRQn);
        HAL_GPIO_WritePin(GPIOA, MOD_Pin, GPIO_PIN_RESET);  
        HAL_GPIO_WritePin(GPIOA, SHD_Pin, GPIO_PIN_RESET);

        while (sync_flag == 0) { // waiting for falling edge on RB0
__nop();


    }

    while (cnt != 16) { // waiting 16 clocks on RB1 (positioning for 1sampling)
        __nop();


    }
         cnt = 0;



    _data[0] = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3);
    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
__nop();


      }
      cnt = 0; // reseting interrupt counter

      _data[data_index] =HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3); // geting bit
      if (data_index & 1)
        if (!(_data[data_index] != _data[data_index - 1])) {
          bad_synch = 1;
                    //GLCD_WriteChar('B');

          break; //bad synchronisation
        }
    }
    if (bad_synch)
        {
            HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_0);
            continue; // try again
        }
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_SET);
    HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
    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;
      }
    }
    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 (1) { // if data in data_valid array pass the CRC check

        for (i = 0; i <= 64; i++) { // This part of the code
          //  dislays the number of the specific RfID CARD
          if (data_valid[i] == 0) {
            GLCD_WriteChar('0');
          } else {
            GLCD_WriteChar('1'); // at the end of this for loop you will get a string of "0" and "1"
          }
        } // specific to a single RfID CARD

      }
    }
        HAL_Delay(5000);
              HAL_NVIC_EnableIRQ(EXTI3_IRQn);

  }


}

中断处理程序:

void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin) {

  if (GPIO_Pin == RDYCLK_Pin) {
    cnt++;
  }

  else if (GPIO_Pin == DMOD_Pin) {
    sync_flag = 1;
    HAL_NVIC_DisableIRQ(EXTI3_IRQn);
    HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);

  }
    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);


}

0 个答案:

没有答案