STM32F103+WS2812调试问题(DMA+PWM)

2019-07-21 08:17发布

前段时间在学习DMA,想着有RGB灯珠,就开始写WS2812的驱动,但是昨晚进硬件故障中断里面,但在句子前面加了大括号就好了,现在能发光,但是不能改变颜 {MOD}是哪里出问题了[mw_shl_code=c,true]
#define H0        25
#define H1        50

u8 buff[24]={0};

void WS2812_init()//ws2812³õʼ»¯Òý½Å£¬¶¨Ê±Æ÷(PWM)£¬DMA£¬ÖжÏ
{
        GPIO_InitTypeDef  GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
        DMA_InitTypeDef DMA_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB,&GPIO_InitStructure);
        //GPIO_PinRemapConfig(GPIO_Remap_TIM4,ENABLE);
       
        //ƵÂÊ=72M/((arr+1)*(psc+1))(µ¥Î»£ºHz)
        TIM_TimeBaseStructure.TIM_Period = 89;//90     800KHz    1.25us 1ÖÜÆÚ
        TIM_TimeBaseStructure.TIM_Prescaler = 0;//1
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//0£»Ê±ÖÓ·Ö¸î
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//ÏòÉϼÆÊýģʽ
        TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//Öظ´¼ÆÊýÖµ
        TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
       
        //TIMÊä³ö±È½Ï¹¦ÄÜ
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ1
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//±È½ÏÊä³öʹÄÜ
        TIM_OCInitStructure.TIM_Pulse = 0;//ÉèÖôý×°È벶»ñ±È½Ï¼Ä´æÆ÷µÄÂö³åÖµ
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;////Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔ¸ß
        TIM_OC1Init(TIM4,&TIM_OCInitStructure);
       
        TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);//ʹÄÜTIM4 PWM CH1
        TIM_DMACmd(TIM4,TIM_DMA_CC1,ENABLE);//ʹÓÃTIM4 CCR1 DMA
        //TIM_Cmd(TIM4,ENABLE);
       
        //DMA1                ÅäÖÃ
        DMA_DeInit(DMA1_Channel1);
        DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&TIM4->CCR1;//Ä¿±êµØַΪTIM4CCR1¼Ä´æÆ÷
        DMA_InitStructure.DMA_MemoryBaseAddr = (u32)buff;//Êý¾Ý´æ´¢µØÖ·
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//ÄÚ´æµØÖ·¼Ä´æÆ÷µÝÔö
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//ÍâÉèµØÖ·¼Ä´æÆ÷²»±ä
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//ÍâÉèÊý¾Ý¿í¶È8λ
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//Êý¾Ý¿í¶ÈΪ8λ
        DMA_InitStructure.DMA_BufferSize = 30*LEN;//DMAͨµÀ»º´æ´óС
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//Êý¾Ý´«Êä·½Ïò´ÓÄÚ´æµ½ÍâÉè
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//¹¤×÷ģʽ->Ñ­»·Ä£Ê½.(Õý³£Ä£Ê½)
        DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMAͨµÀÓÅÏȼ¶
        DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;//²»Ê¹ÓÃÄÚ´æµ½ÄÚ´æ´«Êä
        DMA_Init(DMA1_Channel1,&DMA_InitStructure);//ͨµÀ1³õʼ»¯
        DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);//DMAͨµÀ1´«ÊäÍê³ÉÖжÏ
       
        NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8;//ÇÀÕ¼
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//˳´Ó
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
       
}
//buf:Êý¾Ý´æ´¢µØ
//color:ÐèÒªÊä³öµÄÑÕÉ«
void fill_led(u8 *buf,u8 *color)
{
        int i;
        for(i=0;i<8;i++)//green
                buf=((color[1]<<i) & 0x80) ? H1:H0;
        for(i=0;i<8;i++)//red
                buf[8+i]=((color[0]<<i) & 0x80) ? H1:H0;
        for(i=0;i<8;i++)//blue
                buf[16+i]=((color[2]<<i) & 0x80) ? H1:H0;
}
//&iquest;&ordf;&AElig;&ocirc;DMA&acute;&laquo;&Ecirc;&auml;
void ws2812_send(u8 (*color)[3])
{
        u8 i;
        for(i=0;i<3;i++)
                fill_led(buff,color);
        DMA_Cmd(DMA1_Channel1,ENABLE);
        TIM_Cmd(TIM4,ENABLE);
}
/*void ws2812_send(u8 (*color)[3])
{
        u8 i;
        for(i=0;i<3;i++)
                fill_led(buff,color);
}*/

void DMA1_Channel1_IRQHandler(void)
{
        if(DMA_GetITStatus(DMA1_IT_TC1)==SET)
        {
                DMA_ClearITPendingBit(DMA1_IT_TC1);
               
                DMA_Cmd(DMA1_Channel1,DISABLE);
                TIM_Cmd(TIM4,DISABLE);
                delay_us(55);
        }
}
#define H0        25
#define H1        50

u8 buff[24]={0};

void WS2812_init()//ws2812&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&Ograve;&yacute;&frac12;&Aring;&pound;&not;&para;¨&Ecirc;±&AElig;÷(PWM)&pound;&not;DMA&pound;&not;&Ouml;&ETH;&para;&Iuml;
{
        GPIO_InitTypeDef  GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
        DMA_InitTypeDef DMA_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB,&GPIO_InitStructure);
        //GPIO_PinRemapConfig(GPIO_Remap_TIM4,ENABLE);
       
        //&AElig;&micro;&Acirc;&Ecirc;=72M/((arr+1)*(psc+1))(&micro;&yen;&Icirc;&raquo;&pound;&ordm;Hz)
        TIM_TimeBaseStructure.TIM_Period = 89;//90     800KHz    1.25us 1&Ouml;&Uuml;&AElig;&Uacute;
        TIM_TimeBaseStructure.TIM_Prescaler = 0;//1
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//0&pound;&raquo;&Ecirc;±&Ouml;&Oacute;·&Ouml;&cedil;&icirc;
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//&Iuml;ò&Eacute;&Iuml;&frac14;&AElig;&Ecirc;&yacute;&Auml;&pound;&Ecirc;&frac12;
        TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//&Ouml;&Oslash;&cedil;&acute;&frac14;&AElig;&Ecirc;&yacute;&Ouml;&micro;
        TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
       
        //TIM&Ecirc;&auml;&sup3;&ouml;±&Egrave;&frac12;&Iuml;&sup1;&brvbar;&Auml;&Uuml;
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//&Ntilde;&iexcl;&Ocirc;&ntilde;&para;¨&Ecirc;±&AElig;÷&Auml;&pound;&Ecirc;&frac12;:TIM&Acirc;&ouml;&sup3;&aring;&iquest;í&para;&Egrave;&micro;÷&Ouml;&AElig;&Auml;&pound;&Ecirc;&frac12;1
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//±&Egrave;&frac12;&Iuml;&Ecirc;&auml;&sup3;&ouml;&Ecirc;&sup1;&Auml;&Uuml;
        TIM_OCInitStructure.TIM_Pulse = 0;//&Eacute;è&Ouml;&Atilde;&acute;&yacute;×°&Egrave;&euml;&sup2;&para;&raquo;&ntilde;±&Egrave;&frac12;&Iuml;&frac14;&Auml;&acute;&aelig;&AElig;÷&micro;&Auml;&Acirc;&ouml;&sup3;&aring;&Ouml;&micro;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;////&Ecirc;&auml;&sup3;&ouml;&frac14;&laquo;&ETH;&Ocirc;:TIM&Ecirc;&auml;&sup3;&ouml;±&Egrave;&frac12;&Iuml;&frac14;&laquo;&ETH;&Ocirc;&cedil;&szlig;
        TIM_OC1Init(TIM4,&TIM_OCInitStructure);
       
        TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);//&Ecirc;&sup1;&Auml;&Uuml;TIM4 PWM CH1
        TIM_DMACmd(TIM4,TIM_DMA_CC1,ENABLE);//&Ecirc;&sup1;&Oacute;&Atilde;TIM4 CCR1 DMA
        //TIM_Cmd(TIM4,ENABLE);
       
        //DMA1                &Aring;&auml;&Ouml;&Atilde;
        DMA_DeInit(DMA1_Channel1);
        DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&TIM4->CCR1;//&Auml;&iquest;±ê&micro;&Oslash;&Ouml;·&Icirc;&ordf;TIM4CCR1&frac14;&Auml;&acute;&aelig;&AElig;÷
        DMA_InitStructure.DMA_MemoryBaseAddr = (u32)buff;//&Ecirc;&yacute;&frac34;&Yacute;&acute;&aelig;&acute;&cent;&micro;&Oslash;&Ouml;·
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//&Auml;&Uacute;&acute;&aelig;&micro;&Oslash;&Ouml;·&frac14;&Auml;&acute;&aelig;&AElig;÷&micro;&Yacute;&Ocirc;&ouml;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//&Iacute;&acirc;&Eacute;è&micro;&Oslash;&Ouml;·&frac14;&Auml;&acute;&aelig;&AElig;÷&sup2;&raquo;±&auml;
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//&Iacute;&acirc;&Eacute;è&Ecirc;&yacute;&frac34;&Yacute;&iquest;í&para;&Egrave;8&Icirc;&raquo;
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//&Ecirc;&yacute;&frac34;&Yacute;&iquest;í&para;&Egrave;&Icirc;&ordf;8&Icirc;&raquo;
        DMA_InitStructure.DMA_BufferSize = 30*LEN;//DMA&Iacute;¨&micro;&Agrave;&raquo;&ordm;&acute;&aelig;&acute;ó&ETH;&iexcl;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//&Ecirc;&yacute;&frac34;&Yacute;&acute;&laquo;&Ecirc;&auml;·&frac12;&Iuml;ò&acute;&Oacute;&Auml;&Uacute;&acute;&aelig;&micro;&frac12;&Iacute;&acirc;&Eacute;è
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//&sup1;¤×÷&Auml;&pound;&Ecirc;&frac12;->&Ntilde;&shy;&raquo;·&Auml;&pound;&Ecirc;&frac12;.(&Otilde;&yacute;&sup3;&pound;&Auml;&pound;&Ecirc;&frac12;)
        DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMA&Iacute;¨&micro;&Agrave;&Oacute;&Aring;&Iuml;&Egrave;&frac14;&para;
        DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;//&sup2;&raquo;&Ecirc;&sup1;&Oacute;&Atilde;&Auml;&Uacute;&acute;&aelig;&micro;&frac12;&Auml;&Uacute;&acute;&aelig;&acute;&laquo;&Ecirc;&auml;
        DMA_Init(DMA1_Channel1,&DMA_InitStructure);//&Iacute;¨&micro;&Agrave;1&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;
        DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);//DMA&Iacute;¨&micro;&Agrave;1&acute;&laquo;&Ecirc;&auml;&Iacute;ê&sup3;&Eacute;&Ouml;&ETH;&para;&Iuml;
       
        NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8;//&Ccedil;&Agrave;&Otilde;&frac14;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//&Euml;&sup3;&acute;&Oacute;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
       
}
//buf:&Ecirc;&yacute;&frac34;&Yacute;&acute;&aelig;&acute;&cent;&micro;&Oslash;
//color:&ETH;è&Ograve;&ordf;&Ecirc;&auml;&sup3;&ouml;&micro;&Auml;&Ntilde;&Otilde;&Eacute;&laquo;
void fill_led(u8 *buf,u8 *color)
{
        int i;
        for(i=0;i<8;i++)//green
                buf=((color[1]<<i) & 0x80) ? H1:H0;
        for(i=0;i<8;i++)//red
                buf[8+i]=((color[0]<<i) & 0x80) ? H1:H0;
        for(i=0;i<8;i++)//blue
                buf[16+i]=((color[2]<<i) & 0x80) ? H1:H0;
}
//&iquest;&ordf;&AElig;&ocirc;DMA&acute;&laquo;&Ecirc;&auml;
void ws2812_send(u8 (*color)[3])
{
        u8 i;
        for(i=0;i<3;i++)
                fill_led(buff,color);
        DMA_Cmd(DMA1_Channel1,ENABLE);
        TIM_Cmd(TIM4,ENABLE);
}
/*void ws2812_send(u8 (*color)[3])
{
        u8 i;
        for(i=0;i<3;i++)
                fill_led(buff,color);
}*/

void DMA1_Channel1_IRQHandler(void)
{
        if(DMA_GetITStatus(DMA1_IT_TC1)==SET)
        {
                DMA_ClearITPendingBit(DMA1_IT_TC1);
               
                DMA_Cmd(DMA1_Channel1,DISABLE);
                TIM_Cmd(TIM4,DISABLE);
                delay_us(55);
        }
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
5条回答
正点原子
1楼-- · 2019-07-21 13:53
正点原子
2楼-- · 2019-07-21 17:36
 精彩回答 2  元偷偷看……
way7539512
3楼-- · 2019-07-21 21:38
谢谢原子哥,现在还是没有成功,求助
way7539512
4楼-- · 2019-07-21 22:25
谢谢原子哥找的连接,正在学习中。
宋宋
5楼-- · 2019-07-22 00:09
原子哥,这个STM32F103C8T6 对接10个IO的灯圈(一个灯圈包含4颗灯珠WS2812)   +  一个灯带 (一个灯带包含70颗灯珠)  + 3路串口同时运行 ,使用3.0的库的话  整个能运行么

一周热门 更多>