WS2818 (stm8 寄存器)

WS2818 (stm8 寄存器)WS2818(stm8寄存器)_ws2818单片机编程

 WS2818 (stm8 寄存器)

 

#include "led.h"
#include "time.h"
#include "stm8s.h"
//#include "iostm8s105c6.h"


#define LED_COUNT             8
#define WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
#define WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;     
// u8 WS2818_writeL  ;
 //u8 WS2818_writeH  ;  
unsigned long RGBBuffer[LED_COUNT];
unsigned char GRBBuffer[LED_COUNT*3];
char i=8;
void LED_Config(void);
void WS2812_Set_Led_Color(unsigned char Index, unsigned long RGB);
void WS2812_Initial_IO(void); //一个初始化函数:
void ResetWs2812(void);
void WS2812_byte(u8 *ucByte);
void WS2812_byte1(u8 ucByte);


void LED_Config(void)
{
  GPIO_Init(LED_GPIO_PORT, LED_GPIO_PIN, GPIO_MODE_OUT_PP_HIGH_FAST);
	LED_On;
}


//复位WS2818,  28
void ResetWs2812(void){
	     
     GPIOB->ODR = GPIOB->ODR&0xFE ;
		 
		  Delay_us(300);
 }

 
 //初始化WS2812的端口脚

void WS2812_Initial_IO(void) //一个初始化函数:
{  

  //WS2818_writeL =  GPIOB->ODR =GPIOB->ODR&0xFE ;
 // WS2818_writeH =  GPIOB->ODR =GPIOB->ODR|0x01 ;  
  GPIO_Init(GPIOB, GPIO_PIN_0, GPIO_MODE_OUT_PP_HIGH_FAST);
	 GPIOB->DDR =GPIOB->DDR|0x01   ;
	 WS2818_writeL;
    for(  i = 0; i < LED_COUNT; i++)
    {
        RGBBuffer[i] = 0;
    }
    
 //  WS2812_byte((u8*)0XFF);WS2812_byte((u8*)0XFF);WS2812_byte((u8*)0XFF);
}

//送入的颜色是RGB,所以内部需要转换一下,Index为第几个灯,RGB是颜色参数
void WS2812_Set_Led_Color(unsigned char Index, unsigned long RGB)
{
    unsigned long GRB = 0;
    
		 unsigned char i;
    unsigned char j = 0;
    unsigned char Dat = 0;
		
    GRB = (RGB << 8) & 0x00ff0000;
    GRB |= ((RGB >> 8) & 0x0000ff00);
    GRB |= (RGB & 0x000000ff);
    
    RGBBuffer[Index] = GRB;
    
    for( i = 0; i < LED_COUNT; i++)
    {
        GRBBuffer[i*3] = RGBBuffer[i] >> 16;
        GRBBuffer[i*3 +1] = RGBBuffer[i] >> 8;
        GRBBuffer[i*3 + 2] = RGBBuffer[i] >> 0;
    }
    
  //  GPIOD->ODR =GPIOD->ODR|0x10 ; //normal gpio
   // GPIOB->DDR =GPIOB->DDR|0x10   ;

    
    for(j = 0; j < 3*LED_COUNT; j++)
    {
        Dat = GRBBuffer[j];
        i=8;
       // for(  i = 0; i < 8; i++)
			  while(i--)
        {  
					
				  
          

            if(Dat & 0x80)// 1码	750H	220L
            { 
							WS2818_writeH;
						   nop();nop();
							 nop(); nop(); 
							 nop();nop();nop();nop(); //nop的个数
							  nop();nop();nop();nop(); //nop的个数
					//	nop();nop();
								WS2818_writeL;
					//	nop();
					    //nop(); //nop的个数               
            }
            else        // 0码	220H	750L
            {
            WS2818_writeH;
						nop();nop(); //nop的个数
							nop();nop(); //nop的个数
						WS2818_writeL;
				//		nop();
				     nop();
						//nop();
						//nop(); //nop的个数
						//nop();nop();nop();nop(); //nop的个数
					//	nop();nop();	
						
            }
            Dat <<=1;
        }
    }
   // 	nop();nop();nop();nop(); //nop的个数
	   WS2818_writeL;
			nop();nop();

   // ResetWs2812(); //等待复位结束
}

void WS2812_write_One(void)
{
	        //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();nop();nop();
					//end==============ws2818-write_One========================
						
}
void WS2812_write_Zero(void)
{
  //start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();nop();nop(); //nop();nop();nop();
	//end==============ws2818-write_zero========================
				 
					 
}

void WS2812_bit(u8*  ucByte)
{ 
	
        if (*ucByte & 0x80)
			
        { //1码	750H	220L
          
					    //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();nop();nop();
					//end==============ws2818-write_One========================
        }
        else
        { // 0码	220H	750L
				 //start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();nop();nop();
	       //end==============ws2818-write_zero========================
        }
			
				*ucByte <<=  1;
			
}

void WS2812_byte(u8 * ucByte){
    
		WS2812_bit(ucByte);
		*ucByte <<=  1;
   	WS2812_bit(ucByte);
		*ucByte <<=  1;
    	WS2812_bit(ucByte);
		*ucByte <<=  1;
	   WS2812_bit(ucByte);
		*ucByte <<=  1;
    	WS2812_bit(ucByte);
		*ucByte <<=  1;
	 WS2812_bit(ucByte);
		*ucByte <<=  1;
	 WS2812_bit(ucByte);
		*ucByte <<=  1;
	   WS2812_bit(ucByte);
		*ucByte <<=  1;

}
void WS2812_byte1(u8 ucByte){
	
		 if (ucByte & 0x80)
			
        { //1码	750H	220L
				
				    //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();//nop();nop();
					//end==============ws2818-write_One========================
					
        }
        else
        { // 0码	220H	750L
				//start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();//nop();nop();
	       //end==============ws2818-write_zero========================
        }
				//ucByte <<=  1;
				if (ucByte & 0x40) //第2位:0b0100 0000
			
        { //1码	750H	220L
          
					    //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();//nop();nop();
					//end==============ws2818-write_One========================
        }
        else
        { // 0码	220H	750L
				//start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();//nop();nop();
	       //end==============ws2818-write_zero========================
        }
				//ucByte <<=  1;
				if (ucByte & 0x20) //第3位:0b0010 0000
			
        { //1码	750H	220L
          
					    //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();//nop();nop();
					//end==============ws2818-write_One========================
        }
        else
        { // 0码	220H	750L
				//start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();//nop();nop();
	       //end==============ws2818-write_zero========================
        }
			//	ucByte <<=  1;
				if (ucByte & 0x10) //第4位:0b0001 0000
			
        { //1码	750H	220L
          
					    //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();//nop();nop();
					//end==============ws2818-write_One========================
        }
        else
        { // 0码	220H	750L
				//start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();//nop();nop();
	       //end==============ws2818-write_zero========================
        }
			//	ucByte <<=  1;
				if (ucByte & 0x08) //第5位:0b0000 1000
			
        { //1码	750H	220L
          
					    //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();//nop();nop();
					//end==============ws2818-write_One========================
        }
        else
        { // 0码	220H	750L
				//start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();//nop();nop();
	       //end==============ws2818-write_zero========================
        }
			//	ucByte <<=  1;
				if (ucByte & 0x04) //第6位:0b0000 0100
			
        { //1码	750H	220L
          
					    //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();//nop();nop();
					//end==============ws2818-write_One========================
        }
        else
        { // 0码	220H	750L
				//start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();nop();nop();
	       //end==============ws2818-write_zero========================
        }
			//	ucByte <<=  1;
				if (ucByte & 0x02) //第7位:0b0000 0010
			
        { //1码	750H	220L
          
					     //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();//nop();nop();
					//end==============ws2818-write_One========================
        }
        else
        { // 0码	220H	750L
				 //start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();nop();nop();
	       //end==============ws2818-write_zero========================
        }
				//ucByte <<=  1;
				if (ucByte & 0x01) //第8位:0b0000 0001
			
        { //1码	750H	220L
          
					    //start==============ws2818-write_One========================
            WS2818_writeH;	//  WS2818_writeH   GPIOB->ODR =GPIOB->ODR|0x01 ;    
				//	  nop();nop();nop();
					  nop();nop();nop();
					  WS2818_writeL; // WS2818_writeL   GPIOB->ODR =GPIOB->ODR&0xFE ; 
					  nop();//nop();nop();
					//end==============ws2818-write_One========================
        }
        else
        { // 0码	220H	750L
				 //start==============ws2818-write_zero========================
             WS2818_writeH;	
					  nop();nop();nop();
					  WS2818_writeL;	
					// nop();nop();nop();
					 nop();nop();nop();
	       //end==============ws2818-write_zero========================
        }
				//ucByte <<=  1;
				
}
/*
void WS2812_byte2(u8 ucByte){
	
		 if (ucByte & 0x80)
			
        { //1码	750H	220L
          
					 WS2812_write_One();
        }
        else
        { // 0码	220H	750L
				 WS2812_write_Zero();
        }
				ucByte <<=  1;
				if (ucByte & 0x80)
			
        { //1码	750H	220L
          
					 WS2812_write_One();
        }
        else
        { // 0码	220H	750L
				 WS2812_write_Zero();
        }
				ucByte <<=  1;
				if (ucByte & 0x80)
			
        { //1码	750H	220L
          
					 WS2812_write_One();
        }
        else
        { // 0码	220H	750L
				 WS2812_write_Zero();
        }
				ucByte <<=  1;
				if (ucByte & 0x80)
			
        { //1码	750H	220L
          
					 WS2812_write_One();
        }
        else
        { // 0码	220H	750L
				 WS2812_write_Zero();
        }
				ucByte <<=  1;
				if (ucByte & 0x80)
			
        { //1码	750H	220L
          
					 WS2812_write_One();
        }
        else
        { // 0码	220H	750L
				 WS2812_write_Zero();
        }
				ucByte <<=  1;
				if (ucByte & 0x80)
			
        { //1码	750H	220L
          
					 WS2812_write_One();
        }
        else
        { // 0码	220H	750L
				 WS2812_write_Zero();
        }
				ucByte <<=  1;
				if (ucByte & 0x80)
			
        { //1码	750H	220L
          
					 WS2812_write_One();
        }
        else
        { // 0码	220H	750L
				 WS2812_write_Zero();
        }
				ucByte <<=  1;
				if (ucByte & 0x80)
			
        { //1码	750H	220L
          
					 WS2812_write_One();
        }
        else
        { // 0码	220H	750L
				 WS2812_write_Zero();
        }
				ucByte <<=  1;
				
}
*/




void test_led(void){
 WS2812_Initial_IO();//熄灭所有灯
 //WS2812_byte(0XFF);WS2812_byte(0XFF);WS2812_byte(0XFF);
 WS2812_byte1(0xFF); WS2812_byte1(0XFF);WS2812_byte1(0XFF);
 WS2812_byte1(0XFF);WS2812_byte1(0XFF);WS2812_byte1(0XFF);
 WS2812_byte1(0XFF);WS2812_byte1(0XFF);WS2812_byte1(0XFF);
 //WS2812_byte((u8*)0XFF);WS2812_byte((u8*)0XFF);WS2812_byte((u8*)0XFF);

 //WS2812_byte(0XFF);WS2812_byte(0XFF);WS2812_byte(0XFF);
 //WS2812_byte(0XFF);WS2812_byte(0XFF);WS2812_byte(0XFF);
 ResetWs2812(); //等待复位结束
}

今天的文章WS2818 (stm8 寄存器)分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/64927.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注