专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

NRF9E51读IC卡卡号,并把读取到的数据传送出去

作者:huqin   来源:本站原创   点击数:  更新时间:2013年11月15日   【字体:

 /*================================================================================================  

发送时间间隔是1.2秒  
指示灯:接收灯上电后变红,收到数据后变绿,在3秒内未接收到数据则变红  
        发送灯上电后变红,发送数据变绿  
*==================================================================================================*/   
// Comment out the following line for nRF24E1   
//#define NRF9E5 1   
//卡号是916d47   
   
#ifdef NRF9E5   
#include <Nordic\reg9e5.h>   
#define POWER      3                // 0=min power...3 = max power   
#define HFREQ      1                // 0=433MHz, 1=868/915MHz   
#define CHANNEL  351                // Channel number: f(MHz) = (422.4+CHANNEL/10)*(1+HFREQ)   
#else   
#include <reg24e1.h>   
//#include<absacc.h>   
//#include<stdlib.h>   
#include<intrins.h>   
//#include<stdio.h>   
//#include<math.h>   
#endif   
   
#define uchar unsigned char   
#define uint unsigned int   
#define nop()   _nop_()   
   
   
sbit LED=P0^5;   
sbit data0=P0^4;   
sbit data1=P0^3;   
sbit fsen=P1^0;   
//static volatile uchar timer;   
static volatile uchar t0lrel, t0hrel;   
static volatile uchar t1lrel, t1hrel;   
   
uchar a[3];   
uchar ou_check=0;   
uchar ji_check=0;   
uchar count=0;   
uchar count1=0;   
uint count0=0;   
   
bit over_flag=0;   
bit over1_flag=0;   
//bit over2_flag=0;   
   
uchar code array[3]={0x91,0x6d,0x47};   
   
#ifndef NRF9E5   
   
struct RFConfig   
{   
    unsigned char n;   
    unsigned char buf[15];   
};   
   
typedef struct RFConfig RFConfig;   
   
#define ADDR_INDEX  8               // Index to address bytes in RFConfig.buf   
#define ADDR_COUNT  4               // Number of address bytes   
//暂时3个字节卡号无卡号的校验和   
const RFConfig tconf =   
{   
    15,   
    0x18,                           // Payload size transmitter Rx #2 (not used in this example)   
    0x18,                           // Payload size transmitter Rx #1 (not used in this example)   
    0x00, 0x00, 0x00, 0x00, 0x00,   // Address of transmitter Rx #2 (not used in this example)   
    0x00, 0x12, 0x34, 0x56, 0x78,   // Address of transmitter Rx #1 (not used in this example)   
    0x81, 0x6f, 0x04   
};   
   
const RFConfig rconf =   
{   
    15,   
    0x18,                           // Payload size receiver Rx #2 (not used in this example)   
    0x18,                           // Payload size receiver Rx #1   
    0x00, 0x00, 0x00, 0x00, 0x00,   // Address receiver Rx #2 (not used in this example)   
    0x00, 0x12, 0x34, 0x56, 0x78,   // Address receiver Rx #1 (four lower bytes used here)   
    0x81, 0x6f, 0x05   
};   
   
#endif   
   
void Delay100us(volatile unsigned char n)   
{   
    unsigned char i;   
    while(n--)   
        for(i=0;i<35;i++)   
            ;   
}   
void fs()   
{   uchar i;   
    uchar j;   
    uchar k;   
    uchar ou_check=0;   
    uchar ji_check=0;   
    for(i=0;i<8;i++)   
    ou_check+=((a[0]>>i)&0x01);   
    for(i=4;i<8;i++)   
    ou_check+=((a[1]>>i)&0x01);   
    if(ou_check&0x01)   
    ou_check=1;   
    else   
    ou_check=0;   
    for(i=0;i<4;i++)   
    ji_check+=((a[1]>>i)&0x01);   
    for(i=0;i<8;i++)   
    ji_check+=((a[2]>>i)&0x01);   
    if(ji_check&0x01)   
    ji_check=0;   
    else   
    ji_check=1;   
    if(ou_check)                          //1的个数是奇数   
    {   data1=0;   
        count1=0;   
        over1_flag=0;   
        TR2=1;   
        while(!over1_flag)   
        {   nop();   
        }   
        //Delay100us(1);   
        //data1=1;   
        //Delay100us(10);   
    }   
    else   
    {   data0=0;   
        count1=0;   
        over1_flag=0;   
        TR2=1;   
        while(!over1_flag)   
        {   nop();   
        }          
        //Delay100us(1);   
        //data0=1;   
        //Delay100us(10);   
    }   
    for(i=0;i<3;i++)   
    {   for(j=8;j>0;j--)   
        {    k=j-1;   
             k=(a[i]>>k)&0x01;   
             if(k)   
             {  data1=0;   
                over1_flag=0;   
                count1=0;   
                TR2=1;   
                while(!over1_flag)   
                {;}   
                //Delay100us(1);   
                //data1=1;   
                //Delay100us(10);   
             }   
             else   
             {  data0=0;   
                count1=0;   
                over1_flag=0;   
                TR2=1;   
                while(!over1_flag)   
                {;}                
        //  Delay100us(1);   
                //data0=1;   
            //Delay100us(10);   
                   
            }   
        }   
    }   
    if(ji_check)                  //1的个数是偶数   
    {   
        data1=0;   
        count1=0;   
        over1_flag=0;   
        TR2=1;   
        while(!over1_flag)   
        {;}   
        //Delay100us(1);   
    //  data1=1;   
        //Delay100us(10);   
    }   
    else   
    {   data0=0;   
        count1=0;   
        over1_flag=0;   
        TR2=1;   
        while(!over1_flag)   
        {;}   
        //Delay100us(1);   
        //data0=1;   
        //Delay100us(10);   
    }   
}   
/*void rst_wdog()  
{   if((REGX_CTRL&0x10)!=0x10)  
    {   REGX_MSB=0x75;  
        REGX_LSB=0x30;  
    //  REGX_CTRL=0x08;  
    }  
}*/   
unsigned char SpiReadWrite(unsigned char b)   
{   
    EXIF &= ~0x20;                  // Clear SPI interrupt   
    SPI_DATA = b;                   // Move byte to send to SPI data register   
    while((EXIF & 0x20) == 0x00)    // Wait until SPI hs finished transmitting   
        ;      
    return SPI_DATA;   
}   
void TransmitPacket()   
{   
    unsigned char i;   
    CE = 1;   
    Delay100us(0);   
    // All packets start with the receiver address:   
    for(i=0;i<ADDR_COUNT;i++)   
        SpiReadWrite(tconf.buf[ADDR_INDEX+i]);   
    SpiReadWrite(array[0]);   
    SpiReadWrite(array[1]);   
    SpiReadWrite(array[2]);   
    CE = 0;   
    Delay100us(3);                  // Wait ~300us   
}   
///led000000000000000000000000000000000000   
/*定时器0和定时器1的定时间是10ms*/   
void InitTimer(void)   
{   
    TR0 = 0;   
    TMOD &= ~0x33;   
    TMOD |= 0x11;                   // mode 1   
    CKCON |= 0x98;                  // T0M = 1 (/4 timer clock)   
    t0lrel = 0xC0;                  // 1KHz tick...   
    t0hrel = 0x63;                  // ... = 65536-16e6/(4*1e3) = F060h   
    TF0 = 0;                        // Clear any pending Timer0 interrupts   
    ET0 = 1;                        // Enable Timer0 interrupt   
    TR1=0;   
    t1lrel=0xC0;   
    t1hrel=0x63;   
    TF1 = 0;                        // Clear any pending Timer0 interrupts   
    ET1 = 1;      
    T2CON=0x00;   
    RCAP2H=0xFE;   
    RCAP2L=0x70;   
    ET2=1;     
}   
void Timer0ISR (void) interrupt 1   
{   
    TF0 = 0;                        // Clear Timer0 interrupt   
    TH0 = t0hrel;                   // Reload Timer0 high byte   
    TL0 = t0lrel;                   // Reload Timer0 low byte   
    count0++;   
    if (count0==300)   
    {      
        LED=0;                 // Led off          
        TR0 = 0;                    // Stop timer   
    }   
}   
void Timer1ISR(void) interrupt 3   
{      
    TF1 = 0;                        // Clear Timer1 interrupt   
    TH1 = t1hrel;                   // Reload Timer1 high byte   
    TL1 = t1lrel;                   // Reload Timer1 low byte   
    count++;   
    if (count == 120)   
    {   
       count=0;   
       over_flag=1;   
       TR1=0;   
    }      
}   
void Timer2ISR (void) interrupt 5 using 2   
{   
    TF2 = 0;     
    count1++;   
    if (count1==1)   
    {      
        data0=1;   
        data1=1;   
    }   
    else if(count1==11)   
    {   count1=0;   
        over1_flag=1;   
        TR2=0;   
    }   
}   
void Receiver(void)   
{   
    unsigned char b;   
    CS = 1;   
    Delay100us(0);   
    for(b=0;b<rconf.n;b++)           //写入配置   
    {   
        SpiReadWrite(rconf.buf[b]);   
    }   
    CS = 0;   
       
    for(;;)   
    {   
        CE = 1;    
        while(DR1 == 0)   
        {   //rst_wdog();   
            nop();   
        }   
        a[0]=SpiReadWrite(0);   
        a[1]=SpiReadWrite(1);   
        a[2]= SpiReadWrite(2);     
        CE = 0;   
        LED=1;   
        count0=0;   
        TR0 = 1;   
        if(fsen==0)   
        {      
            fs();   
        }   
        //rst_wdog();      
    }   
}   
   
void Transmitter(void)   
{   
    unsigned char b;   
    CS = 1;   
    Delay100us(0);   
    for(b=0;b<tconf.n;b++)   
    {   
        SpiReadWrite(tconf.buf[b]);   
    }   
    CS = 0;      
    for(;;)   
    {   //rst_wdog();   
        TransmitPacket();        // Transmit data   
        over_flag=0;   
        TR1=1;   
        LED=1;         
        while(!over_flag)   
        {   nop();   
        }          
    }   
}   
   
void Init(void)   
{   uchar i;   
    uchar j;   
    P0_DIR=0x00;               
    P0=0xfb;   
    P1=0xff;             
    SPICLK=1;   
    SPI_CTRL=0x02;          //连接到RADIO上   
    PWR_UP = 1;                     // Turn on Radio on 24E1   
    Delay100us(30);                 // Wait > 3ms   
    InitTimer();   
    EA = 1;    
    for(j=0;j<2;j++)   
    {   LED=1;   
        for(i=0;i<10;i++)   
        {   Delay100us(250);   
            Delay100us(250);           
        }   
        LED=0;   
        for(i=0;i<10;i++)   
        {   Delay100us(250);   
            Delay100us(250);           
        }   
    }   
}   
   
void main(void)   
{      
    Init();   
   /*f((REGX_CTRL&0x10)!=0x10)  
    {  
        REGX_MSB=0x75;  
        REGX_LSB=0x30;  
        REGX_CTRL=0x08;  
    }  */   
    //Receiver();   
    Transmitter();   
}   
关闭窗口

相关文章