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

📄 main.c

📁 msp430+无线芯片cc1020程序
💻 C
字号:



#include "main.h"



/* Set the transceiver in TX/RX state       */

// RAM variables
__no_init char TXANALOG;
__no_init char RXANALOG;

// RAM variables
__no_init char PA_POWER;

// RAM variables
__no_init char PreambleLength;

char UnitAddress;

volatile char TXBuffer[TX_BUFFER_SIZE];
volatile char RXBuffer[RX_BUFFER_SIZE];

// Index pointers for use with buffers
volatile char TXBufferIndex;
char RXBufferReadIndex;
volatile char RXBufferWriteIndex;

// Counter variables
char PreambleCount;
char PreambleError;
char ByteCounter;
char BitCounter;

// I just want to check whether I can configure  the device correctly...

char Reg_Dump;
char timer_int;


// Contains the total number of bytes to send in TX, including preamble and header
char BytesToSend;
// The number of bytes of data to receive in RX
char BytesToReceive;

// State variable stores the current state of the state machine
char State;

// This variable stores the state to switch to
// It is updated by the interrupt routine, while the main program
// does the actual state switch
char NextState;

// This struct stores various flags in a space-efficient manner
volatile struct
{
  unsigned char PreambleFound:1;
  unsigned char UI1Found:1;
  unsigned char LastPreambleBit:1;
  unsigned char LastDataBit:1;
};



// This routine initialises the TX buffer at startup
void InitializeTXBuffer(void)
{
  char i;

  // Put preamble into buffer
  for(i=0;i<PreambleLength;i++){
    TXBuffer[i]=PREAMBLE_BYTE;
  }

  TXBuffer[PreambleLength+0]=UI1;            // First byte of unique identifier
  TXBuffer[PreambleLength+1]=UI2;            // Second byte of unique identifier
  TXBuffer[PreambleLength+2]=UnitAddress;    // Unit address
  TXBuffer[PreambleLength+3]=0x00;           // Data length, default : no data

  /*
  for (i=PreambleLength+HEADER_SIZE; i<PreambleLength+HEADER_SIZE+20;i++)
  {
      TXBuffer[i]=0x42-PreambleLength+HEADER_SIZE+i;
  }
  TXBufferIndex=i;
  */
}



// Union for shifting bits in or out of the CC1020
union {
  char ShiftReg;
  struct {
    unsigned char ShiftRegLSB :1;
    unsigned char :1;
    unsigned char :1;
    unsigned char :1;
    unsigned char :1;
    unsigned char :1;
    unsigned char :1;
    unsigned char ShiftRegMSB :1;
  };
};


// This routine handles setup needed when changing states
void ChangeState(void)
{
  switch(NextState){
    case RX_STATE:
      if(State==TX_STATE){

        /* PIC calls...*/

        /*OPTION=0xC0;        // INT on rising edge
        TRISC|=0x02;        // Set DIO as input
        */

        /* MSP430 calls */

        TI_CC_GDO2_PxIES &= ~TI_CC_GDO2_PIN;  // Int on raising edge
        TI_CC_GDO0_PxDIR &= ~TI_CC_GDO0_PIN;  // Set DIO as input
        SetupCC1020RX(RXANALOG, PA_POWER);
      }

      State=RX_STATE;

      /*
      SET_RXLED(ON);
      SET_TXLED(OFF);
      READY=1;  // HW Handshake : Not Ready
       */
      TI_CC_CTS_PxOUT |= TI_CC_CTS_PIN;     //Not ready
      BitCounter=0;
      ByteCounter=0;
      break;

    case TX_STATE:
      if(State!=TX_STATE){

        /* PIC calls
        OPTION=0x80;        // INT on falling edge
        TRISC&=~(0x02);     // Set DIO as output
        */

        TI_CC_GDO2_PxIES |= TI_CC_GDO2_PIN; //INT on falling edge
        TI_CC_GDO0_PxDIR |= TI_CC_GDO0_PIN; // Set DIO as output

        SetupCC1020TX(TXANALOG, PA_POWER);
      }

      State=TX_STATE;
      TI_CC_CTS_PxOUT |= TI_CC_CTS_PIN;     //Not ready
      //ME1 &= ~URXE0;
      //ME1 &= ~UTXE0;
      /*
      SET_RXLED(OFF);
      SET_TXLED(ON);
      READY=1;  // HW Handshake : Not Ready
      RCIE=0;   // Disable UART Interrupts
      */
      BytesToSend=TXBufferIndex; // Number of bytes to send

      TXBuffer[PreambleLength+3]=BytesToSend-HEADER_SIZE-PreambleLength;

      LastDataBit = FALSE;
      TXBufferIndex=0;
      BitCounter=0;
      ShiftReg=TXBuffer[TXBufferIndex++];
      break;

    case IDLE_STATE:
      if(State==TX_STATE){

        /*
        OPTION=0xC0;        // INT on rising edge
        TRISC|=0x02;        // Set DIO as input
        */
        TI_CC_GDO2_PxIES &= ~TI_CC_GDO2_PIN;  // Int on raising edge
        TI_CC_GDO0_PxDIR &= ~TI_CC_GDO0_PIN;  // Set DIO as input
        SetupCC1020RX(RXANALOG, PA_POWER);
      }
      TI_CC_CTS_PxOUT &= ~TI_CC_CTS_PIN;
      State=IDLE_STATE;
      /*
      SET_RXLED(OFF);
      SET_TXLED(OFF);
      READY=0;  // HW Handshake : Ready
      RCIE=1;   // Enable UART Interrupts
      */
      //ME1|=UTXE0|URXE0;
      TXBufferIndex=HEADER_SIZE+PreambleLength;
      PreambleCount=0;
      PreambleError=0;
      PreambleFound=FALSE;
      UI1Found=FALSE;

      break;
  }
}





void DumpCC1020Regs(void)
{
  char RegCounter;
  char Value;

  for(RegCounter=0x01; RegCounter<=0x20; RegCounter++) {

    Value=ReadFromCC1020Register(RegCounter);
    Reg_Dump=Value;

  }
}


void main( void )
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  DCOCTL|= 0xE0;
  BCSCTL1 |= 0x07;

  /* Configuration of LEDs and Buttons, at the moment I use only one LED and
   * one button...test purpose*/

  TI_CC_SW_PxIES = TI_CC_SW1+TI_CC_SW2+TI_CC_SW3+TI_CC_SW4;//Int on falling edge
  TI_CC_SW_PxIFG &= ~(TI_CC_SW1+TI_CC_SW2+TI_CC_SW3+TI_CC_SW4);//Clr flags
  TI_CC_SW_PxIE = TI_CC_SW1+TI_CC_SW2+TI_CC_SW3+TI_CC_SW4;//Activate enables
  TI_CC_LED_PxDIR = TI_CC_LED1 + TI_CC_LED2 + TI_CC_LED3 + TI_CC_LED4; //Out


  SetupCC1020ForSPI();
  uart_init();

  RXBufferReadIndex=0;
  RXBufferWriteIndex=0;

  SetupCC1020PD();
  ResetCC1020();



  SetupCC1020All();

  DumpCC1020Regs();
  WakeUpCC1020ToTX(TXANALOG);




  TACCR0 = 50000;
  TACTL = TASSEL_2;



  TI_CC_CTS_PxDIR |= TI_CC_CTS_PIN;
  TI_CC_TEST_PxDIR |=  TI_CC_TEST_PIN;

  TI_CC_GDO2_PxIES |= TI_CC_GDO2_PIN;       //INT on falling edge
  TI_CC_GDO0_PxDIR |= TI_CC_GDO0_PIN;       // Set DIO as output

  if (!CalibrateCC1020(PA_POWER))
      UnitAddress=2;                        // debug...

  WakeUpCC1020ToRX(RXANALOG);

  TI_CC_GDO2_PxIES &= ~TI_CC_GDO2_PIN;      // Int on Freising edge
  TI_CC_GDO0_PxDIR &= ~TI_CC_GDO0_PIN;      // Set DIO as input

  if (!CalibrateCC1020(PA_POWER))
     UnitAddress=3;                         // debug...

   // Force update
  State=TX_STATE;
  NextState=IDLE_STATE;

  LastDataBit = FALSE;

  InitializeTXBuffer();

  //I wanna check the MCLK, I am expecting a f around 800kHz

  P5DIR|=0x10;                              //MCLK as output...
  P5SEL|=0x10;

  TI_CC_GDO2_PxIFG &= ~TI_CC_GDO2_PIN;      // Clear flag
  TI_CC_GDO2_PxIE |= TI_CC_GDO2_PIN;

  _EINT();
  //putchar(0x63);
 // _BIS_SR(LPM3_bits + GIE);                 // Enter LPM3, enable interrupts

   while(1){
    // If new state requested, enter requested state
    if(State != NextState){
      ChangeState();
    }//if State

    /* If data in receive buffer...*/
    if (RXBufferReadIndex!=RXBufferWriteIndex) {


      putchar(RXBuffer[RXBufferReadIndex]);

      // Increase read index, wrap around when reached end of buffer
      //RXBufferReadIndex=(RXBufferReadIndex+1)%RX_BUFFER_SIZE;
      RXBufferReadIndex++;
      RXBufferReadIndex&=0x3F;
    }// if Data received
  /*
     if (State == IDLE_STATE)
          NextState=TX_STATE;
    */
      /*
     if (!(IFG1 & URXIFG0) && State == IDLE_STATE)
     {
          TI_CC_CTS_PxOUT &= ~TI_CC_CTS_PIN;      //ready
     }
      */
     if((IFG1 & URXIFG0)){
      if(State==IDLE_STATE){


        if ((TACTL & 0x0030)== 0)
        {
          TAR=0;
          TACTL = MC_1 + TASSEL_2 + ID_3;
          TI_CC_TEST_PxOUT &= ~TI_CC_TEST_PIN;
        }
        // Read data from UART
        TXBuffer[TXBufferIndex++]=RXBUF0;

        // IMPORTANT:
        // We may have another interrupt or two occur before we can change mode.
        // Therefore, leave safety margin!
        if(TXBufferIndex>=(TX_BUFFER_SIZE-3)){
          TI_CC_CTS_PxOUT |= TI_CC_CTS_PIN;
          TACTL = MC_0 + TASSEL_2 + ID_3;
          //TAR=0;
          NextState=TX_STATE;

        }


        IFG1&=~URXIFG0;

      }
    }



    if(TACTL & TAIFG){
      // Request TX state to send last UART data via RF

      /*if (TI_CC_CTS_PxOUT & TI_CC_CTS_PIN)
              TI_CC_CTS_PxOUT &= ~ TI_CC_CTS_PIN;
      else
              TI_CC_CTS_PxOUT |=  TI_CC_CTS_PIN;
      */
      TI_CC_CTS_PxOUT |= TI_CC_CTS_PIN;
      TI_CC_TEST_PxOUT |= TI_CC_TEST_PIN;
      TACTL= MC_0 + TASSEL_2;
      TAR=0;
      NextState=TX_STATE;
      TACTL &= ~TAIFG;
    }

   }//while


}


// The ISR assumes the interrupt came from a press of one of the four buttons
// and therefore does not check the other four inputs.
#pragma vector=PORT1_VECTOR
__interrupt void port1_ISR (void)
{
  // Build packet
  TXBuffer[TXBufferIndex++]=0x01;
  NextState=TX_STATE;

  P1IFG &= ~(TI_CC_SW1+TI_CC_SW2+TI_CC_SW3+TI_CC_SW4);//Clr flag that caused int

}

⌨️ 快捷键说明

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