#include "stm32f10x.h"
void DAC_Config(void)
{
DAC_InitTypeDef DAC_InitStructure;/ DAC结构体
GPIO_InitTypeDef GPIO_InitStructure;//GPIO结构体
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定时器结构体
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//DAC时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//使能定时器时钟
/*将GPIO配置为DAC的模拟复用功能*/
//PA4对应DAC通道1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM2配置*/
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period =20-1; //计数器值
TIM_TimeBaseStructure.TIM_Prescaler = 2-1;//预分频值,2分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //采样分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//初始化定时器结构体
/* TIM2设置为溢出触发*/
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/*DAC通道1配置*/
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; //定时器2触发
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle;//设置为产生三角波模式
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_2047; //设置通道1三角波幅值
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;//不使能输出缓冲
DAC_Init(DAC_Channel_1, &DAC_InitStructure);//根据以上参数初始化DAC结构体
/* 使能DAC通道1*/
DAC_Cmd(DAC_Channel_1, ENABLE);
//使能定时器2
TIM_Cmd(TIM2, ENABLE);
}
int main(void)
{
SystemInit(); //STM32系统初始化(包括时钟,倍频,Flash配置)
DAC_Config(); //DAC初始化配置
while(1);
}
书上找的一段代码,用的是STM32F103VET6 ,我下载到STM32F103RET6用示波器测试PA.4为什么没有波形产生。求大神开光。。。。 |