我现在遇到的问题是怎么PA4的电压时3.3v而PA5的电压时ov啊
#include "stm32f10x.h"
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void DAC_Configuration(void);
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
DAC_Configuration();
while (1)
{
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
//设置系统时钟到72Mhz
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */ // 外部高速时钟使能
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ //ErrorStatus HSEStartUpStatus;
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if (HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{}
}
else
{
/* If HSE fails to start-up, the application will have wrong clock configuration.
User can add here some code to deal with this error */
/* Go to infinite loop */
while (1)
{}
}
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
/* DAC Periph clock enable */
}
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //确定中断向量放在RAM或者FLASH
#endif
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOF, ENABLE);
/*set the GPIO of DAC*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //
GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //
GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
}
void DAC_Configuration(void)
{
DAC_InitTypeDef DAC_Struct;
RCC_APB1PeriphClockCmd( RCC_APB1Periph_DAC, ENABLE);
/* DAC channel1 Configuration */
DAC_DeInit();
DAC_StructInit(&DAC_Struct);//将DAC_Struct的值设为默认
DAC_Struct.DAC_Trigger=DAC_Trigger_Software;//由软件触发
DAC_Struct.DAC_WaveGeneration=DAC_WaveGeneration_None;//关闭波形生成
DAC_Struct.DAC_LFSRUnmask_TriangleAmplitude=DAC_TriangleAmplitude_4095;
DAC_Struct.DAC_OutputBuffer=DAC_OutputBuffer_Enable;//使能DAC通道缓存
DAC_Init(DAC_Channel_1,&DAC_Struct);
DAC_Init(DAC_Channel_2,&DAC_Struct);
/* Enable DAC Channel1 */
DAC_Cmd(DAC_Channel_1,ENABLE); //
DAC_Cmd(DAC_Channel_2, ENABLE);
DAC_Cmd(DAC_Channel_1, ENABLE);
/* Enable DAC Channel2 */
DAC_Cmd(DAC_Channel_2, ENABLE);
DAC_SetChannel1Data(DAC_Align_12b_R,0x7ff);//12位右对齐,输出一半的基准电压
DAC_SetChannel2Data(DAC_Align_12b_R,0x7ff);//输出一半的基准电压
DAC_DualSoftwareTriggerCmd(ENABLE);//使能软件触发,更新DAC的值
}
|