實驗4:基于RFID的電子錢包綜合實驗

實驗目的

  • 了解IAR開發(fā)環(huán)境的應用
  • 熟悉MFRC531的使用

實驗環(huán)境

  • 硬件: RFID射頻模塊嫩与,USB仿真器攒暇,PC機指巡。
  • 軟件:IAR Embedded Workbench for STMicroelectronics STM8 1.30 Evaluation

實驗內(nèi)容

  1. 用IAR環(huán)境蚤认,在電子標簽卡片接觸RFID模塊時队魏,按KEY按鍵振峻,電子標簽中的數(shù)據(jù)加1和減1臼疫,即充值1和減去1,可通過PC機串口軟件觀測扣孟。
  2. 在串口軟件界面輸出卡號烫堤、余額。

實驗步驟

實驗代碼

1) 輸出

  • 使用tag標識不同的輸出形式凤价,置1為字符串輸出鸽斟,置0為十六進制輸出。
int tag=0;

  • tag為1利诺,使用sprintf()函數(shù)對card序列號轉十進制富蓄,以字符串形式輸出card序列值,輸出提示“please press the key to charge money.”立轧,輸出十六進制的充值金額格粪,再輸出十六進制的總額,然后再次使用sprintf()函數(shù)將十六進制的總額轉換為十進制氛改,再輸出十進制的總額數(shù)帐萎。
    tag為0,則使用UART2_SendString()函數(shù)以十六進制形式輸出胜卤。
   if(tag==1){
      char buf[8] = {0};
      char card[8] = {0}; 

      sprintf(card,"%02x%02x%02x%02x",g_cSNR[0],g_cSNR[1],g_cSNR[2],g_cSNR[3]);
      printf("Card ID: %s\n",card);
      //printf("Card ID: %x %x %x %x\n", g_cSNR[0],g_cSNR[1],g_cSNR[2],g_cSNR[3]);
      printf("please press the key to charge money.\n");
      printf("充值金額:%x %x %x %x\n", money_buf[3], money_buf[2], money_buf[1], money_buf[0]);
      printf("Total Moeny:  %x %x %x %x\n", tx_buf[9], tx_buf[10], tx_buf[11], tx_buf[12]);
      //printf("value: %d%d%d%d\n",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
      sprintf(buf,"%02x%02x%02x%02x",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
      printf("value: %s\n",buf);
   }
   else UART2_SendString(tx_buf, 14);

  • 以上是以充值金額做舉例疆导,扣款同理。應注意buf和card數(shù)組的清零葛躏。
char buf[8] = {0};
char card[8] = {0};

2) 按鍵控制

  • 判斷是否PC1引腳觸發(fā)澈段,若是,則實現(xiàn)按鍵控制舰攒,首先設置不接收中斷信息败富,設置rx_buf為CC EE FE 01 01 20 18 42 75 FF,其中摩窃,CC EE FE 01為固定值兽叮,01為充值命令芬骄,20 18 42 75表示一次按鍵所充值的金額數(shù)量,F(xiàn)F表示結束鹦聪,然后置兩個flag為1账阻,表示識別到卡,置tag為1泽本,表示此時的輸出形式為字符串形式淘太,最后再打開中斷接收信號。
if ((GPIO_ReadInputData(GPIOC) & GPIO_PIN_1) == 0x00)//判斷是否PC1引腳觸發(fā)
//實現(xiàn)按鍵控制
{
    disableInterrupts();
    rx_buf[0] = 0xCC;
    rx_buf[1] = 0xEE;
    rx_buf[2] = 0xFE;
    rx_buf[3] = 0x01;     
    rx_buf[4] = 0x01;
    rx_buf[5] = 0x20;
    rx_buf[6] = 0x18;
    rx_buf[7] = 0x42;   
    rx_buf[8] = 0x75;
    rx_buf[9] = 0xFF;
    Uart_RecvFlag = 1;
    Flag=1;
    tag=1;
    enableInterrupts();
}


3) 蜂鳴聲響

  • 讀取串口指令完成后规丽,蜂鳴器響100ms蒲牧。
    BEEP_On();
    delay_ms(100);
    BEEP_Off();

硬件連接

  1. 把RFID模塊插到實驗箱的主板上的串口
  2. 把ST-Link配合JTAG仿真器插到標有ST-Link標志的串口上
  3. 把仿真器一端的USB線插到PC機的USB端口,通過主板上的“加”“減”按鍵調(diào)整要實驗的RFID模塊(會有黃色LED燈提示)嘁捷,硬件連接完畢造成。

編譯、燒錄并測試

  1. 我們用IAR SWSTM8 1.30軟件雄嚣,打開..\RFID_電子錢包實驗\Project\MFRC531_ATM8.eww晒屎。
  2. 工程編譯:點擊“Project”->“Rebuild All”。
  3. 點擊“Rebuild All”進行編譯缓升。
  4. 將卡片放在燒錄板上鼓鲁,把程序燒到模塊里,點擊“ ”中間的Download and Debug進行燒錄港谊,完成后聽到蜂鳴器響一聲骇吭。
  5. 關閉上述已打開程序,打開串口測試軟件歧寺,將傳感器模塊連接到串口轉USB模塊上燥狰,將USB2UART模塊的USB線連接到PC機的USB端口,然后打開串口工具斜筐,配置好串口龙致,波特率115200,8個數(shù)據(jù)位顷链,一個停止位目代,無校驗位,串口開始工作嗤练。

實驗結果

充值

  • 在感應器上放上卡片榛了,收碼區(qū)選擇字符串,按下按鍵key煞抬,此時輸出為“Card ID:6d6d6f51”霜大,這是卡序列號,輸出“please press the key to charge money.”革答,這是按下按鍵的提醒战坤,輸出“充值金額:20 18 42 75”遮婶,按下按鍵就會向卡片中充值20 18 42 75個單位的錢,輸出“Total Money: 20 18 42 75”湖笨,因為之前卡內(nèi)余額為0,所以總金額與充值金額一致蹦骑,輸出“value:20184275”輸出十進制表示的余額慈省,此處并未嚴格換算。


    按鍵充值結果截圖

扣款

  • 收碼區(qū)選擇HEX碼眠菇,發(fā)送數(shù)據(jù)“CC EE FE 01 02 20 18 42 75 FF”,其中边败,CC EE FE 01為固定數(shù)據(jù),02表示扣款捎废,20 18 42 75表示扣款金額笑窜,F(xiàn)F表示結束,輸出為“EE CC FE 01 02 6D 6D 6F 51 00 00 00 00 FF”登疗,其中排截,EE CC FE 01為固定數(shù)據(jù),02表示扣款辐益,6D 6D 6F 51為卡號断傲,00 00 00 00表示卡內(nèi)余額,因為扣款金額為余額的總數(shù)智政,所以更新后的卡內(nèi)余額為0认罩,F(xiàn)F表示結束。


    串口命令扣款結果截圖

代碼

main.c

#include "main.h"
#include "stdio.h"


#ifdef _RAISONANCE_
#define PUTCHAR_PROTOTYPE int putchar (char c)
#define GETCHAR_PROTOTYPE int getchar (void)
#elif defined (_COSMIC_)
#define PUTCHAR_PROTOTYPE char putchar (char c)
#define GETCHAR_PROTOTYPE char getchar (void)
#else /* _IAR_ */
#define PUTCHAR_PROTOTYPE int putchar (int c)
#define GETCHAR_PROTOTYPE int getchar (void)
#endif /* _RAISONANCE_ */



u8 rx_buf[10]; //串口輸入字符
u8 rx_counter;
u8 Uart_RecvFlag = 0; 
int tag=0;

u8 tx_buf[14];
u8 tx_buff[14];
u8 g_cSNR[4];       //M1卡序列號
u8 g_pTagType[2];   // M1卡型號代碼
u8 money[16];       //余額
u8 p_KeyA[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
u8 p_Key[12];
u8 money_buf[4];
u8 Flag = 0;    //是否發(fā)現(xiàn)卡標志



//電子錢包-扇區(qū)0 塊號1

// 時鐘設置续捂,內(nèi)部時鐘16M
void CLK_Config(void)                
{
    CLK_DeInit();
    CLK_HSICmd(ENABLE);
    CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
    CLK_ClockSecuritySystemEnable();
}


void TIM4_Config(void)
{
    TIM4_DeInit();
    TIM4_TimeBaseInit(TIM4_PRESCALER_128,125);  // 1ms 中斷
    TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
    TIM4_Cmd(ENABLE);
}


void BEEP_Config(void)
{
    BEEP_DeInit();
    BEEP_Init(BEEP_FREQUENCY_4KHZ);
    BEEP_LSICalibrationConfig(LSI_FREQUENCY_MAX);
}

void BEEP_On(void)
{
    BEEP_Cmd(ENABLE);
}


void BEEP_Off(void)
{
    BEEP_Cmd(DISABLE);
}

void EXTI_Config(void)
{
    GPIO_Init(GPIOC, GPIO_PIN_1, GPIO_MODE_IN_PU_IT); 
    EXTI_DeInit();
    EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);
}


void EEPROM_Config(void)
{
    FLASH_DeInit();
    FLASH_Unlock(FLASH_MEMTYPE_DATA);
    FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);   
}

unsigned char buf[16];


void main(void)
{
    //int value1,value2;
    u8 i = 0;
    u8 buf_temp[16];
    signed char status;
    
    CLK_Config();
    
    Uart2_Init();
    
    LED_Init();
    
    EXTI_Config();
    enableInterrupts();
    
    BEEP_Config();

    delay_ms(50);
    
    for(i = 0;i < 14;i++)
        tx_buf[i] = 0;
    tx_buf[0] = 0xEE;
    tx_buf[1] = 0xCC;
    tx_buf[2] = 0xFE;
    tx_buf[3] = 0x01;
    tx_buf[13] = 0xFF;

    if(MFRC531_Init() == MI_OK)
    {
        BEEP_On();
        delay_ms(200);
        BEEP_Off();
        LED_On();
    }
    else
    {
        BEEP_On();
        delay_ms(200);
        BEEP_Off();
        delay_ms(200);
        BEEP_On();
        delay_ms(200);
        BEEP_Off();
        LED_Off();
        while(1);
    }
    
    MFRC531_CfgISOType('A'); 
    
    rx_counter = 0;
    Uart_RecvFlag = 0;
    Flag = 0;
 
    enableInterrupts();
       
    while (1)
    { 
        if(Flag == 0) // 未發(fā)現(xiàn)卡垦垂,尋卡
        {

            status = MFRC531_OpenAnt();
            delay_ms(10);
        
            status = PcdRequest(0x52, g_pTagType);
        
            status = PcdAnticoll(g_cSNR);
        
            status = PcdSelect(g_cSNR,&buf_temp[0]);

            if(MI_OK != status)
            {
                Flag = 0;
            }
            else              // 獲取卡號和余額
            {
                Flag = 1;
                ChangeCodeKey(p_KeyA,p_Key);    //轉換密鑰格式
                PcdAuthKey(p_Key);              //傳送密鑰到RC531 FIFO
                status = PcdAuthState(0x60, 1, g_cSNR);  //驗證密鑰                            
                status = PcdRead(1, money);
                // 判斷是否進行過初始化,如果沒有牙瓢,初始化錢包內(nèi)的余額為0
                if(money[12] == 0x01)
                {
                    tx_buf[4] = 0x03;
                    tx_buf[5] = g_cSNR[0];
                    tx_buf[6] = g_cSNR[1];
                    tx_buf[7] = g_cSNR[2];
                    tx_buf[8] = g_cSNR[3];
                    tx_buf[9] = money[3];
                    tx_buf[10] = money[2];
                    tx_buf[11] = money[1];
                    tx_buf[12] = money[0];
                    
                    tx_buff[0] = g_cSNR[0];
                    tx_buff[1] = g_cSNR[1];
                    tx_buff[2] = g_cSNR[2];
                    tx_buff[3] = g_cSNR[3];
                    
                }
                else
                {
                    ChangeCodeKey(p_KeyA,p_Key);    //轉換密鑰格式
                    PcdAuthKey(p_Key);              //傳送密鑰到RC531 FIFO
                    status = PcdAuthState(0x60, 1, g_cSNR);  //驗證密鑰 
                    buf_temp[0] = 0;
                    buf_temp[1] = 0;
                    buf_temp[2] = 0;
                    buf_temp[3] = 0;
                    buf_temp[4] = 0xff;
                    buf_temp[5] = 0xff;
                    buf_temp[6] = 0xff;
                    buf_temp[7] = 0xff;
                    buf_temp[8] = 0;
                    buf_temp[9] = 0;
                    buf_temp[10] = 0;
                    buf_temp[11] = 0;
                    buf_temp[12] = 0x01;
                    buf_temp[13] = 0xfe;
                    buf_temp[14] = 0x01;
                    buf_temp[15] = 0xfe;
                    status = PcdWrite(1,buf_temp);
                    
                    
                    tx_buf[4] = 0x03;
                    tx_buf[5] = g_cSNR[0];
                    tx_buf[6] = g_cSNR[1];
                    tx_buf[7] = g_cSNR[2];
                    tx_buf[8] = g_cSNR[3];
                    tx_buf[9] = 0;
                    tx_buf[10] = 0;
                    tx_buf[11] = 0;
                    tx_buf[12] = 0;
                    
                    tx_buff[0] = g_cSNR[0];
                    tx_buff[1] = g_cSNR[1];
                    tx_buff[2] = g_cSNR[2];
                    tx_buff[3] = g_cSNR[3];
                    
                    
                }
            }  
        }

        if(Uart_RecvFlag&Flag)  // 已發(fā)現(xiàn)卡劫拗,讀寫
        {
           
            /*讀取串口輸入指令*/
            money_buf[0] = rx_buf[8];
            money_buf[1] = rx_buf[7];
            money_buf[2] = rx_buf[6];
            money_buf[3] = rx_buf[5];
            
            //讀取串口指令完成后,蜂鳴器響100ms
            BEEP_On();
            delay_ms(100);
            BEEP_Off();
            
            
            switch(rx_buf[4])
            {
            case 0x01:  // 充值
                tx_buf[4] = 0x01;
                ChangeCodeKey(p_KeyA,p_Key);    //轉換密鑰格式
                PcdAuthKey(p_Key);              //傳送密鑰到RC531 FIFO
                PcdAuthState(0x60, 1, g_cSNR);  //驗證密鑰 
                status = PcdValue(0xC1, 1, money_buf);
                ChangeCodeKey(p_KeyA,p_Key);    //轉換密鑰格式
                PcdAuthKey(p_Key);              //傳送密鑰到RC531 FIFO
                PcdAuthState(0x60, 1, g_cSNR);  //驗證密鑰
                status = PcdRead(1, money);
                if(MI_OK == status)
                {
                    tx_buf[9] = money[3];
                    tx_buf[10] = money[2];
                    tx_buf[11] = money[1];
                    tx_buf[12] = money[0];
                    if(tag==1){
                        char buf[8] = {0};
                        char card[8] = {0};    
                        sprintf(card,"%02x%02x%02x%02x",g_cSNR[0],g_cSNR[1],g_cSNR[2],g_cSNR[3]);
                        printf("Card ID: %s\n",card);
                        //printf("Card ID: %x %x %x %x\n", g_cSNR[0],g_cSNR[1],g_cSNR[2],g_cSNR[3]);
                        printf("please press the key to charge money.\n");
                        printf("充值金額:%x %x %x %x\n", money_buf[3],money_buf[2],money_buf[1],money_buf[0]);
                        printf("Total Moeny:  %x %x %x %x\n",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
                        //printf("value: %d%d%d%d\n",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
                        sprintf(buf,"%02x%02x%02x%02x",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
                        printf("value: %s\n",buf);
                   }
                    else UART2_SendString(tx_buf, 14);
                }
                else
                {
                    Flag = 0; 
                }
                //printf("Card ID: %x %x %x %x\n", g_cSNR[0],g_cSNR[1],g_cSNR[2],g_cSNR[3]);
                //printf("please press the key to charge money.\n");
                //printf("value:  %x %x %x %x\n",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
                break;
            case 0x02:  // 扣款
                tx_buf[4] = 0x02;
                ChangeCodeKey(p_KeyA,p_Key);    //轉換密鑰格式
                PcdAuthKey(p_Key);              //傳送密鑰到RC531 FIFO
                PcdAuthState(0x60, 1, g_cSNR);  //驗證密鑰 
                status = PcdValue(0xC0, 1, money_buf);
                ChangeCodeKey(p_KeyA,p_Key);    //轉換密鑰格式
                PcdAuthKey(p_Key);              //傳送密鑰到RC531 FIFO
                PcdAuthState(0x60, 1, g_cSNR);  //驗證密鑰
                status = PcdRead(1, money);
                if(MI_OK == status)
                {
                    tx_buf[9] = money[3];
                    tx_buf[10] = money[2];
                    tx_buf[11] = money[1];
                    tx_buf[12] = money[0];
                        
                    if(tag==1){
                        char buf[8] = {0};
                        char card[8] = {0};    
                        sprintf(card,"%02x%02x%02x%02x",g_cSNR[0],g_cSNR[1],g_cSNR[2],g_cSNR[3]);
                        printf("Card ID: %s\n",card);
                        //printf("Card ID: %x %x %x %x\n", g_cSNR[0],g_cSNR[1],g_cSNR[2],g_cSNR[3]);
                        printf("please press the key to charge money.\n");
                        printf("扣款金額:%x %x %x %x\n", money_buf[3],money_buf[2],money_buf[1],money_buf[0]);
                        printf("Total Moeny:  %x %x %x %x\n",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
                        //printf("value: %d%d%d%d\n",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
                        sprintf(buf,"%02x%02x%02x%02x",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
                        printf("value: %s\n",buf);
                    }
                    else UART2_SendString(tx_buf, 14);
                    
                }
                else
                {
                    Flag = 0; 
                }
                //printf("Card ID: %x %x %x %x\n", g_cSNR[0],g_cSNR[1],g_cSNR[2],g_cSNR[3]);
                //printf("please press the key to charge money.\n");
               // printf("value:  %x %x %x %x\n",tx_buf[9],tx_buf[10],tx_buf[11],tx_buf[12]);
                break;   
            default:
                break;   
            }
   
            LED_Toggle();
        Uart_RecvFlag = 0;

        }
    }
}





/**
  * @brief Retargets the C library printf function to the UART.
  * @param c Character to send
  * @retval char Character sent
  */
PUTCHAR_PROTOTYPE
{
  /* Write a character to the UART1 */
  UART2_SendData8(c);
  /* Loop until the end of transmission */
  while (UART2_GetFlagStatus(UART2_FLAG_TXE) == RESET);

  return (c);
}

/**
  * @brief Retargets the C library scanf function to the USART.
  * @param None
  * @retval char Character to Read
  */
GETCHAR_PROTOTYPE
{
#ifdef _COSMIC_
  char c = 0;
#else
  int c = 0;
#endif
  /* Loop until the Read data register flag is SET */
  while (UART2_GetFlagStatus(UART2_FLAG_RXNE) == RESET);
    c = UART2_ReceiveData8();
  return (c);
}


#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif



/**
  * @brief Retargets the C library printf function to the UART.
  * @param c Character to send
  * @retval char Character sent
  */





stm8s_it.c

/**
  ******************************************************************************
  * @file     stm8s_it.c
  * @author   MCD Application Team
  * @version  V2.0.1
  * @date     18-November-2011
  * @brief    Main Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm8s_it.h"
#include "main.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/* Public functions ----------------------------------------------------------*/

/** @addtogroup GPIO_Toggle
  * @{
  */
#ifdef _COSMIC_
/**
  * @brief  Dummy interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(NonHandledInterrupt, 25)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#endif /*_COSMIC_*/

/**
  * @brief  TRAP interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER_TRAP(TRAP_IRQHandler)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
/**
  * @brief  Top Level Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TLI_IRQHandler, 0)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  Auto Wake Up Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(AWU_IRQHandler, 1)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  Clock Controller Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(CLK_IRQHandler, 2)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  External Interrupt PORTA Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  External Interrupt PORTB Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  External Interrupt PORTC Interrupt routine
  * @param  None
  * @retval None
  */

extern u8 Flag;
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
     if ((GPIO_ReadInputData(GPIOC) & GPIO_PIN_1) == 0x00)//判斷是否PC1引腳觸發(fā)
    //實現(xiàn)按鍵控制
    {
        disableInterrupts();
        rx_buf[0] = 0xCC;
        rx_buf[1] = 0xEE;
        rx_buf[2] = 0xFE;
        rx_buf[3] = 0x01;     
        rx_buf[4] = 0x01;
        rx_buf[5] = 0x20;
        rx_buf[6] = 0x18;
        rx_buf[7] = 0x42;   
        rx_buf[8] = 0x75;
        rx_buf[9] = 0xFF;
        Uart_RecvFlag = 1;
        Flag=1;
        tag=1;
        enableInterrupts();
    }
}

/**
  * @brief  External Interrupt PORTD Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  External Interrupt PORTE Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#ifdef STM8S903
/**
  * @brief  External Interrupt PORTF Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(EXTI_PORTF_IRQHandler, 8)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#endif /*STM8S903*/

#if defined (STM8S208) || defined (STM8AF52Ax)
/**
  * @brief CAN RX Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  CAN TX Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(CAN_TX_IRQHandler, 9)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#endif /*STM8S208 || STM8AF52Ax */

/**
  * @brief  SPI Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(SPI_IRQHandler, 10)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  Timer1 Update/Overflow/Trigger/Break Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  Timer1 Capture/Compare Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

#ifdef STM8S903
/**
  * @brief  Timer5 Update/Overflow/Break/Trigger Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
/**
  * @brief  Timer5 Capture/Compare Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM5_CAP_COM_IRQHandler, 14)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

#else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */
/**
  * @brief  Timer2 Update/Overflow/Break Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  Timer2 Capture/Compare Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#endif /*STM8S903*/

#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
    defined(STM8S005) ||  defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
/**
  * @brief Timer3 Update/Overflow/Break Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM3_UPD_OVF_BRK_IRQHandler, 15)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  Timer3 Capture/Compare Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM3_CAP_COM_IRQHandler, 16)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#endif /*STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */

#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
    defined(STM8S003) ||  defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
/**
  * @brief  UART1 TX Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief  UART1 RX Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}


#endif /*STM8S105*/

/**
  * @brief  I2C Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(I2C_IRQHandler, 19)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

#if defined(STM8S105) || defined(STM8S005) ||  defined (STM8AF626x)
/**
  * @brief  UART2 TX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20)
{
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
  }

/**
  * @brief  UART2 RX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21)
{
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
    u8 data;
    if(UART2_GetITStatus(UART2_IT_RXNE )!= RESET)  
    {
        disableInterrupts();
        data = UART2_ReceiveData8();
        if (!Uart_RecvFlag)
        {
            rx_buf[rx_counter] = data;
            switch (rx_counter)
            {
                case 0:
                if (data == 0xCC)     rx_counter = 1;
                    break;
                case 1:
                    if (data == 0xEE)     rx_counter = 2;
                    break;
                case 2:
                    if (data == 0xFE)     rx_counter = 3;
                    break;
                case 3:
                case 4:          
                case 5:
                case 6:
                case 7:
                case 8:
                    rx_counter++;
                    break;
                case 9:
                    if (data == 0xFF)  
                        Uart_RecvFlag = 1;
                    rx_counter = 0;
                    break;
                default:
                    rx_counter = 0;
                    break;
            }
        }
        else
            rx_counter = 0;
        tag=2;
        enableInterrupts();
    }
  }
#endif /* STM8S105*/

#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
/**
  * @brief  UART3 TX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART3_TX_IRQHandler, 20)
{
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
  }

/**
  * @brief  UART3 RX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART3_RX_IRQHandler, 21)
{
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
}
#endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */

#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
/**
  * @brief  ADC2 interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(ADC2_IRQHandler, 22)
{

    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
    return;

}
#else /*STM8S105, STM8S103 or STM8S903 or STM8AF626x */
/**
  * @brief  ADC1 interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{

    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
    return;

}
#endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */

#ifdef STM8S903
/**
  * @brief  Timer6 Update/Overflow/Trigger Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */
/**
  * @brief  Timer4 Update/Overflow Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
    //TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
}
#endif /*STM8S903*/

/**
  * @brief  Eeprom EEC Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @}
  */

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

————————————————
版權聲明:本文為CSDN博主「路燈謠」的原創(chuàng)文章一罩,遵循CC 4.0 BY-SA版權協(xié)議杨幼,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/lululumiao/article/details/117182962

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末聂渊,一起剝皮案震驚了整個濱河市差购,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌汉嗽,老刑警劉巖欲逃,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異饼暑,居然都是意外死亡稳析,警方通過查閱死者的電腦和手機洗做,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來彰居,“玉大人诚纸,你說我怎么就攤上這事〕露瑁” “怎么了畦徘?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵,是天一觀的道長抬闯。 經(jīng)常有香客問我井辆,道長,這世上最難降的妖魔是什么溶握? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任杯缺,我火速辦了婚禮,結果婚禮上睡榆,老公的妹妹穿的比我還像新娘萍肆。我一直安慰自己,他們只是感情好胀屿,可當我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布匾鸥。 她就那樣靜靜地躺著,像睡著了一般碉纳。 火紅的嫁衣襯著肌膚如雪勿负。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天劳曹,我揣著相機與錄音奴愉,去河邊找鬼。 笑死铁孵,一個胖子當著我的面吹牛锭硼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蜕劝,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼檀头,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了岖沛?” 一聲冷哼從身側響起暑始,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎婴削,沒想到半個月后廊镜,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡唉俗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年嗤朴,在試婚紗的時候發(fā)現(xiàn)自己被綠了配椭。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡雹姊,死狀恐怖除呵,靈堂內(nèi)的尸體忽然破棺而出手销,到底是詐尸還是另有隱情泄隔,我是刑警寧澤隔箍,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站坎背,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏寄雀。R本人自食惡果不足惜得滤,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望盒犹。 院中可真熱鬧懂更,春花似錦、人聲如沸急膀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽卓嫂。三九已至慷暂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間晨雳,已是汗流浹背行瑞。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留餐禁,地道東北人血久。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像帮非,于是被迫代替她去往敵國和親氧吐。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容