使用c语言编写基于STM32F103ZET6芯片的双正弦信号(使用DAC,DMA,定时器,且PE2和PE3按键控制TIM2定时器的周期值增大和减小(500)
时间: 2023-06-25 21:04:20 浏览: 160
以下是基于STM32F103ZET6芯片的C语言代码,实现双正弦信号的输出和按键控制TIM2定时器周期值的增大和减小:
```c
#include "stm32f10x.h"
#define DAC_DHR12R2_ADDRESS 0x40007414
uint16_t sinTable[256] = {
2047, 2097, 2147, 2197, 2247, 2297, 2347, 2397,
2447, 2496, 2546, 2595, 2644, 2693, 2741, 2790,
2838, 2886, 2933, 2981, 3028, 3074, 3121, 3167,
3212, 3257, 3302, 3347, 3391, 3435, 3478, 3521,
3563, 3605, 3647, 3688, 3729, 3769, 3809, 3849,
3888, 3927, 3965, 4003, 4040, 4077, 4114, 4150,
4185, 4220, 4254, 4288, 4321, 4354, 4386, 4418,
4449, 4480, 4510, 4540, 4569, 4598, 4626, 4654,
4681, 4708, 4734, 4759, 4784, 4809, 4832, 4856,
4878, 4900, 4922, 4942, 4963, 4982, 5001, 5020,
5038, 5055, 5072, 5088, 5104, 5119, 5134, 5148,
5161, 5174, 5186, 5198, 5209, 5220, 5230, 5240,
5249, 5258, 5266, 5274, 5281, 5288, 5294, 5300,
5305, 5310, 5315, 5319, 5323, 5326, 5329, 5331,
5333, 5335, 5336, 5337, 5337, 5337, 5337, 5336,
5335, 5333, 5331, 5329, 5326, 5323, 5319, 5315,
5310, 5305, 5300, 5294, 5288, 5281, 5274, 5266,
5258, 5249, 5240, 5230, 5220, 5209, 5198, 5186,
5174, 5161, 5148, 5134, 5119, 5104, 5088, 5072,
5055, 5038, 5020, 5001, 4982, 4963, 4942, 4922,
4900, 4878, 4856, 4832, 4809, 4784, 4759, 4734,
4708, 4681, 4654, 4626, 4598, 4569, 4540, 4510,
4480, 4449, 4418, 4386, 4354, 4321, 4288, 4254,
4220, 4185, 4150, 4114, 4077, 4040, 4003, 3965,
3927, 3888, 3849, 3809, 3769, 3729, 3688, 3647,
3605, 3563, 3521, 3478, 3435, 3391, 3347, 3302,
3257, 3212, 3167, 3121, 3074, 3028, 2981, 2933,
2886, 2838, 2790, 2741, 2693, 2644, 2595, 2546,
2496, 2447, 2397, 2347, 2297, 2247, 2197, 2147,
2097, 2047, 1996, 1946, 1896, 1846, 1796, 1746,
1696, 1647, 1597, 1548, 1499, 1450, 1401, 1353,
1304, 1256, 1208, 1160, 1113, 1065, 1018, 972,
925, 879, 833, 787, 742, 697, 652, 607,
563, 519, 475, 432, 389, 346, 304, 262,
221, 180, 140, 100, 61, 22, -17, -55,
-92, -129, -165, -201, -236, -270, -304, -337,
-369, -401, -431, -461, -490, -518, -545, -572,
-597, -622, -645, -668, -689, -710, -729, -748,
-765, -782, -797, -812, -825, -837, -848, -858,
-867, -875, -881, -887, -892, -896, -899, -901,
-902, -902, -901, -899, -896, -892, -887, -881,
-875, -867, -858, -848, -837, -825, -812, -797,
-782, -765, -748, -729, -710, -689, -668, -645,
-622, -597, -572, -545, -518, -490, -461, -431,
-401, -369, -337, -304, -270, -236, -201, -165,
-129, -92, -55, -17, 22, 61, 100, 140,
180, 221, 262, 304, 346, 389, 432, 475,
519, 563, 607, 652, 697, 742, 787, 833,
879, 925, 972, 1018, 1065, 1113, 1160, 1208,
1256, 1304, 1353, 1401, 1450, 1499, 1548, 1597,
1647, 1696, 1746, 1796, 1846, 1896, 1946, 1996
};
volatile uint32_t TIM2_period = 500;
void TIM2_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = TIM2_period;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void DAC_Config(void)
{
DAC_InitTypeDef DAC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_2, ENABLE);
}
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)sinTable;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 256;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel3, ENABLE);
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOE, &GPIO_InitStructure);
TIM2_Config();
DAC_Config();
DMA_Config();
while(1)
{
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2) == RESET && TIM2_period < 1000)
{
TIM2_period += 10;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = TIM2_period;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
}
if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3) == RESET && TIM2_period > 10)
{
TIM2_period -= 10;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = TIM2_period;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
}
}
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
DMA_Cmd(DMA1_Channel3, DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel3, 256);
DMA_Cmd(DMA1_Channel3, ENABLE);
}
}
```
该代码使用DAC和DMA输出正弦波,定时器TIM2控制DMA传输的周期。按键PE2和PE3分别控制TIM2的周期值增大和减小,以改变正弦波的频率。
阅读全文