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0xD3, 0x20,
0x2F, 0xE2, 0xD1, 0x1E,
0x32, 0xE4, 0xCE, 0x1C,
0x34, 0xE6, 0xCC, 0x1A,
0x37, 0xE8, 0xC9, 0x18,
0x39, 0xEA, 0xC7, 0x16,
0x3C, 0xEB, 0xC4, 0x15,
0x3F, 0xED, 0xC1, 0x13,
0x41, 0xEF, 0xBF, 0x11,
0x44, 0xF0, 0xBC, 0x10,
0x47, 0xF1, 0xB9, 0x0F,
0x4A, 0xF3, 0xB6, 0x0D,
0x4D, 0xF4, 0xB3, 0x0C,
0x4F, 0xF5, 0xB1, 0x0B,
0x52, 0xF6, 0xAE, 0x0A,
0x55, 0xF8, 0xAB, 0x08,
0x58, 0xF9, 0xA8, 0x07,
0x5B, 0xFA, 0xA5, 0x06,
0x5E, 0xFA, 0xA2, 0x06,
0x61, 0xFB, 0x9F, 0x05,
0x64, 0xFC, 0x9C, 0x04,
0x67, 0xFD, 0x99, 0x03,
0x6A, 0xFD, 0x96, 0x03,
0x6D, 0xFE, 0x93, 0x02,
0x70, 0xFE, 0x90, 0x02,
0x74, 0xFE, 0x8C, 0x02,
0x77, 0xFF, 0x89, 0x01,
0x7A, 0xFF, 0x86, 0x01,
0x7D, 0xFF, 0x83, 0x01
};
void init_tim(void)
{
RCC->AHBENR|=RCC_AHBENR_DMA1EN|RCC_AHBENR_GPIOAEN;
RCC->APB1ENR|=RCC_APB1ENR_TIM2EN;
GPIOA->MODER &=~(GPIO_MODER_MODER0
|GPIO_MODER_MODER1
|GPIO_MODER_MODER2
|GPIO_MODER_MODER3);
GPIOA->MODER |=GPIO_MODER_MODER0_1
|GPIO_MODER_MODER1_1
|GPIO_MODER_MODER2_1
|GPIO_MODER_MODER3_1;
GPIOA->AFR[0] |= (2<<4*0)
|(2<<4*1)
|(2<<4*2)
|(2<<4*3);
TIMx->PSC = 0; /* (1) */
TIMx->ARR = 255; /* (2) */
TIMx->CCR1 = 7;
TIMx->CCR2 = 7; /* (3) */
TIMx->CCR3 = 7;
TIMx->CCR4 = 7;
TIMx->CCMR1 |=
TIM_CCMR1_OC1M_2
| TIM_CCMR1_OC1M_1
| TIM_CCMR1_OC1PE
| TIM_CCMR1_OC2M_2
| TIM_CCMR1_OC2M_1
| TIM_CCMR1_OC2PE; /* (4) */
TIMx->CCMR2 |=
TIM_CCMR2_OC3M_2
| TIM_CCMR2_OC3M_1
| TIM_CCMR2_OC3PE
| TIM_CCMR2_OC4M_2
| TIM_CCMR2_OC4M_1
| TIM_CCMR2_OC4PE;
TIMx->CCER |= TIM_CCER_CC1E
| TIM_CCER_CC2E
| TIM_CCER_CC3E
| TIM_CCER_CC4E; /* (5) */
//TIMx->BDTR |= TIM_BDTR_MOE; /* (6) */
TIMx->CR1 |= TIM_CR1_CMS_0;
TIMx->CR1 |= TIM_CR1_CEN; /* (7) */
TIMx->EGR |= TIM_EGR_UG; /* (8) */
DMA1_Channel2->CPAR = (uint32_t)(&(TIMx->DMAR)); /* (1) */
DMA1_Channel2->CMAR = (uint32_t)( sine_wave); /* (2) */
DMA1_Channel2->CNDTR = 256*4; /* (3) */
DMA1_Channel2->CCR |=
DMA_CCR_CIRC
//| DMA_CCR_MSIZE_0
| DMA_CCR_PSIZE_1
| DMA_CCR_MINC
| DMA_CCR_DIR; /* (4) */
TIMx->DCR = (3 << 8)
+ ((((uint32_t)(&TIMx->CCR1))
- ((uint32_t)(&TIMx->CR1))) >> 2); /* (5) */
TIMx->DIER |= TIM_DIER_UDE; /* (6) */
TIMx->CR1 |= TIM_CR1_CEN; /* (7) */
DMA1_Channel2->CCR |= DMA_CCR_EN; /* (8) */
}