正弦波发生 频率可调.docx

上传人:b****6 文档编号:12413368 上传时间:2023-06-05 格式:DOCX 页数:11 大小:16.46KB
下载 相关 举报
正弦波发生 频率可调.docx_第1页
第1页 / 共11页
正弦波发生 频率可调.docx_第2页
第2页 / 共11页
正弦波发生 频率可调.docx_第3页
第3页 / 共11页
正弦波发生 频率可调.docx_第4页
第4页 / 共11页
正弦波发生 频率可调.docx_第5页
第5页 / 共11页
正弦波发生 频率可调.docx_第6页
第6页 / 共11页
正弦波发生 频率可调.docx_第7页
第7页 / 共11页
正弦波发生 频率可调.docx_第8页
第8页 / 共11页
正弦波发生 频率可调.docx_第9页
第9页 / 共11页
正弦波发生 频率可调.docx_第10页
第10页 / 共11页
正弦波发生 频率可调.docx_第11页
第11页 / 共11页
亲,该文档总共11页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

正弦波发生 频率可调.docx

《正弦波发生 频率可调.docx》由会员分享,可在线阅读,更多相关《正弦波发生 频率可调.docx(11页珍藏版)》请在冰点文库上搜索。

正弦波发生 频率可调.docx

正弦波发生频率可调

正弦波发生频率可调

/********************(C)COPYRIGHT2019STMicroelectronics********************/

/*Includes------------------------------------------------------------------*/

#include"stm32f10x_lib.h"

#include"math.h"

#include"platform_config.h"

/*Privatetypedef-----------------------------------------------------------*/

/*Privatedefine------------------------------------------------------------*/

#defineDAC_DHR12RD_Address0x40007420

#definepi

3.[***********][***********][***********][***********][***********]3421170679

/*InitStructuredefinition*/

DAC_InitTypeDefDAC_InitStructure;

DMA_InitTypeDefDMA_InitStructure;

TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;

GPIO_InitTypeDefGPIO_InitStructure;

/*Privatemacro-------------------------------------------------------------*/

/*Privatevariables---------------------------------------------------------*/

ErrorStatusHSEStartUpStatus;

/*

uc16Sine12bit[32]={2047,2447,2831,3185,3498,3750,3939,4056,4095,4056,

3939,3750,3495,3185,2831,2447,2047,1647,1263,909,599,344,155,38,0,38,155,344,599,909,1263,1647};*/u32DualSine12bit[32];

u32sina[32];

u32sinb[32];

intfre=19;

u8Idx=0;

u8i;

/*Privatefunctionprototypes-----------------------------------------------*/

voidRCC_Configuration(void);

voidGPIO_Configuration(void);

voidNVIC_Configuration(void);

voidDelay(vu32nCount);

/*Privatefunctions---------------------------------------------------------*/

/*******************************************************************************

*FunctionName:

main

*Description:

Mainprogram.

*Input:

None

*Output:

None

*Return:

None

*******************************************************************************/

intmain(void)

{

#ifdefDEBUG

debug();

#endif

/*SystemClocksConfiguration*/

RCC_Configuration();

/*GPIOconfiguration*/

GPIO_Configuration();

/*NVICConfiguration*/

NVIC_Configuration();

/*TIM8Configuration*/

//dataformat

/*for(Idx=0;Idx

{

DualSine12bit[Idx]=(Sine12bit[Idx]

}*///16

while

(1)

{if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0)//如果PA0=0

{

/*Timebaseconfiguration*/

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

TIM_TimeBaseStructure.TIM_Period=fre;//数值增大1,频率减小0.1TIM_TimeBaseStructure.TIM_Prescaler=fre;

TIM_TimeBaseStructure.TIM_ClockDivision=0x0;

TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0);}if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)==0)//如果PA8=0{}while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)==0);if(fre2){fre=fre-2;}for(i=0;i

TIM_TimeBaseInit(TIM8,&TIM_TimeBaseStructure);

/*TIM8TRGOselection*/

TIM_SelectOutputTrigger(TIM8,TIM_TRGOSource_Update);

/*DACchannel1Configuration*/

DAC_StructInit(&DAC_InitStructure);

DAC_InitStructure.DAC_Trigger=DAC_Trigger_T8_TRGO;

DAC_InitStructure.DAC_WaveGeneration=DAC_WaveGeneration_None;

DAC_InitStructure.DAC_OutputBuffer=DAC_OutputBuffer_Disable;

DAC_Init(DAC_Channel_1,&DAC_InitStructure);

/*DACchannel2Configuration*/

DAC_Init(DAC_Channel_2,&DAC_InitStructure);

/*FillSine32bittable*/

/*DMA2channel4configuration*/

DMA_DeInit(DMA2_Channel4);//DAC通道2

DMA_InitStructure.DMA_PeripheralBaseAddr=DAC_DHR12RD_Address;

DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&sina;//DualSine12bit

DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;

DMA_InitStructure.DMA_BufferSize=32;

DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;

DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;

DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Word;

DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Word;

DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;

DMA_InitStructure.DMA_Priority=DMA_Priority_High;

DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;

DMA_Init(DMA2_Channel4,&DMA_InitStructure);

/*EnableDMA2Channel4*/

DMA_Cmd(DMA2_Channel4,ENABLE);

DMA_Cmd(DMA2_Channel3,ENABLE);

/*EnableDACChannel1*/

DAC_Cmd(DAC_Channel_1,ENABLE);

/*EnableDACChannel2*/

DAC_Cmd(DAC_Channel_2,ENABLE);

/*EnableDMAforDACChannel2*/

DAC_DMACmd(DAC_Channel_2,ENABLE);sina

/*TIM8enablecounter*/

TIM_Cmd(TIM8,ENABLE);

}

}

/*******************************************************************************

*FunctionName:

RCC_Configuration

*Description:

Configuresthedifferentsystemclocks.

*Input:

None

*Output:

None

*Return:

None

*******************************************************************************/

voidRCC_Configuration(void)

{

/*RCCsystemreset(fordebugpurpose)*/

RCC_DeInit();

/*EnableHSE*/

RCC_HSEConfig(RCC_HSE_ON);

/*WaittillHSEisready*/

HSEStartUpStatus=RCC_WaitForHSEStartUp();

if(HSEStartUpStatus==SUCCESS)

{

/*EnablePrefetchBuffer*/

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/*Flash2waitstate*/

FLASH_SetLatency(FLASH_Latency_2);

/*HCLK=SYSCLK*/

RCC_HCLKConfig(RCC_SYSCLK_Div1);

/*PCLK2=HCLK*/RCC_PCLK2Config(RCC_HCLK_Div1);/*PCLK1=HCLK/2*/RCC_PCLK1Config(RCC_HCLK_Div2);

/*PLLCLK=8MHz*9=72MHz*/

RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

/*EnablePLL*/

RCC_PLLCmd(ENABLE);

/*WaittillPLLisready*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|

RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOF,ENABLE);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_8;//PA0PA8设置为输入GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA,&GPIO_InitStructure);

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)

{

}

/*SelectPLLassystemclocksource*/

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/*WaittillPLLisusedassystemclocksource*/

while(RCC_GetSYSCLKSource()!

=0x08)

{

}

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx,ENABLE);

}

/*Enableperipheralclocks--------------------------------------------------*/

/*DMAclockenable*/

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2,ENABLE);

/*AFIOandGPIOAPeriphclockenable*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA,ENABLE);

/*DACPeriphclockenable*/

RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC,ENABLE);

/*TIM8Periphclockenable*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);

}

/*******************************************************************************

*FunctionName:

GPIO_Configuration

*Description:

ConfiguresthedifferentGPIOports.

*Input:

None

*Output:

None

*Return:

None

*******************************************************************************/

voidGPIO_Configuration(void)

{

GPIO_InitTypeDefGPIO_InitStructure;

//#ifdefinedUSE_USART2&&definedUSE_STM3210E_EVAL

/*EnableAFIOclock*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

/*EnabletheUSART2PinsSoftwareRemapping*/

//GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);

//#endif

/*ConfigureUSARTx_Txasalternatefunctionpush-pull*/

GPIO_InitStructure.GPIO_Pin=GPIO_TxPin;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

GPIO_Init(GPIOx,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

GPIO_Init(GPIOx,&GPIO_InitStructure);

/*ConfigureUSARTx_Rxasinputfloating*/

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOx,&GPIO_InitStructure);

/*GPIO_InitTypeDefGPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

GPIO_Init(GPIOA,&GPIO_InitStructure);*/

}

/*******************************************************************************

*FunctionName:

NVIC_Configuration

*Description:

ConfiguresVectorTablebaselocation.

*Input:

None

*Output:

None

*Return:

None

*******************************************************************************/

voidNVIC_Configuration(void)

{

#ifdefVECT_TAB_RAM

/*SettheVectorTablebaselocationat0x20000000*/

NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);

#else/*VECT_TAB_FLASH*/

/*SettheVectorTablebaselocationat0x08000000*/

NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);

#endif

}

/*******************************************************************************

*FunctionName:

Delay

*Description:

Insertsadelaytime.

*Input:

nCount:

specifiesthedelaytimelength.

*Output:

None

*Return:

None

*******************************************************************************/

voidDelay(vu32nCount)

{

for(;nCount!

=0;nCount--);

}

#ifdefDEBUG

/*******************************************************************************

*FunctionName:

assert_failed

*Description:

Reportsthenameofthesourcefileandthesourcelinenumber

*wheretheassert_paramerrorhasoccurred.

*Input:

-file:

pointertothesourcefilename

*-line:

assert_paramerrorlinesourcenumber

*Output:

None

*Return:

None

*******************************************************************************/

voidassert_failed(u8*file,u32line)

{

/*Usercanaddhisownimplementationtoreportthefilenameandlinenumber,

ex:

printf("Wrongparametersvalue:

file%sonline%d\r\n",file,line)*/

/*Infiniteloop*/

while

(1)

{

}

}

#endif

/*******************(C)COPYRIGHT2019STMicroelectronics*****ENDOFFILE****/

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2