cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F100C8T6 can't generate sinewave of correct frequency

cTomm
Associate II

The chip I am using is STM32F100C8T6.  

I want to generate a sine wave with 40Khz between 0 to 2V ,but I cant't get correct frequency. 

I have finished reading STM32F100 datasheet (RM0041) and AN3126,AN4566. After that,I am writing a program based on the files. what mistake did i make?

1.​AN3126 sinewave equation:

fsineware = 40Khz ,ns=20 ftimertrgo = 40K*20 =800Khz

0690X000006DFk6QAG.png

2. ftimertrgo equation:

Ttimertrgo= (1/ftimertrgo) = (TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK

TIMxCLK =24Mhz, Set TIM_Period=9, get TIM_Prescaler=2:

3.Oscilloscope 13.33Khz the frequency is wrong 0690X000006DFkBQAW.jpg

4.code

void Rcc_init(void)

{

RCC_DeInit();

RCC_HSICmd(ENABLE);

if(RCC_WaitForHSEStartUp()==SUCCESS)

{

RCC_PREDIV1Config(RCC_PREDIV1_Source_HSE, RCC_PREDIV1_Div1);

RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_6);

RCC_PLLCmd(ENABLE);

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET){}

RCC_HCLKConfig(RCC_SYSCLK_Div1);//24M

RCC_PCLK1Config(RCC_HCLK_Div1); //24M

RCC_PCLK2Config(RCC_HCLK_Div1); //24M

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

FLASH_SetLatency(FLASH_Latency_2);

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

while(RCC_GetSYSCLKSource()!=0x08){}

}

else

{

}

}

/*sine table*/

uint16_t sine_20p[] ={

 1242,1644,2003,2280,2444,2478,2377,2154,1832,1445,

 1036, 650, 328, 104,  4, 38, 202, 478, 838,1241

};

void DAC_init(void)

{

DAC_InitTypeDef DAC_InitStruct;

GPIO_InitTypeDef GPIO_InitStruct;

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStruct);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC,ENABLE);

DAC_DeInit();

DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095;

DAC_InitStruct.DAC_OutputBuffer = DAC_OutputBuffer_Disable;

DAC_InitStruct.DAC_Trigger = DAC_Trigger_T6_TRGO;

DAC_InitStruct.DAC_WaveGeneration = DAC_WaveGeneration_None;

DAC_Init(DAC_Channel_1, &DAC_InitStruct);

DAC_SetChannel1Data(DAC_Align_12b_R,0);

DAC_DMACmd(DAC_Channel_1,ENABLE);

DAC_ITConfig(DAC_Channel_1,DAC_IT_DMAUDR,ENABLE); 

NVIC_Configuration_DAC();

}

void DAC_DMA_Wave(void)

{

DMA_InitTypeDef DMA_InitStruct;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);

DMA_InitStruct.DMA_BufferSize = 20;

DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;

DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;

DMA_InitStruct.DMA_MemoryBaseAddr = ((uint32_t)sine_20p);

DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;

DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;

DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)(&(DAC->DHR12R1));

DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord ;

DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh;

DMA_Init(DMA1_Channel3,&DMA_InitStruct);

DMA_Cmd(DMA1_Channel3,ENABLE);

}

void NVIC_Configuration_DAC(void)

{

NVIC_InitTypeDef NVIC_InitStruct;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 

NVIC_InitStruct.NVIC_IRQChannel = TIM6_DAC_IRQn;

NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;

NVIC_Init(&NVIC_InitStruct);

}

void TIM6_TRGO_Init(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);

TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);

TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInitStruct.TIM_Period = 9;

TIM_TimeBaseInitStruct.TIM_Prescaler =2;

TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStruct);

TIM_SelectOutputTrigger(TIM6,TIM_TRGOSource_Update);

TIM_ClearFlag(TIM6,TIM_FLAG_Update);

TIM_ClearITPendingBit(TIM1,TIM_IT_Update);

TIM_Cmd(TIM6,ENABLE);

}

void TIM6_DAC_IRQHandler(void)

{

volatile int i=0;

if(DAC_GetFlagStatus(DAC_Channel_1,DAC_FLAG_DMAUDR) ==SET)

{

}

DAC_ClearITPendingBit(DAC_Channel_1,DAC_IT_DMAUDR);

}

1 ACCEPTED SOLUTION

Accepted Solutions

Don't wait for HSE startup when using HSI. HSI clock should already be running.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

3 REPLIES 3

Don't wait for HSE startup when using HSI. HSI clock should already be running.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
cTomm
Associate II

Thanks, I get it . Thanks for your help, you do gave me a big favor. 

​result:

0690X000006DFkfQAG.png

fix code:

void Rcc_init(void)

{

RCC_DeInit();

RCC_HSICmd(ENABLE);

//if(RCC_WaitForHSEStartUp()==SUCCESS)

{

//RCC_PREDIV1Config(RCC_PREDIV1_Source_HSE, RCC_PREDIV1_Div1);

RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_6);

RCC_PLLCmd(ENABLE);

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET){}

RCC_HCLKConfig(RCC_SYSCLK_Div1);//24M

RCC_PCLK1Config(RCC_HCLK_Div1); //24M

RCC_PCLK2Config(RCC_HCLK_Div1); //24M

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

FLASH_SetLatency(FLASH_Latency_2);

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

while(RCC_GetSYSCLKSource()!=0x08){}

}

// else

// {

//

// }

}

Thanks, I get it . Thanks for your help, you do gave me a big favor. 

result:0690X000006DFkfQAG.png

fix code:

void Rcc_init(void)

{

RCC_DeInit();

RCC_HSICmd(ENABLE);

//if(RCC_WaitForHSEStartUp()==SUCCESS)

{

//RCC_PREDIV1Config(RCC_PREDIV1_Source_HSE, RCC_PREDIV1_Div1);

RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_6);

RCC_PLLCmd(ENABLE);

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET){}

RCC_HCLKConfig(RCC_SYSCLK_Div1);//24M

RCC_PCLK1Config(RCC_HCLK_Div1); //24M

RCC_PCLK2Config(RCC_HCLK_Div1); //24M

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

FLASH_SetLatency(FLASH_Latency_2);

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

while(RCC_GetSYSCLKSource()!=0x08){}

}

// else

// {

//

// }

}