⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cc2420hal.c

📁 CC2420初始化程序
💻 C
字号:
#include <msp430x16x.h>
#include "cc2420const.h"
#include "port_config_CC2420.h"
int isTxEmpty(){//发送缓冲区为空返回true
    if (U0TCTL & TXEPT) {
      return 1;
    }
    return 0;
  }

  int isTxIntrPending()	//TXFIFO EMPTY 返回true
{
  if (IFG1 & UTXIFG0){
    IFG1 &= ~UTXIFG0;
    return 1;
  }
  return 0;
}
int isRxIntrPending()//RXFIFO FULL 返回true
{
  if (IFG1 & URXIFG0){
    IFG1 &= ~URXIFG0;
    return 1;
  }
  return 0;
}
void SPI_Tx(unsigned char data) //SPI tx
{

  while((UTCTL0 & 0x01 )!=0x01);        //等待直到没有数据发送
  TXBUF0 = data;

}
unsigned char SPI_Rx() //SPI rx
{
  unsigned char data;
  data = RXBUF0;
  return data;
}
void HPLCC2420_cmd(char addr) {


   P4OUT &= ~0x04;
      isTxIntrPending();
      SPI_Rx();
      SPI_Tx(addr);
      while(!(isRxIntrPending())) ;
   P4OUT |= 0x04;



}
 /**
   * Read 16-bit data
   *
   * @return 16-bit register value
   */
int HPLCC2420_read(char addr) {
    int data = 0;
    PHY_Selected();
      isTxIntrPending();
      SPI_Rx();
      SPI_Tx(addr | 0x40);
      while(!isRxIntrPending()) ;
      SPI_Rx();
      SPI_Tx(0);
      while(!(isRxIntrPending())) ;
      data = SPI_Rx();
      SPI_Tx(0);
      while( !(isRxIntrPending())) ;
      data = ((data << 8) & 0x0FF00 )| (SPI_Rx() & 0x0FF);
    PHY_Deselected();
   return data;
}
void flushRxfifo(void)
{
     HPLCC2420_read(CC2420_RXFIFO);          //flush Rx fifo
     HPLCC2420_cmd(0x08);//FASTSPI_STROBE(CC2420_SFLUSHRX);
     HPLCC2420_cmd(0x08);//FASTSPI_STROBE(CC2420_SFLUSHRX);
}

char HPLCC2420_write(char addr, int data) {
    char status = 0;
    PHY_Selected();

      isTxIntrPending();
     SPI_Rx();
     SPI_Tx(addr);

      while(!(isRxIntrPending())) ;


      status = (SPI_Rx()&0x7E);
      SPI_Tx((data >> 8) & 0x0FF);
      while( !(isTxIntrPending())) ;
      SPI_Tx(data & 0x0FF);
      while(!(isTxEmpty())) ;
      PHY_Deselected();
    return status;
}
void HPLCC2420_RAMread(signed short Raddr, unsigned char length, unsigned char *addr)
{
      char i = 0,ramlen;signed short ramaddr;unsigned char *addrbuf;
      ramaddr = Raddr;
      ramlen = length;

      addrbuf =addr;

      PHY_Selected();
      // clear the RX flag if set
      isTxIntrPending();
      SPI_Rx(); //isRxIntrPending();

      SPI_Tx((ramaddr & 0x7F) | 0x80);
      while(!(isRxIntrPending())) ;
      SPI_Rx();
      SPI_Tx(((ramaddr >> 1) & 0xC0) | 0x20);
      while( !(isRxIntrPending())) ;
      SPI_Rx();

      if (ramlen > 0) {
        for (i = 0; i < ramlen; i++) {
          SPI_Tx(0);
	  while(!(isRxIntrPending())) ;
          addrbuf[i] = SPI_Rx();
        }
      }
      PHY_Deselected();


}

void HPLCC2420_RAMwrite(signed short Raddr, unsigned char length, unsigned char *addr) {
      unsigned char i = 0,ramlen;signed short ramaddr;unsigned char *addrbuf;
     ramaddr = Raddr;
        ramlen = length;
        addrbuf = addr;

      PHY_Selected();
            // clear the RX flag if set
      isTxIntrPending();
      SPI_Rx(); //isRxIntrPending();
      SPI_Tx((ramaddr & 0x7F) | 0x80);
      while( !(isTxIntrPending())) ;
      SPI_Tx(((ramaddr >> 1) & 0xC0));
      while( !(isTxIntrPending())) ;
      for (i = 0; i < ramlen; i++) {
        SPI_Tx(addrbuf[i]);
	while(!(isTxIntrPending())) ;
      }
      while(!(isTxEmpty())) ;
      PHY_Deselected();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -