基于TM4C123GH6的RFID读写器设计问题

2019-07-17 13:40发布

为啥进不去寻卡函数?不出现象“?求救啊

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>


#include <stdarg.h>
// RC522
#include "RC522.h"




#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

/*
  PA2 - RST
  PA3 - NSS(CS)
        PA4 - SCK
       
        PD6 - SI
        PD3 - S0  主机接收

gpio 初始化

*/
void RC522_gpio(void)
{
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA|SYSCTL_PERIPH_GPIOD);

                  ROM_GPIODirModeSet(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4, GPIO_DIR_MODE_OUT);

            ROM_GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);


                 ROM_GPIODirModeSet(GPIO_PORTD_BASE, GPIO_PIN_6,GPIO_DIR_MODE_OUT);

                  ROM_GPIODirModeSet(GPIO_PORTD_BASE, GPIO_PIN_3,GPIO_DIR_MODE_IN);

          //ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA|SYSCTL_PERIPH_GPIOD);

        //  ROM_GPIODirModeSet(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4, GPIO_DIR_MODE_OUT);
       
   // ROM_GPIOPadConfigSet(GPIO_PORTA_BASE, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
       
    //
   //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4);

          //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6);
           //ROM_GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_7);

       
}


void RC522_init(void)
{
     PcdReset();        //复位RC522
     delay_ms(10);
     PcdAntennaOff(); //关闭天线
     delay_ms(10);
     PcdAntennaOn();  //打开天线
     delay_ms(10);
         M500PcdConfigISOType( 'A' );//设置RC522工作方式

}
//M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
//4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
unsigned char  data2[4]  = {0x12,0,0,0};
unsigned char  DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char g_ucTempbuf[20]={0};   
unsigned char  MLastSelectedSnr[4]={0};
unsigned char  RevBuffer[30]={0};  
unsigned char  SerBuffer[20]={0};
uint8_t CmdValid =0;                          //串口接收完成标志位  



#define CMD_BUFFER_LEN 127

//void UARTI1ntHandler(void)
//{
//               
//}
//*****************************************************************************
//
// The UART interrupt handler.
//
//*****************************************************************************
       
       
uint8_t uart0_rx[12]={0};
void
UARTIntHandler(void)
{
    uint32_t ui32Status;

          uint8_t  Res=0;
          static uint8_t uart_cnt=0;
    //
    // Get the interrrupt status.
    //
    ui32Status = ROM_UARTIntStatus(UART0_BASE, true);

    //
    // Clear the asserted interrupts.
    //
    ROM_UARTIntClear(UART0_BASE, ui32Status);

    //
    // Loop while there are characters in the receive FIFO.
    //
    while(ROM_UARTCharsAvail(UART0_BASE))
    {
        //
        // Read the next character from the UART and write it back to the UART.
        //
      // ROM_UARTCharPutNonBlocking(UART0_BASE, ROM_UARTCharGetNonBlocking(UART0_BASE));
                        //3
                     Res =        ROM_UARTCharGetNonBlocking(UART0_BASE);

                          if(uart_cnt >= 12)
                                        uart_cnt =0;
                                  uart0_rx[uart_cnt++]=0;
                               
                                //
                                if(Res==' '||Res==' ')
                                {
                                       
                                        if(uart_cnt > 1)
                                        {

                                                uart0_rx[uart_cnt]=0;
                                               
                                                CmdValid = 0x1;
                                               
                                                 uart_cnt =0;
                                               
                                        }
                               
                                         
                                }
                       
        //
        // Blink the LED to show a character transfer is occuring.
        //
//      GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);

//        //
//        // Delay for 1 millisecond.  Each SysCtlDelay is about 3 clocks.
//        //
//       SysCtlDelay(SysCtlClockGet() / (1000 * 3));

//        //
//        // Turn off the LED
//        //
//        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
                }
}

//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************


void
UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count)
{
    //
    // Loop while there are more characters to send.
    //
    while(ui32Count--)
    {
        //
        // Write the next character to the UART.
        //
        ROM_UARTCharPutNonBlocking(UART0_BASE, *pui8Buffer++);
    }
}


void
InitConsole(void)
{
    //
    // Enable GPIO port A which is used for UART0 pins.
    // TODO: change this to whichever GPIO port you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Configure the pin muxing for UART0 functions on port A0 and A1.
    // This step is not necessary if your part does not support pin muxing.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);

    //
    // Enable UART0 so that we can configure the clock.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Use the internal 16MHz oscillator as the UART clock source.
    //
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

    //
    // Select the alternate (UART) function for these pins.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(0, 115200, 16000000);
}



//*****************************************************************************
//
// This example demonstrates how to send a string of data to the UART.
//
//*****************************************************************************

uint8_t RFRead[16];                        //存放RFID
uint8_t RFWrite[16]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};

uint8_t card_1[4]={0xc3,0x6b,0x97,0xf4};    //异形卡
uint8_t card_2[4]={0x46,0x5f,0x9a,0xb5};    //白卡

uint8_t KEY[6]={0xff,0xff,0xff,0xff,0xff,0xff};

uint8_t s=0x08,i=0;

int main(void)
{
    //
    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
        unsigned char status;
        uint8_t temp;
        uint8_t CT[2]; //卡类型
        uint8_t SN[4]; //卡号

    ROM_FPUEnable();
    ROM_FPULazyStackingEnable();

    //
    // Set the clocking to run directly from the crystal.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

        InitConsole();

    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    //
    // Enable the GPIO pins for the LED (PF2).
    //
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);

    // Enable the peripherals used by this example.
    //
   ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
   ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    // Enable processor interrupts.
    //
    ROM_IntMasterEnable();

    // Set GPIO A0 and A1 as UART pins.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure the UART for 115,200, 8-N-1 operation.
    //
    ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200,
                            (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                             UART_CONFIG_PAR_NONE));

    //
    // Enable the UART interrupt.
    //
    ROM_IntEnable(INT_UART0);
    ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);




    //
    // Prompt for text to be entered.
    //
    UARTprintf("rc522 example ");

    //
    // Loop forever echoing data through the UART.
    //
        RC522_gpio();
        RC522_init();

    while(1)
    {

                        //if(CmdValid) //输入 串口输入回车 开始读取卡实验
                        {
                                UARTprintf("rc522 ");
                                        //CmdValid = 0;
                                 delay_ms(1000);
                                 //delay_ms(1000);

                                                status = PcdRequest(PICC_REQALL,CT);  //寻卡测试
                                                if(status!=MI_OK)
                                                {
                                                        //RC522_gpio();
                                                        //RC522_init();
                                                             PcdReset();
                                                             PcdAntennaOff();
                                                             PcdAntennaOn();
                                                                 continue;
                                                }
                                                UARTprintf("卡的类型:");
                                                            for(i=0;i<2;i++)
                                                                                        {
                                                                                                temp=CT[i];
                                                                                                UARTprintf("%X",temp);

                                                                                        }
                                               
                                                status = PcdAnticoll(SN);  //防冲撞测试
                                                if (status!=MI_OK)
                                                {
                                                        //UARTprintf("防冲撞OK ");
                                                        //UARTprintf("ID:%02x %02x %02x %02x ",SN[0],SN[1],SN[2],SN[3]);//发送卡号
                                                        //if(!memcmp(SN,card_1,4))UARTprintf("是异形卡 ");
                                                        //else if(!memcmp(SN,card_2,4))UARTprintf("是白 {MOD}卡 ");
                                                         continue;
                                                }
                                                UARTprintf("卡序列号:");        //超级终端显示,
                                                                                        for(i=0;i<4;i++)
                                                                                        {
                                                                                                temp=SN[i];
                                                                                                UARTprintf("%X",temp);

                                                                                        }
                                               
                                                status =PcdSelect(SN);   //选卡测试
                                                if (status!=MI_OK)
                                                {
                                                        //UARTprintf("选卡OK ");
                                                        continue;
                                                }
                                               
                                                status =PcdAuthState(0x60,0x09,KEY,SN);//验证实验
                                                if(status==MI_OK)
                                                {
                                                        UARTprintf("验证OK ");
                                                }
                                               
                                                status=PcdWrite(s,RFWrite);//写卡实验
                                                if(status==MI_OK)
                                                {
                                                        UARTprintf("写卡OK ");
                                                }
                                               
                                                status=PcdRead(s,RFRead);//读卡实验
                                                if(status==MI_OK)
                                                {
                                                        UARTprintf("读卡OK ");
                                                        UARTprintf("RFID=");
                                                        for(i=0;i<16;i++)
                                                                UARTprintf("%02x ",RFRead[i]);
                                                        UARTprintf(" ");
                                                }
                                               
   }
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。