📄 remote_controller.c
字号:
/**************************************************************
* Includes
**************************************************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "device_header.h" /* include peripheral declarations */
#include "pub_def.h"
#include "MC13192_hw_config.h"
#include "mcu_hw_config.h"
#include "simple_mac.h"
#include "remote_controller.h"
#include "bootloader user api.h"
#include "SCI.h"
#include "LCD.h"
#include "ascii_utilities.h"
#include "freescale_radio_hardware.h"
/**************************************************************
* Data definitions
**************************************************************/
UINT8 gu8RTxMode; /* Current mode of the MC13192 XCVR */
tTxPacket gsTxPacket;
tRxPacket gsRxPacket;
UINT8 gauTxDataBuffer[26];
UINT8 gauRxDataBuffer[26];
UINT8 gau8String[20];
/**************************************************************
* Main Program
**************************************************************/
void main(void)
{
/* Initialize variables. */
UINT8 u8Channel = 0;
UINT8 u8DeviceLed = 0x00;
UINT8 u8Count1 = 0;
UINT8 u8Garbage = 0;
UINT32 u32ChannelPower = 0x00000000;
UINT8 u8PeakChannelPower = 0x00;
UINT16 u16Count2 = 0;
UINT8 u8TempChannelPower = 0x00000000;
UINT8 u8pChannel = 0;
UINT8 u8aChannel = 0;
UINT8 u8MaxpChannelPower = 0x00;
UINT32 u32MaxaChannelPower = 0x00000000;
UINT16 u16OldTime = 0;
UINT16 u16RTXOldTime = 0;
UINT16 u16NewTime = 0;
UINT16 u16Count = 0;
UINT8 u8Toggle = 0;
UINT8 u8ToCount = 0;
UINT8 u8AppStatus = 0;
UINT8 u8LightLed = 0x00;
UINT8 u8LinkQuality = 0;
UINT8 u8RetryCount = 0;
UINT8 u8Dsn = 0xFF;
UINT8 number=0x00;
UINT8 u8Flag=0;
UINT8 i=0x00;
UINT8 my[16]={0x07,0x01,0x08,0x02,0x04,0x0a,0x06,0x07,0x0d,0x09,0x0f,0x00,0x0c,0x02,0x0a,0x0f};
gsTxPacket.u8DataLength = 0;
gsTxPacket.pu8Data = &gauTxDataBuffer[0];
gsRxPacket.u8DataLength = 0;
gsRxPacket.pu8Data = &gauRxDataBuffer[0];
gsRxPacket.u8MaxDataLength = 30;
gsRxPacket.u8Status = 0;
/* Begin init */
MCUInit();
RadioInit();
LED1DIR = 1; /* LED directions */
LED2DIR = 1;
LED3DIR = 1;
LED4DIR = 1;
MLMESetMC13192ClockRate(1); /* 8MHz CLKo, 4MHz bus clock */
/******************************************************************
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
UseExternalClock();
/* Application init code. */
SRTISC=SRTISC&~0x07; /* Disable wake up timer. */
SPMSC2=SPMSC2&~0x03; /* Enable deep sleep mode stop3. */
TPM1SC = 0x0E; /* Timer divide by 64. (16uS timebase for 4MHz bus clock). */
PB0PU = 1; /* Pushbutton directions and pull-ups */
PB0DIR = 0;
PB1PU = 1;
PB1DIR = 0;
PB2PU = 1;
PB2DIR = 0;
PB3PU = 1;
PB3DIR = 0;
/* Enable all IRQ sources. Ready to go. */
MC13192_IRQ_IE_BIT = 1;
EnableInterrupts;
/***************************************************************/
/* Begin App */
/***************************************************************/
/* First time application start-up code. */
/* All LEDs on for start-up. Indicates controller */
LED1 = 0;
LED2 = 0;
LED3 = 0;
LED4 = 0;
/* Allow LEDs to glow for a short. Indicates Controller. */
MCUDelay(LONGFLASHON);
/* Test to see if manual u8Channel selection is desired. */
if ((PB0 ==0) || (PB1 ==0) || (PB2 ==0) || (PB3 ==0))
/* Set the manual u8Channel. */
{
if (PB0 == 0)
{
u8Channel = 1;
}
if (PB1 == 0)
{
u8Channel = 2;
}
if (PB2 == 0)
{
u8Channel = 3;
}
if (PB3 == 0)
{
u8Channel = 4;
}
}
/* Now all LEDs off. */
LedDrive(u8DeviceLed);
MCUDelay (LONGFLASHOFF);
/* Allow SECURITY LED to glow for a short. Indicates code. */
LedDrive(SECURITY);
MCUDelay(LONGFLASHON);
/* Now all LEDs off. */
LedDrive(u8DeviceLed);
MCUDelay (LONGFLASHOFF);
if (u8Channel == 0) /* Automatic u8Channel selection. */
{
/* All LEDs on for duration of u8Channel scan. */
LED1 = 0;
LED2 = 0;
LED3 = 0;
LED4 = 0;
/* u8Channel scan. Choose the cleanest u8Channel. */
/* u8Channel is mapped as 1=0, 2=5, 3=10, 4=15. */
for (u8Count1=1; u8Count1<(5); u8Count1++) /* Scan through "u8Channels" 1-4. */
{
u8Garbage = (u8Count1-1);
u8Garbage = ((u8Garbage << 2) + u8Garbage);
MLMESetChannelRequest(u8Garbage); /* Set actual u8Channel. */
u32ChannelPower = 0x00000000;
u8PeakChannelPower = 0xFE;
for (u16Count2=1; u16Count2<(2049); u16Count2++) /* Average/peak detect the readings over 2048 measurements. */
{
u8TempChannelPower = MLMEEnergyDetect();
if (u8TempChannelPower < u8PeakChannelPower)
{
u8PeakChannelPower = u8TempChannelPower; /* Find peak power for the u8Channel */
}
u32ChannelPower = u8TempChannelPower + u32ChannelPower; /* Sum the readings. */
}
u32ChannelPower = (u32ChannelPower>>11); /* Divide to get the average. */
if (u8PeakChannelPower > u8MaxpChannelPower)
{
u8MaxpChannelPower = u8PeakChannelPower;
u8pChannel = u8Count1; /* Weaker peak power on this u8Channel */
}
if (u32ChannelPower > u32MaxaChannelPower) /* Pick the u8Channel with the largest (i.e. weakest) reading. */
{
u32MaxaChannelPower = u32ChannelPower;
u8aChannel = u8Count1; /* Weaker average power on this u8Channel */
}
}
u8Channel = u8pChannel; /* Final u8Channel can be based on peak or average */
}
else /* Manual u8Channel selection. Wait for pushbutton release, then debounce. */
{
while ((PB0 ==0) || (PB1 ==0) || (PB2 ==0) || (PB3 ==0)); /* A PB is still depressed */
MCUDelay(DEBOUNCE); /* All PBs released. Wait for debounce. */
}
/* Now all LEDs off. */
LedDrive(u8DeviceLed);
MCUDelay(LONGFLASHOFF);
/* Allow selected u8Channel LED to glow for a short. */
LedDrive(u8Channel);
MCUDelay(LONGFLASHON);
/* Now all LEDs off. Initialization complete. */
LedDrive(u8DeviceLed);
/**************************************************************
* Main Loop, repeated now forever
**************************************************************/
for(;;)
{
u8DeviceLed = 0x00; /* LEDs all off */
u8LightLed = 0x00;
LedDrive(u8DeviceLed);
u8AppStatus = 0;
/* Enable keyboard irq. Depressing PB0 will cause a wake up */
KBI1SC_KBIMOD = 0;
PB0IE =1;
PB1IE =1;
PB2IE =1;
KBI1SC_KBI1E = 1;
/* MC13192 Reset mode and MCU STOP3. */
/* This is the stand-by low power mode. */
UseMcuClock(); /* MCU internal clock selected since we'll lose our external clock */
MC13192ContReset(); /* Place the MC13192 into Reset mode */
KBDWAIT(); /* Wait for PB1 to be depressed. Note: requires Rev C GB60 board */
/* PB0 depressed */
u8Dsn++; /* increment the data sequence number */
if (u8Dsn == 0xFF) /* 0xFF is reserved and used at startup only for devices */
{
u8Dsn = 0x00;
}
/* Restart to external clock appx. 10.8mS */
MC13192Restart(); /* Bring the MC13192 into the desired Idle condition. */
RadioInit();
MLMESetChannelRequest(((u8Channel-1)<<2) + (u8Channel-1)); /* Set the selected u8Channel. */
MLMESetMC13192ClockRate(1); /* Back to 8MHz CLKo */
UseExternalClock(); /* Use external accurate clock */
u16OldTime = MCUReadTmr1();
u16NewTime = u16OldTime;
u8ToCount = 0;
/**************************************************************
* Main While Loop - activated by pushbutton
**************************************************************/
while (u8ToCount < TONUM) /* Auto shutdown if inactive */
{
if ((u16NewTime-u16OldTime) > TO)
{
u8ToCount++;
u16OldTime = MCUReadTmr1 ();
u16NewTime = u16OldTime;
}
if (u8AppStatus == 0)
{
if ((PB1 ==0) ||(PB0 ==0) || (PB2 ==0) || (PB3 ==0))
{
if (PB1 == 0)
{ u8DeviceLed=0x02;
}
if (PB0 == 0)
{
u8DeviceLed = 0x01;
}
if (PB2 == 0)
{
u8DeviceLed= 0x03;
}
if (PB3 == 0)
{
u8DeviceLed= 0x04;
}
LedDrive(u8DeviceLed);
MCUDelay(DEBOUNCE); /* Wait H-L debounce. */
while ((PB1 ==0) ||(PB0 ==0) || (PB2 ==0) || (PB3 ==0)); /* Wait for L-H and debounce. */
MCUDelay(DEBOUNCE);
u8AppStatus=2;
u8RetryCount=0;
break;
}
}
u16NewTime = MCUReadTmr1 ();
}
while(u8AppStatus==2) /* Transaction selected. Transceive in process */
{u8Dsn++; /* increment the data sequence number */
if (u8Dsn == 0xFF) /* 0xFF is reserved and used at startup only for devices */
{
u8Dsn = 0x00;
}
i++;
if(i==0x10)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -