[mw_shl_code=c,true]static void TIM3_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/*????DMA?±??*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//NVIC_Config(); //????DMA????
DMA_DeInit(DMA1_Channel2);
/*?è??DMA?????®???????????÷???·*/
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)TIM3_CCR3_Address;
/*???????·(????????±?????????)*/
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)LED_BYTE_Buffer;
/*·??ò?????????????è*/
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
/*?????ó??DMA_BufferSize=SENDBUFF_SIZE*/
DMA_InitStructure.DMA_BufferSize = 42;
/*???è???·????*/
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
/*???????·×???*/
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
/*???è????????*/
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
/*???????????? 8bit*/
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
/*DMA?????????????·*/
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;
//DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
/*??????????*/
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
/*???????????????????? */
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
/*????DMA1??4?¨??*/
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
/*????DMA*/
DMA_Cmd (DMA1_Channel2,ENABLE);
//TIM_DMACmd(TIM3, TIM_DMA_Update, ENABLE);
//DMA_ITConfig(DMA1_Channel2,DMA_IT_TC,ENABLE); //????DMA·????ê???ó?ú?ú????
}
/**
* @brief
* @param ??
* @retval ??
*/
void Timer3_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* GPIOA Configuration: TIM3 Channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Compute the prescaler value */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 29; // 800kHz
TIM_TimeBaseStructure.TIM_Prescaler = 2;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM3_DMA_Config();
/* configure DMA */
/* TIM3 CC1 DMA Request enable */
TIM_DMACmd(TIM3, TIM_DMA_CC3, ENABLE);
}
/* This function sends data bytes out to a string of WS2812s
* The first argument is a pointer to the first RGB triplet to be sent
* The seconds argument is the number of LEDs in the chain
*
* This will result in the RGB triplet passed by argument 1 being sent to
* the LED that is the furthest away from the controller (the point where
* data is injected into the chain)
*/
void WS2812_send(uint8_t (*color)[3], uint16_t len)
{
uint8_t j;
uint8_t led;
uint16_t memaddr;
uint16_t buffersize;
buffersize = (len*24)+42; // number of bytes needed is #LEDs * 24 bytes + 42 trailing bytes
memaddr = 0; // reset buffer memory index
led = 0; // reset led index
// fill transmit buffer with correct compare values to achieve
// correct pulse widths according to color values
while (len)
{
for (j = 0; j < 8; j++) // GREEN data
{
if ( (color[led][1]<<j) & 0x80 ) // data sent MSB first, j = 0 is MSB j = 7 is LSB
{
LED_BYTE_Buffer[memaddr] = 20; // compare value for logical 1
}
else
{
LED_BYTE_Buffer[memaddr] = 6; // compare value for logical 0
}
memaddr++;
}
for (j = 0; j < 8; j++) // RED data
{
if ( (color[led][0]<<j) & 0x80 ) // data sent MSB first, j = 0 is MSB j = 7 is LSB
{
LED_BYTE_Buffer[memaddr] = 20; // compare value for logical 1
}
else
{
LED_BYTE_Buffer[memaddr] = 6; // compare value for logical 0
}
memaddr++;
}
for (j = 0; j < 8; j++) // BLUE data
{
if ( (color[led][2]<<j) & 0x80 ) // data sent MSB first, j = 0 is MSB j = 7 is LSB
{
LED_BYTE_Buffer[memaddr] = 20; // compare value for logical 1
}
else
{
LED_BYTE_Buffer[memaddr] = 6; // compare value for logical 0
}
memaddr++;
}
led++;
len--;
}
// add needed delay at end of byte cycle, pulsewidth = 0
while(memaddr < buffersize)
{
LED_BYTE_Buffer[memaddr] = 0;
memaddr++;
}
//DMA_SetCurrDataCounter(DMA1_Channel6, buffersize); // load number of bytes to be transferred
DMA_Cmd(DMA1_Channel2, ENABLE); // enable DMA channel 6
TIM_Cmd(TIM3, ENABLE); // enable Timer 3
while(!DMA_GetFlagStatus(DMA1_FLAG_TC2)); // wait until transfer complete
TIM_Cmd(TIM3, DISABLE); // disable Timer 3
DMA_Cmd(DMA1_Channel2, DISABLE); /
DMA_ClearFlag(DMA1_FLAG_TC2);
}[/mw_shl_code]
[mw_shl_code=c,true]调了好几天了老是不通,希望原子哥能帮帮忙[/mw_shl_code]
|