本人使用的是stm32f407vet6的板子,程序编译也没什么问题就是不知道哪里错了请大神指点暂时先弄得4个灯但是没有一个亮
ws2812b.c
#include "WS2812B.h"
#include "spi.h"
#include "rcc.h"
#define RGBLED_SPI SPI2
void RGB_Config(void)
{
RCC_Config();
RGBLED_SPI_Config();
}
void RGB_SetColor(uint8_t RGB_R,uint8_t RGB_G,uint8_t RGB_B)
{
uint8_t i,j,k;
for(i=0;i<8;i++)
{//·¢Ëí0Âë1Âë
if(((RGB_G<<i)&0x80)==0x80)
{//·¢Ëí0Âë
while(SPI_I2S_GetFlagStatus(RGBLED_SPI,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(RGBLED_SPI,0XFC);
}
else
{//·¢Ëí1Âë
while(SPI_I2S_GetFlagStatus(RGBLED_SPI,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(RGBLED_SPI,0XC0);
}
}
for(j=0;j<8;j++)
{
if(((RGB_R<<j)&0x80)==0x80)
{
while(SPI_I2S_GetFlagStatus(RGBLED_SPI,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(RGBLED_SPI,0XFC);
}
else
{
while(SPI_I2S_GetFlagStatus(RGBLED_SPI,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(RGBLED_SPI,0XC0);
}
}
for(k=0;k<8;k++)
{
if(((RGB_B<<j)&0x80)==0x80)
{
while(SPI_I2S_GetFlagStatus(RGBLED_SPI,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(RGBLED_SPI,0XFC);
}
else
{
while(SPI_I2S_GetFlagStatus(RGBLED_SPI,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(RGBLED_SPI,0XC0);
}
}
}
void RGBLED_SPI_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//ê1ÄüSPI1ê±Öó
//GPIOFB3,4,53õê¼»ˉéèÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;//PB3~5¸′óÃ1|Äüêä3ö
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉ
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; //éèÖÃSPIμ¥Ïò»òÕßË«ÏòμÄêy¾YÄ£ê½:SPIéèÖÃÎaË«ÏßË«Ïòè«Ë«1¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //éèÖÃSPI1¤×÷Ä£ê½:éèÖÃÎaÖ÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //éèÖÃSPIμÄêy¾Y′óD¡:SPI·¢Ëí½óêÕ8λ֡½á11
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low ; //′®DDí¬2½ê±ÖóμÄ¿ÕÏD×′ì¬Îa¸ßμçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //′®DDí¬2½ê±ÖóμÄμú¶t¸öìø±äÑØ£¨éÏéy»òϽ죩êy¾Y±»2éÑù
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSDÅoÅóéó2¼t£¨NSS1ü½Å£©»1êÇèí¼t£¨ê1óÃSSI룩1üàí:Äú2¿NSSDÅoÅóDSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //¶¨òå2¨ìØÂêÔ¤·ÖÆμμÄÖμ:2¨ìØÂêÔ¤·ÖÆμÖμÎa256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; //Ö¸¶¨êy¾Y′«êä′óMSBλ»1êÇLSBλ¿aê¼:êy¾Y′«êä′óMSBλ¿aê¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖμ¼ÆËãμĶàÏîê½
SPI_Init(RGBLED_SPI, &SPI_InitStructure); //¸ù¾YSPI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèSPIx¼Ä′æÆ÷
SPI_Cmd(RGBLED_SPI,ENABLE);
}
ws2812b.h
#ifndef __WS2812B_H
#define __WS2812B_H
#include "stm32f4xx.h"
//typedef struct
//{
// uint8_t Color_RED_Value;
// uint8_t Color_GREEN_Value;
// uint8_t Color_BLUE_Value;
//}RGB_SetColorTypedef;
void RGBLED_SPI_Config(void);
void RGB_Config(void);
void RGB_SetColor(uint8_t RGB_R,uint8_t RGB_G,uint8_t RGB_B);
#endif
rcc.c
#include "rcc.h"
#include "stm32f4xx_rcc.h"
void RCC_Config(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_SYSCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE,4,50,2,4);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x80)
{
}
}
}
main.c
//#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "rcc.h"
#include "spi.h"
#include "WS2812B.h"
int main(void)
{
RGB_Config();
// RCC_GetClockFreq(RCC_ClocksStruct);
while(1)
{
RGB_SetColor(255,0,0);
RGB_SetColor(255,0,0);
RGB_SetColor(255,0,0);
RGB_SetColor(255,0,0);
delay_ms(200);
RGB_SetColor(255,0,0);
RGB_SetColor(255,0,0);
RGB_SetColor(0,255,0);
RGB_SetColor(0,255,0);
delay_ms(200);
RGB_SetColor(255,0,0);
RGB_SetColor(255,0,0);
RGB_SetColor(0,0,255);
RGB_SetColor(0,0,255);
}
}
那你就用串口打印呗····
一周热门 更多>