2019 8 8 STM32F407DAC DMA通道生成周期1msSIN波(1um一个点)相关配置
还没有生成COS,需要另一个定时器进行触发,注释代码不能用
1 #include "STM32_WaveOutput.h" 2 #include "delay.h" 3 /********Éú³ÉÕýÏÒ²¨ÐÎ1Êä³ö±í***********/ 4 5 u16 SineWave_Value[1000]; 6 //u16 CosWave_Value[1000]; 7 8 void SineWave_Data() 9 { 10 //void sin_Generation(void) 11 //{ 12 u16 n; 13 for(n=0;n<1000;n++) 14 { 15 // SineWave_Value[n] = (sin(2*PI*n/1000)+1)*2047; 16 // SineWave_Value[n] = n*2; 17 SineWave_Value[n] = (u16)((Um*sin(( 1.0*n/(1000))*2*PI)+Um)*4095/3.3); 18 // CosWave_Value[n] = (u16)((Um*sin(( 1.0*n/(1000))*2*PI+PI/2)+Um)*4095/3.3); 19 } 20 //} 21 } 22 23 //void gpio_init() //PA4 24 //{ 25 // GPIO_InitTypeDef GPIO_InitStructure; 26 // 27 // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ʹÄÜGPIOAʱÖÓ 28 //// RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//ʹÄÜDACʱÖÓ 29 // 30 // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; 31 // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//Ä£Äâ 32 // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//ÏÂÀ 33 //// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 34 // GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯ 35 //} 36 37 void tim2_init() 38 { 39 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; 40 41 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); ///ʹÄÜTIM3ʱÖÓ 42 43 TIM_TimeBaseInitStructure.TIM_Period = 84; //×Ô¶¯ÖØ×°ÔØÖµ 44 TIM_TimeBaseInitStructure.TIM_Prescaler=0; //¶¨Ê±Æ÷·ÖƵ 45 TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ 46 TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 47 48 TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);//³õʼ»¯TIM3 49 50 TIM2->CR2 &= (u16)~((u16)0x0070);//ÉèÖÃTIM2Êä³ö´¥·¢Îª¸üÐÂģʽ 51 TIM2->CR2 |=0x0020;//触发更新模式 52 // TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update); 53 54 TIM_Cmd(TIM2,ENABLE); //ʹÄܶ¨Ê±Æ÷2 55 // 56 // NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //¶¨Ê±Æ÷3ÖÐ¶Ï 57 // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //ÇÀÕ¼ÓÅÏȼ¶1 58 // NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01; //×ÓÓÅÏȼ¶1 59 // NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; 60 // NVIC_Init(&NVIC_InitStructure); 61 } 62 63 void dma_init() 64 { 65 DMA_InitTypeDef DMA_InitStructure; 66 // RCC->AHBENR|=1<<1; //¿ªÆôDMA2ʱÖÓ 67 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1ʱÖÓʹÄÜ 68 delay_ms(5); //µÈ´ýDMAʱÖÓÎȶ¨ 69 70 DMA_DeInit(DMA1_Stream5); 71 // DMA_DeInit(DMA1_Stream6); 72 DMA_Cmd(DMA1_Stream5, DISABLE); 73 // 74 // while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//µÈ´ýDMA¿ÉÅäÖà 75 76 /* ÅäÖà DMA Stream */ 77 DMA_InitStructure.DMA_Channel = DMA_Channel_7; //通道选择 78 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&(DAC->DHR12R1);//DMA外设地址 79 DMA_InitStructure.DMA_Memory0BaseAddr = (u32)SineWave_Value;//DMA内存地址 80 DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//方向 81 DMA_InitStructure.DMA_BufferSize = 1000;//数据量 82 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量 83 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//内存增量 84 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外设数据长度:16 85 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//存储器数据长度:16 86 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式 87 DMA_InitStructure.DMA_Priority = DMA_Priority_High;//高优先级 88 // DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; 89 // DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; 90 // DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//´æ´¢Æ÷Í»·¢µ¥´Î´«Êä 91 // DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//ÍâÉèÍ»·¢µ¥´Î´«Êä 92 DMA_Init(DMA1_Stream5, &DMA_InitStructure);// 93 94 // DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&(DAC->DHR12R2);//DMAÍâÉèµØÖ· 95 // DMA_InitStructure.DMA_Memory0BaseAddr = (u32)CosWave_Value;//DMA ´æ´¢Æ÷0µØÖ· 96 // DMA_Init(DMA1_Stream6, &DMA_InitStructure);//³õʼ»¯DMA Stream 97 98 } 99 100 void dAC_Init() 101 { 102 103 GPIO_InitTypeDef GPIO_InitStructure; 104 DAC_InitTypeDef DAC_InitType; 105 106 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ʹÄÜGPIOAʱÖÓ 107 RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//ʹÄÜDACʱÖÓ 108 109 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; 110 // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5; 111 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//Ä£ÄâÊäÈë 112 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//ÏÂÀ 113 GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯ 114 115 DAC_InitType.DAC_Trigger=DAC_Trigger_T2_TRGO; //定时器2进行触发 116 DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;//不使用波形发生 117 DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;// 118 DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ; //DAC1输出缓存关闭BOFF1=1 119 120 DAC_Init(DAC_Channel_1,&DAC_InitType); //³õʼ»¯DACͨµÀ1 121 // DAC_Init(DAC_Channel_2,&DAC_InitType); //³õʼ»¯DACͨµÀ2 122 123 124 125 // DAC_InitTypeDef DAC_InitStructure; 126 // RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); 127 // 128 // DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; 129 // DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; 130 // DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; 131 //// DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude 132 // DAC_Init(DAC_Channel_1, &DAC_InitStructure); 133 // 134 // DAC_Cmd(DAC_Channel_1, ENABLE); 135 // DAC_DMACmd(DAC_Channel_1, ENABLE); 136 } 137 138 139 void SineWave_Init() 140 { 141 SineWave_Data(); 142 // gpio_init(); 143 tim2_init(); 144 dma_init(); 145 dAC_Init(); 146 147 DAC_Cmd(DAC_Channel_1, ENABLE); //ʹÄÜDACͨµÀ1 148 // DAC_Cmd(DAC_Channel_2, ENABLE); //ʹÄÜDACͨµÀ1 149 DAC_DMACmd(DAC_Channel_1, ENABLE); 150 // DAC_DMACmd(DAC_Channel_2, ENABLE); 151 DMA_Cmd(DMA1_Stream5, ENABLE); 152 // DMA_Cmd(DMA1_Stream6, ENABLE); 153 } 154 155 156 //#define DAC_DHR12R1_ADDRESS (u32)(DAC->DHR12R1) 0x40007408 157 158 159 //#define DAC_DHR12R1_ADDRESS (u32)(DAC->DHR12R1) 160 161 //u16 sinTable[tableSize]; 162 163 //void sin_Generation(void) 164 //{ 165 // u16 n; 166 // for(n=0;n<tableSize;n++) 167 // { 168 // sinTable[n] = (sin(2*PI*n/tableSize)+1)*2047; 169 // } 170 //} 171 172 //void TIM6_Configuration(void) 173 //{ 174 // RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); 175 176 // TIM_PrescalerConfig(TIM6, 0x0, TIM_PSCReloadMode_Update); 177 // TIM_SetAutoreload(TIM6, 0x1); 178 // TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); 179 // TIM_Cmd(TIM6, ENABLE); 180 //} 181 182 //void GPIO_Configuration(void) 183 //{ 184 // GPIO_InitTypeDef GPIO_InitStructure; 185 186 // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 187 188 // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; 189 // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; 190 // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 191 // GPIO_Init(GPIOA, &GPIO_InitStructure); 192 //} 193 194 //void DAC_DMA_Configuration(void) 195 //{ 196 // DAC_InitTypeDef DAC_InitStructure; 197 198 // DMA_InitTypeDef DMA_InitStructure; 199 200 // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); 201 // 202 // RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); 203 204 // DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO; 205 // DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; 206 // DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; 207 // DAC_Init(DAC_Channel_1, &DAC_InitStructure); 208 209 // DMA_DeInit(DMA1_Stream5); 210 // DMA_InitStructure.DMA_Channel = DMA_Channel_7; 211 // DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_ADDRESS; 212 // DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&sinTable; 213 // DMA_InitStructure.DMA_BufferSize = tableSize; 214 // DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; 215 // DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord; 216 // DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; 217 // DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 218 // DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 219 // DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 220 // DMA_InitStructure.DMA_Priority = DMA_Priority_High; 221 // DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; 222 // DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; 223 // DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; 224 // DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; 225 // DMA_Init(DMA1_Stream5, &DMA_InitStructure); 226 // 227 // DMA_Cmd(DMA1_Stream5, ENABLE); 228 // 229 // DAC_Cmd(DAC_Channel_1, ENABLE); 230 // 231 // DAC_DMACmd(DAC_Channel_1, ENABLE); 232 //}