/*************************************************************
Function : Escalatar_GPIO_Init
Description: 阶梯波相关引脚配置
Input : none
return : none
*************************************************************/
static void Escalatar_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //初始化引脚时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; //DAC CH1与CH2对应的引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
#define _800Hz 72000000/6/800 //频率值为800Hz
#define _1600Hz 72000000/6/1600 //频率值为1600Hz
/*************************************************************
Function : Escalatar_TIM_Init
Description: 阶梯波定时器初始化
Input : none
return : none
*************************************************************/
static void Escalatar_TIM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM6, ENABLE);//初始化定时器2与6的时钟
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = _800Hz;//正弦波1频率设置
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;//没有预分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;//时钟不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//增计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = _1600Hz;//正弦波2频率设置
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);//更新TIM2输出触发
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);//更新TIM6输出触发
TIM_Cmd(TIM2, ENABLE);//打开TIM2
TIM_Cmd(TIM6, ENABLE);//打开TIM6
}
/*************************************************************
Function : Escalatar_DAC_Init
Description: 阶梯波DAC初始化
Input : none
return : none
*************************************************************/
static void Escalatar_DAC_Init(void)
{
DAC_InitTypeDef DAC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);//初始化DAC的时钟
DAC_StructInit(&DAC_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;//指定DAC1的触发定时器TIM2
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;//无波形产生
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;//不是能DAC输出缓冲
DAC_Init(DAC_Channel_1, &DAC_InitStructure);//初始化DAC channel1
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;//指定DAC2的触发定时器TIM6
DAC_Init(DAC_Channel_2, &DAC_InitStructure);//初始化DAC channel2
DAC_Cmd(DAC_Channel_1, ENABLE);//使能DAC channel1
DAC_Cmd(DAC_Channel_2, ENABLE);//使能DAC channel2
DAC_DMACmd(DAC_Channel_1, ENABLE);//使能DAC Channel1的DMA
DAC_DMACmd(DAC_Channel_2, ENABLE);//使能DAC Channel2的DMA
}
#define DAC_DHR8R1_Address 0x40007410//DAC通道1的8位右对齐数据保持寄存器地址
#define DAC_DHR8R2_Address 0x4000741C//DAC童道2的8位有对齐数据保持寄存器地址
const uint8_t Escalator8bit[6] = {0x0, 0x33, 0x66, 0x99, 0xCC, 0xFF};//阶梯形描点
/*************************************************************
Function : Escalatar_DMA_Init
Description: 阶梯波DAM初始化
Input : none
return : none
*************************************************************/
static void Escalatar_DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);//初始化DMA2的时钟
DMA_DeInit(DMA2_Channel3);//将DMA配置成默认值
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R1_Address;//指定DMA2通道3的目标地址为DAC1_DHR12R1
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Escalator8bit;//指定DMA的源地址为数组Escalator8bit
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//外设作为数据传输的目的地
DMA_InitStructure.DMA_BufferSize = 6;//DMA缓冲区大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设机地址存器不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//内存地址寄存器递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//外设数据宽度为半字
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //内存数据宽度为半字
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//工作在循环缓存模式,数据传输数为0时,自动恢复配置初值
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//非常高优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//通道未被设置成内存到内存模式,与循环模式相对
DMA_Init(DMA2_Channel3, &DMA_InitStructure);//初始化DMA
DMA_DeInit(DMA2_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR8R2_Address;//指定DMA2通道3的目标地址为DAC2_DHR12R2
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_Init(DMA2_Channel4, &DMA_InitStructure);
DMA_Cmd(DMA2_Channel3, ENABLE);//使能DMA的channel3
DMA_Cmd(DMA2_Channel4, ENABLE);//使能DMA的channel4
}
/*************************************************************
Function : Escalatar_Init
Description: 阶梯波初始化
Input : none
return : none
*************************************************************/
void Escalatar_Init(void)
{
Escalatar_GPIO_Init();
Escalatar_TIM_Init();
Escalatar_DAC_Init();
Escalatar_DMA_Init();
}
#ifndef __ESCALATOR_H__
#define __ESCALATOR_H__
#include "stm32f10x.h"
void Escalatar_Init(void);
#endif
/*************************************************************
Function : main
Description: main入口
Input : none
return : none
*************************************************************/
int main(void)
{
BSP_Init();
Escalatar_Init();
PRINTF("\nmain() is running!\r\n");
while(1)
{
LED1_Toggle();
Delay_ms(1000);
}
}
上一篇:STM32 三角波输出
下一篇:STM32 六步PWM输出
推荐阅读最新更新时间:2024-03-16 15:14