基于STM32F030的WS2812B的程序设计

基于STM32F030的WS2812B的程序设计

#include "ws2812b.h"
#include "timer.h"
#define  numLEDs  255            //最大点亮的像素
#define  ws28     GPIO_Pin_0                   //信号引脚
unsigned char buf_R[numLEDs] = {0};//颜色缓存  
unsigned char buf_G[numLEDs] = {0};  
unsigned char buf_B[numLEDs] = {0};

unsigned char len;     
void WS2812B_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}

void WS28_Reset(void)          //复位函数
{
	   uint16_t y=0;
	   GPIOB->ODR=0;
	   for(y=0;y<1000;y++);
}

void WS28_WriteColor(unsigned char g_col,unsigned char r_col,unsigned char b_col)
{
	  unsigned char i;
	  uint16_t y=0;
	  for(i=0;i<8;i++)
	  {
			g_col=g_col<<1;
			if((g_col&0x80)==0x80)
			{
				   GPIOB->ODR=1;
					 Delay_850nS_H();
					 GPIOB->ODR=0;
			}
			else
			{
				 GPIOB->ODR=1;
				 Delay_400nS_H();
				 GPIOB->ODR=0;
				 Delay_850nS_L();
			}
	  }
		for(i=0;i<8;i++)
	  {
		  r_col=r_col<<1;
			if((r_col&0x80)==0x80)
			{
				   GPIOB->ODR=1;
					 Delay_850nS_H();
					 GPIOB->ODR=0;
			}
			else
			{
				 GPIOB->ODR=1;
				 Delay_400nS_H();
				 GPIOB->ODR=0;
				 Delay_850nS_L();
			}
	  }
		for(i=0;i<8;i++)
	  {
			b_col=b_col<<1;
			if((b_col&0x80)==0x80)
			{
				   GPIOB->ODR=1;
					 Delay_850nS_H();
					 GPIOB->ODR=0;
			}
			else
			{
				 GPIOB->ODR=1;
				 Delay_400nS_H();
				 GPIOB->ODR=0;
				 Delay_850nS_L();
			}
	  }
}

void SetPixelColor(unsigned int num,unsigned long c,unsigned flg)
{
     unsigned int i,j;  
     for(i=0;i<numLEDs;i++)  
     {  
				buf_R[num] = (unsigned char)(c>>16);  
				buf_G[num] = (unsigned char)(c>>8);  
				buf_B[num] = (unsigned char)(c);  
     }  
		 for(i=0;i<numLEDs;i++)  
		 {  
			  if(flg==1)
				{
				   buf_R[i]=buf_R[i]-15;
				}
				WS28_WriteColor(buf_G[i],buf_R[i],buf_B[i]);				
		 }  
}  

void ColorWipe(unsigned long c, unsigned int wait,unsigned flg)
{
    unsigned int i=0;  
    for( i=0; i<numLEDs; i++)   
    {  
			SetPixelColor(i,c,flg);    //第一个LED灯 控制完成 
			WS28_Reset();  
			Delay_ms(wait);  
    }  
}	

int main(void)
{		
   uint32_t i;
	 unsigned char Flg;
	 Flg=0;
     for(i=5000;i>0;i--);	
	 SysClock_InterConfig();
	 User_SysTick_Init();
	 WS2812B_GPIO_Init();
   while(1)
   {	
		 r_color=55;
		 g_color=55;
		 b_color=55;
		 for(;;)
		 {
			 ColorWipe(0x55CCAA, 100,1);    
		 }
   } 
   return 1;  
}

你可能感兴趣的:(RGB灯光设置)