| 复制代码#include "intrins.h"
#include "ws2812.h"
#include        "GPIO.h"
#include        "delay.h"
#include "UART.h"
unsigned char s[8][3];
unsigned char c[8][3];
#define GREEN 0
#define RED 1
#define BLUE 2
unsigned char comm_buffer[5];
/*************        ±¾µØ±äÁ¿ÉùÃ÷        **************/
//u8        idata RX1_Buffer[RX1_Lenth];        //½ÓÊÕ»º³å
u8        TX1_Cnt;        //·¢ËͼÆÊý
bit        B_TX1_Busy;        //·¢ËÍæ±êÖ¾
u8        RX1_Cnt;        //½ÓÊÕ¼ÆÊý
bit Rec_On;
bit Rec_Ok;
/******************* IOÅäÖú¯Êý *******************/
void        GPIO_config(void)
{
        GPIO_InitTypeDef        GPIO_InitStructure;                //½á¹¹¶¨Òå
        
        GPIO_InitStructure.Pin  = GPIO_Pin_5 ;                //Ö¸¶¨Òª³õʼ»¯µÄIO, GPIO_Pin_0 ~ GPIO_Pin_7
        GPIO_InitStructure.Mode = GPIO_PullUp;        //Ö¸¶¨IOµÄÊäÈë»òÊä³ö·½Ê½,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
        GPIO_Inilize(GPIO_P5,&GPIO_InitStructure);        //³õʼ»¯
}
void UartInit(void)        //9600bps@22.1184MHz
{
        SCON = 0x50;                
        AUXR |= 0x40;                
        AUXR &= 0xFE;                
        TMOD &= 0x0F;                
        TL1 = 0xC0;                        
        TH1 = 0xFD;                        
        ET1 = 0;                        
        TR1 = 1;                        
}
void        UART_config(void)
{
        COMx_InitDefine                COMx_InitStructure;                                        //½á¹¹¶¨Òå
        COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;        //ģʽ, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
        COMx_InitStructure.UART_BRT_Use   = BRT_Timer1;                        //Ñ¡Ôñ²¨ÌØÂÊ·¢ÉúÆ÷, BRT_Timer1, BRT_Timer2 (×¢Òâ: ´®¿Ú2¹Ì¶¨Ê¹ÓÃBRT_Timer2)
        COMx_InitStructure.UART_BaudRate  = 9600ul;                        //²¨ÌØÂÊ, Ò»°ã 110 ~ 115200
        COMx_InitStructure.UART_RxEnable  = ENABLE;                                //½ÓÊÕÔÊÐí,   ENABLE»òDISABLE
        COMx_InitStructure.BaudRateDouble = DISABLE;                        //²¨ÌØÂʼӱ¶, ENABLE»òDISABLE
        COMx_InitStructure.UART_Interrupt = ENABLE;                                //ÖжÏÔÊÐí,   ENABLE»òDISABLE
        COMx_InitStructure.UART_Priority    = Priority_0;                        //Ö¸¶¨ÖжÏÓÅÏȼ¶(µÍµ½¸ß) Priority_0,Priority_1,Priority_2,Priority_3
        COMx_InitStructure.UART_P_SW      = UART1_SW_P30_P31;        //Çл»¶Ë¿Ú,   UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
        UART_Configuration(UART1, &COMx_InitStructure);                //³õʼ»¯´®¿Ú1 UART1,UART2,UART3,UART4
        
}
void GPI_Init(void)
{
        P0M0 = 0x00;
        P0M1 = 0x00;
        P1M0 = 0x00;
        P1M1 = 0x00;
        P2M0 = 0x00;
        P2M1 = 0x00;
        P3M0 = 0x00;
        P3M1 = 0x00;
        P4M0 = 0x00;
        P4M1 = 0x00;
        P5M0 = 0x00;
        P5M1 = 0x00;
}
void UartSend_Data(unsigned char dat)
{
        SBUF = dat;  // °ÑÒª·¢Ë͵ÄÊý¾Ý¸øSBUF¼Ä´æÆ÷
        while(!TI);  // µÈ´ýÊý¾Ý·¢ËÍÍê³É
        TI = 0;      // Çå³ý·¢ËÍÍê³É±ê־λ
}
void changeOneColorRed(unsigned char i,unsigned char b){
  s[i][GREEN]=0;
        s[i][RED]=(b<<1)*1;
        s[i][BLUE]=0;
  SendOneFrame(&s[0][0]);
}
void changeOneColorGreen(unsigned char i,unsigned char b){
  s[i][GREEN]=(b<<1)*1;
        s[i][RED]=0;
        s[i][BLUE]=0;
  SendOneFrame(&s[0][0]);
}
void changeOneColorBlue(unsigned char i,unsigned char b){
  s[i][GREEN]=0;
        s[i][RED]=0;
        s[i][BLUE]=(b<<1)*1;
  SendOneFrame(&s[0][0]);
}
void main(void)
{
        u8 i; 
        int j;
        B_TX1_Busy = 0;
        RX1_Cnt = 0;
        Rec_On = 0;
  Rec_Ok = 0;
                EAXSFR();                /* À©Õ¹¼Ä´æÆ÷·ÃÎÊʹÄÜ */
        GPIO_config();
        UartInit();
        UART_config();
        EA = 1;
        
        for(i=0;i<8;i++){
                s[i][0]=20;
                s[i][1]=1;
                s[i][2]=1;
        }
        SendOneFrame(&s[0][0]);
        
        delay_ms(500);
        delay_ms(500);
        delay_ms(500);
        
                for(i=0;i<8;i++){
                s[i][0]=0;
                s[i][1]=0;
                s[i][2]=0;
        }
        
        SendCloseFrame();
        
        while (1)
        {
                delay_ms(1);
        if (Rec_On==1 && j<=4) {
                                                        comm_buffer[j] = RX1_Buffer[i];
                                                        j++;                                                                
                                                }        
                
                        if (COM1.RX_Cnt ==5)   //light No. ok
                        {
                                if(RX1_Buffer[RX1_Cnt-4]== 0x18){
                                        SendCloseFrame();
                                } else if(RX1_Buffer[RX1_Cnt-4]== 0x19){
                                        SendOneFrame(&s[0][0]);                                        
                                } else if(RX1_Buffer[RX1_Cnt-3] == 0x0A){
                                  changeOneColorRed(RX1_Buffer[RX1_Cnt-4],RX1_Buffer[RX1_Cnt-2]);
                          } else if(RX1_Buffer[RX1_Cnt-3] == 0x0B){
                            changeOneColorGreen(RX1_Buffer[RX1_Cnt-4],RX1_Buffer[RX1_Cnt-2]);
                          } else if(RX1_Buffer[RX1_Cnt-3] == 0x0C){
                            changeOneColorBlue(RX1_Buffer[RX1_Cnt-4],RX1_Buffer[RX1_Cnt-2]);
                          }
                                UartSend_Data('O');
                                UartSend_Data('K');
                        } else {
                          //UartSend_Data('F');
                                //UartSend_Data('A');
                        }
                        RX1_Cnt = 0;
                        Rec_Ok = 0;
        
}
}
 
 
 |