标题:
STM32的示波器开发程序
[打印本页]
作者:
123123112233
时间:
2019-3-21 16:54
标题:
STM32的示波器开发程序
今年在做单片机项目,是一个基于labview的虚拟示波器,程序全部开源 可供大家下载
所有资料51hei提供下载:
数字存储示波器.7z
(1.25 MB, 下载次数: 24)
2019-3-22 00:48 上传
点击文件名下载附件
下载积分: 黑币 -5
单片机源程序如下:
#include "stm32f10x.h"
#include <stdio.h>
#include "usart.h"
#include "touch_panel.h"
#include "systick.h"
#include "tft_lcd.h"
#include "back_light.h"
#include "oscilloscope.h"
#include "button.h"
#include "wave_maker.h"
/*******************************************************************************
函数名:main
输 入: void
输 出: int
功 能:用户程序入口
********************************************************************************/
int main(void)
{
SystemInit(); //系统初始化
TP_Configuration(); //触摸屏初始化
USART_Configuration(); //串口初始化
ButtonInit(); //按键初始化
SysTick_Config(SystemFrequency / 1000);//滴答时钟配置,1MS进入中断一次
OscilloscopeMain();
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
复制代码
#include "stm32f10x.h"
#include <stdio.h>
#include "systick.h"
#include "wave_maker.h"
//波形由PA4输出, PA4 用作 DAC_OUT1, DAC的参考电压 = 3.3V
//正弦波
const uint16_t g_Sine32[32] =
{
2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056,
3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909,
599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647
};
//方波
const uint16_t g_Square32[32] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095,
4095, 4095, 4095, 4095, 4095, 4095, 4095, 4095
};
uint16_t g_Wave[32];
/*******************************************************************************
函数名:WaveMakerInit
输 入:void
输 出:void
功 能:初始化波形发生器。
配置PA4 为DAC_OUT1, 启用DMA2
*******************************************************************************/
void WaveMakerInit(void)
{
uint32_t i;
for (i = 0; i < 32; i++)
{
g_Wave[i] = g_Sine32[i] * 0.606; //调整正弦波幅度
}
{
//配置时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
}
{
//配置GPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //配置PA4为DAC_OUT1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
#if 1
{
//配置TIM6
TIM_PrescalerConfig(TIM6, 0, TIM_PSCReloadMode_Update);
//决定DAC输出的采样频率 , x = 72000000 / 频率
//TIM_SetAutoreload(TIM6, 562); //562 输出1KHz的正弦波
//TIM_SetAutoreload(TIM6, 22); //22 输出100KHz的正弦波
TIM_SetAutoreload(TIM6, 220); //22 输出10KHz的正弦波
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); //更新事件被用作触发输出
}
#else
{
//配置TIME2
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0x19;
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);//更新事件被用作输出触发
}
#endif
{
//配置DAC_1
DAC_InitTypeDef DAC_InitStructure;
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
//DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
}
for (i = 0; i < 32; i++)
{
g_Wave[i] = g_Sine32[i] * 0.606;//调整正弦波幅度
}
{
//配置DMA2_3
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA2_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&g_Wave;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 32;
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(DMA2_Channel3, &DMA_InitStructure);
DMA_Cmd(DMA2_Channel3, ENABLE);
}
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_DMACmd(DAC_Channel_1, ENABLE);
TIM_Cmd(TIM6, ENABLE);
//TIM_Cmd(TIM2, ENABLE);
}
/************************************************************************
函数名:SetWaveType
输 入:波形
输 出:void
功 能:设置输出的波形
************************************************************************/
void SetWaveType(uint8_t _type)
{
/*
计算波形幅度。
DAC寄存器最大值 4096 对应3.3V
我们期望获得2V的幅度,系数 = 0.606
4096 * 0.606 = 2482
*/
TIM_Cmd(TIM6, DISABLE);
switch(_type)
{
case SINE: //正弦波
{
uint32_t i;
for (i = 0; i < 32; i++)
{
g_Wave[i] = g_Sine32[i] * 0.606;//调整正弦波幅度
}
}
break;
case SQUARE: //方波
{
uint32_t i;
for (i = 0; i < 32; i++)
{
g_Wave[i] = g_Square32[i] * 0.606;
}
}
break;
}
TIM_Cmd(TIM6, ENABLE);
}
复制代码
作者:
51hei团团
时间:
2019-3-22 01:02
压缩包里面只有程序 没有原理图?
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1