asdsad

/***********************************************************************/
/* Copyright: 2014, Datang Mobile. Co., Ltd.                           */
/* File Name: Rfid.c                                                   */
/* Author   : Lijianjian                                               */
/* Version  : 1.0                                                      */
/* Date     : 2014\8                                                   */
/* Descrp   : 初始化及主函数实现                                       */
/* Others   : None.                                                    */   
/* History  : None.                                                    */
/***********************************************************************/

#include "Include.h"

#ifdef  __cplusplus
extern "C" {
#endif
 
  __ramfunc static void SetSysDividers(ULONG uiOutDiv1, ULONG uiOutDiv2, ULONG uiOutDiv3, ULONG uiOutDiv4);
 
 extern BYTE Chk_ByteData[30];   
 extern U8 REV_UART1 ;
 extern U8 isr_flag ;

/* 以下是将寻到卡的UID号按字节,写入下面数组中 */
U8 Card_UID[8]={0xE0, 0x04, 0x01, 0x00, 0x28, 0x28, 0x6D, 0xE2}; 


/***FUNC+*********************************************************************/
/* Name   : ParseMsg                                                        */
/* Descrp : Inventory a special card                                           */
/* Input  : None.                                                            */   
/* Output : None.                                                            */
/* Return : None.                                                            */   
/***FUNC-*********************************************************************/
void Send_ParseMsg_Err(BYTE err)
{
  switch(err)
  {
    case 0:
      Uart_Printf("Error: Dec_OK\r\n");
    break;
    case 1:
    Uart_Printf("Error: NO_Res\r\n");
    break;
    case 5:
    Uart_Printf("Error: Crc_Err\r\n");
    break;
  default:
    break;
  }
}

/***FUNC+*********************************************************************/
/* Name   : Inventory_One_Card                                                        */
/* Descrp : Inventory a special card                                           */
/* Input  : None.                                                            */   
/* Output : None.                                                            */
/* Return : None.                                                            */   
/***FUNC-*********************************************************************/
void Inventory_One_Card(void)
{
  BYTE err ;
  SendViccLedOnOff(50); //指示灯闪烁
  Inventory(INVENTORY_FLAG_SINGLE_CARD);//0x26对应单卡命令
  err = ParseMsg(INVENTORY,INVENTORY_bit,INVENTORY_count) ;
  if(err ==0)
  {
    BuzzerOnOff(100);
  }
}
/***FUNC+*********************************************************************/
/* Name   : Card_Select                                                        */
/* Descrp : Select a special card                               */
/* Input  : None.                                                            */   
/* Output : None.                                                            */
/* Return : None.                                                            */   
/***FUNC-*********************************************************************/
void Card_Select(BYTE uid[])
{ 
  BYTE err ;
   int i;
  U8 card_uid[8];
  for(i=0; i<8 ;i++)
  {
    card_uid[i] = uid[i];
  }
  SendViccLedOnOff(50); //指示灯闪烁
  Uart_Printf("Select card [%02X %02X %02X %02X %02X %02X %02X %02X]\r\n", \
               uid[0], uid[1], uid[2], uid[3], uid[4], uid[5], uid[6], uid[7]);
  Uart_Printf("Card Response: ");
  Select(SELECT_FLAG, card_uid);
  err = ParseMsg(SELECT,SELECT_bit,SELECT_count) ;
  Uart_Printf("\r\n");
  if(err ==0)
  {
    BuzzerOnOff(100);
  }
  else
  {
    Send_ParseMsg_Err(err);
    Uart_Printf("Select Failed \r\n",err);
  }
}


/***FUNC+*********************************************************************/
/* Name   : PLLInit                                                         */
/* Descrp : Initialize PLL                                                   */
/* Input  : None.                                                            */   
/* Output : None.                                                            */
/* Return : None.                                                            */   
/***FUNC-*********************************************************************/
void PLL_Init(void)
{

    MCG_C2 = 0;                        // x x RANGE HGO EREFS LP IRCS
                                       // 0 0   00    0    0   0   0
                                       // RANGE=00:Low frequency range selected for the crystal oscillator of 32 kHz to 40 kHz
                                       // HGO  =0 :Configure crystal oscillator for low-power operation
                                       // EREFS=0 :External reference clock requested 
    
    SIM_SCGC4 |= SIM_SCGC4_LLWU_MASK;
    LLWU_CS |= LLWU_CS_ACKISO_MASK;
    
    MCG_C1 = MCG_C1_CLKS(2) |          // CLKS FRDIV IREFS IRCLKEN IREFSTEN
             MCG_C1_FRDIV(3);          //  10   011    1     0        0  
                                       // CLKS =10 :External reference clock is selected
                                       // FRDIV=011:Divide Factor is 8
    
    while (MCG_S & MCG_S_IREFST_MASK)  // wait until source of FLL reference clock is the external reference clock
    {
        ;  
    }
    
    while (((MCG_S & MCG_S_CLKST_MASK) // wait until external reference clock is selected
          >> MCG_S_CLKST_SHIFT) != 0x2)
    {
        ;   
    }
    
    MCG_C5 = MCG_C5_PRDIV(0x0B);       // x PLLCLKEN PLLSTEN PRDIV
                                       // 0    0        0    11000
                                       // PRDIV=11000:Divide Factor is 25 
       
    SetSysDividers(0, 1, 1, 3);        // MCG=PLL, core=MCG, bus=MCG/2, FlexBus=MCG/2, Flash clock=MCG/4
    
    MCG_C6 = MCG_C6_PLLS_MASK |        // LOLIE PLLS CME VDIV  
             MCG_C6_VDIV(24);          //   0    1    0  11000    
                                       // PLLS=1    :PLL is selected 
                                       // VDIV=11000:Multiply Factor is 48
                                       // PLL = 2MHz x 48 = 96MHz
    
    while (!(MCG_S & MCG_S_PLLST_MASK))// wait until source of PLLS clock is PLL clock
    {
        ;   
    }
    
    while (!(MCG_S & MCG_S_LOCK_MASK)) // wait until PLL is locked
    {
        ;
    }
    
    MCG_C1 &= ~MCG_C1_CLKS_MASK;       // Output of FLL or PLL is selected
    
    while (((MCG_S & MCG_S_CLKST_MASK) // wait until Output of the PLL is selected 
          >> MCG_S_CLKST_SHIFT) != 0x3)
    {
        ;
    }
}

/***FUNC+*********************************************************************/
/* Name   : SetSysDividers                                                   */
/* Descrp : This routine must be placed in RAM.It is a                       */
/*          workaround for errata e2448.Flash prefetch must                  */
/*          be disabled when the flash clock divider is                      */
/*          changed.This cannot be performed while executing                 */
/*          out of flash.There must be a short delay after                   */
/*          the clock dividers are changed before prefetch                   */
/*          can be re-enabled.                                               */
/* Input  : None.                                                            */   
/* Output : None.                                                            */
/* Return : None.                                                            */   
/***FUNC-*********************************************************************/
__ramfunc static void SetSysDividers(ULONG uiOutDiv1, ULONG uiOutDiv2, ULONG uiOutDiv3, ULONG uiOutDiv4)
{

    ULONG uiTemp;
  
    INT  i;
  
    // store present value of FMC_PFAPR
    uiTemp = FMC_PFAPR; 
  
    // set M0PFD through M7PFD to 1 to disable prefetch
    FMC_PFAPR |= FMC_PFAPR_M7PFD_MASK | FMC_PFAPR_M6PFD_MASK | FMC_PFAPR_M5PFD_MASK
              | FMC_PFAPR_M4PFD_MASK | FMC_PFAPR_M3PFD_MASK | FMC_PFAPR_M2PFD_MASK
              | FMC_PFAPR_M1PFD_MASK | FMC_PFAPR_M0PFD_MASK;
  
    // set clock dividers to desired value  
    SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(uiOutDiv1) | SIM_CLKDIV1_OUTDIV2(uiOutDiv2) 
                | SIM_CLKDIV1_OUTDIV3(uiOutDiv3) | SIM_CLKDIV1_OUTDIV4(uiOutDiv4);

    // wait for dividers to change
    for (i = 0; i < uiOutDiv4; i++)
    {
        ;   
    }
 
    // re-store original value of FMC_PFAPR
    FMC_PFAPR = uiTemp; 
} 

/***FUNC+*********************************************************************/
/* Name   : main                                                             */
/* Descrp :                                                                  */
/* Input  : None.                                                            */   
/* Output : None.                                                            */
/* Return : None.                                                            */   
/***FUNC-*********************************************************************/
void main(void)
{
    BYTE key_value;
    // Close all irq.                                        
    DisableInterrupts;  
    
    //Watchdog disable.                                     
    WatchdogDisable();

    //Copy the vector table to RAM . 
    VECTableInit();

    //Enable all of the port clocks. These have to be enabled to configure
    //pin muxing options, so most code will need all of these on anyway.
    SIM_SCGC5 |= (SIM_SCGC5_PORTA_MASK
                      | SIM_SCGC5_PORTB_MASK
                      | SIM_SCGC5_PORTC_MASK
                      | SIM_SCGC5_PORTD_MASK
                      | SIM_SCGC5_PORTE_MASK );

    //Initialize PLL.                                        
    PLL_Init();
    
    //InitI/O 
    InitGpio();
    
    //Config Led & Buzzer
    InitPeripheral();
    
    //Config key
    Init_Key();

    UART_Init(PERIPH_SYS_CLK, BAUD_RATE_9600, UART1);//设置波特率为9600

    key_value = 0;
    
    Uart_Printf("Press KEY1 to select RFID card... \r\n");
    Uart_Printf("\r\n/*******************************************/\r\n");
    
    while(1)//使用KEY1控制寻卡
    {
      key_value = Key_Scan();
      switch(key_value)
      {
        case KEY1_DOWN:
         Card_Select(Card_UID);
          break;
        default:
          break;
      }
    }
    
}

#ifdef  __cplusplus
}
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值