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

📄 完整版.c

📁 CC2430无线语音测试代码。两结点间通讯。IAR开发环境的工程文件。
💻 C
字号:
#include "hal.h"
#include "app_ex.h"
#include "cul.h"

//////////////////////////////////////////////////////////////////

#define AUDIO_SEND     0
#define AUDIO_RECEIVE  1
#define ADDRESS_0 0x01
#define ADDRESS_1 0x02
#define RECEIVE_TIMEOUT               4
#define SENDLENGTH                    85
#define BUTTON_PUSH_IF_MASK          0x02

UINT8 audio_mode;
UINT8 myAddr;
UINT8 remoteAddr;

BOOL play;
BOOL modechange = FALSE;
BOOL remoteAddressSet = FALSE;
BOOL myAddressSet = FALSE;

BYTE sendbuffer[SENDLENGTH];
BYTE* receiveBuffer;

UINT8 adc_value;
UINT8 adc_num;
UINT8 pulse_length,a;
UINT16 pulse_length_num;
int wait;

void initAudio(void);
void sendAudio(void);
void receiveAudio(void);

UINT32 getFrequency(void);
UINT8 getAddress(UINT8 a);

////////////////////////////////////////////////////////////////
void initAudio(void)
{
   char *menuText[] = {(char *)" Client?", (char *)" Host?", (char *)" Set Freq?", (char *)" Set MyAddr?", (char *)" SetRemoteAddr"};
   BYTE result;
   UINT32 frequency = 2405000;

  initLcd();
  lcdUpdate((char *)"AUDIO MODE",(char *)"");
  waitVisible(CLEARLY_VISIBLE);

   INIT_JOYSTICK();
   INIT_BUTTON();

   SET_MAIN_CLOCK_SOURCE(CRYSTAL);

   INIT_GLED();
   INIT_YLED();

   remoteAddressSet = FALSE;
   myAddressSet = FALSE;

   TXCTRLL=0xff;//cywu set txpwr to max

   while((result = lcdMenu(menuText, 5)) > 1)
   {
      if(result == 2)
      {
         frequency = getFrequency();
      }
      else if(result == 3)
      {
         lcdUpdate((char *)"Set myAddress",(char *)"");
         myAddr = getAddress(myAddr);
         myAddressSet = TRUE;
      }
      else if(result == 4)
      {
         lcdUpdate((char *)"Set remoteAddr",(char *)"");
         remoteAddr = getAddress(remoteAddr);
         remoteAddressSet = TRUE;
      }
      else{ // ABORT_MENU
         return;
      }
   }

   if(result == 1) // Host
   {
      audio_mode = AUDIO_RECEIVE;
      if(myAddressSet == FALSE)
      {
         myAddr = ADDRESS_1;
      }
      if(remoteAddressSet == FALSE)
      {
         remoteAddr = ADDRESS_0;
      }
      GLED = LED_ON;
      lcdUpdate((char *)"Host",(char *)"");
   }
   else // Client
   {
      audio_mode = AUDIO_SEND;
      if(myAddressSet == FALSE)
      {
         myAddr = ADDRESS_0;
      }
      if(remoteAddressSet == FALSE)
      {
         remoteAddr = ADDRESS_1;
      }
      YLED = LED_ON;
      lcdUpdate((char *)"Please Speak",(char *)"");
      waitVisible(CLEARLY_VISIBLE);
      lcdUpdate((char *)"If over",(char *)"  Press Cancel");
      waitVisible(CLEARLY_VISIBLE);
      lcdUpdate((char *)"Client",(char *)"");
   }
   waitVisible(CLEARLY_VISIBLE);
   radioInit(frequency, myAddr);

}

////////////////////////////////////////////////////////////////
void main()
{
  adc_num = 0;
  pulse_length_num = 0;
  play=0;
  wait=0;
  for(int i=0;i<SENDLENGTH;i++)
  sendbuffer[i] = 0;

  while(getJoystickDirection() != CENTRED);
  initAudio();

   P0IFG = 0x00;
   PICTL |= 0x08;                  // Enable interrupt from P0_1 low
   PICTL |= 0x01;                  // Falling edge gives interrupt
   INT_ENABLE(INUM_P0INT, INT_ON);

  P0SEL=0x00;
  P1SEL=0x00;
  IO_DIR_PORT_PIN(0, 0, IO_IN);
  IO_ADC_PORT0_PIN(0, IO_ADC_EN);
  IO_DIR_PORT_PIN(1, 1, IO_OUT);


  TIMER1_INIT();
  halSetTimer1Period(128);

  TIMER34_INIT(4);
  halSetTimer34Period(4,128);
  TIMER34_PWM_CONFIG(4);

  ADC_ENABLE_CHANNEL(0);

  INT_GLOBAL_ENABLE(INT_ON);
  INT_ENABLE(INUM_T1, INT_ON);
  TIMER1_ENABLE_OVERFLOW_INT(TRUE);

   // Starting timer 1
  TIMER1_RUN(TRUE);
  TIMER4_RUN(TRUE);
  while(1)
  {
    if(play)
      TIMER4_RUN(TRUE);
    else
      TIMER4_RUN(FALSE);
      if(audio_mode == AUDIO_RECEIVE)
    {
             receiveAudio();
    }
    else
    {
         if(modechange)
         {
          BYTE lastsendbuffer[]="Please response";
          radioSend(lastsendbuffer, sizeof(lastsendbuffer), remoteAddr, DO_NOT_ACK);
          modechange=FALSE;
          audio_mode=!audio_mode;
          YLED = LED_OFF;
          GLED = LED_ON;
          wait=0;
         }
         else
          sendAudio();
    }
  }
}

///////////////////////////////////////////
   #pragma vector=T1_VECTOR
   __interrupt void T1_IRQ(void)
{
  if(T1CTL & 0x10)
  {
    ADC_ENABLE_CHANNEL(0);
    ADC_SINGLE_CONVERSION(ADC_REF_AVDD | ADC_8_BIT | ADC_AIN0);
    while(!ADC_SAMPLE_READY());
    ADC_DISABLE_CHANNEL(0);

    adc_value = ADCH;
    sendbuffer[adc_num] = adc_value*2;
    if(adc_num<84)
      adc_num++;
    else
    {
      adc_num=0;
    }
    if(pulse_length_num<84)
    {
      pulse_length = receiveBuffer[pulse_length_num];
      pulse_length_num++;
    }
    else
    {
      pulse_length_num=0;
      pulse_length = 0xff;
    }
    TIMER34_SET_PWM_PULSE_LENGTH(4,pulse_length);
  }
  T1CTL &= ~0x10;
}

//////////////////////////////////////////////
void sendAudio(void)
{
 radioSend(sendbuffer, sizeof(sendbuffer), remoteAddr, DO_NOT_ACK);
}
//////////////////////////////////////////////////
void receiveAudio(void)
{
   BYTE length;
   BYTE res;
   BYTE sender;

      res = radioReceive(&receiveBuffer, &length, RECEIVE_TIMEOUT,&sender);
      if(res == TRUE)
      {
        wait=0;
        if(receiveBuffer[0]==0x50&&receiveBuffer[1]==0x6c&&receiveBuffer[2]==0x65)
        {
          audio_mode=!audio_mode;
          play=0;
          GLED = LED_OFF;
          YLED = LED_ON;
        }
        else
        {
        play=1;
        }
      }
     else
      {
         wait++;
         if(wait==3)
         {
           play=0;
         }
      }
}
/////////////////////////////////////////////////////////////////////////////////////
UINT8 getAddress(UINT8 a)
{
   UINT8 address = a;
   char line[16];
   JOYSTICK_DIRECTION jsv;

   while(getJoystickDirection() != CENTRED);

   while((jsv = getJoystickDirection()) != RIGHT)
   {
      while(getJoystickDirection() != CENTRED);

      if(jsv == UP)
      {
         address++;
      }
      else if (jsv == DOWN)
      {
         address--;
      }

      sprintf(line,(char *)"Address: %d     ",address);
      lcdUpdateLine(LINE2, line);
   }
   return address;
}
////////////////////////////////////////////////////////////////////////////////////////
UINT32 getFrequency(void)
{
   UINT8 selection;
   char *menuText[] = {(char*)" 2405", (char*)" 2410", (char*)" 2415", (char*)" 2420", (char*)" 2425", (char*)" 2430", (char*)" 2435", (char*)" 2440", (char*)" 2445", (char*)" 2450", (char*)" 2455", (char*)" 2460", (char*)" 2465", (char*)" 2470", (char*)" 2475", (char*)" 2480", (char*)" 2485"};
   selection = lcdMenu(menuText, 17);
   if(selection != ABORT_MENU)
   {
      return (2405000 + 5000*selection);
   }
   else
   {
      return (2405000);
   }

}

////////////////////////////////////////////////////////////
#pragma vector=P0INT_VECTOR
__interrupt void P0_IRQ(void){
   // check if PUSH_BUTTON is pressed
   if(P0IFG & BUTTON_PUSH_IF_MASK){
    if(audio_mode == AUDIO_SEND)
     {
      modechange=TRUE;
     }
      P0IFG &= ~BUTTON_PUSH_IF_MASK;
   }
   P0IF = FALSE;
}
/////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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