2022中职国赛灯光控制系统

2022中职国赛灯光控制系统

image-1
image-2

#include "hal_defs.h"
#include "hal_cc8051.h"
#include "hal_int.h"
#include "hal_mcu.h"
#include "hal_board.h"
#include "hal_led.h"
#include "hal_rf.h"
#include "basic_rf.h"
#include "hal_uart.h" 
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

// 定义端口
#define LED1 P1_3
#define LED2 P1_4
#define LED3 P1_0
#define LED4 P1_1
#define SW1  P1_2
#define SW2  P0_1
#define lamp P1_7
#define fun  P2_0

void touch_SW();//声明函数


/*****点对点通讯地址设置******/


#define RF_CHANNEL                1         // 频道 11~26
#define PAN_ID                    0x1     //网络id 
#define MY_ADDR                   0x1     //本机模块地址
#define SEND_ADDR                 0x2     //发送地址

/**************************************************/
static basicRfCfg_t basicRfConfig;
// 无线RF初始化
void ConfigRf_Init(void)
{
  
    basicRfConfig.panId       =   PAN_ID;
    basicRfConfig.channel     =   RF_CHANNEL;
    basicRfConfig.myAddr      =   MY_ADDR;
    basicRfConfig.ackRequest  =   TRUE;
    while(basicRfInit(&basicRfConfig) == FAILED)
    basicRfReceiveOn();
 
}

unsigned char SW1_T = 0;
unsigned char SW1_flag = 0;

unsigned char SW2_T = 0;
unsigned char SW2_flag = 0;

void init(){

  P1SEL &= ~0x9F;
  P1DIR |= 0x9B;
  
  P0SEL &= ~0x02;
  P0DIR &= ~0x02;
  
  P2SEL &= ~0x01;
  P2DIR |= ~0x01;
  
  P1 = P2 = P0 = 0;
}

void Delay(unsigned int time){

  unsigned int i,j;
  for(i = 0; i < time;i ++){
    
    for(j = 0;j < 240;j++){
    
      touch_SW();
      
    }
    
  }
  

}

unsigned char LED_R[] = {0xA5,0x06,0x00,0xA0,0xFF,0x00,0x00,0xEE,0x5A};
unsigned char LED_G[] = {0xA5,0x06,0x00,0xA0,0x00,0xFF,0x00,0xEE,0x5A};
unsigned char LED_B[] = {0xA5,0x06,0x00,0xA0,0x00,0x00,0xFF,0xEE,0x5A};
unsigned char LED_C[] = {0xA5,0x06,0x00,0xA0,0x00,0x00,0x00,0xEE,0x5A};







void touch_SW(){//按键检测

  // SW1

  

        if(SW1 == 0){
        SW2_flag = 0;
          while(SW1 == 0);
        
          halMcuWaitMs(150);
          if(SW1 == 0){//是否是双击
          while(SW1 == 0);
            SW1_flag = 22;
          } else SW1_flag ++;   
        }
        if(SW1_flag == 23){//反复执行
          SW1_flag = 1;
        }
        
  //SW2
        if(SW2 == 0){
        SW1_flag = 0;
        while(SW2 == 0);
        
          halMcuWaitMs(150);
          if(SW2 == 0){//是否是双击
          while(SW2 == 0);
          SW2_flag = 22;
          }else SW2_flag ++;
        }
        
        if(SW2_flag == 23){//反复执行
          SW2_flag = 1;
        }
  
  

}



void LSD1(){

  LED1 = LED2 = LED3 = LED4 = 0;
  LED4 = 1;
  Delay(800);
  LED3 = 1;
  LED4 = 0;halUartWrite(LED_R,sizeof(LED_R));
  Delay(800);
  LED3 = 0;
  LED2 = 1;halUartWrite(LED_G,sizeof(LED_G));
  Delay(800);
  LED2 = 0;
  LED1= 1;halUartWrite(LED_B,sizeof(LED_B));
  Delay(800);
  LED1 = 0;
  
  
}

void LSD2(){

  LED1 = LED2 = 0;
  LED3 = LED4 = 1;
 Delay(5000);
  LED3 = LED4 = 0;  
  LED1 = LED2 = 1;
 Delay(5000);
 
  

}







/********************MAIN************************/
void main(void)
{
    halBoardInit();//选手不得在此函数内添加代码
    //ConfigRf_Init();//选手不得在此函数内添加代码
   
    init();
    
    
    
    while(1)
    {
    /* user code start */

      
      touch_SW();
      if(SW1_flag == 1){LED1 = LED2 = LED3 = LED4 = 0;
        LED4 = 1;
        halUartWrite(LED_R,sizeof(LED_R));
        Delay(2000);
        
      }
      
 if(SW1_flag == 2){LED1 = LED2 = LED3 = LED4 = 0;
        LED4 = 0;LED3 = 1;
        halUartWrite(LED_G,sizeof(LED_G));
        Delay(2000);
      }
       if(SW1_flag == 3){LED1 = LED2 = LED3 = LED4 = 0;
        LED2 = 1;LED3 = 0;
        halUartWrite(LED_B,sizeof(LED_B));
        Delay(2000);
      }
      if(SW1_flag == 22){LED1 = LED2 = LED3 = LED4 = 0; //SW1双击
      LSD1();
      }
      if(SW2_flag == 1){
      lamp = 1;fun = 0;
        LSD2();
      
      }
       if(SW2_flag == 2){LED1 = LED2 = LED3 = LED4 = 0;
      
         
         
         fun = 1;
         lamp = 0;
         LED1 = LED2 = LED3 = LED4 = 1;


      }
      
      if(SW2_flag == 22){//SW2双击
      
         fun = 0;
         lamp = 0;
         LED1 = LED2 = LED3 = LED4 = 0;
         
          halUartWrite(LED_C,sizeof(LED_C));
          Delay(2000);

      }
      
      
      
      
       
    /* user code end */
    }
}
posted @ 2022-10-08 20:38  ~内个臣呐~  阅读(245)  评论(0)    收藏  举报