本帖最后由 l441867854 于 2019-7-13 13:34 编辑 #define TIM_PERIOD 59 // Number of CPU cycles that will constitute 1 period #define PWM_HIGH_WIDTH 45 // Duty cycle of pwm signal for a logical 1 to be read by the ws2812 chip. //Duty cycle = PWM_HIGH_WIDTH/TIM_PERIOD*100 #define PWM_LOW_WIDTH 10 uint8_t ledBuff[69]={0}; void TIM3_PWM_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; DMA_InitTypeDef DMA_InitStructure; /* 配置相应引脚PB1*/ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_DMA1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1); GPIO_Init(GPIOB, &GPIO_InitStructure); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3时钟 TIM_TimeBaseStructure.TIM_Period = TIM_PERIOD;// 设置自动重装周期值 TIM_TimeBaseStructure.TIM_Prescaler = 0;//设置预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0;//设置时钟分割 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始化定时器3 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;// PWM2模式 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//比较输出使能 TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出高 TIM_OC4Init(TIM3, &TIM_OCInitStructure); TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能预装载寄存器 DMA_DeInit(DMA1_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(TIM3->CCR4); // physical address of Timer 3 CCR4 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ledBuff; // this is the buffer memory DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // data shifted from memory to peripheral DMA_InitStructure.DMA_BufferSize = sizeof(ledBuff); DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // automatically increase buffer index DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // stop DMA feed after buffer size is reached DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel3, &DMA_InitStructure); TIM_DMACmd(TIM3, TIM_DMA_CC4, ENABLE); } void Ws2812_reflash(void) { DMA_SetCurrDataCounter(DMA1_Channel3, sizeof(ledBuff)); DMA_Cmd(DMA1_Channel3,ENABLE); TIM_Cmd(TIM3, ENABLE); while(!DMA_GetFlagStatus(DMA1_FLAG_TC3)); TIM_Cmd(TIM3, DISABLE); DMA_Cmd(DMA1_Channel3,DISABLE); DMA_ClearFlag(DMA1_FLAG_TC3); } void Set2812RGB(uint8_t redcolor,uint8_t greencolor,uint8_t bluecolor) { uint8_t i=0; uint32_t rgb_value=redcolor<<16|greencolor<<8|bluecolor; for(i=0;i<69;i++) { if((rgb_value<<i)&0x800000) { ledBuff=PWM_HIGH_WIDTH; } else { ledBuff=PWM_LOW_WIDTH; } } int main(void) { //delay_init(); TIM3_PWM_Init(); while(1) { Set2812RGB(0,57,0); Ws2812_reflash(); } } [img]file:///C:\Users\Administrator\AppData\Roaming\Tencent\Users\441867853\QQ\WinTemp\RichOle\WEUORX)~(R[ZUVX259~FMXM.png[/img] 测试出来的PWM波形频率只有8K 为什么!!! |
程序问题较多
uint8_t ledBuff[69]={0}; -- 定义是8位数组;
ledBuff=PWM_HIGH_WIDTH; --这样赋值为哪般?ledBuff是指针,不是得给数组的元素赋值吗?赋完值后,指针也要增加的。或者改成ledBuff[i++] = PWM_HIGH_WIDTH;
if((rgb_value<<i)&0x800000) ---这里的 rgb_value<<i 是个什么东西?i的取值可是0到68呀!
既然你的ledBuff是8位的数组,那么DMA传数也得是8位的。
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
是不是应该改成字节呢?定时器是16位的,初始化高8位为0即可,DMA只传输低8位。
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
另外,你对WS2812灯的理解是不是正确呢?这个灯是RGB888,正确点一颗灯,需要发送24个脉冲。依此类推,点2颗灯需要48个脉冲,3灯72个脉冲。而你用的ledBuff[69],69个元素,与谁都不搭边啊。
评分
查看全部评分
1,是前面的数组位数定义错了,改成16位重新测试了一下,正常了。非常感谢
2,i在for里面有增加。本来是24,因为频率出不来,就改了。
3 这个程序只是初次来测试电平时间用的,只点一颗灯, 只是为了在后面加一个低电平复位时间。 非常感谢