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

📄 lighting_demo_device.c

📁 都是主从板程序
💻 C
📖 第 1 页 / 共 2 页
字号:

/**************************************************************
*    Includes
**************************************************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "device_header.h" /* include peripheral declarations */
#include "pub_def.h"
#include "mcu_hw_config.h"
#include "MC13192_hw_config.h"
#include "simple_mac.h"
#include "bootloader user api.h"
#include "lighting_demo_device.h"
#include "freescale_radio_hardware.h"

/**************************************************************
*    Data definitions
**************************************************************/
byte gu8RTxMode; /* Current mode of the MC13192 XCVR */
byte LEDS; /* LED on/off information */
byte rxcount;


/**************************************************************
*    Main Program
**************************************************************/
void main(void)
{
  tTxPacket sTxPacket;
  tRxPacket sRxPacket;
  byte au8RxDataBuffer[26];
  byte au8TxDataBuffer[26];

  /* Initialize variables */
  UINT16 u16LEDOldTime = 0;
  UINT16 u16RTXOldTime = 0;
  UINT16 u16PBOldTime = 0;
  UINT16 u16OldTime = 0;
  UINT16 u16NewTime = 0;
  UINT16 u16LagTime1 = 0;
  UINT16 u16LagTime2 = 0;
  UINT16 u16Count = 0;
  UINT8 u8PBState = 0;
  UINT8 u8AppStatus = 0;
  UINT8 u8GetLed = 0;
  UINT8 u8Crap = 0;
  UINT8 u8XcvrMode = 0;
  UINT8 u8LEDStatus = 1;
  UINT8 u8DeviceNum = 1;
  UINT8 u8DeviceLed = u8DeviceNum;
  UINT8 u8LightLed = 0x00; /* initial */
  UINT8 u8Garbage = 0;
  UINT8 u8Channel = 1;
  UINT8 u8LastDsn = 0xFF;
  UINT8 u8Count1 = 0;
  UINT8 u8RfIcVersion=0;
  UINT8 flag=0;
  UINT8 u8flame=0x00;
  UINT8 timef1=0;
  sTxPacket.u8DataLength = 0;
  sTxPacket.pu8Data = &au8TxDataBuffer[0];
  sRxPacket.u8DataLength = 0;
  sRxPacket.pu8Data = &au8RxDataBuffer[0];
  sRxPacket.u8MaxDataLength = 30;
  sRxPacket.u8Status = 0;


  MCUInit();
  RadioInit();
  
  /* Begin init */
  rxcount = 0x00; /* Number of times device received the same request */

  LED1DIR = 1; /* LED directions */
  LED2DIR = 1;
  LED3DIR = 1;
  LED4DIR = 1;

  MLMESetMC13192ClockRate(MC13192_CLKO_FREQ); /* 8 MHz bus clock (see lighting_demo_device.h) */
  UseExternalClock();
  
  /******************************************************************
  To adjust output power call the MLME_MC13192_PA_output_adjust() with:
  
  MAX_POWER     (+3 to +5dBm)
  NOMINAL_POWER (0 dBm)
  MIN_POWER     ~(-16dBm)
  
  or somewhere custom ? (0-15, 11 (NOMINAL_POWER) being Default power)

    ******************************************************************/
    //MLMEMC13192PAOutputAdjust(MAX_POWER);    //Set MAX power setting
  //MLMEMC13192PAOutputAdjust(MIN_POWER);    //Set MIN power setting
  MLMEMC13192PAOutputAdjust(NOMINAL_POWER);    //Set Nominal power setting    
    

  
  /* Application init */
  SRTISC=SRTISC&~0x07; /* Disable wake up timer. */
  SPMSC2=SPMSC2&~0x03; /* Enable deep sleep mode stop3. */
  TPM1SC = 0x0F; /* Timer divide by 128. (16uS timebase for 8MHz bus clock). */
  PB0PU = 1; /* Pushbutton directions and pull-ups */
  PB0DIR = 0;
  PB1PU = 1;
  PB1DIR = 0;
  PB2PU = 1;
  PB2DIR = 0;
  PB3PU = 1;
  PB3DIR = 0;
  KBI1SC_KBIMOD = 0;
  KBI1SC_KBI1E =0; /* Pushbutton interrupts not used */
  PB0IE =0;
  PB1IE =0;
  PB2IE =0;
  PB3IE =0;
  
  

  /* Enable all interrupts and ready to go */
  MC13192_IRQ_IE_BIT = 1;
  EnableInterrupts;
  /* "Walk" LEDs to indicate this is a device. */
  LedDrive(0x01);
  MCUDelay (WALKON);
  LedDrive(0x02);
  MCUDelay (WALKON);
  LedDrive(0x04);
  MCUDelay (WALKON);
  LedDrive(0x08);
  MCUDelay (WALKON);
  /* LEDs off for a short */ 
  LedDrive(u8LightLed);
  MCUDelay (LONGFLASHOFF);
  /* Allow SECURITY LED to glow for a short. Indicates Group. */
  LedDrive(SECURITY);
  MCUDelay(LONGFLASHON);
  /* Now all LEDs off. */
  LedDrive(u8LightLed);
  /* Wait for user to select the u8Channel. */
  while ((PB0 == 1) && (PB1 == 1) && (PB2 == 1) && (PB3 == 1)); /* All PBs released */ 
  MCUDelay(DEBOUNCE); /* PB pushed. Wait for debounce. */
  if (PB0 == 0)
  {
    u8Channel = 1;
  }
  if (PB1 == 0)
  {
    u8Channel = 2;
  }
  if (PB2 == 0)
  {
    u8Channel = 3;
  }
  if (PB3 == 0)
  {
    u8Channel = 4;
  }
  while ((PB0 ==0) || (PB1 ==0) || (PB2 ==0) || (PB3 ==0)); /* A PB is still depressed */
  MCUDelay(DEBOUNCE); /* All PBs released. Wait for debounce. */
  LedDrive(u8Channel); /* Display selected u8Channel. */ 
  MCUDelay(LONGFLASHON);
  LedDrive(u8LightLed); /* LEDs off. */
  /* Set u8Channel */
  MLMESetChannelRequest(((u8Channel-1)<<2) + (u8Channel-1)); /* Set the selected u8Channel. */
  u8DeviceNum = 1;
  LedDrive(u8DeviceNum); /* Display selected device number. */
  u8DeviceLed = u8DeviceNum; 
  if (u8DeviceNum == 1) /* Set repeater TDMA values */
  {
      u16LagTime1 = 63; /* 1ms */
  }
  if (u8DeviceNum == 2)
  {
      u16LagTime1 = 250; /* 4ms */
  }
  if (u8DeviceNum == 3)
  {
      u16LagTime1 = 625; /* 10ms */
  }
  if (u8DeviceNum == 4)
  {
      u16LagTime1 = 1188; /* 19ms */
  }
  u16LagTime2 = 2563-u16LagTime1; /* 41ms - value */
  /* Assumes total time for Rx extraction and Tx is 1-3mS */
  /* Maximum lag is 4+3+10+3+19+3 or 42mS */
  /* Minimum lag time is 1+1 or 2mS */
  MCUDelay(LONGFLASHON);
  LedDrive(u8LightLed); /* LEDs off. Initialization complete. */
  /* Init timer vars and global LEDS */
  u16LEDOldTime = MCUReadTmr1 ();
  u16RTXOldTime = u16LEDOldTime;
  LEDS = LEDPORT; /* Initialize LEDS to the actual port setting */
  MLMERXEnableRequest(&sRxPacket, 0x); /* Activate the receiver */

  /**************************************************************
  *    Main While Loop - Runs continously
  **************************************************************/
  for (;;)
  {  if(flag==1) 
    { for(timef1=0;timef1<6;timef1++) 
     {
     LedDrive(0x06);
     MCUDelay (WALKON);
     LedDrive(0x09);
     MCUDelay (WALKON);
     LedDrive(0x00);
     MCUDelay(WALKON);
     }
    flag=0;
    
   }
    if(flag==5) 
    {
    for(u8flame=0;u8flame<0x10;u8flame++)
     {
      LedDrive(u8flame); 
      MCUDelay(TIME500);
     }
    flag=0;
    }
  /* u8PBState=0 is waiting for a PB to be pushed. */
  /* u8PBState=1 is waiting for a PB high-to-low debounce. */
  /* u8PBState=2 is waiting for PBs to ALL be released. */
  /* u8PBState=3 is waiting for a PB low-to-high debounce. */
    u16NewTime = MCUReadTmr1 ();
    if ((u8PBState == 0) && ((PB0 ==0) || (PB1 ==0) || (PB2 ==0) || (PB3 ==0))) /* A PB is depressed */
    {
        u8PBState = 1;
        u16PBOldTime = u16NewTime;
    }
    if (u8PBState == 1)
    {
        if ((u16NewTime-u16PBOldTime) > DEBOUNCE) /* H-L debounce complete */
        {
            u8PBState = 2; /* Update LEDS. A bit funky. */
            if (PB0 == 0)
            {
               for(timef1=0;timef1<4;timef1++) 
               {
               LedDrive(0x06);
               MCUDelay (WALKON);
               LedDrive(0x09);
               MCUDelay (WALKON);
               LedDrive(0x00);
               MCUDelay(WALKON);
               }
            }
            if (PB1 == 0)
            {
                LedDrive(u8Channel);
            }
            if (PB2 == 0)
            {
                LedDrive(u8DeviceNum);
            }
            if (PB3 == 0)
            {
                LED4 ^=1;
            }
            u16PBOldTime = u16NewTime;
        }
    }
    if (u8PBState == 2)
    {
        if ((PB0 == 1) && (PB1 == 1) && (PB2 == 1) && (PB3 == 1)) /* All PBs released */ 
        {
            u16PBOldTime = u16NewTime;
            u8PBState = 3;
        }
    }
    if (u8PBState == 3)
    {
        if ((u16NewTime-u16PBOldTime) > DEBOUNCE) /* L-H debounce complete */
        {
            u8PBState = 0;
        }
    }
    if (gu8RTxMode == IDLE_MODE) /* RX is now done */
    {
        u16RTXOldTime = u16NewTime;
        u8XcvrMode = 0; /* Assume packet not intended for us */
        /* Check for code bytes match and successful packet reception */
        if ((sRxPacket.u8Status == SUCCESS) && (au8RxDataBuffer[0] == 0xE1) && (au8RxDataBuffer[1] == 0xCC) && (au8RxDataBuffer[2] == SECURITY))
        {
            if ((au8RxDataBuffer[3] == u8DeviceLed)||(au8RxDataBuffer[3] ==4)) /* Packet is for me. */
            {
                if ((au8RxDataBuffer[5] == u8LastDsn)) /* Re-received? */
                {
                    rxcount++; 
                }
                else
                {
                    rxcount = 1;
                    u8LastDsn = au8RxDataBuffer[5]; /* Update last dsn */
                }
                if ((au8RxDataBuffer[7] == TOGGLECMD)&& (rxcount == 1))  /* TOGGLE and not repeated */
                /* Toggle selected LED */
                { //if( au8RxDataBuffer[3] == u8DeviceLed) 
                  
                   // flag=1;
                   LedDrive(au8RxDataBuffer[4]);
                   
                 // if(au8RxDataBuffer[3] == 4)
                    //flag=5;
                
                u8XcvrMode = 2; 
                }/* ACKBACK required */
                if (au8RxDataBuffer[7] == QUERYCMD) /* Just a query */
                {
                    u8XcvrMode = 3; /* QUERYBACK required */
                }
            }
            else /* repeat */
            {
                u8LastDsn = au8RxDataBuffer[5]; /* Update last dsn */
                for (u8Count1=0; u8Count1<sRxPacket.u8DataLength+1; u8Count1++)
                {
                    au8TxDataBuffer[u8Count1] = au8RxDataBuffer[u8Count1]; 
                }
                au8TxDataBuffer[sRxPacket.u8DataLength] = u8DeviceLed; 
                sTxPacket.u8DataLength = sRxPacket.u8DataLength+1;
                u16NewTime = MCUReadTmr1 ();

⌨️ 快捷键说明

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