4.2 软件模拟SPI驱动参考

  1.  
     

    #define Rom_csH     P1_1 = 1

    #define Rom_csL     P1_1 = 0

    #define MOSIH       P1_2 = 1

    #define MOSIL       P1_2 = 0

    #define Rom_sckH    P1_3 = 1

    #define Rom_sckL    P1_3 = 0

    #define MISO        P1_4

     

    void zk_init(void)

    {

      Rom_csH;

      MOSIH;

      Rom_sckH;

    }

     

    void Send_Byte(unsigned char out)

    {  

        unsigned char i=0;

     

        for(i=0;i<8;i++)

        {

          Rom_sckL;

          if(((out<<i)&0x80)==0)

              MOSIL;  

          else

              MOSIH;

          Rom_sckH;

      }

    }

     

    unsigned char Get_Byte(void)        

    {

        unsigned char i;

        unsigned char read_dat;

     

        Rom_sckH;

        for(i=0;i<8;i++)

        {

            Rom_sckL;

            read_dat=read_dat<<1;

            if(MISO)                

                read_dat|=0x01;

            else

                read_dat&=0xfe;

            Rom_sckH;

        }

        return(read_dat);

    }

     

    void SPI_Address(unsigned char AddH,unsigned char AddM,unsigned char AddL)

    {

        Send_Byte(AddH);

        Send_Byte(AddM);

        Send_Byte(AddL);

    }

     

    unsigned long r_dat_bat(unsigned long address,unsigned long DataLen,unsigned char *pBuff)

    {

        unsigned long i;

        unsigned char addrHigh;

        unsigned char addrMid;

        unsigned char addrLow;

        addrHigh=address>>16;

        addrMid=address>>8;

        addrLow=(unsigned char)address;

     

        Rom_csL;          

        Send_Byte(0x03);    

        SPI_Address(addrHigh,addrMid,addrLow);  

        for(i=0;i<DataLen;i++)

            *(pBuff+i)=Get_Byte();

        Rom_csH;

        return i;

    }

     

    unsigned char gt_read_data(unsigned char* sendbuf , unsigned char sendlen , unsigned char* receivebuf, unsigned int receivelen)

    {

        unsigned int i;

        Rom_csL;

        for(i = 0; i < sendlen;i++)

        {

            Send_Byte(sendbuf[i]);

        }

        for(i = 0; i < receivelen;i++)

        {

            receivebuf[i] = Get_Byte();

        }

        Rom_csH;

        return 1;

    }

 

 

    • 联系电话

    • 0755—83453881
    • 服务时间

    • 周一至周五 9:00-18:00
    • 高通GT-HMI交流群