|
今年在做單片機(jī)項(xiàng)目,是一個(gè)基于labview的虛擬示波器,程序全部開源 可供大家下載
所有資料51hei提供下載:
數(shù)字存儲(chǔ)示波器.7z
(1.25 MB, 下載次數(shù): 24)
2019-3-22 00:48 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
單片機(jī)源程序如下:
- #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"
- /*******************************************************************************
- 函數(shù)名:main
- 輸 入: void
- 輸 出: int
- 功 能:用戶程序入口
- ********************************************************************************/
- int main(void)
- {
- SystemInit(); //系統(tǒng)初始化
- TP_Configuration(); //觸摸屏初始化
- USART_Configuration(); //串口初始化
- ButtonInit(); //按鍵初始化
- SysTick_Config(SystemFrequency / 1000);//滴答時(shí)鐘配置,1MS進(jìn)入中斷一次
- 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****/
復(fù)制代碼
- #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];
- /*******************************************************************************
- 函數(shù)名:WaveMakerInit
- 輸 入:void
- 輸 出:void
- 功 能:初始化波形發(fā)生器。
- 配置PA4 為DAC_OUT1, 啟用DMA2
- *******************************************************************************/
- void WaveMakerInit(void)
- {
- uint32_t i;
- for (i = 0; i < 32; i++)
- {
- g_Wave[i] = g_Sine32[i] * 0.606; //調(diào)整正弦波幅度
- }
- {
- //配置時(shí)鐘
- 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); //更新事件被用作觸發(fā)輸出
- }
- #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);//更新事件被用作輸出觸發(fā)
- }
- #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;//調(diào)整正弦波幅度
- }
-
- {
- //配置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);
- }
- /************************************************************************
- 函數(shù)名:SetWaveType
- 輸 入:波形
- 輸 出:void
- 功 能:設(shè)置輸出的波形
- ************************************************************************/
- void SetWaveType(uint8_t _type)
- {
- /*
- 計(jì)算波形幅度。
- DAC寄存器最大值 4096 對(duì)應(yīng)3.3V
- 我們期望獲得2V的幅度,系數(shù) = 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;//調(diào)整正弦波幅度
- }
- }
- 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);
- }
復(fù)制代碼
|
|