Advertisement
Guest User

stm8 dac+dma+tim4

a guest
Oct 23rd, 2012
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. void dmadactim4init(void)
  2. {
  3.     uint8_t tmpreg1=0;
  4.   //DMA style
  5.   CLK_PeripheralClockConfig(CLK_Peripheral_DAC,ENABLE);
  6.   CLK_PeripheralClockConfig(CLK_Peripheral_DMA1,ENABLE);
  7.   CLK_PeripheralClockConfig(CLK_Peripheral_TIM4,ENABLE);
  8.   //DMA Init
  9.   //Reset DMA Channelx control register
  10.   DMA1_Channel3->CCR  = DMA_CCR_RESET_VALUE;
  11.   //Set DMA direction & Mode & Incremantal Memory mode
  12.   DMA1_Channel3->CCR |= ((uint8_t)DMA_DIR_MemoryToPeripheral
  13.     //| (uint8_t)DMA_Mode_Circular
  14.     |(uint8_t) DMA_MemoryIncMode_Inc);
  15.   //Clear old priority and memory data size  option
  16.   DMA1_Channel3->CSPR &= (uint8_t)~(DMA_CSPR_PL | DMA_CSPR_16BM);
  17.   //Set old priority and memory data size  option
  18.   DMA1_Channel3->CSPR |= ((uint8_t)DMA_Priority_VeryHigh |(uint8_t)DMA_MemoryDataSize_HalfWord);
  19.   //Write to DMA Channelx CNDTR
  20.   DMA1_Channel3->CNBTR = 128;
  21.   //--------------------------- DMA Channel3 CPAR Configuration ----------------
  22.   DMA1_Channel3->CPARH = (uint8_t)(DAC_CH1RDHRH_ADDRESS >> (uint8_t)8);
  23.   DMA1_Channel3->CPARL = (uint8_t)(DAC_CH1RDHRH_ADDRESS);
  24.   //--------------------------- DMA Channel3 CMAR Configuration ----------------
  25.   DMA1_Channel3->CM0ARH = (uint8_t)((uint16_t)&(dac_buf[0]) >> (uint8_t)8);
  26.   DMA1_Channel3->CM0ARL = (uint8_t)((uint16_t)&(dac_buf[0]));
  27.   //DMA1 Channel 3 enable
  28.   DMA1_Channel3->CCR |= (uint8_t)(DMA_CCR_CE | DMA_CCR_TCIE | DMA_CCR_HTIE);
  29.   //Enable the  DMA      
  30.   DMA1->GCSR |= (uint8_t)DMA_GCSR_GE;
  31.  
  32.   //DAC Channel1 Config
  33.   //Fill DAC Init param DAC_Trigger_T4_TRGO* and  DAC Channel1 Init
  34.   //DAC CR1 Config
  35.   tmpreg1 = DAC->CH1CR1;
  36.   //Clear TENx, TSELx bits
  37.   tmpreg1 &= (uint8_t)~(DAC_CR1_TEN | DAC_CR1_TSEL );
  38.   //Configure for the selected DAC channel: buffer output, trigger
  39.   //Set BOFFx bit Output Buffer disable
  40.   tmpreg1 |= (uint8_t)(DAC_CR1_BOFF );
  41.   //Set TSELx and TEN  bits according to DAC_Trigger value
  42.   tmpreg1 |= (uint8_t)(DAC_CR1_TEN | DAC_Trigger_T4_TRGO) ;
  43.   //Write to DAC CR1
  44.   DAC->CH1CR1 = tmpreg1;
  45.   //Enable DAC Channel1
  46.   DAC->CH1CR1 |= DAC_CR1_EN;
  47.   DAC->CH1CR2 |= DAC_CR2_DMAEN;
  48.  
  49.   // TIM4 Config
  50.   //Set output signal frequency approx. equal to 22050Hz Range 1
  51.   TIM4->ARR = (uint8_t)(181);
  52.   //Set the Prescaler value
  53.   TIM4->PSCR = (uint8_t)(TIM4_Prescaler_4);
  54.  
  55.   //TIM4 TRGO selection
  56.   tmpreg1 = TIM4->CR2;
  57.   //Reset the MMS Bits
  58.   tmpreg1 &= (uint8_t)(~TIM4_CR2_MMS);
  59.   //Select the TRGO source
  60.   tmpreg1 |=  (uint8_t)TIM4_TRGOSource_Update;
  61.   TIM4->CR2 = tmpreg1;
  62.  
  63.   //TIM4 enable counter
  64.   TIM4->CR1 |= TIM4_CR1_CEN ;
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement