//timer.c file
#include "timer.h"
/* Buffer that holds one complete DMA transmission
*
* Ensure that this buffer is big enough to hold
* all data bytes that need to be sent
*
* The buffer size can be calculated as follows:
* number of LEDs * 24 bytes + 42 bytes
*
* This leaves us with a maximum string length of
* (2^16 bytes per DMA stream - 42 bytes)/24 bytes per LED = 2728 LEDs
*/
#define TIM3_CCR3_Address 0x4000043c // physical memory address of Timer 3 CCR1 register
//#define TIM3_CCR1_Address 0x40000434 // physical memory address of Timer 3 CCR1 register
//#define TIM2_CCR4_Address 0x40000040
#define TIMING_ONE 25
#define TIMING_ZERO 11
uint16_t LED_BYTE_Buffer[300];
uint16_t testcount = 0;
uint16_t testcount1 = 0;
//---------------------------------------------------------------//
void Timer2_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
/* GPIOA Configuration: TIM2 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(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Compute the prescaler value */
//PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 38; // 400kHz
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; // 失能输出比较N状态
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
// 选择空闲状态下的非工作状态
// 当MOE=0重置TIM1输出比较N空闲状态
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; // 互补输出极性
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
//TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
// TIM_ARRPreloadConfig(TIM3, ENABLE);
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
/* configure DMA */
/* DMA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA1 Channel6 Config */
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&TIM2->CCR1); // physical address of Timer 3 CCR1
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LED_BYTE_Buffer; // this is the buffer memory
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; // data shifted from memory to peripheral
DMA_InitStructure.DMA_BufferSize = 42;
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_Channel2, &DMA_InitStructure);
/* TIM3 CC1 DMA Request enable */
TIM_DMACmd(TIM2, TIM_DMA_Update, 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 i;
uint16_t memaddr;
uint16_t buffersize;
buffersize = (len*24)+10; // number of bytes needed is #LEDs * 24 bytes + 42 trailing bytes
memaddr = 0; // reset buffer memory index
while (len)
{
for(i=0; i<8; i++) // GREEN data
{
LED_BYTE_Buffer[memaddr] = ((color[0][1]<© COPYRIGHT 2011 STMicroelectronics
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include
#include "delay.h"
#include "dma.h"
#include "timer.h"
uint8_t r[][3] = {255,255,255};
uint8_t g[][3] = {0,0,0};
uint8_t b[][3] = {0,0,255};
uint8_t cheng[][3] = {255,128,0};
uint8_t huang[][3] = {255,255,0};
uint8_t qing[][3] = {0,255,255};
uint8_t zi[][3] = {128,0,255};
uint32_t testt = (uint32_t)(&TIM3->CCR3);
int main(void)
{
unsigned char i,j;
//RCC_Configuration();
delay_init(); //延时函数初始化
//LED_Init(); //初始化与LED连接的硬件接口
Timer2_init();
WS2812_send(g,7);
delay_ms(30);
while(1)
{
for(i= 0;i<7;i++)
{
for(j= 0;j<7;j++)
{
if(j<=i)
WS2812_send(r,1);
else WS2812_send(g,1);
}
delay_ms(300);
}
i = 0;
j= 0;
WS2812_send(r,7);
delay_ms(3000);
WS2812_send(g,7);
delay_ms(3000);
for(i= 0;i<7;i++)
{
for(j= 7;j>0;j--)
{
if(j>=i)
WS2812_send(r,1);
else WS2812_send(g,1);
}
delay_ms(300);
}
i = 0;
j= 0;
delay_ms(3000);
for(i= 0;i<7;i++)
{
for(j= 0;j<7;j++)
{
if(j<=i)
WS2812_send(g,1);
else WS2812_send(r,1);
}
delay_ms(300);
}
i = 0;
j= 0;
WS2812_send(r,7);
delay_ms(3000);
WS2812_send(g,7);
delay_ms(3000);
for(i= 0;i<7;i++)
{
for(j= 0;j<7;j++)
{
if(j>=i)
WS2812_send(g,1);
else WS2812_send(r,1);
}
delay_ms(300);
}
i = 0;
j= 0;
WS2812_send(g,7);
delay_ms(3000);
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
使用原子开发板io输出