前段时间在学习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;
}
//¿ªÆôDMA´«Êä
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³õʼ»¯Òý½Å£¬¶¨Ê±Æ÷(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;
}
//¿ªÆôDMA´«Êä
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]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>