STM32F103控制WS2812B


#include "WS281x.h"
#include "stdio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_dma.h"    
#include "stm32f10x_gpio.h"    
#include "misc.h"
    

uint16_t WS_SendBuff[(LED_NUM*24) + 58] = {0,0,0};

uint16_t WS2_SendBuff[(LED_NUM*24) + 58] = {29,58,0};
uint16_t WS3_SendBuff[(LED_NUM*24) + 58] = {0,0,0};
uint16_t WS4_SendBuff[(LED_NUM*24) + 58] = {0,0,0};

//ÈýÖÖÔ­É«  RGB
uint8_t colorss[][3] = 
{
    {0xFF, 0x00, 0x00},       // ºì
    {0x00, 0xFF, 0x00},       // ÂÌ       
    {0x00, 0x00, 0xFF},       // À¶
//    {0xFF, 0xFF, 0xFF},       // 
//    {0xFF, 0xFF, 0x00},       // 
//    {0x00, 0xFF, 0xFF},       // 
//    {0x00, 0xFF, 0xFF},       // 
//    {0x00, 0x80, 0xFF},       // 
//    {0x00, 0x00, 0xFF},       // 
//    {0x80, 0x00, 0xFF},       // 
//    {0xFF, 0x00, 0xFF},       // 
//    {0xFF, 0x00, 0x80}       // 
};


/************************
**³ÌÐòÃû³Æ:Delay
**²ÎÊý:   
**·µ»ØÖµ: none
**˵Ã÷: 
************************/
void Delay(__IO uint32_t nCount) 
{
  while(nCount--) 
  {
  }
}


/************************
**³ÌÐòÃû³Æ:WS_RCC_Config
**²ÎÊý: ÎÞ
**·µ»ØÖµ: none
**˵Ã÷:  TIM \ GPIO \  DMA µÈʹÄÜÇëÇóÅäÖÃ
************************/
void WS_RCC_Config(void)
{
    
   /*ʹÄܶ¨Ê±Æ÷ʱÖÓ */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    
  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);    
    
    /*ʹÄܶ¨Ê±Æ÷ʱÖÓ */   //sq add 2018-8-21 20:01:38
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);    
    
        /* DMA clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}

/************************
**³ÌÐòÃû³Æ:WS_GPIO_Config
**²ÎÊý: ÎÞ
**·µ»ØÖµ: none
**˵Ã÷: IO ¹Ü½ÅÅäÖÃ
************************/
void WS_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_12 |  GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    
    //sq add 2018-8-21 15:35:22  
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 |  GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
}

/************************
**³ÌÐòÃû³Æ:WS_TIM1_ModeConfig
**²ÎÊý:   none
**·µ»ØÖµ: none
**˵Ã÷: ÅäÖÃTIM1Êä³öµÄPWMÐźŵÄģʽ£¬ÈçÖÜÆÚ¡¢¼«ÐÔ¡¢Õ¼¿Õ±È
************************/
static void WS_TIM1_ModeConfig(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;   //ʱ¼ä¼ÆÊý½á¹¹Ìå
    TIM_OCInitTypeDef  TIM_OCInitStructure;   //¶¨Ê±Æ÷³õʼ»¯½á¹¹Ìå
//  NVIC_InitTypeDef NVIC_InitStructure;
    
    /*******************
      PWM1+CH1ͨµÀ£¬ÅäÖÃPWM1ΪÊä³öģʽ ÅäÖÃΪ800KHzµÄƵÂÊ ÄÚºËʱÖÓ 72/90 = 800kHz£¬ WS2812B µÄ¸ßµÍµçƽÈçÏ     
        TIMCLK = 72ZHZ  WSTIM=WSH+WSL = 1.25 
        0Â룺 H0(0.4us) + L0(0.85us) = 1.25us  Îó²î ¡À150ns ¼´ pulseL0 = (0.85 *90) / 1.25 = 61.2
        1Â룺 H1(0.8us) + L1(0.45us) = 1.25us  Îó²î ¡À150ns ¼´ pulseL1 = (0.45 *90) / 1.25 = 32    
    ***************************/
    //ÉèÖÃÏÂÒ»¸ö¸üÐÂʼþתÈë»î¶¯µÄ×Ô¶¯ÖØ×°¼Ä´æÆ÷ÖÜÆÚµÄÖµ È¡Öµ·¶Î§±ØÐëÔÚ0x0000 ~ 0xFFFF    
  TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD - 1;
    //ÉèÖÃʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ = ϵͳʱÖÓ/ TIM_Prescaler 
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
    //ÉèÖÃʱÖÓ·Ö¸î
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    //Ñ¡Ôñ¼ÆÊýÆ÷ģʽ ÉèÖÃΪÏòÉϼÆÊýģʽ
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM1

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值