GD32F103RCT6 DMA PWM驱动WS2812

void ws2812b_Reset_l(void)
{
    for(int i = LED_RESET - 16;i < LED_RESET;i++)
    {
        l_buff[i] = 2;
    }
}

/**RGB L**/
void led_rgb_pwm_ch0_DMA_ch4_init(void)
{
    dma_parameter_struct dma_str;
    rcu_periph_clock_enable(RCU_DMA1);
    dma_deinit(DMA1, DMA_CH4);
    
    dma_str.direction = DMA_MEMORY_TO_PERIPHERAL;
    dma_str.memory_addr = (uint32_t)l_buff;
    dma_str.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
    dma_str.memory_width = DMA_MEMORY_WIDTH_8BIT;
    dma_str.number = TRANSFER_NUM;
    dma_str.periph_addr = (uint32_t)&TIMER_CH0CV(TIMER4);
    dma_str.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
    dma_str.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
    dma_str.priority = DMA_PRIORITY_MEDIUM;
    dma_init(DMA1,DMA_CH4,&dma_str);
    
    dma_circulation_enable(DMA1,DMA_CH4);
    dma_memory_to_memory_disable(DMA1,DMA_CH4);
    dma_channel_enable(DMA1,DMA_CH4);
 }

/**RGB L**/
void led_rgb_timer4_ch0_init(void)
{
    timer_oc_parameter_struct timer_oc_str;
    timer_parameter_struct timer_str;
    
    rcu_periph_clock_enable(RCU_TIMER4);
    timer_str.prescaler = 0;
    timer_str.period = 135-1;
    timer_str.alignedmode = TIMER_COUNTER_EDGE;
    timer_str.counterdirection = TIMER_COUNTER_UP;
    timer_str.repetitioncounter = 0;
    timer_str.clockdivision = TIMER_CKDIV_DIV1;
    timer_init(TIMER4,&timer_str);
    
    timer_oc_str.outputstate = TIMER_CCX_ENABLE;
    timer_oc_str.outputnstate = TIMER_CCXN_DISABLE;
    timer_oc_str.ocpolarity = TIMER_OC_POLARITY_HIGH;
    timer_oc_str.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
    timer_oc_str.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
    timer_oc_str.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER4,TIMER_CH_0,&timer_oc_str);
    
    
    timer_channel_output_pulse_value_config(TIMER4,TIMER_CH_0,0);
    timer_channel_output_mode_config(TIMER4,TIMER_CH_0,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER4,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);
    
    timer_primary_output_config(TIMER4,ENABLE);
    timer_dma_enable(TIMER4,TIMER_DMA_CH0D);
    
    timer_auto_reload_shadow_enable(TIMER4);

    timer_enable(TIMER4);
    
    
}

void led_rgb_write_l(int index,int color)
{

    u8 i = 0;
    u8 r = (u8)(color >> 16);
    u8 g = (u8)(color >> 8);
    u8 b = (u8)(color);
    u32 offset = LED_RESET + index * 24;
    
    for(i = 0;i < 8;i++)
    {
        if((g << i)&0x80){
            l_buff[offset+i] = HIGH;
        }else{
            l_buff[offset+i] = LOW;
        }
        
    }
    offset+=8;
    for(i = 0;i < 8;i++)
    {
        if((r << i)&0x80){
            l_buff[offset+i] = HIGH;
        }else{
            l_buff[offset+i] = LOW;
        }
    }
    offset+=8;
    for(i = 0;i < 8;i++)
    {
        if((b << i)&0x80){
            l_buff[offset+i] = HIGH;
        }else{
            l_buff[offset+i] = LOW;
        }
    }
    
}

void led_rgb_show_color_all(void)
{
    uint32_t i = 0;
    uint32_t color = save_Reg[46]&0x0000ffff;
    color <<= 16;
    color |= save_Reg[47];
    
    input_Reg[46] = save_Reg[46];
    input_Reg[47] = save_Reg[47];
    
    
    l_color = color;
    r_color = color;
    curr_l_color = l_color;
    curr_r_color = r_color;
    for(i = 0;i < LED_NUMBER;i++){
        led_rgb_write_l(i,l_color);
        led_rgb_write_r(i,r_color);
    }
}

#define LED_NUMBER 55
#define LED_RESET 300
#define TRANSFER_NUM  LED_NUMBER * 24+LED_RESET        
uint8_t l_buff[TRANSFER_NUM]={0};
uint8_t r_buff[TRANSFER_NUM]={0};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值