/*********************************************************************************** Filename: light_switch.c Description: This application function either as a light or a switch toggling the ligh. The role of the application is chosen in the menu with the joystick at initialisation. Push S1 to enter the menu. Choose either switch or light and confirm choice with S1. Joystick Up: Sends data from switch to light ***********************************************************************************/ /*********************************************************************************** * INCLUDES */ #include <hal_lcd.h> #include <hal_led.h> #include <hal_joystick.h> #include <hal_assert.h> #include <hal_board.h> #include <hal_int.h> #include "hal_mcu.h" #include "hal_button.h" #include "hal_rf.h" #include "util_lcd.h" #include "basic_rf.h" /*********************************************************************************** * CONSTANTS */ // Application parameters #define RF_CHANNEL 25 // 2.4 GHz RF channel // BasicRF address definitions #define PAN_ID 0x2007 #define SWITCH_ADDR 0x2520 #define LIGHT_ADDR 0xBEEF #define APP_PAYLOAD_LENGTH 1 #define LIGHT_TOGGLE_CMD 0 // Application states #define IDLE 0 #define SEND_CMD 1 // Application role #define NONE 0 #define SWITCH 1 #define LIGHT 2 #define APP_MODES 2 /*********************************************************************************** * LOCAL VARIABLES */ static uint8 pTxData[APP_PAYLOAD_LENGTH]; static uint8 pRxData[APP_PAYLOAD_LENGTH]; static basicRfCfg_t basicRfConfig; // Mode menu static menuItem_t pMenuItems[] = { #ifdef ASSY_EXP4618_CC2420 // Using Softbaugh 7-seg display " L S ", SWITCH, " LIGHT ", LIGHT #else // SRF04EB and SRF05EB "Switch", SWITCH, "Light", LIGHT #endif }; static menu_t pMenu = { pMenuItems, N_ITEMS(pMenuItems) }; #ifdef SECURITY_CCM // Security key static uint8 key[]= { 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, }; #endif /*********************************************************************************** * LOCAL FUNCTIONS */ static void appLight(); static void appSwitch(); static uint8 appSelectMode(void); /*********************************************************************************** * @fn appLight * * @brief Application code for light application. Puts MCU in endless * loop waiting for user input from joystick. * * @param basicRfConfig - file scope variable. Basic RF configuration data * pRxData - file scope variable. Pointer to buffer for RX data * * @return none */ static void appLight() { // halLcdWriteLine(HAL_LCD_LINE_1, "Light"); // halLcdWriteLine(HAL_LCD_LINE_2, "Ready"); #ifdef ASSY_EXP4618_CC2420 halLcdClearLine(1); halLcdWriteSymbol(HAL_LCD_SYMBOL_RX, 1); #endif // Initialize BasicRF basicRfConfig.myAddr = LIGHT_ADDR; if(basicRfInit(&basicRfConfig)==FAILED) { HAL_ASSERT(FALSE); } basicRfReceiveOn(); // Main loop while (TRUE) { while(!basicRfPacketIsReady()); if(basicRfReceive(pRxData, APP_PAYLOAD_LENGTH, NULL)>0) { if(pRxData[0] == LIGHT_TOGGLE_CMD) { halLedToggle(1); } } } } /*********************************************************************************** * @fn appSwitch * * @brief Application code for switch application. Puts MCU in * endless loop to wait for commands from from switch * * @param basicRfConfig - file scope variable. Basic RF configuration data * pTxData - file scope variable. Pointer to buffer for TX * payload * appState - file scope variable. Holds application state * * @return none */ static void appSwitch() { // halLcdWriteLine(HAL_LCD_LINE_1, "Switch"); // halLcdWriteLine(HAL_LCD_LINE_2, "Joystick Push"); // halLcdWriteLine(HAL_LCD_LINE_3, "Send Command"); #ifdef ASSY_EXP4618_CC2420 halLcdClearLine(1); halLcdWriteSymbol(HAL_LCD_SYMBOL_TX, 1); #endif pTxData[0] = LIGHT_TOGGLE_CMD; // Initialize BasicRF basicRfConfig.myAddr = SWITCH_ADDR; if(basicRfInit(&basicRfConfig)==FAILED) { HAL_ASSERT(FALSE); } // Keep Receiver off when not needed to save power basicRfReceiveOff(); // Main loop while (TRUE) { // if( halJoystickPushed() ) { //bu qiujie tech if(halButtonPushed()==HAL_BUTTON_1){ basicRfSendPacket(LIGHT_ADDR, pTxData, APP_PAYLOAD_LENGTH); // Put MCU to sleep. It will wake up on joystick interrupt halIntOff(); halMcuSetLowPowerMode(HAL_MCU_LPM_3); // Will turn on global // interrupt enable halIntOn(); } } } /*********************************************************************************** * @fn main * * @brief This is the main entry of the "Light Switch" application. * After the application modes are chosen the switch can * send toggle commands to a light device. * * @param basicRfConfig - file scope variable. Basic RF configuration * data * appState - file scope variable. Holds application state * * @return none */ void main(void) { uint8 appMode = NONE; // Config basicRF basicRfConfig.panId = PAN_ID; basicRfConfig.channel = RF_CHANNEL; basicRfConfig.ackRequest = TRUE; #ifdef SECURITY_CCM basicRfConfig.securityKey = key; #endif // Initalise board peripherals halBoardInit(); // halJoystickInit();//BY QIUJIE // Initalise hal_rf if(halRfInit()==FAILED) { HAL_ASSERT(FALSE); } // Indicate that device is powered halLedSet(1); // Print Logo and splash screen on LCD // utilPrintLogo("Light Switch"); // Wait for user to press S1 to enter menu // while (halButtonPushed()!=HAL_BUTTON_1); // halMcuWaitMs(350); // halLcdClear(); // Set application role // appMode = appSelectMode(); // halLcdClear(); // appMode = SWITCH; // Transmitter application // if(appMode == SWITCH) { // No return from here //注:函数appSwitch()和appLight()只能打开一个 //作为开关板打开此函数(appSwitch) //appSwitch(); //被点灯的板打开此函数(appLight) appLight(); // } // Receiver application // else if(appMode == LIGHT) { // No return from here // } // Role is undefined. This code should not be reached // HAL_ASSERT(FALSE); } /**************************************************************************************** * @fn appSelectMode * * @brief Select application mode * * @param none * * @return uint8 - Application mode chosen */ static uint8 appSelectMode(void) { halLcdWriteLine(1, "Device Mode: "); return utilMenuSelect(&pMenu); } /**************************************************************************************** Copyright 2007 Texas Instruments Incorporated. All rights reserved. IMPORTANT: Your use of this Software is limited to those specific rights granted under the terms of a software license agreement between the user who downloaded the software, his/her employer (which must be your employer) and Texas Instruments Incorporated (the "License"). You may not use this Software unless you agree to abide by the terms of the License. The License limits your use, and you acknowledge, that the Software may not be modified, copied or distributed unless embedded on a Texas Instruments microcontroller or used solely and exclusively in conjunction with a Texas Instruments radio frequency transceiver, which is integrated into your product. Other than for the foregoing purpose, you may not use, reproduce, copy, prepare derivative works of, modify, distribute, perform, display or sell this Software and/or its documentation for any purpose. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. Should you have any questions regarding your right to use this Software, contact Texas Instruments Incorporated at www.TI.com. ***********************************************************************************/ 这是SW1控制LED1的代码,我想改为SW2控制LED2的代码
时间: 2025-06-01 14:06:14 浏览: 22
### 修改代码以实现SW2控制LED2
要将代码从SW1控制LED1修改为SW2控制LED2,需要对以下部分进行调整:
#### 1. 引脚配置
在原代码中,`P1_2`引脚用于检测按键SW1的状态,而`P1_0`引脚用于控制LED1的亮灭。为了实现SW2控制LED2的功能,需要重新配置引脚。
- 将`P1_3`(假设SW2连接到`P1_3`)设置为输入口。
- 将`P1_1`(假设LED2连接到`P1_1`)设置为输出口。
```c
P1SEL &= ~0x0A; // 设置P1_1和P1_3为通用I/O口
P1DIR |= 0x02; // 设置P1_1为输出口,LED2连接着P1_1
P1DIR &= ~0x08; // 设置P1_3为输入口
```
#### 2. 主循环逻辑
在主循环中,需要将判断条件从`P1_2`改为`P1_3`,并将LED状态切换从`P1_0`改为`P1_1`。
```c
while(1) {
if(P1_3 == 0) { // 如果按键SW2被按下
P1_1 = ~P1_1; // 反转LED2的亮灭状态
while(!P1_3); // 等待按键松开
}
}
```
#### 3. 宏定义与函数声明
如果使用了外部头文件(如`led.h`[^3]),需要确保宏定义和函数声明支持新的LED编号。例如,可以添加以下内容:
```c
#define LED2_PIN 0x02 // 定义LED2对应的引脚
void LEDToggle(unsigned char n); // 声明函数用于切换LED状态
```
在实现中,`LEDToggle`函数可以根据参数`n`来决定操作哪个LED:
```c
void LEDToggle(unsigned char n) {
if (n == 1) {
P1_0 = ~P1_0; // 切换LED1
} else if (n == 2) {
P1_1 = ~P1_1; // 切换LED2
}
}
```
#### 4. 模式切换(可选)
如果涉及模式切换(如引用[4]所述[^4]),需要确保CPSR寄存器的值正确设置以支持新的硬件配置。例如,可以通过以下步骤切换到SVC模式:
```c
__asm("MRS R0, CPSR"); // 将CPSR值保存到R0
__asm("BIC R0, R0, #0x1F"); // 清零低五位
__asm("ORR R0, R0, #0x13"); // 设置为SVC模式
__asm("MSR CPSR_c, R0"); // 写回CPSR
```
#### 5. 示例完整代码
以下是修改后的完整代码示例:
```c
#include "ioCC2530.h" // 引用CC2530头文件
void main(void) {
P1SEL &= ~0x0A; // 设置P1_1和P1_3为通用I/O口
P1DIR |= 0x02; // 设置P1_1为输出口,LED2连接着P1_1
P1DIR &= ~0x08; // 设置P1_3为输入口
P1_1 = 0; // 熄灭LED2
while(1) {
if(P1_3 == 0) { // 如果按键SW2被按下
P1_1 = ~P1_1; // 反转LED2的亮灭状态
while(!P1_3); // 等待按键松开
}
}
}
```
###
阅读全文