Newbie help! About MSP430G2553 control RC522 RF card reader/writer program problem[Copy link]
As the title says, I want to use MSP430G2553 to control the RC522 card reader to read and write cards. Functions to be implemented: 2553 performs AD conversion on the input voltage (single-channel multiple sampling, timer interrupt, AD conversion is started when the P1.3 port queries the button is pressed), and the obtained value is automatically stored in ADC10MEM. I assign ADC10MEM to array data1, and then write the data to the card through the pointer pointing to data1 in the card reading and writing program, and then read the data out through the card reading program, and it has been confirmed that it works normally. My design idea is to divide the entire program into two parts: the AD sampling part and the RC522 card reading and writing part. When running alone, the AD conversion part can complete the AD conversion normally, and the RC522 card reading and writing program can also complete the card reading and writing normally; however, I have been making mistakes in the process of transferring the value of ADC10MEM to RC522, and I am not familiar with the use of pointers~~~ Attached is the program, please help me~~~ #include
#include "PIN_DEF.h" #include "RC522.h" //static unsigned int FirstADCVal; unsigned char data1[2]; #define ADCDeltaOn 3 unsigned char Temp[20]; unsigned char DefaultKey[6] = {0xFF, 0xFF , 0xFF, 0xFF, 0xFF, 0xFF}; unsigned char status; //unsigned char data1[16] = {0x12,0x34,0x56,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE}; //If a block of the M1 card is written in the following format, then the block is a wallet and can receive deduction and recharge commands //4-byte amount (low byte first) + 4-byte amount inverted + 4-byte amount + 1 byte Block address + 1-byte block address inverted + 1-byte block address + 1-byte block address inverted unsigned char data2[4] = {0x02,0,0,0}; //void P1_IODect(); void Init_Port() ; int RC522(void); /*Consider the RC522 card reading and writing process as a function and call it*/ int RC522(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer _DINT(); // Init_Port (); PcdReset(); //Reset RC522 PcdAntennaOff(); Delay(1000); PcdAntennaOn(); //Turn on antenna transmission RED_LED_OFF; BELL_OFF; /* int KEYDATA; KEYDATA=(P1IN&BIT3); RED_LED_OFF; */ if(PcdRequest(0x52,Temp)==MI_OK) //Find card, 0x52 = Find sensing area All cards that meet the 14443A standard in Temp return the card type { RED_LED_ON; //P1.0 set to 1; BELL_ON; } status = PcdAnticoll(Temp);//Anti-collision status = PcdSelect(Temp);//Card selection status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, Temp);//Verify password status = PcdWrite(1, data1);//Write card//status = PcdValue(PICC_DECREMENT,1,data2);//Deduction//status = PcdBakValue(1, 2);//Backup status = PcdRead(1, Temp);//Read card PcdHalt();//Command card to sleep return 0; } void main(void) { WDTCTL=WDTPW+WDTHOLD; Init_Port( ); ADC10CTL1 =ADC10DIV_3+INCH_1+SHS_1+CONSEQ_2; ADC10CTL0 =SREF_1+ADC10SHT_3+REF2_5V+ADC10IE+REFON+ADC10ON; /*Clock source is different!!!!!!ADC10OSC-5MHz; reference voltage is 2.5V; // /////The actual voltage value is about 2.46417 */ _enable_interrupt(); //Set timer delay; TACCR0 =30; //Set TA0CCR0 of Time0_A3 to 30; TACCTL0 |=CCIE; //Capture compare interrupt enable; TACTL=TASSEL_2 + MC_1; //Clock source is SMCLK, working mode is UP, count from 0 to TACCRO; TA0R=0x0001 LPM0; //TA0R=0x0003; TACCTL0 &=~CCIE; //After execution, jump into the timer interrupt, and use the timer interrupt as the trigger source; then jump back, CCIE is cleared, and capture/compare interrupt enable is disabled; _disable_interrupt(); //Enable ADC10 interrupt; for(;;) { if (!(0x08 & P1IN)) //When the P1.3 button is pressed; { ADC10CTL0 |=ENC; //ADC10 conversion is turned on; ADC10 is busy (ADC10BUSY is set to 1), ADC10IFG is still 0; TACCTL1=OUTMOD_4; //Comparison function, timed interrupt, output signal period is twice that of the timer; TACTL=TASSEL_2+MC_2; //Clock source: SMCLK-1MHz; Working mode: continuous mode, repeatedly counting 0~0xFFFF; ////TA0R:11>13 while(!(ADC10IFG&ADC10CTL0)); //Conversion is completed, interrupt flag is reset; // ////////Conversion has not completed the judgment condition,At this step ADC10IFG is still 0, ADC10BUSY is 1; _bis_SR_register(LPM0_bits+GIE); // while(ADC10CTL1 & ADC10BUSY); } } } #pragma vector = ADC10_VECTOR interrupt void ADC10_ISR(void) { // FirstADCVal =ADC10MEM; data1[0] =ADC10MEM; RC522(); //After jumping out, temp already has a value of 250; /* if(ADC10MEM >=FirstADCVal+ADCDeltaOn) ///////Is the function of ADCDeltaOn the same as TACCRx, setting the comparison value? P1OUT |= 0x01; else P1OUT &=~0x01; */ } #pragma vector = TIMER0_A0_VECTOR interrupt void ta0_isr(void) //Timer interrupt occurs (TAIFG=1), TACCTL1/2 capture/compare interrupt occurs (CCIFG=1); TAR=0x0008; { TACTL = 0; //Timer stops after jumping into interrupt, TAIFG=0 after execution; clock source and working mode are cleared, TA0R=11 LPM0_EXIT; } void Init_Port(void) { P1DIR |= LED_RED+BELL+RF_LPCTL+RF_SS; //////P1.0/4/5/6 are set to 1; P2DIR |= RF_SCLK+RF_DATA_OUT; //////P2.0/1 is set to 1; P1OUT = 0xFF; P2OUT = 0xFF; // P2OUT &=RF_DATA_IN; ////Deprecated; P2OUT &=~RF_DATA_IN; //////P2.2 is set to 0, which is input; P2DIR &=~ (RF_DATA_IN); // //P2REN=RF_DATA_IN; P1DIR &=~ BIT3; /* P1REN |=BIT3; //P1.3 is set as a pull-up resistor; P1OUT |=BIT3; P1IES |= BIT3; // P1.3.... is set to a falling edge interrupt P1IE |= BIT3; // Enable P1.3.. interrupt*/ } The card writing program is as follows: char PcdWrite(unsigned char addr,unsigned char *pData) { char status ; unsigned int unLen ; unsigned char i,ucComMF522Buf[MAXRLEN] ; ucComMF522Buf[0] = PICC_WRITE ; ucComMF522Buf[1] = addr ; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]) ; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4, ucComMF522Buf,&unLen ) ; if( ( status != MI_OK)||(unLen != 4) ||( (ucComMF522Buf[0]&0x0F)!= 0x0A)) status = MI_ERR ; if (status == MI_OK) { for (i=0; i<16; i++) ucComMF522Buf[i] = *(pData+i) ; CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf[16]) ; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf, 18,ucComMF522Buf,&unLen ) ; if( (status != MI_OK)||(unLen != 4 ) ||((ucComMF522Buf[0]&0x0F)!= 0x0A)) status = MI_ERR ; } return status ; }2 is set to 0, which is input; P2DIR &=~ (RF_DATA_IN); // //P2REN=RF_DATA_IN; P1DIR &=~ BIT3; /* P1REN |=BIT3; //P1.3 is set as a pull-up resistor; P1OUT |=BIT3; P1IES |= BIT3; // P1.3.... is set to a falling edge interrupt P1IE |= BIT3; // Allow P1.3.. interrupt*/ } The card writing program is as follows: char PcdWrite(unsigned char addr,unsigned char *pData) { char status ; unsigned int unLen ; unsigned char i,ucComMF522Buf[MAXRLEN] ; ucComMF522Buf[0] = PICC_WRITE ; ucComMF522Buf[1] = addr ; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]) ; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4, ucComMF522Buf,&unLen ) ; if( ( status != MI_OK)||(unLen != 4) ||((ucComMF522Buf[0]&0x0F)!= 0x0A)) status = MI_ERR ; if (status == MI_OK) { for (i=0; i<16; i++) ucComMF522Buf[i] = *(pData+i) ; CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf[16]) ; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf, 18,ucComMF522Buf,&unLen ) ; if( (status != MI_OK)||(unLen != 4 ) ||((ucComMF522Buf[0]&0x0F)!= 0x0A)) status = MI_ERR ; } return status ; }2 is set to 0, which is input; P2DIR &=~ (RF_DATA_IN); // //P2REN=RF_DATA_IN; P1DIR &=~ BIT3; /* P1REN |=BIT3; //P1.3 is set as a pull-up resistor; P1OUT |=BIT3; P1IES |= BIT3; // P1.3.... is set to a falling edge interrupt P1IE |= BIT3; // Allow P1.3.. interrupt*/ } The card writing program is as follows: char PcdWrite(unsigned char addr,unsigned char *pData) { char status ; unsigned int unLen ; unsigned char i,ucComMF522Buf[MAXRLEN] ; ucComMF522Buf[0] = PICC_WRITE ; ucComMF522Buf[1] = addr ; CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]) ; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4, ucComMF522Buf,&unLen ) ; if( ( status != MI_OK)||(unLen != 4) ||((ucComMF522Buf[0]&0x0F)!= 0 x0A)) status = MI_ERR ; if (status == MI_OK) { for (i=0; i<16; i++) ucComMF522Buf[i] = *(pData+i) ; CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf[16]) ; status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf, 18,ucComMF522Buf,&unLen ) ; if( (status != MI_OK)||(unLen != 4 ) ||((ucComMF522Buf[0]&0x0F)!= 0x0A)) status = MI_ERR ; } return status ; }