stm32f103c8t6DAC正弦波代码
时间: 2023-04-06 16:00:37 浏览: 179
stm32的DAC输出正弦波.doc
5星 · 资源好评率100%
以下是一个基于STM32F103C8T6的DAC正弦波代码示例:
#include "stm32f10x.h"
#define PI 3.14159265358979323846
void DAC_Configuration(void);
void TIM_Configuration(void);
int main(void)
{
DAC_Configuration();
TIM_Configuration();
while(1)
{
}
}
void DAC_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 100;
TIM_TimeBaseStructure.TIM_Prescaler = 72;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 50;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
static uint16_t i = 0;
static uint16_t sin_table[100] = {2048, 2098, 2148, 2198, 2248, 2298, 2347, 2396, 2444, 2492, 2539, 2585, 2631, 2675, 2719, 2762, 2804, 2845, 2885, 2924, 2962, 2999, 3034, 3069, 3102, 3134, 3164, 3193, 3220, 3246, 3270, 3293, 3314, 3333, 3351, 3367, 3381, 3394, 3405, 3414, 3422, 3427, 3431, 3433, 3433, 3431, 3427, 3422, 3414, 3405, 3394, 3381, 3367, 3351, 3333, 3314, 3293, 3270, 3246, 3220, 3193, 3164, 3134, 3102, 3069, 3034, 2999, 2962, 2924, 2885, 2845, 2804, 2762, 2719, 2675, 2631, 2585, 2539, 2492, 2444, 2396, 2347, 2298, 2248, 2198, 2148, 2098, 2048, 1998, 1948, 1898, 1848, 1798, 1749, 1700, 1652, 1604, 1557, 1511, 1465, 1421, 1377, 1334, 1292, 1251, 1211, 1172, 1134, 1097, 1062, 1027, 994, 962, 931, 902, 874, 847, 821, 796, 773, 751, 730, 711, 693, 677, 662, 648, 635, 623, 613, 604, 596, 590, 585, 581, 579, 578, 578};
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
DAC_SetChannel1Data(DAC_Align_12b_R, sin_table[i]);
i++;
if(i >= 100)
{
i = 0;
}
}
}
阅读全文